2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.util
.List
;
23 import lib
.MultiMethodTest
;
25 import util
.ValueComparer
;
27 import com
.sun
.star
.io
.XDataInputStream
;
28 import com
.sun
.star
.io
.XInputStream
;
29 import com
.sun
.star
.io
.XTextInputStream
;
30 import com
.sun
.star
.sdbc
.SQLException
;
31 import com
.sun
.star
.sdbc
.XRow
;
32 import com
.sun
.star
.sdbc
.XRowUpdate
;
33 import com
.sun
.star
.uno
.UnoRuntime
;
34 import com
.sun
.star
.util
.Date
;
35 import com
.sun
.star
.util
.DateTime
;
36 import com
.sun
.star
.util
.Time
;
39 * Testing <code>com.sun.star.sdbc.XRowUpdate</code>
42 * <li><code> updateNull()</code></li>
43 * <li><code> updateBoolean()</code></li>
44 * <li><code> updateByte()</code></li>
45 * <li><code> updateShort()</code></li>
46 * <li><code> updateInt()</code></li>
47 * <li><code> updateLong()</code></li>
48 * <li><code> updateFloat()</code></li>
49 * <li><code> updateDouble()</code></li>
50 * <li><code> updateString()</code></li>
51 * <li><code> updateBytes()</code></li>
52 * <li><code> updateDate()</code></li>
53 * <li><code> updateTime()</code></li>
54 * <li><code> updateTimestamp()</code></li>
55 * <li><code> updateBinaryStream()</code></li>
56 * <li><code> updateCharacterStream()</code></li>
57 * <li><code> updateObject()</code></li>
58 * <li><code> updateNumericObject()</code></li>
60 * Object relations required :
62 * <li> <code>'CurrentRowData'</code> : (may be used in other
63 * interface tests) is a <code>java.util.Vector</code> object
64 * that contains column types and values in current row. Each
65 * element of vector corresponds to appropriate column (element
66 * with index 0 to column 1, 1 -> 2, etc.). <p>
67 * The following <code>XRowUpdate</code> methods correspond to classes
70 * <li> <code>setBinaryStream</code> -
71 * <code>com.sun.star.io.XDataInputStream</code> class. </li>
72 * <li> <code>setCharacterStream</code> -
73 * <code>com.sun.star.io.XTextInputStream</code> class. </li>
74 * <li> <code>setObject</code> -
75 * <code>java.lang.Object[]</code> class, the element with
76 * index 0 must be used. </li>
78 * Other methods uses types they return (i.e. <code>String</code>
79 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
80 * for <code>setRef</code> method).
82 * <li> <code>'XRowUpdate.XRow'</code> : implementation of <code>
83 * com.sun.star.sdbc.XRow</code> interface for checking updated data.
86 * The test <b>damages</b> the object, so it is recreated finally.
87 * @see com.sun.star.sdbc.XRowUpdate
88 * @see com.sun.star.sdbc.XRow
90 public class _XRowUpdate
extends MultiMethodTest
{
92 // oObj filled by MultiMethodTest
93 public XRowUpdate oObj
= null ;
95 private List
<Object
> rowData
= null ;
96 private XRow row
= null ;
102 public void before() {
103 rowData
= (List
<Object
>) tEnv
.getObjRelation("CurrentRowData") ;
104 if (rowData
== null) {
105 log
.println("!!! 'CurrentRowData' relation not found !!!") ;
107 row
= (XRow
) tEnv
.getObjRelation("XRowUpdate.XRow") ;
108 if (rowData
== null) {
109 log
.println("!!! 'XRowUpdate.XRow' relation not found !!!") ;
114 * Try to set NULL value for each column. Then using <code>XRow</code>
115 * relation check if NULL was really set. <p>
116 * Has OK status if for every column NULL value was successfully set.
117 * @see com.sun.star.sdbc.XRow
119 public void _updateNull() {
120 boolean result
= true ;
121 for (int i
= 0; i
< rowData
.size(); i
++) {
122 if (rowData
.get(i
) == null) continue ;
123 log
.print(" Setting NULL at column #" + (i
+1) + " ...") ;
125 oObj
.updateNull(i
+ 1) ;
127 if (rowData
.get(i
) instanceof String
) row
.getString(i
+ 1) ;
128 if (rowData
.get(i
) instanceof Boolean
) row
.getBoolean(i
+ 1) ;
129 if (rowData
.get(i
) instanceof Byte
) row
.getByte(i
+ 1) ;
130 if (rowData
.get(i
) instanceof Short
) row
.getShort(i
+ 1) ;
131 if (rowData
.get(i
) instanceof Integer
) row
.getInt(i
+ 1) ;
132 if (rowData
.get(i
) instanceof Long
) row
.getLong(i
+ 1) ;
133 if (rowData
.get(i
) instanceof Float
) row
.getFloat(i
+ 1) ;
134 if (rowData
.get(i
) instanceof Double
) row
.getDouble(i
+ 1) ;
135 if (rowData
.get(i
) instanceof byte[]) row
.getBytes(i
+ 1) ;
136 if (rowData
.get(i
) instanceof Date
) row
.getDate(i
+ 1) ;
137 if (rowData
.get(i
) instanceof Time
) row
.getTime(i
+ 1) ;
138 if (rowData
.get(i
) instanceof DateTime
)
139 row
.getTimestamp(i
+ 1) ;
140 if (rowData
.get(i
) instanceof XDataInputStream
)
141 row
.getBinaryStream(i
+ 1) ;
142 if (rowData
.get(i
) instanceof XTextInputStream
)
143 row
.getCharacterStream(i
+ 1) ;
145 if (!row
.wasNull()) {
146 log
.println("FAILED") ;
147 log
.println("Not NULL was returned !!!") ;
152 } catch (SQLException e
) {
153 log
.println("FAILED") ;
154 e
.printStackTrace(log
) ;
159 tRes
.tested("updateNull()", result
) ;
163 * Updates column with the appropriate type (if exists) and then
164 * checks result with interface <code>XRow</code>.<p>
165 * Has OK status if column successfully updated, ahd the same
168 public void _updateBoolean() {
169 boolean result
= true ;
170 int idx
= findColumnOfType(Boolean
.class) ;
173 log
.println("Required type not found") ;
174 tRes
.tested("updateBoolean()", Status
.skipped(true)) ;
179 boolean newVal
= !row
.getBoolean(idx
) ;
180 oObj
.updateBoolean(idx
, newVal
) ;
181 boolean getVal
= row
.getBoolean(idx
) ;
182 result
= newVal
== getVal
;
183 } catch (SQLException e
) {
184 e
.printStackTrace(log
) ;
188 tRes
.tested("updateBoolean()", result
) ;
192 * Updates column with the appropriate type (if exists) and then
193 * checks result with interface <code>XRow</code>.<p>
194 * Has OK status if column successfully updated, ahd the same
197 public void _updateByte() {
198 boolean result
= true ;
199 int idx
= findColumnOfType(Byte
.class) ;
202 log
.println("Required type not found") ;
203 tRes
.tested("updateByte()", Status
.skipped(true)) ;
208 byte newVal
= (byte) (row
.getByte(idx
) + 1) ;
209 oObj
.updateByte(idx
, newVal
) ;
210 byte getVal
= row
.getByte(idx
) ;
211 result
= newVal
== getVal
;
212 } catch (SQLException e
) {
213 e
.printStackTrace(log
) ;
217 tRes
.tested("updateByte()", result
) ;
221 * Updates column with the appropriate type (if exists) and then
222 * checks result with interface <code>XRow</code>.<p>
223 * Has OK status if column successfully updated, ahd the same
226 public void _updateShort() {
227 boolean result
= true ;
228 int idx
= findColumnOfType(Short
.class) ;
231 log
.println("Required type not found") ;
232 tRes
.tested("updateShort()", Status
.skipped(true)) ;
237 short newVal
= (short) (row
.getShort(idx
) + 1) ;
238 oObj
.updateShort(idx
, newVal
) ;
239 short getVal
= row
.getShort(idx
) ;
240 result
= newVal
== getVal
;
241 } catch (SQLException e
) {
242 e
.printStackTrace(log
) ;
246 tRes
.tested("updateShort()", result
) ;
250 * Updates column with the appropriate type (if exists) and then
251 * checks result with interface <code>XRow</code>.<p>
252 * Has OK status if column successfully updated, ahd the same
255 public void _updateInt() {
256 boolean result
= true ;
257 int idx
= findColumnOfType(Integer
.class) ;
260 log
.println("Required type not found") ;
261 tRes
.tested("updateInt()", Status
.skipped(true)) ;
266 int newVal
= 1 + row
.getInt(idx
) ;
267 oObj
.updateInt(idx
, newVal
) ;
268 int getVal
= row
.getInt(idx
) ;
269 result
= newVal
== getVal
;
270 } catch (SQLException e
) {
271 e
.printStackTrace(log
) ;
275 tRes
.tested("updateInt()", result
) ;
279 * Updates column with the appropriate type (if exists) and then
280 * checks result with interface <code>XRow</code>.<p>
281 * Has OK status if column successfully updated, ahd the same
284 public void _updateLong() {
285 boolean result
= true ;
286 int idx
= findColumnOfType(Long
.class) ;
289 log
.println("Required type not found") ;
290 tRes
.tested("updateLong()", Status
.skipped(true)) ;
295 long newVal
= 1 + row
.getLong(idx
) ;
296 oObj
.updateLong(idx
, newVal
) ;
297 long getVal
= row
.getLong(idx
) ;
298 result
= newVal
== getVal
;
299 } catch (SQLException e
) {
300 e
.printStackTrace(log
) ;
304 tRes
.tested("updateLong()", result
) ;
308 * Updates column with the appropriate type (if exists) and then
309 * checks result with interface <code>XRow</code>.<p>
310 * Has OK status if column successfully updated, ahd the same
313 public void _updateFloat() {
314 boolean result
= true ;
315 int idx
= findColumnOfType(Float
.class) ;
318 log
.println("Required type not found") ;
319 tRes
.tested("updateFloat()", Status
.skipped(true)) ;
324 float newVal
= (float) (1.1 + row
.getFloat(idx
));
325 oObj
.updateFloat(idx
, newVal
) ;
326 float getVal
= row
.getFloat(idx
) ;
327 result
= newVal
== getVal
;
328 } catch (SQLException e
) {
329 e
.printStackTrace(log
) ;
333 tRes
.tested("updateFloat()", result
) ;
337 * Updates column with the appropriate type (if exists) and then
338 * checks result with interface <code>XRow</code>.<p>
339 * Has OK status if column successfully updated, ahd the same
342 public void _updateDouble() {
343 boolean result
= true ;
344 int idx
= findColumnOfType(Double
.class) ;
347 log
.println("Required type not found") ;
348 tRes
.tested("updateDouble()", Status
.skipped(true)) ;
353 double newVal
= 1.1 + row
.getDouble(idx
) ;
354 oObj
.updateDouble(idx
, newVal
) ;
355 double getVal
= row
.getDouble(idx
) ;
356 result
= newVal
== getVal
;
357 } catch (SQLException e
) {
358 e
.printStackTrace(log
) ;
362 tRes
.tested("updateDouble()", result
) ;
366 * Updates column with the appropriate type (if exists) and then
367 * checks result with interface <code>XRow</code>.<p>
368 * Has OK status if column successfully updated, ahd the same
371 public void _updateString() {
372 boolean result
= true ;
373 int idx
= findColumnOfType(String
.class) ;
376 log
.println("Required type not found") ;
377 tRes
.tested("updateString()", Status
.skipped(true)) ;
382 String newVal
= "_" + row
.getString(idx
) ;
383 oObj
.updateString(idx
, newVal
) ;
384 String getVal
= row
.getString(idx
) ;
385 result
= newVal
.equals(getVal
) ;
386 log
.println("New value = '" + newVal
+ "', get value = '"
388 } catch (SQLException e
) {
389 e
.printStackTrace(log
) ;
393 tRes
.tested("updateString()", result
) ;
397 * Updates column with the appropriate type (if exists) and then
398 * checks result with interface <code>XRow</code>.<p>
399 * Has OK status if column successfully updated, ahd the same
402 public void _updateBytes() {
403 boolean result
= true ;
404 int idx
= findColumnOfType(byte[].class) ;
407 log
.println("Required type not found") ;
408 tRes
.tested("updateBytes()", Status
.skipped(true)) ;
413 byte[] newVal
= row
.getBytes(idx
) ;
414 if (newVal
== null || newVal
.length
== 0) {
415 newVal
= new byte[] {34, 111, 98} ;
417 newVal
= new byte[] {(byte) (newVal
[0] + 1), 111, 98} ;
419 oObj
.updateBytes(idx
, newVal
) ;
420 byte[] getVal
= row
.getBytes(idx
) ;
421 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
422 } catch (SQLException e
) {
423 e
.printStackTrace(log
) ;
427 tRes
.tested("updateBytes()", result
) ;
431 * Updates column with the appropriate type (if exists) and then
432 * checks result with interface <code>XRow</code>.<p>
433 * Has OK status if column successfully updated, ahd the same
436 public void _updateDate() {
437 boolean result
= true ;
438 int idx
= findColumnOfType(Date
.class) ;
441 log
.println("Required type not found") ;
442 tRes
.tested("updateDate()", Status
.skipped(true)) ;
447 Date newVal
= row
.getDate(idx
) ;
449 oObj
.updateDate(idx
, newVal
) ;
450 Date getVal
= row
.getDate(idx
) ;
451 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
452 } catch (SQLException e
) {
453 e
.printStackTrace(log
) ;
457 tRes
.tested("updateDate()", result
) ;
461 * Updates column with the appropriate type (if exists) and then
462 * checks result with interface <code>XRow</code>.<p>
463 * Has OK status if column successfully updated, ahd the same
466 public void _updateTime() {
467 boolean result
= true ;
468 int idx
= findColumnOfType(Time
.class) ;
471 log
.println("Required type not found") ;
472 tRes
.tested("updateTime()", Status
.skipped(true)) ;
477 Time newVal
= row
.getTime(idx
) ;
479 oObj
.updateTime(idx
, newVal
) ;
480 Time getVal
= row
.getTime(idx
) ;
481 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
482 } catch (SQLException e
) {
483 e
.printStackTrace(log
) ;
487 tRes
.tested("updateTime()", result
) ;
491 * Updates column with the appropriate type (if exists) and then
492 * checks result with interface <code>XRow</code>.<p>
493 * Has OK status if column successfully updated, ahd the same
496 public void _updateTimestamp() {
497 boolean result
= true ;
498 int idx
= findColumnOfType(DateTime
.class) ;
501 log
.println("Required type not found") ;
502 tRes
.tested("updateTimestamp()", Status
.skipped(true)) ;
507 DateTime newVal
= row
.getTimestamp(idx
) ;
509 oObj
.updateTimestamp(idx
, newVal
) ;
510 DateTime getVal
= row
.getTimestamp(idx
) ;
511 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
512 } catch (SQLException e
) {
513 e
.printStackTrace(log
) ;
517 tRes
.tested("updateTimestamp()", result
) ;
522 * Updates column with the appropriate type (if exists) and then
523 * checks result with interface <code>XRow</code>.<p>
524 * Has OK status if column successfully updated, ahd the same
527 public void _updateBinaryStream() {
528 boolean result
= true ;
529 int idx
= findColumnOfType(XDataInputStream
.class) ;
532 log
.println("Required type not found") ;
533 tRes
.tested("updateBinaryStream()", Status
.skipped(true)) ;
538 Object oStream
= tParam
.getMSF().
539 createInstance("com.sun.star.io.DataInputStream") ;
540 XInputStream newVal
= UnoRuntime
.queryInterface
541 (XInputStream
.class, oStream
);
543 oObj
.updateBinaryStream(idx
, newVal
, 0) ;
544 XInputStream getVal
= row
.getBinaryStream(idx
) ;
545 result
= UnoRuntime
.areSame(newVal
, getVal
) ;
546 } catch (SQLException e
) {
547 e
.printStackTrace(log
) ;
549 } catch (com
.sun
.star
.uno
.Exception e
) {
550 log
.println("Unexpected exception:") ;
551 e
.printStackTrace(log
) ;
555 tRes
.tested("updateBinaryStream()", result
) ;
559 * Updates column with the appropriate type (if exists) and then
560 * checks result with interface <code>XRow</code>.<p>
561 * Has OK status if column successfully updated, ahd the same
564 public void _updateCharacterStream() {
565 boolean result
= true ;
566 int idx
= findColumnOfType(XTextInputStream
.class) ;
569 log
.println("Required type not found") ;
570 tRes
.tested("updateCharacterStream()", Status
.skipped(true)) ;
575 Object oStream
= tParam
.getMSF().
576 createInstance("com.sun.star.io.TextInputStream") ;
577 XInputStream newVal
= UnoRuntime
.queryInterface
578 (XInputStream
.class, oStream
);
580 oObj
.updateCharacterStream(idx
, newVal
, 0) ;
581 XInputStream getVal
= row
.getCharacterStream(idx
) ;
582 result
= UnoRuntime
.areSame(newVal
, getVal
) ;
583 } catch (SQLException e
) {
584 e
.printStackTrace(log
) ;
586 } catch (com
.sun
.star
.uno
.Exception e
) {
587 log
.println("Unexpected exception:") ;
588 e
.printStackTrace(log
) ;
592 tRes
.tested("updateCharacterStream()", result
) ;
596 * Updates column with the appropriate type (if exists) and then
597 * checks result with interface <code>XRow</code>.<p>
598 * Has OK status if column successfully updated, ahd the same
601 public void _updateObject() {
602 boolean result
= true ;
603 int idx
= findColumnOfType(Object
[].class) ;
606 log
.println("Required type not found") ;
607 tRes
.tested("updateObject()", Status
.skipped(true)) ;
612 Object newVal
= tParam
.getMSF().
613 createInstance("com.sun.star.io.Pipe") ;
615 oObj
.updateObject(idx
, newVal
) ;
616 } catch (SQLException e
) {
617 e
.printStackTrace(log
) ;
619 } catch (com
.sun
.star
.uno
.Exception e
) {
620 log
.println("Unexpected exception:") ;
621 e
.printStackTrace(log
) ;
625 tRes
.tested("updateObject()", result
) ;
629 * Updates column with the appropriate type (if exists) and then
630 * checks result with interface <code>XRow</code>.<p>
631 * Has OK status if column successfully updated, ahd the same
634 public void _updateNumericObject() {
635 boolean result
= true ;
636 int idx
= findColumnOfType(Object
[].class) ;
639 log
.println("Required type not found") ;
640 tRes
.tested("updateNumericObject()", Status
.skipped(true)) ;
645 Object newVal
= tParam
.getMSF().
646 createInstance("com.sun.star.io.Pipe") ;
648 oObj
.updateNumericObject(idx
, newVal
, 0) ;
649 } catch (SQLException e
) {
650 e
.printStackTrace(log
) ;
652 } catch (com
.sun
.star
.uno
.Exception e
) {
653 log
.println("Unexpected exception:") ;
654 e
.printStackTrace(log
) ;
658 tRes
.tested("updateNumericObject()", result
) ;
662 * Finds in relation vector index of column of the appropriate
665 protected int findColumnOfType(Class
<?
> clz
) {
667 for (int i
= 0; i
< rowData
.size(); i
++)
668 if (clz
.isInstance(rowData
.get(i
))) return i
+ 1 ;
673 * Disposes environment.
676 public void after() {
677 disposeEnvironment() ;
680 } // finish class _XRow