1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XRowUpdate.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.util
.Vector
;
35 import lib
.MultiMethodTest
;
37 import util
.ValueComparer
;
39 import com
.sun
.star
.io
.XDataInputStream
;
40 import com
.sun
.star
.io
.XInputStream
;
41 import com
.sun
.star
.io
.XTextInputStream
;
42 import com
.sun
.star
.lang
.XMultiServiceFactory
;
43 import com
.sun
.star
.sdbc
.SQLException
;
44 import com
.sun
.star
.sdbc
.XRow
;
45 import com
.sun
.star
.sdbc
.XRowUpdate
;
46 import com
.sun
.star
.uno
.UnoRuntime
;
47 import com
.sun
.star
.util
.Date
;
48 import com
.sun
.star
.util
.DateTime
;
49 import com
.sun
.star
.util
.Time
;
52 * Testing <code>com.sun.star.sdbc.XRowUpdate</code>
55 * <li><code> updateNull()</code></li>
56 * <li><code> updateBoolean()</code></li>
57 * <li><code> updateByte()</code></li>
58 * <li><code> updateShort()</code></li>
59 * <li><code> updateInt()</code></li>
60 * <li><code> updateLong()</code></li>
61 * <li><code> updateFloat()</code></li>
62 * <li><code> updateDouble()</code></li>
63 * <li><code> updateString()</code></li>
64 * <li><code> updateBytes()</code></li>
65 * <li><code> updateDate()</code></li>
66 * <li><code> updateTime()</code></li>
67 * <li><code> updateTimestamp()</code></li>
68 * <li><code> updateBinaryStream()</code></li>
69 * <li><code> updateCharacterStream()</code></li>
70 * <li><code> updateObject()</code></li>
71 * <li><code> updateNumericObject()</code></li>
73 * Object relations required :
75 * <li> <code>'CurrentRowData'</code> : (may be used in other
76 * interface tests) is a <code>java.util.Vector</code> object
77 * that contains column types and values in current row. Each
78 * element of vector corresponds to appropriate column (element
79 * with index 0 to column 1, 1 -> 2, etc.). <p>
80 * The following <code>XRowUpdate</code> methods correspond to classes
83 * <li> <code>setBinaryStream</code> -
84 * <code>com.sun.star.io.XDataInputStream</code> class. </li>
85 * <li> <code>setCharacterStream</code> -
86 * <code>com.sun.star.io.XTextInputStream</code> class. </li>
87 * <li> <code>setObject</code> -
88 * <code>java.lang.Object[]</code> class, the element with
89 * index 0 must be used. </li>
91 * Other methods uses types they return (i.e. <code>java.lang.String</code>
92 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
93 * for <code>setRef</code> method).
95 * <li> <code>'XRowUpdate.XRow'</code> : implementation of <code>
96 * com.sun.star.sdbc.XRow</code> interface for checking updated data.
99 * The test <b>damages</b> the object, so it is recreated finally.
100 * @see com.sun.star.sdbc.XRowUpdate
101 * @see com.sun.star.sdbc.XRow
103 public class _XRowUpdate
extends MultiMethodTest
{
105 // oObj filled by MultiMethodTest
106 public XRowUpdate oObj
= null ;
108 private Vector rowData
= null ;
109 private XRow row
= null ;
114 public void before() {
115 rowData
= (Vector
) tEnv
.getObjRelation("CurrentRowData") ;
116 if (rowData
== null) {
117 log
.println("!!! 'CurrentRowData' relation not found !!!") ;
119 row
= (XRow
) tEnv
.getObjRelation("XRowUpdate.XRow") ;
120 if (rowData
== null) {
121 log
.println("!!! 'XRowUpdate.XRow' relation not found !!!") ;
126 * Try to set NULL value for each column. Then using <code>XRow</code>
127 * relation check if NULL was really set. <p>
128 * Has OK status if for every column NULL value was successfully set.
129 * @see com.sun.star.sdbc.XRow
131 public void _updateNull() {
132 boolean result
= true ;
133 for (int i
= 0; i
< rowData
.size(); i
++) {
134 if (rowData
.get(i
) == null) continue ;
135 log
.print(" Setting NULL at column #" + (i
+1) + " ...") ;
137 oObj
.updateNull(i
+ 1) ;
139 if (rowData
.get(i
) instanceof String
) row
.getString(i
+ 1) ;
140 if (rowData
.get(i
) instanceof Boolean
) row
.getBoolean(i
+ 1) ;
141 if (rowData
.get(i
) instanceof Byte
) row
.getByte(i
+ 1) ;
142 if (rowData
.get(i
) instanceof Short
) row
.getShort(i
+ 1) ;
143 if (rowData
.get(i
) instanceof Integer
) row
.getInt(i
+ 1) ;
144 if (rowData
.get(i
) instanceof Long
) row
.getLong(i
+ 1) ;
145 if (rowData
.get(i
) instanceof Float
) row
.getFloat(i
+ 1) ;
146 if (rowData
.get(i
) instanceof Double
) row
.getDouble(i
+ 1) ;
147 if (rowData
.get(i
) instanceof byte[]) row
.getBytes(i
+ 1) ;
148 if (rowData
.get(i
) instanceof Date
) row
.getDate(i
+ 1) ;
149 if (rowData
.get(i
) instanceof Time
) row
.getTime(i
+ 1) ;
150 if (rowData
.get(i
) instanceof DateTime
)
151 row
.getTimestamp(i
+ 1) ;
152 if (rowData
.get(i
) instanceof XDataInputStream
)
153 row
.getBinaryStream(i
+ 1) ;
154 if (rowData
.get(i
) instanceof XTextInputStream
)
155 row
.getCharacterStream(i
+ 1) ;
156 //if (rowData.get(i) instanceof Object[]) row.getObject(i) ;
158 if (!row
.wasNull()) {
159 log
.println("FAILED") ;
160 log
.println("Not NULL was returned !!!") ;
165 } catch (SQLException e
) {
166 log
.println("FAILED") ;
167 e
.printStackTrace(log
) ;
172 tRes
.tested("updateNull()", result
) ;
176 * Updates column with the appropriate type (if exists) and then
177 * checks result with interface <code>XRow</code>.<p>
178 * Has OK status if column successfully updated, ahd the same
181 public void _updateBoolean() {
182 boolean result
= true ;
183 int idx
= findColumnOfType(Boolean
.class) ;
186 log
.println("Required type not found") ;
187 tRes
.tested("updateBoolean()", Status
.skipped(true)) ;
192 boolean newVal
= !row
.getBoolean(idx
) ;
193 oObj
.updateBoolean(idx
, newVal
) ;
194 boolean getVal
= row
.getBoolean(idx
) ;
195 result
= newVal
== getVal
;
196 } catch (SQLException e
) {
197 e
.printStackTrace(log
) ;
201 tRes
.tested("updateBoolean()", result
) ;
205 * Updates column with the appropriate type (if exists) and then
206 * checks result with interface <code>XRow</code>.<p>
207 * Has OK status if column successfully updated, ahd the same
210 public void _updateByte() {
211 boolean result
= true ;
212 int idx
= findColumnOfType(Byte
.class) ;
215 log
.println("Required type not found") ;
216 tRes
.tested("updateByte()", Status
.skipped(true)) ;
221 byte newVal
= (byte) (row
.getByte(idx
) + 1) ;
222 oObj
.updateByte(idx
, newVal
) ;
223 byte getVal
= row
.getByte(idx
) ;
224 result
= newVal
== getVal
;
225 } catch (SQLException e
) {
226 e
.printStackTrace(log
) ;
230 tRes
.tested("updateByte()", result
) ;
234 * Updates column with the appropriate type (if exists) and then
235 * checks result with interface <code>XRow</code>.<p>
236 * Has OK status if column successfully updated, ahd the same
239 public void _updateShort() {
240 boolean result
= true ;
241 int idx
= findColumnOfType(Short
.class) ;
244 log
.println("Required type not found") ;
245 tRes
.tested("updateShort()", Status
.skipped(true)) ;
250 short newVal
= (short) (row
.getShort(idx
) + 1) ;
251 oObj
.updateShort(idx
, newVal
) ;
252 short getVal
= row
.getShort(idx
) ;
253 result
= newVal
== getVal
;
254 } catch (SQLException e
) {
255 e
.printStackTrace(log
) ;
259 tRes
.tested("updateShort()", result
) ;
263 * Updates column with the appropriate type (if exists) and then
264 * checks result with interface <code>XRow</code>.<p>
265 * Has OK status if column successfully updated, ahd the same
268 public void _updateInt() {
269 boolean result
= true ;
270 int idx
= findColumnOfType(Integer
.class) ;
273 log
.println("Required type not found") ;
274 tRes
.tested("updateInt()", Status
.skipped(true)) ;
279 int newVal
= 1 + row
.getInt(idx
) ;
280 oObj
.updateInt(idx
, newVal
) ;
281 int getVal
= row
.getInt(idx
) ;
282 result
= newVal
== getVal
;
283 } catch (SQLException e
) {
284 e
.printStackTrace(log
) ;
288 tRes
.tested("updateInt()", result
) ;
292 * Updates column with the appropriate type (if exists) and then
293 * checks result with interface <code>XRow</code>.<p>
294 * Has OK status if column successfully updated, ahd the same
297 public void _updateLong() {
298 boolean result
= true ;
299 int idx
= findColumnOfType(Long
.class) ;
302 log
.println("Required type not found") ;
303 tRes
.tested("updateLong()", Status
.skipped(true)) ;
308 long newVal
= 1 + row
.getLong(idx
) ;
309 oObj
.updateLong(idx
, newVal
) ;
310 long getVal
= row
.getLong(idx
) ;
311 result
= newVal
== getVal
;
312 } catch (SQLException e
) {
313 e
.printStackTrace(log
) ;
317 tRes
.tested("updateLong()", result
) ;
321 * Updates column with the appropriate type (if exists) and then
322 * checks result with interface <code>XRow</code>.<p>
323 * Has OK status if column successfully updated, ahd the same
326 public void _updateFloat() {
327 boolean result
= true ;
328 int idx
= findColumnOfType(Float
.class) ;
331 log
.println("Required type not found") ;
332 tRes
.tested("updateFloat()", Status
.skipped(true)) ;
337 float newVal
= (float) (1.1 + row
.getFloat(idx
));
338 oObj
.updateFloat(idx
, newVal
) ;
339 float getVal
= row
.getFloat(idx
) ;
340 result
= newVal
== getVal
;
341 } catch (SQLException e
) {
342 e
.printStackTrace(log
) ;
346 tRes
.tested("updateFloat()", result
) ;
350 * Updates column with the appropriate type (if exists) and then
351 * checks result with interface <code>XRow</code>.<p>
352 * Has OK status if column successfully updated, ahd the same
355 public void _updateDouble() {
356 boolean result
= true ;
357 int idx
= findColumnOfType(Double
.class) ;
360 log
.println("Required type not found") ;
361 tRes
.tested("updateDouble()", Status
.skipped(true)) ;
366 double newVal
= 1.1 + row
.getDouble(idx
) ;
367 oObj
.updateDouble(idx
, newVal
) ;
368 double getVal
= row
.getDouble(idx
) ;
369 result
= newVal
== getVal
;
370 } catch (SQLException e
) {
371 e
.printStackTrace(log
) ;
375 tRes
.tested("updateDouble()", result
) ;
379 * Updates column with the appropriate type (if exists) and then
380 * checks result with interface <code>XRow</code>.<p>
381 * Has OK status if column successfully updated, ahd the same
384 public void _updateString() {
385 boolean result
= true ;
386 int idx
= findColumnOfType(String
.class) ;
389 log
.println("Required type not found") ;
390 tRes
.tested("updateString()", Status
.skipped(true)) ;
395 String newVal
= "_" + row
.getString(idx
) ;
396 oObj
.updateString(idx
, newVal
) ;
397 String getVal
= row
.getString(idx
) ;
398 result
= newVal
.equals(getVal
) ;
399 log
.println("New value = '" + newVal
+ "', get value = '"
401 } catch (SQLException e
) {
402 e
.printStackTrace(log
) ;
406 tRes
.tested("updateString()", result
) ;
410 * Updates column with the appropriate type (if exists) and then
411 * checks result with interface <code>XRow</code>.<p>
412 * Has OK status if column successfully updated, ahd the same
415 public void _updateBytes() {
416 boolean result
= true ;
417 int idx
= findColumnOfType(byte[].class) ;
420 log
.println("Required type not found") ;
421 tRes
.tested("updateBytes()", Status
.skipped(true)) ;
426 byte[] newVal
= row
.getBytes(idx
) ;
427 if (newVal
== null || newVal
.length
== 0) {
428 newVal
= new byte[] {34, 111, 98} ;
430 newVal
= new byte[] {(byte) (newVal
[0] + 1), 111, 98} ;
432 oObj
.updateBytes(idx
, newVal
) ;
433 byte[] getVal
= row
.getBytes(idx
) ;
434 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
435 } catch (SQLException e
) {
436 e
.printStackTrace(log
) ;
440 tRes
.tested("updateBytes()", result
) ;
444 * Updates column with the appropriate type (if exists) and then
445 * checks result with interface <code>XRow</code>.<p>
446 * Has OK status if column successfully updated, ahd the same
449 public void _updateDate() {
450 boolean result
= true ;
451 int idx
= findColumnOfType(Date
.class) ;
454 log
.println("Required type not found") ;
455 tRes
.tested("updateDate()", Status
.skipped(true)) ;
460 Date newVal
= row
.getDate(idx
) ;
462 oObj
.updateDate(idx
, newVal
) ;
463 Date getVal
= row
.getDate(idx
) ;
464 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
465 } catch (SQLException e
) {
466 e
.printStackTrace(log
) ;
470 tRes
.tested("updateDate()", result
) ;
474 * Updates column with the appropriate type (if exists) and then
475 * checks result with interface <code>XRow</code>.<p>
476 * Has OK status if column successfully updated, ahd the same
479 public void _updateTime() {
480 boolean result
= true ;
481 int idx
= findColumnOfType(Time
.class) ;
484 log
.println("Required type not found") ;
485 tRes
.tested("updateTime()", Status
.skipped(true)) ;
490 Time newVal
= row
.getTime(idx
) ;
492 oObj
.updateTime(idx
, newVal
) ;
493 Time getVal
= row
.getTime(idx
) ;
494 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
495 } catch (SQLException e
) {
496 e
.printStackTrace(log
) ;
500 tRes
.tested("updateTime()", result
) ;
504 * Updates column with the appropriate type (if exists) and then
505 * checks result with interface <code>XRow</code>.<p>
506 * Has OK status if column successfully updated, ahd the same
509 public void _updateTimestamp() {
510 boolean result
= true ;
511 int idx
= findColumnOfType(DateTime
.class) ;
514 log
.println("Required type not found") ;
515 tRes
.tested("updateTimestamp()", Status
.skipped(true)) ;
520 DateTime newVal
= row
.getTimestamp(idx
) ;
522 oObj
.updateTimestamp(idx
, newVal
) ;
523 DateTime getVal
= row
.getTimestamp(idx
) ;
524 result
= ValueComparer
.equalValue(newVal
, getVal
) ;
525 } catch (SQLException e
) {
526 e
.printStackTrace(log
) ;
530 tRes
.tested("updateTimestamp()", result
) ;
535 * Updates column with the appropriate type (if exists) and then
536 * checks result with interface <code>XRow</code>.<p>
537 * Has OK status if column successfully updated, ahd the same
540 public void _updateBinaryStream() {
541 boolean result
= true ;
542 int idx
= findColumnOfType(XDataInputStream
.class) ;
545 log
.println("Required type not found") ;
546 tRes
.tested("updateBinaryStream()", Status
.skipped(true)) ;
551 Object oStream
= ((XMultiServiceFactory
)tParam
.getMSF()).
552 createInstance("com.sun.star.io.DataInputStream") ;
553 XInputStream newVal
= (XInputStream
) UnoRuntime
.queryInterface
554 (XInputStream
.class, oStream
);
556 oObj
.updateBinaryStream(idx
, newVal
, 0) ;
557 XInputStream getVal
= row
.getBinaryStream(idx
) ;
558 result
= UnoRuntime
.areSame(newVal
, getVal
) ;
559 } catch (SQLException e
) {
560 e
.printStackTrace(log
) ;
562 } catch (com
.sun
.star
.uno
.Exception e
) {
563 log
.println("Unexpected exception:") ;
564 e
.printStackTrace(log
) ;
568 tRes
.tested("updateBinaryStream()", result
) ;
572 * Updates column with the appropriate type (if exists) and then
573 * checks result with interface <code>XRow</code>.<p>
574 * Has OK status if column successfully updated, ahd the same
577 public void _updateCharacterStream() {
578 boolean result
= true ;
579 int idx
= findColumnOfType(XTextInputStream
.class) ;
582 log
.println("Required type not found") ;
583 tRes
.tested("updateCharacterStream()", Status
.skipped(true)) ;
588 Object oStream
= ((XMultiServiceFactory
)tParam
.getMSF()).
589 createInstance("com.sun.star.io.TextInputStream") ;
590 XInputStream newVal
= (XInputStream
) UnoRuntime
.queryInterface
591 (XInputStream
.class, oStream
);
593 oObj
.updateCharacterStream(idx
, newVal
, 0) ;
594 XInputStream getVal
= row
.getCharacterStream(idx
) ;
595 result
= UnoRuntime
.areSame(newVal
, getVal
) ;
596 } catch (SQLException e
) {
597 e
.printStackTrace(log
) ;
599 } catch (com
.sun
.star
.uno
.Exception e
) {
600 log
.println("Unexpected exception:") ;
601 e
.printStackTrace(log
) ;
605 tRes
.tested("updateCharacterStream()", result
) ;
609 * Updates column with the appropriate type (if exists) and then
610 * checks result with interface <code>XRow</code>.<p>
611 * Has OK status if column successfully updated, ahd the same
614 public void _updateObject() {
615 boolean result
= true ;
616 int idx
= findColumnOfType(Object
[].class) ;
619 log
.println("Required type not found") ;
620 tRes
.tested("updateObject()", Status
.skipped(true)) ;
625 Object newVal
= ((XMultiServiceFactory
)tParam
.getMSF()).
626 createInstance("com.sun.star.io.Pipe") ;
628 oObj
.updateObject(idx
, newVal
) ;
629 //Object getVal = row.getObject(idx) ;
630 //result = UnoRuntime.areSame(newVal, getVal) ;
631 } catch (SQLException e
) {
632 e
.printStackTrace(log
) ;
634 } catch (com
.sun
.star
.uno
.Exception e
) {
635 log
.println("Unexpected exception:") ;
636 e
.printStackTrace(log
) ;
640 tRes
.tested("updateObject()", result
) ;
644 * Updates column with the appropriate type (if exists) and then
645 * checks result with interface <code>XRow</code>.<p>
646 * Has OK status if column successfully updated, ahd the same
649 public void _updateNumericObject() {
650 boolean result
= true ;
651 int idx
= findColumnOfType(Object
[].class) ;
654 log
.println("Required type not found") ;
655 tRes
.tested("updateNumericObject()", Status
.skipped(true)) ;
660 Object newVal
= ((XMultiServiceFactory
)tParam
.getMSF()).
661 createInstance("com.sun.star.io.Pipe") ;
663 oObj
.updateNumericObject(idx
, newVal
, 0) ;
664 //Object getVal = row.getObject(idx) ;
665 //result = UnoRuntime.areSame(newVal, getVal) ;
666 } catch (SQLException e
) {
667 e
.printStackTrace(log
) ;
669 } catch (com
.sun
.star
.uno
.Exception e
) {
670 log
.println("Unexpected exception:") ;
671 e
.printStackTrace(log
) ;
675 tRes
.tested("updateNumericObject()", result
) ;
679 * Finds in relation vector index of column of the appropriate
682 protected int findColumnOfType(Class clz
) {
684 for (int i
= 0; i
< rowData
.size(); i
++)
685 if (clz
.isInstance(rowData
.get(i
))) return i
+ 1 ;
690 * Disposes environment.
692 public void after() {
693 disposeEnvironment() ;
696 } // finish class _XRow