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
;
26 import util
.ValueComparer
;
28 import com
.sun
.star
.io
.XDataInputStream
;
29 import com
.sun
.star
.io
.XInputStream
;
30 import com
.sun
.star
.io
.XTextInputStream
;
31 import com
.sun
.star
.sdbc
.SQLException
;
32 import com
.sun
.star
.sdbc
.XRow
;
33 import com
.sun
.star
.sdbc
.XRowUpdate
;
34 import com
.sun
.star
.uno
.UnoRuntime
;
35 import com
.sun
.star
.util
.Date
;
36 import com
.sun
.star
.util
.DateTime
;
37 import com
.sun
.star
.util
.Time
;
40 * Testing <code>com.sun.star.sdbc.XRowUpdate</code>
43 * <li><code> updateNull()</code></li>
44 * <li><code> updateBoolean()</code></li>
45 * <li><code> updateByte()</code></li>
46 * <li><code> updateShort()</code></li>
47 * <li><code> updateInt()</code></li>
48 * <li><code> updateLong()</code></li>
49 * <li><code> updateFloat()</code></li>
50 * <li><code> updateDouble()</code></li>
51 * <li><code> updateString()</code></li>
52 * <li><code> updateBytes()</code></li>
53 * <li><code> updateDate()</code></li>
54 * <li><code> updateTime()</code></li>
55 * <li><code> updateTimestamp()</code></li>
56 * <li><code> updateBinaryStream()</code></li>
57 * <li><code> updateCharacterStream()</code></li>
58 * <li><code> updateObject()</code></li>
59 * <li><code> updateNumericObject()</code></li>
61 * Object relations required :
63 * <li> <code>'CurrentRowData'</code> : (may be used in other
64 * interface tests) is a <code>java.util.Vector</code> object
65 * that contains column types and values in current row. Each
66 * element of vector corresponds to appropriate column (element
67 * with index 0 to column 1, 1 -> 2, etc.). <p>
68 * The following <code>XRowUpdate</code> methods correspond to classes
71 * <li> <code>setBinaryStream</code> -
72 * <code>com.sun.star.io.XDataInputStream</code> class. </li>
73 * <li> <code>setCharacterStream</code> -
74 * <code>com.sun.star.io.XTextInputStream</code> class. </li>
75 * <li> <code>setObject</code> -
76 * <code>java.lang.Object[]</code> class, the element with
77 * index 0 must be used. </li>
79 * Other methods uses types they return (i.e. <code>String</code>
80 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
81 * for <code>setRef</code> method).
83 * <li> <code>'XRowUpdate.XRow'</code> : implementation of <code>
84 * com.sun.star.sdbc.XRow</code> interface for checking updated data.
87 * The test <b>damages</b> the object, so it is recreated finally.
88 * @see com.sun.star.sdbc.XRowUpdate
89 * @see com.sun.star.sdbc.XRow
91 public class _XRowUpdate
extends MultiMethodTest
{
93 // oObj filled by MultiMethodTest
94 public XRowUpdate oObj
= null ;
96 private List
<Object
> rowData
= null ;
97 private XRow row
= null ;
102 @SuppressWarnings("unchecked")
104 public void before() {
105 rowData
= (List
<Object
>) tEnv
.getObjRelation("CurrentRowData") ;
106 if (rowData
== null) {
107 log
.println("!!! 'CurrentRowData' relation not found !!!") ;
109 row
= (XRow
) tEnv
.getObjRelation("XRowUpdate.XRow") ;
110 if (rowData
== null) {
111 log
.println("!!! 'XRowUpdate.XRow' relation not found !!!") ;
116 * Try to set NULL value for each column. Then using <code>XRow</code>
117 * relation check if NULL was really set. <p>
118 * Has OK status if for every column NULL value was successfully set.
119 * @see com.sun.star.sdbc.XRow
121 public void _updateNull() {
122 boolean result
= true ;
123 for (int i
= 0; i
< rowData
.size(); i
++) {
124 if (rowData
.get(i
) == null) continue ;
125 log
.print(" Setting NULL at column #" + (i
+1) + " ...") ;
127 oObj
.updateNull(i
+ 1) ;
129 if (rowData
.get(i
) instanceof String
) row
.getString(i
+ 1) ;
130 if (rowData
.get(i
) instanceof Boolean
) row
.getBoolean(i
+ 1) ;
131 if (rowData
.get(i
) instanceof Byte
) row
.getByte(i
+ 1) ;
132 if (rowData
.get(i
) instanceof Short
) row
.getShort(i
+ 1) ;
133 if (rowData
.get(i
) instanceof Integer
) row
.getInt(i
+ 1) ;
134 if (rowData
.get(i
) instanceof Long
) row
.getLong(i
+ 1) ;
135 if (rowData
.get(i
) instanceof Float
) row
.getFloat(i
+ 1) ;
136 if (rowData
.get(i
) instanceof Double
) row
.getDouble(i
+ 1) ;
137 if (rowData
.get(i
) instanceof byte[]) row
.getBytes(i
+ 1) ;
138 if (rowData
.get(i
) instanceof Date
) row
.getDate(i
+ 1) ;
139 if (rowData
.get(i
) instanceof Time
) row
.getTime(i
+ 1) ;
140 if (rowData
.get(i
) instanceof DateTime
)
141 row
.getTimestamp(i
+ 1) ;
142 if (rowData
.get(i
) instanceof XDataInputStream
)
143 row
.getBinaryStream(i
+ 1) ;
144 if (rowData
.get(i
) instanceof XTextInputStream
)
145 row
.getCharacterStream(i
+ 1) ;
147 if (!row
.wasNull()) {
148 log
.println("FAILED") ;
149 log
.println("Not NULL was returned !!!") ;
154 } catch (SQLException e
) {
155 log
.println("FAILED") ;
156 e
.printStackTrace(log
) ;
161 tRes
.tested("updateNull()", result
) ;
165 * Updates column with the appropriate type (if exists) and then
166 * checks result with interface <code>XRow</code>.<p>
167 * Has OK status if column successfully updated, ahd the same
170 public void _updateBoolean() {
171 boolean result
= true ;
172 int idx
= findColumnOfType(Boolean
.class) ;
175 log
.println("Required type not found") ;
176 tRes
.tested("updateBoolean()", Status
.skipped(true)) ;
181 boolean newVal
= !row
.getBoolean(idx
) ;
182 oObj
.updateBoolean(idx
, newVal
) ;
183 boolean getVal
= row
.getBoolean(idx
) ;
184 result
= newVal
== getVal
;
185 } catch (SQLException e
) {
186 e
.printStackTrace(log
) ;
190 tRes
.tested("updateBoolean()", result
) ;
194 * Updates column with the appropriate type (if exists) and then
195 * checks result with interface <code>XRow</code>.<p>
196 * Has OK status if column successfully updated, ahd the same
199 public void _updateByte() {
200 boolean result
= true ;
201 int idx
= findColumnOfType(Byte
.class) ;
204 log
.println("Required type not found") ;
205 tRes
.tested("updateByte()", Status
.skipped(true)) ;
210 byte newVal
= (byte) (row
.getByte(idx
) + 1) ;
211 oObj
.updateByte(idx
, newVal
) ;
212 byte getVal
= row
.getByte(idx
) ;
213 result
= newVal
== getVal
;
214 } catch (SQLException e
) {
215 e
.printStackTrace(log
) ;
219 tRes
.tested("updateByte()", result
) ;
223 * Updates column with the appropriate type (if exists) and then
224 * checks result with interface <code>XRow</code>.<p>
225 * Has OK status if column successfully updated, ahd the same
228 public void _updateShort() {
229 boolean result
= true ;
230 int idx
= findColumnOfType(Short
.class) ;
233 log
.println("Required type not found") ;
234 tRes
.tested("updateShort()", Status
.skipped(true)) ;
239 short newVal
= (short) (row
.getShort(idx
) + 1) ;
240 oObj
.updateShort(idx
, newVal
) ;
241 short getVal
= row
.getShort(idx
) ;
242 result
= newVal
== getVal
;
243 } catch (SQLException e
) {
244 e
.printStackTrace(log
) ;
248 tRes
.tested("updateShort()", result
) ;
252 * Updates column with the appropriate type (if exists) and then
253 * checks result with interface <code>XRow</code>.<p>
254 * Has OK status if column successfully updated, ahd the same
257 public void _updateInt() {
258 boolean result
= true ;
259 int idx
= findColumnOfType(Integer
.class) ;
262 log
.println("Required type not found") ;
263 tRes
.tested("updateInt()", Status
.skipped(true)) ;
268 int newVal
= 1 + row
.getInt(idx
) ;
269 oObj
.updateInt(idx
, newVal
) ;
270 int getVal
= row
.getInt(idx
) ;
271 result
= newVal
== getVal
;
272 } catch (SQLException e
) {
273 e
.printStackTrace(log
) ;
277 tRes
.tested("updateInt()", result
) ;
281 * Updates column with the appropriate type (if exists) and then
282 * checks result with interface <code>XRow</code>.<p>
283 * Has OK status if column successfully updated, ahd the same
286 public void _updateLong() {
287 boolean result
= true ;
288 int idx
= findColumnOfType(Long
.class) ;
291 log
.println("Required type not found") ;
292 tRes
.tested("updateLong()", Status
.skipped(true)) ;
297 long newVal
= 1 + row
.getLong(idx
) ;
298 oObj
.updateLong(idx
, newVal
) ;
299 long getVal
= row
.getLong(idx
) ;
300 result
= newVal
== getVal
;
301 } catch (SQLException e
) {
302 e
.printStackTrace(log
) ;
306 tRes
.tested("updateLong()", result
) ;
310 * Updates column with the appropriate type (if exists) and then
311 * checks result with interface <code>XRow</code>.<p>
312 * Has OK status if column successfully updated, ahd the same
315 public void _updateFloat() {
316 boolean result
= true ;
317 int idx
= findColumnOfType(Float
.class) ;
320 log
.println("Required type not found") ;
321 tRes
.tested("updateFloat()", Status
.skipped(true)) ;
326 float newVal
= (float) (1.1 + row
.getFloat(idx
));
327 oObj
.updateFloat(idx
, newVal
) ;
328 float getVal
= row
.getFloat(idx
) ;
329 result
= newVal
== getVal
;
330 } catch (SQLException e
) {
331 e
.printStackTrace(log
) ;
335 tRes
.tested("updateFloat()", result
) ;
339 * Updates column with the appropriate type (if exists) and then
340 * checks result with interface <code>XRow</code>.<p>
341 * Has OK status if column successfully updated, ahd the same
344 public void _updateDouble() {
345 boolean result
= true ;
346 int idx
= findColumnOfType(Double
.class) ;
349 log
.println("Required type not found") ;
350 tRes
.tested("updateDouble()", Status
.skipped(true)) ;
355 double newVal
= 1.1 + row
.getDouble(idx
) ;
356 oObj
.updateDouble(idx
, newVal
) ;
357 double getVal
= row
.getDouble(idx
) ;
358 result
= utils
.approxEqual(newVal
, getVal
);
359 } catch (SQLException e
) {
360 e
.printStackTrace(log
) ;
364 tRes
.tested("updateDouble()", result
) ;
368 * Updates column with the appropriate type (if exists) and then
369 * checks result with interface <code>XRow</code>.<p>
370 * Has OK status if column successfully updated, ahd the same
373 public void _updateString() {
374 boolean result
= true ;
375 int idx
= findColumnOfType(String
.class) ;
378 log
.println("Required type not found") ;
379 tRes
.tested("updateString()", Status
.skipped(true)) ;
384 String newVal
= "_" + row
.getString(idx
) ;
385 oObj
.updateString(idx
, newVal
) ;
386 String getVal
= row
.getString(idx
) ;
387 result
= newVal
.equals(getVal
) ;
388 log
.println("New value = '" + newVal
+ "', get value = '"
390 } catch (SQLException e
) {
391 e
.printStackTrace(log
) ;
395 tRes
.tested("updateString()", result
) ;
399 * Updates column with the appropriate type (if exists) and then
400 * checks result with interface <code>XRow</code>.<p>
401 * Has OK status if column successfully updated, ahd the same
404 public void _updateBytes() {
405 boolean result
= true ;
406 int idx
= findColumnOfType(byte[].class) ;
409 log
.println("Required type not found") ;
410 tRes
.tested("updateBytes()", Status
.skipped(true)) ;
415 byte[] newVal
= row
.getBytes(idx
) ;
416 if (newVal
== null || newVal
.length
== 0) {
417 newVal
= new byte[] {34, 111, 98} ;
419 newVal
= new byte[] {(byte) (newVal
[0] + 1), 111, 98} ;
421 oObj
.updateBytes(idx
, newVal
) ;
422 byte[] getVal
= row
.getBytes(idx
) ;
423 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
424 } catch (SQLException e
) {
425 e
.printStackTrace(log
) ;
429 tRes
.tested("updateBytes()", result
) ;
433 * Updates column with the appropriate type (if exists) and then
434 * checks result with interface <code>XRow</code>.<p>
435 * Has OK status if column successfully updated, ahd the same
438 public void _updateDate() {
439 boolean result
= true ;
440 int idx
= findColumnOfType(Date
.class) ;
443 log
.println("Required type not found") ;
444 tRes
.tested("updateDate()", Status
.skipped(true)) ;
449 Date newVal
= row
.getDate(idx
) ;
451 oObj
.updateDate(idx
, newVal
) ;
452 Date getVal
= row
.getDate(idx
) ;
453 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
454 } catch (SQLException e
) {
455 e
.printStackTrace(log
) ;
459 tRes
.tested("updateDate()", result
) ;
463 * Updates column with the appropriate type (if exists) and then
464 * checks result with interface <code>XRow</code>.<p>
465 * Has OK status if column successfully updated, ahd the same
468 public void _updateTime() {
469 boolean result
= true ;
470 int idx
= findColumnOfType(Time
.class) ;
473 log
.println("Required type not found") ;
474 tRes
.tested("updateTime()", Status
.skipped(true)) ;
479 Time newVal
= row
.getTime(idx
) ;
481 oObj
.updateTime(idx
, newVal
) ;
482 Time getVal
= row
.getTime(idx
) ;
483 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
484 } catch (SQLException e
) {
485 e
.printStackTrace(log
) ;
489 tRes
.tested("updateTime()", result
) ;
493 * Updates column with the appropriate type (if exists) and then
494 * checks result with interface <code>XRow</code>.<p>
495 * Has OK status if column successfully updated, ahd the same
498 public void _updateTimestamp() {
499 boolean result
= true ;
500 int idx
= findColumnOfType(DateTime
.class) ;
503 log
.println("Required type not found") ;
504 tRes
.tested("updateTimestamp()", Status
.skipped(true)) ;
509 DateTime newVal
= row
.getTimestamp(idx
) ;
511 oObj
.updateTimestamp(idx
, newVal
) ;
512 DateTime getVal
= row
.getTimestamp(idx
) ;
513 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
514 } catch (SQLException e
) {
515 e
.printStackTrace(log
) ;
519 tRes
.tested("updateTimestamp()", result
) ;
524 * Updates column with the appropriate type (if exists) and then
525 * checks result with interface <code>XRow</code>.<p>
526 * Has OK status if column successfully updated, ahd the same
529 public void _updateBinaryStream() {
530 boolean result
= true ;
531 int idx
= findColumnOfType(XDataInputStream
.class) ;
534 log
.println("Required type not found") ;
535 tRes
.tested("updateBinaryStream()", Status
.skipped(true)) ;
540 Object oStream
= tParam
.getMSF().
541 createInstance("com.sun.star.io.DataInputStream") ;
542 XInputStream newVal
= UnoRuntime
.queryInterface
543 (XInputStream
.class, oStream
);
545 oObj
.updateBinaryStream(idx
, newVal
, 0) ;
546 XInputStream getVal
= row
.getBinaryStream(idx
) ;
547 result
= UnoRuntime
.areSame(newVal
, getVal
) ;
548 } catch (SQLException e
) {
549 e
.printStackTrace(log
) ;
551 } catch (com
.sun
.star
.uno
.Exception e
) {
552 log
.println("Unexpected exception:") ;
553 e
.printStackTrace(log
) ;
557 tRes
.tested("updateBinaryStream()", result
) ;
561 * Updates column with the appropriate type (if exists) and then
562 * checks result with interface <code>XRow</code>.<p>
563 * Has OK status if column successfully updated, ahd the same
566 public void _updateCharacterStream() {
567 boolean result
= true ;
568 int idx
= findColumnOfType(XTextInputStream
.class) ;
571 log
.println("Required type not found") ;
572 tRes
.tested("updateCharacterStream()", Status
.skipped(true)) ;
577 Object oStream
= tParam
.getMSF().
578 createInstance("com.sun.star.io.TextInputStream") ;
579 XInputStream newVal
= UnoRuntime
.queryInterface
580 (XInputStream
.class, oStream
);
582 oObj
.updateCharacterStream(idx
, newVal
, 0) ;
583 XInputStream getVal
= row
.getCharacterStream(idx
) ;
584 result
= UnoRuntime
.areSame(newVal
, getVal
) ;
585 } catch (SQLException e
) {
586 e
.printStackTrace(log
) ;
588 } catch (com
.sun
.star
.uno
.Exception e
) {
589 log
.println("Unexpected exception:") ;
590 e
.printStackTrace(log
) ;
594 tRes
.tested("updateCharacterStream()", result
) ;
598 * Updates column with the appropriate type (if exists) and then
599 * checks result with interface <code>XRow</code>.<p>
600 * Has OK status if column successfully updated, ahd the same
603 public void _updateObject() {
604 boolean result
= true ;
605 int idx
= findColumnOfType(Object
[].class) ;
608 log
.println("Required type not found") ;
609 tRes
.tested("updateObject()", Status
.skipped(true)) ;
614 Object newVal
= tParam
.getMSF().
615 createInstance("com.sun.star.io.Pipe") ;
617 oObj
.updateObject(idx
, newVal
) ;
618 } catch (SQLException e
) {
619 e
.printStackTrace(log
) ;
621 } catch (com
.sun
.star
.uno
.Exception e
) {
622 log
.println("Unexpected exception:") ;
623 e
.printStackTrace(log
) ;
627 tRes
.tested("updateObject()", result
) ;
631 * Updates column with the appropriate type (if exists) and then
632 * checks result with interface <code>XRow</code>.<p>
633 * Has OK status if column successfully updated, ahd the same
636 public void _updateNumericObject() {
637 boolean result
= true ;
638 int idx
= findColumnOfType(Object
[].class) ;
641 log
.println("Required type not found") ;
642 tRes
.tested("updateNumericObject()", Status
.skipped(true)) ;
647 Object newVal
= tParam
.getMSF().
648 createInstance("com.sun.star.io.Pipe") ;
650 oObj
.updateNumericObject(idx
, newVal
, 0) ;
651 } catch (SQLException e
) {
652 e
.printStackTrace(log
) ;
654 } catch (com
.sun
.star
.uno
.Exception e
) {
655 log
.println("Unexpected exception:") ;
656 e
.printStackTrace(log
) ;
660 tRes
.tested("updateNumericObject()", result
) ;
664 * Finds in relation vector index of column of the appropriate
667 protected int findColumnOfType(Class
<?
> clz
) {
669 for (int i
= 0; i
< rowData
.size(); i
++)
670 if (clz
.isInstance(rowData
.get(i
))) return i
+ 1 ;
675 * Disposes environment.
678 public void after() {
679 disposeEnvironment() ;
682 } // finish class _XRow