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