Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / sdbc / _XRowUpdate.java
blob343b1de4dec0a0f138705122236a7097f7f89b7d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XRowUpdate.java,v $
10 * $Revision: 1.6 $
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 ************************************************************************/
31 package ifc.sdbc;
33 import java.util.Vector;
35 import lib.MultiMethodTest;
36 import lib.Status;
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;
51 /**
52 * Testing <code>com.sun.star.sdbc.XRowUpdate</code>
53 * interface methods :
54 * <ul>
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>
72 * </ul> <p>
73 * Object relations required :
74 * <ul>
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
81 * in Vector :
82 * <ul>
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>
90 * </ul>
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).
94 * </li>
95 * <li> <code>'XRowUpdate.XRow'</code> : implementation of <code>
96 * com.sun.star.sdbc.XRow</code> interface for checking updated data.
97 * </li>
98 * </ul> <p>
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 ;
112 * Gets relations.
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) + " ...") ;
136 try {
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 !!!") ;
161 result = false ;
162 } else {
163 log.println("OK") ;
165 } catch (SQLException e) {
166 log.println("FAILED") ;
167 e.printStackTrace(log) ;
168 result = false ;
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
179 * result returned.
181 public void _updateBoolean() {
182 boolean result = true ;
183 int idx = findColumnOfType(Boolean.class) ;
185 if (idx < 0) {
186 log.println("Required type not found") ;
187 tRes.tested("updateBoolean()", Status.skipped(true)) ;
188 return ;
191 try {
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) ;
198 result = false ;
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
208 * result returned.
210 public void _updateByte() {
211 boolean result = true ;
212 int idx = findColumnOfType(Byte.class) ;
214 if (idx < 0) {
215 log.println("Required type not found") ;
216 tRes.tested("updateByte()", Status.skipped(true)) ;
217 return ;
220 try {
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) ;
227 result = false ;
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
237 * result returned.
239 public void _updateShort() {
240 boolean result = true ;
241 int idx = findColumnOfType(Short.class) ;
243 if (idx < 0) {
244 log.println("Required type not found") ;
245 tRes.tested("updateShort()", Status.skipped(true)) ;
246 return ;
249 try {
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) ;
256 result = false ;
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
266 * result returned.
268 public void _updateInt() {
269 boolean result = true ;
270 int idx = findColumnOfType(Integer.class) ;
272 if (idx < 0) {
273 log.println("Required type not found") ;
274 tRes.tested("updateInt()", Status.skipped(true)) ;
275 return ;
278 try {
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) ;
285 result = false ;
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
295 * result returned.
297 public void _updateLong() {
298 boolean result = true ;
299 int idx = findColumnOfType(Long.class) ;
301 if (idx < 0) {
302 log.println("Required type not found") ;
303 tRes.tested("updateLong()", Status.skipped(true)) ;
304 return ;
307 try {
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) ;
314 result = false ;
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
324 * result returned.
326 public void _updateFloat() {
327 boolean result = true ;
328 int idx = findColumnOfType(Float.class) ;
330 if (idx < 0) {
331 log.println("Required type not found") ;
332 tRes.tested("updateFloat()", Status.skipped(true)) ;
333 return ;
336 try {
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) ;
343 result = false ;
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
353 * result returned.
355 public void _updateDouble() {
356 boolean result = true ;
357 int idx = findColumnOfType(Double.class) ;
359 if (idx < 0) {
360 log.println("Required type not found") ;
361 tRes.tested("updateDouble()", Status.skipped(true)) ;
362 return ;
365 try {
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) ;
372 result = false ;
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
382 * result returned.
384 public void _updateString() {
385 boolean result = true ;
386 int idx = findColumnOfType(String.class) ;
388 if (idx < 0) {
389 log.println("Required type not found") ;
390 tRes.tested("updateString()", Status.skipped(true)) ;
391 return ;
394 try {
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 = '"
400 + getVal + "'") ;
401 } catch (SQLException e) {
402 e.printStackTrace(log) ;
403 result = false ;
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
413 * result returned.
415 public void _updateBytes() {
416 boolean result = true ;
417 int idx = findColumnOfType(byte[].class) ;
419 if (idx < 0) {
420 log.println("Required type not found") ;
421 tRes.tested("updateBytes()", Status.skipped(true)) ;
422 return ;
425 try {
426 byte[] newVal = row.getBytes(idx) ;
427 if (newVal == null || newVal.length == 0) {
428 newVal = new byte[] {34, 111, 98} ;
429 } else {
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) ;
437 result = false ;
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
447 * result returned.
449 public void _updateDate() {
450 boolean result = true ;
451 int idx = findColumnOfType(Date.class) ;
453 if (idx < 0) {
454 log.println("Required type not found") ;
455 tRes.tested("updateDate()", Status.skipped(true)) ;
456 return ;
459 try {
460 Date newVal = row.getDate(idx) ;
461 newVal.Year ++ ;
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) ;
467 result = false ;
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
477 * result returned.
479 public void _updateTime() {
480 boolean result = true ;
481 int idx = findColumnOfType(Time.class) ;
483 if (idx < 0) {
484 log.println("Required type not found") ;
485 tRes.tested("updateTime()", Status.skipped(true)) ;
486 return ;
489 try {
490 Time newVal = row.getTime(idx) ;
491 newVal.Seconds ++ ;
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) ;
497 result = false ;
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
507 * result returned.
509 public void _updateTimestamp() {
510 boolean result = true ;
511 int idx = findColumnOfType(DateTime.class) ;
513 if (idx < 0) {
514 log.println("Required type not found") ;
515 tRes.tested("updateTimestamp()", Status.skipped(true)) ;
516 return ;
519 try {
520 DateTime newVal = row.getTimestamp(idx) ;
521 newVal.Year ++ ;
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) ;
527 result = false ;
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
538 * result returned.
540 public void _updateBinaryStream() {
541 boolean result = true ;
542 int idx = findColumnOfType(XDataInputStream.class) ;
544 if (idx < 0) {
545 log.println("Required type not found") ;
546 tRes.tested("updateBinaryStream()", Status.skipped(true)) ;
547 return ;
550 try {
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) ;
561 result = false ;
562 } catch (com.sun.star.uno.Exception e) {
563 log.println("Unexpected exception:") ;
564 e.printStackTrace(log) ;
565 result = false ;
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
575 * result returned.
577 public void _updateCharacterStream() {
578 boolean result = true ;
579 int idx = findColumnOfType(XTextInputStream.class) ;
581 if (idx < 0) {
582 log.println("Required type not found") ;
583 tRes.tested("updateCharacterStream()", Status.skipped(true)) ;
584 return ;
587 try {
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) ;
598 result = false ;
599 } catch (com.sun.star.uno.Exception e) {
600 log.println("Unexpected exception:") ;
601 e.printStackTrace(log) ;
602 result = false ;
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
612 * result returned.
614 public void _updateObject() {
615 boolean result = true ;
616 int idx = findColumnOfType(Object[].class) ;
618 if (idx < 0) {
619 log.println("Required type not found") ;
620 tRes.tested("updateObject()", Status.skipped(true)) ;
621 return ;
624 try {
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) ;
633 result = false ;
634 } catch (com.sun.star.uno.Exception e) {
635 log.println("Unexpected exception:") ;
636 e.printStackTrace(log) ;
637 result = false ;
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
647 * result returned.
649 public void _updateNumericObject() {
650 boolean result = true ;
651 int idx = findColumnOfType(Object[].class) ;
653 if (idx < 0) {
654 log.println("Required type not found") ;
655 tRes.tested("updateNumericObject()", Status.skipped(true)) ;
656 return ;
659 try {
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) ;
668 result = false ;
669 } catch (com.sun.star.uno.Exception e) {
670 log.println("Unexpected exception:") ;
671 e.printStackTrace(log) ;
672 result = false ;
675 tRes.tested("updateNumericObject()", result) ;
679 * Finds in relation vector index of column of the appropriate
680 * type.
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 ;
686 return -1 ;
690 * Disposes environment.
692 public void after() {
693 disposeEnvironment() ;
696 } // finish class _XRow