1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.sdbc;
29 
30 import java.util.Vector;
31 
32 import lib.MultiMethodTest;
33 import lib.Status;
34 import util.ValueComparer;
35 
36 import com.sun.star.io.XDataInputStream;
37 import com.sun.star.io.XInputStream;
38 import com.sun.star.io.XTextInputStream;
39 import com.sun.star.lang.XMultiServiceFactory;
40 import com.sun.star.sdbc.SQLException;
41 import com.sun.star.sdbc.XRow;
42 import com.sun.star.sdbc.XRowUpdate;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.util.Date;
45 import com.sun.star.util.DateTime;
46 import com.sun.star.util.Time;
47 
48 /**
49 * Testing <code>com.sun.star.sdbc.XRowUpdate</code>
50 * interface methods :
51 * <ul>
52 *  <li><code> updateNull()</code></li>
53 *  <li><code> updateBoolean()</code></li>
54 *  <li><code> updateByte()</code></li>
55 *  <li><code> updateShort()</code></li>
56 *  <li><code> updateInt()</code></li>
57 *  <li><code> updateLong()</code></li>
58 *  <li><code> updateFloat()</code></li>
59 *  <li><code> updateDouble()</code></li>
60 *  <li><code> updateString()</code></li>
61 *  <li><code> updateBytes()</code></li>
62 *  <li><code> updateDate()</code></li>
63 *  <li><code> updateTime()</code></li>
64 *  <li><code> updateTimestamp()</code></li>
65 *  <li><code> updateBinaryStream()</code></li>
66 *  <li><code> updateCharacterStream()</code></li>
67 *  <li><code> updateObject()</code></li>
68 *  <li><code> updateNumericObject()</code></li>
69 * </ul> <p>
70 * Object relations required :
71 * <ul>
72 * <li> <code>'CurrentRowData'</code> : (may be used in other
73 *   interface tests) is a <code>java.util.Vector</code> object
74 *   that contains column types and values in current row. Each
75 *   element of vector corresponds to appropriate column (element
76 *   with index 0 to column 1, 1 -> 2, etc.). <p>
77 *   The following <code>XRowUpdate</code> methods correspond to classes
78 *   in Vector :
79 *   <ul>
80 *   <li> <code>setBinaryStream</code> -
81 *        <code>com.sun.star.io.XDataInputStream</code> class. </li>
82 *   <li> <code>setCharacterStream</code> -
83 *        <code>com.sun.star.io.XTextInputStream</code> class. </li>
84 *   <li> <code>setObject</code> -
85 *        <code>java.lang.Object[]</code> class, the element with
86 *         index 0 must be used. </li>
87 *   </ul>
88 *   Other methods uses types they return (i.e. <code>java.lang.String</code>
89 *   for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
90 *   for <code>setRef</code> method).
91 * </li>
92 * <li> <code>'XRowUpdate.XRow'</code> : implementation of <code>
93 *   com.sun.star.sdbc.XRow</code> interface for checking updated data.
94 * </li>
95 * </ul> <p>
96 * The test <b>damages</b> the object, so it is recreated finally.
97 * @see com.sun.star.sdbc.XRowUpdate
98 * @see com.sun.star.sdbc.XRow
99 */
100 public class _XRowUpdate extends MultiMethodTest {
101 
102     // oObj filled by MultiMethodTest
103     public XRowUpdate oObj = null ;
104 
105     private Vector rowData = null ;
106     private XRow row = null ;
107 
108     /**
109     * Gets relations.
110     */
111     public void before() {
112         rowData = (Vector) tEnv.getObjRelation("CurrentRowData") ;
113         if (rowData == null) {
114             log.println("!!! 'CurrentRowData' relation not found !!!") ;
115         }
116         row = (XRow) tEnv.getObjRelation("XRowUpdate.XRow") ;
117         if (rowData == null) {
118             log.println("!!! 'XRowUpdate.XRow' relation not found !!!") ;
119         }
120     }
121 
122     /**
123     * Try to set NULL value for each column. Then using <code>XRow</code>
124     * relation check if NULL was really set. <p>
125     * Has OK status if for every column NULL value was successfully set.
126     * @see com.sun.star.sdbc.XRow
127     */
128     public void _updateNull() {
129         boolean result = true ;
130         for (int i = 0; i < rowData.size(); i++) {
131             if (rowData.get(i) == null) continue ;
132             log.print("  Setting NULL at column #" + (i+1) + " ...") ;
133             try {
134                 oObj.updateNull(i + 1) ;
135 
136                 if (rowData.get(i) instanceof String) row.getString(i + 1) ;
137                 if (rowData.get(i) instanceof Boolean) row.getBoolean(i + 1) ;
138                 if (rowData.get(i) instanceof Byte) row.getByte(i + 1) ;
139                 if (rowData.get(i) instanceof Short) row.getShort(i + 1) ;
140                 if (rowData.get(i) instanceof Integer) row.getInt(i + 1) ;
141                 if (rowData.get(i) instanceof Long) row.getLong(i + 1) ;
142                 if (rowData.get(i) instanceof Float) row.getFloat(i + 1) ;
143                 if (rowData.get(i) instanceof Double) row.getDouble(i + 1) ;
144                 if (rowData.get(i) instanceof byte[]) row.getBytes(i + 1) ;
145                 if (rowData.get(i) instanceof Date) row.getDate(i + 1) ;
146                 if (rowData.get(i) instanceof Time) row.getTime(i + 1) ;
147                 if (rowData.get(i) instanceof DateTime)
148                     row.getTimestamp(i + 1) ;
149                 if (rowData.get(i) instanceof XDataInputStream)
150                     row.getBinaryStream(i + 1) ;
151                 if (rowData.get(i) instanceof XTextInputStream)
152                     row.getCharacterStream(i + 1) ;
153                 //if (rowData.get(i) instanceof Object[]) row.getObject(i) ;
154 
155                 if (!row.wasNull()) {
156                     log.println("FAILED") ;
157                     log.println("Not NULL was returned !!!") ;
158                     result = false ;
159                 } else {
160                     log.println("OK") ;
161                 }
162             } catch (SQLException e) {
163                 log.println("FAILED") ;
164                 e.printStackTrace(log) ;
165                 result = false ;
166             }
167         }
168 
169         tRes.tested("updateNull()", result) ;
170     }
171 
172     /**
173     * Updates column with the appropriate type (if exists) and then
174     * checks result with interface <code>XRow</code>.<p>
175     * Has OK status if column successfully updated, ahd the same
176     * result returned.
177     */
178     public void _updateBoolean() {
179         boolean result = true ;
180         int idx = findColumnOfType(Boolean.class) ;
181 
182         if (idx < 0) {
183             log.println("Required type not found") ;
184             tRes.tested("updateBoolean()", Status.skipped(true)) ;
185             return ;
186         }
187 
188         try {
189             boolean newVal = !row.getBoolean(idx) ;
190             oObj.updateBoolean(idx, newVal) ;
191             boolean getVal = row.getBoolean(idx) ;
192             result = newVal == getVal ;
193         } catch (SQLException e) {
194             e.printStackTrace(log) ;
195             result = false ;
196         }
197 
198         tRes.tested("updateBoolean()", result) ;
199     }
200 
201     /**
202     * Updates column with the appropriate type (if exists) and then
203     * checks result with interface <code>XRow</code>.<p>
204     * Has OK status if column successfully updated, ahd the same
205     * result returned.
206     */
207     public void _updateByte() {
208         boolean result = true ;
209         int idx = findColumnOfType(Byte.class) ;
210 
211         if (idx < 0) {
212             log.println("Required type not found") ;
213             tRes.tested("updateByte()", Status.skipped(true)) ;
214             return ;
215         }
216 
217         try {
218             byte newVal = (byte) (row.getByte(idx) + 1) ;
219             oObj.updateByte(idx, newVal) ;
220             byte getVal = row.getByte(idx) ;
221             result = newVal == getVal ;
222         } catch (SQLException e) {
223             e.printStackTrace(log) ;
224             result = false ;
225         }
226 
227         tRes.tested("updateByte()", result) ;
228     }
229 
230     /**
231     * Updates column with the appropriate type (if exists) and then
232     * checks result with interface <code>XRow</code>.<p>
233     * Has OK status if column successfully updated, ahd the same
234     * result returned.
235     */
236     public void _updateShort() {
237         boolean result = true ;
238         int idx = findColumnOfType(Short.class) ;
239 
240         if (idx < 0) {
241             log.println("Required type not found") ;
242             tRes.tested("updateShort()", Status.skipped(true)) ;
243             return ;
244         }
245 
246         try {
247             short newVal = (short) (row.getShort(idx) + 1) ;
248             oObj.updateShort(idx, newVal) ;
249             short getVal = row.getShort(idx) ;
250             result = newVal == getVal ;
251         } catch (SQLException e) {
252             e.printStackTrace(log) ;
253             result = false ;
254         }
255 
256         tRes.tested("updateShort()", result) ;
257     }
258 
259     /**
260     * Updates column with the appropriate type (if exists) and then
261     * checks result with interface <code>XRow</code>.<p>
262     * Has OK status if column successfully updated, ahd the same
263     * result returned.
264     */
265     public void _updateInt() {
266         boolean result = true ;
267         int idx = findColumnOfType(Integer.class) ;
268 
269         if (idx < 0) {
270             log.println("Required type not found") ;
271             tRes.tested("updateInt()", Status.skipped(true)) ;
272             return ;
273         }
274 
275         try {
276             int newVal = 1 + row.getInt(idx)  ;
277             oObj.updateInt(idx, newVal) ;
278             int getVal = row.getInt(idx) ;
279             result = newVal == getVal ;
280         } catch (SQLException e) {
281             e.printStackTrace(log) ;
282             result = false ;
283         }
284 
285         tRes.tested("updateInt()", result) ;
286     }
287 
288     /**
289     * Updates column with the appropriate type (if exists) and then
290     * checks result with interface <code>XRow</code>.<p>
291     * Has OK status if column successfully updated, ahd the same
292     * result returned.
293     */
294     public void _updateLong() {
295         boolean result = true ;
296         int idx = findColumnOfType(Long.class) ;
297 
298         if (idx < 0) {
299             log.println("Required type not found") ;
300             tRes.tested("updateLong()", Status.skipped(true)) ;
301             return ;
302         }
303 
304         try {
305             long newVal = 1 + row.getLong(idx) ;
306             oObj.updateLong(idx, newVal) ;
307             long getVal = row.getLong(idx) ;
308             result = newVal == getVal ;
309         } catch (SQLException e) {
310             e.printStackTrace(log) ;
311             result = false ;
312         }
313 
314         tRes.tested("updateLong()", result) ;
315     }
316 
317     /**
318     * Updates column with the appropriate type (if exists) and then
319     * checks result with interface <code>XRow</code>.<p>
320     * Has OK status if column successfully updated, ahd the same
321     * result returned.
322     */
323     public void _updateFloat() {
324         boolean result = true ;
325         int idx = findColumnOfType(Float.class) ;
326 
327         if (idx < 0) {
328             log.println("Required type not found") ;
329             tRes.tested("updateFloat()", Status.skipped(true)) ;
330             return ;
331         }
332 
333         try {
334             float newVal = (float) (1.1 + row.getFloat(idx));
335             oObj.updateFloat(idx, newVal) ;
336             float getVal = row.getFloat(idx) ;
337             result = newVal == getVal ;
338         } catch (SQLException e) {
339             e.printStackTrace(log) ;
340             result = false ;
341         }
342 
343         tRes.tested("updateFloat()", result) ;
344     }
345 
346     /**
347     * Updates column with the appropriate type (if exists) and then
348     * checks result with interface <code>XRow</code>.<p>
349     * Has OK status if column successfully updated, ahd the same
350     * result returned.
351     */
352     public void _updateDouble() {
353         boolean result = true ;
354         int idx = findColumnOfType(Double.class) ;
355 
356         if (idx < 0) {
357             log.println("Required type not found") ;
358             tRes.tested("updateDouble()", Status.skipped(true)) ;
359             return ;
360         }
361 
362         try {
363             double newVal = 1.1 + row.getDouble(idx) ;
364             oObj.updateDouble(idx, newVal) ;
365             double getVal = row.getDouble(idx) ;
366             result = newVal == getVal ;
367         } catch (SQLException e) {
368             e.printStackTrace(log) ;
369             result = false ;
370         }
371 
372         tRes.tested("updateDouble()", result) ;
373     }
374 
375     /**
376     * Updates column with the appropriate type (if exists) and then
377     * checks result with interface <code>XRow</code>.<p>
378     * Has OK status if column successfully updated, ahd the same
379     * result returned.
380     */
381     public void _updateString() {
382         boolean result = true ;
383         int idx = findColumnOfType(String.class) ;
384 
385         if (idx < 0) {
386             log.println("Required type not found") ;
387             tRes.tested("updateString()", Status.skipped(true)) ;
388             return ;
389         }
390 
391         try {
392             String newVal = "_" + row.getString(idx) ;
393             oObj.updateString(idx, newVal) ;
394             String getVal = row.getString(idx) ;
395             result = newVal.equals(getVal) ;
396             log.println("New value = '" + newVal + "', get value = '"
397                 + getVal + "'") ;
398         } catch (SQLException e) {
399             e.printStackTrace(log) ;
400             result = false ;
401         }
402 
403         tRes.tested("updateString()", result) ;
404     }
405 
406     /**
407     * Updates column with the appropriate type (if exists) and then
408     * checks result with interface <code>XRow</code>.<p>
409     * Has OK status if column successfully updated, ahd the same
410     * result returned.
411     */
412     public void _updateBytes() {
413         boolean result = true ;
414         int idx = findColumnOfType(byte[].class) ;
415 
416         if (idx < 0) {
417             log.println("Required type not found") ;
418             tRes.tested("updateBytes()", Status.skipped(true)) ;
419             return ;
420         }
421 
422         try {
423             byte[] newVal = row.getBytes(idx) ;
424             if (newVal == null || newVal.length == 0) {
425                 newVal = new byte[] {34, 111, 98} ;
426             } else {
427                 newVal = new byte[] {(byte) (newVal[0] + 1), 111, 98} ;
428             }
429             oObj.updateBytes(idx, newVal) ;
430             byte[] getVal = row.getBytes(idx) ;
431             result = ValueComparer.equalValue(newVal, getVal) ;
432         } catch (SQLException e) {
433             e.printStackTrace(log) ;
434             result = false ;
435         }
436 
437         tRes.tested("updateBytes()", result) ;
438     }
439 
440     /**
441     * Updates column with the appropriate type (if exists) and then
442     * checks result with interface <code>XRow</code>.<p>
443     * Has OK status if column successfully updated, ahd the same
444     * result returned.
445     */
446     public void _updateDate() {
447         boolean result = true ;
448         int idx = findColumnOfType(Date.class) ;
449 
450         if (idx < 0) {
451             log.println("Required type not found") ;
452             tRes.tested("updateDate()", Status.skipped(true)) ;
453             return ;
454         }
455 
456         try {
457             Date newVal = row.getDate(idx) ;
458             newVal.Year ++ ;
459             oObj.updateDate(idx, newVal) ;
460             Date getVal = row.getDate(idx) ;
461             result = ValueComparer.equalValue(newVal, getVal) ;
462         } catch (SQLException e) {
463             e.printStackTrace(log) ;
464             result = false ;
465         }
466 
467         tRes.tested("updateDate()", result) ;
468     }
469 
470     /**
471     * Updates column with the appropriate type (if exists) and then
472     * checks result with interface <code>XRow</code>.<p>
473     * Has OK status if column successfully updated, ahd the same
474     * result returned.
475     */
476     public void _updateTime() {
477         boolean result = true ;
478         int idx = findColumnOfType(Time.class) ;
479 
480         if (idx < 0) {
481             log.println("Required type not found") ;
482             tRes.tested("updateTime()", Status.skipped(true)) ;
483             return ;
484         }
485 
486         try {
487             Time newVal = row.getTime(idx) ;
488             newVal.Seconds ++ ;
489             oObj.updateTime(idx, newVal) ;
490             Time getVal = row.getTime(idx) ;
491             result = ValueComparer.equalValue(newVal, getVal) ;
492         } catch (SQLException e) {
493             e.printStackTrace(log) ;
494             result = false ;
495         }
496 
497         tRes.tested("updateTime()", result) ;
498     }
499 
500     /**
501     * Updates column with the appropriate type (if exists) and then
502     * checks result with interface <code>XRow</code>.<p>
503     * Has OK status if column successfully updated, ahd the same
504     * result returned.
505     */
506     public void _updateTimestamp() {
507         boolean result = true ;
508         int idx = findColumnOfType(DateTime.class) ;
509 
510         if (idx < 0) {
511             log.println("Required type not found") ;
512             tRes.tested("updateTimestamp()", Status.skipped(true)) ;
513             return ;
514         }
515 
516         try {
517             DateTime newVal = row.getTimestamp(idx) ;
518             newVal.Year ++ ;
519             oObj.updateTimestamp(idx, newVal) ;
520             DateTime getVal = row.getTimestamp(idx) ;
521             result = ValueComparer.equalValue(newVal, getVal) ;
522         } catch (SQLException e) {
523             e.printStackTrace(log) ;
524             result = false ;
525         }
526 
527         tRes.tested("updateTimestamp()", result) ;
528     }
529 
530 
531     /**
532     * Updates column with the appropriate type (if exists) and then
533     * checks result with interface <code>XRow</code>.<p>
534     * Has OK status if column successfully updated, ahd the same
535     * result returned.
536     */
537     public void _updateBinaryStream() {
538         boolean result = true ;
539         int idx = findColumnOfType(XDataInputStream.class) ;
540 
541         if (idx < 0) {
542             log.println("Required type not found") ;
543             tRes.tested("updateBinaryStream()", Status.skipped(true)) ;
544             return ;
545         }
546 
547         try {
548             Object oStream = ((XMultiServiceFactory)tParam.getMSF()).
549                 createInstance("com.sun.star.io.DataInputStream") ;
550             XInputStream newVal = (XInputStream) UnoRuntime.queryInterface
551                 (XInputStream.class, oStream);
552 
553             oObj.updateBinaryStream(idx, newVal, 0) ;
554             XInputStream getVal = row.getBinaryStream(idx) ;
555             result = UnoRuntime.areSame(newVal, getVal) ;
556         } catch (SQLException e) {
557             e.printStackTrace(log) ;
558             result = false ;
559         } catch (com.sun.star.uno.Exception e) {
560             log.println("Unexpected exception:") ;
561             e.printStackTrace(log) ;
562             result = false ;
563         }
564 
565         tRes.tested("updateBinaryStream()", result) ;
566     }
567 
568     /**
569     * Updates column with the appropriate type (if exists) and then
570     * checks result with interface <code>XRow</code>.<p>
571     * Has OK status if column successfully updated, ahd the same
572     * result returned.
573     */
574     public void _updateCharacterStream() {
575         boolean result = true ;
576         int idx = findColumnOfType(XTextInputStream.class) ;
577 
578         if (idx < 0) {
579             log.println("Required type not found") ;
580             tRes.tested("updateCharacterStream()", Status.skipped(true)) ;
581             return ;
582         }
583 
584         try {
585             Object oStream = ((XMultiServiceFactory)tParam.getMSF()).
586                 createInstance("com.sun.star.io.TextInputStream") ;
587             XInputStream newVal = (XInputStream) UnoRuntime.queryInterface
588                 (XInputStream.class, oStream);
589 
590             oObj.updateCharacterStream(idx, newVal, 0) ;
591             XInputStream getVal = row.getCharacterStream(idx) ;
592             result = UnoRuntime.areSame(newVal, getVal) ;
593         } catch (SQLException e) {
594             e.printStackTrace(log) ;
595             result = false ;
596         } catch (com.sun.star.uno.Exception e) {
597             log.println("Unexpected exception:") ;
598             e.printStackTrace(log) ;
599             result = false ;
600         }
601 
602         tRes.tested("updateCharacterStream()", result) ;
603     }
604 
605     /**
606     * Updates column with the appropriate type (if exists) and then
607     * checks result with interface <code>XRow</code>.<p>
608     * Has OK status if column successfully updated, ahd the same
609     * result returned.
610     */
611     public void _updateObject() {
612         boolean result = true ;
613         int idx = findColumnOfType(Object[].class) ;
614 
615         if (idx < 0) {
616             log.println("Required type not found") ;
617             tRes.tested("updateObject()", Status.skipped(true)) ;
618             return ;
619         }
620 
621         try {
622             Object newVal = ((XMultiServiceFactory)tParam.getMSF()).
623                 createInstance("com.sun.star.io.Pipe") ;
624 
625             oObj.updateObject(idx, newVal) ;
626             //Object getVal = row.getObject(idx) ;
627             //result = UnoRuntime.areSame(newVal, getVal) ;
628         } catch (SQLException e) {
629             e.printStackTrace(log) ;
630             result = false ;
631         } catch (com.sun.star.uno.Exception e) {
632             log.println("Unexpected exception:") ;
633             e.printStackTrace(log) ;
634             result = false ;
635         }
636 
637         tRes.tested("updateObject()", result) ;
638     }
639 
640     /**
641     * Updates column with the appropriate type (if exists) and then
642     * checks result with interface <code>XRow</code>.<p>
643     * Has OK status if column successfully updated, ahd the same
644     * result returned.
645     */
646     public void _updateNumericObject() {
647         boolean result = true ;
648         int idx = findColumnOfType(Object[].class) ;
649 
650         if (idx < 0) {
651             log.println("Required type not found") ;
652             tRes.tested("updateNumericObject()", Status.skipped(true)) ;
653             return ;
654         }
655 
656         try {
657             Object newVal = ((XMultiServiceFactory)tParam.getMSF()).
658                                 createInstance("com.sun.star.io.Pipe") ;
659 
660             oObj.updateNumericObject(idx, newVal, 0) ;
661             //Object getVal = row.getObject(idx) ;
662             //result = UnoRuntime.areSame(newVal, getVal) ;
663         } catch (SQLException e) {
664             e.printStackTrace(log) ;
665             result = false ;
666         } catch (com.sun.star.uno.Exception e) {
667             log.println("Unexpected exception:") ;
668             e.printStackTrace(log) ;
669             result = false ;
670         }
671 
672         tRes.tested("updateNumericObject()", result) ;
673     }
674 
675     /**
676     * Finds in relation vector index of column of the appropriate
677     * type.
678     */
679     protected int findColumnOfType(Class clz) {
680 
681         for (int i = 0; i < rowData.size(); i++)
682             if (clz.isInstance(rowData.get(i))) return i + 1 ;
683         return -1 ;
684     }
685 
686     /**
687     * Disposes environment.
688     */
689     public void after() {
690         disposeEnvironment() ;
691     }
692 
693 }  // finish class _XRow
694 
695 
696