Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / sdbc / _XRowUpdate.java
blob68e76a9c6519f09c6f68e8002e49bd44ef8e7080
1 /*
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 .
19 package ifc.sdbc;
21 import java.util.List;
23 import lib.MultiMethodTest;
24 import lib.Status;
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;
38 /**
39 * Testing <code>com.sun.star.sdbc.XRowUpdate</code>
40 * interface methods :
41 * <ul>
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>
59 * </ul> <p>
60 * Object relations required :
61 * <ul>
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
68 * in Vector :
69 * <ul>
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>
77 * </ul>
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).
81 * </li>
82 * <li> <code>'XRowUpdate.XRow'</code> : implementation of <code>
83 * com.sun.star.sdbc.XRow</code> interface for checking updated data.
84 * </li>
85 * </ul> <p>
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 ;
98 /**
99 * Gets relations.
101 @Override
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) + " ...") ;
124 try {
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 !!!") ;
148 result = false ;
149 } else {
150 log.println("OK") ;
152 } catch (SQLException e) {
153 log.println("FAILED") ;
154 e.printStackTrace(log) ;
155 result = false ;
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
166 * result returned.
168 public void _updateBoolean() {
169 boolean result = true ;
170 int idx = findColumnOfType(Boolean.class) ;
172 if (idx < 0) {
173 log.println("Required type not found") ;
174 tRes.tested("updateBoolean()", Status.skipped(true)) ;
175 return ;
178 try {
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) ;
185 result = false ;
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
195 * result returned.
197 public void _updateByte() {
198 boolean result = true ;
199 int idx = findColumnOfType(Byte.class) ;
201 if (idx < 0) {
202 log.println("Required type not found") ;
203 tRes.tested("updateByte()", Status.skipped(true)) ;
204 return ;
207 try {
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) ;
214 result = false ;
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
224 * result returned.
226 public void _updateShort() {
227 boolean result = true ;
228 int idx = findColumnOfType(Short.class) ;
230 if (idx < 0) {
231 log.println("Required type not found") ;
232 tRes.tested("updateShort()", Status.skipped(true)) ;
233 return ;
236 try {
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) ;
243 result = false ;
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
253 * result returned.
255 public void _updateInt() {
256 boolean result = true ;
257 int idx = findColumnOfType(Integer.class) ;
259 if (idx < 0) {
260 log.println("Required type not found") ;
261 tRes.tested("updateInt()", Status.skipped(true)) ;
262 return ;
265 try {
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) ;
272 result = false ;
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
282 * result returned.
284 public void _updateLong() {
285 boolean result = true ;
286 int idx = findColumnOfType(Long.class) ;
288 if (idx < 0) {
289 log.println("Required type not found") ;
290 tRes.tested("updateLong()", Status.skipped(true)) ;
291 return ;
294 try {
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) ;
301 result = false ;
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
311 * result returned.
313 public void _updateFloat() {
314 boolean result = true ;
315 int idx = findColumnOfType(Float.class) ;
317 if (idx < 0) {
318 log.println("Required type not found") ;
319 tRes.tested("updateFloat()", Status.skipped(true)) ;
320 return ;
323 try {
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) ;
330 result = false ;
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
340 * result returned.
342 public void _updateDouble() {
343 boolean result = true ;
344 int idx = findColumnOfType(Double.class) ;
346 if (idx < 0) {
347 log.println("Required type not found") ;
348 tRes.tested("updateDouble()", Status.skipped(true)) ;
349 return ;
352 try {
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) ;
359 result = false ;
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
369 * result returned.
371 public void _updateString() {
372 boolean result = true ;
373 int idx = findColumnOfType(String.class) ;
375 if (idx < 0) {
376 log.println("Required type not found") ;
377 tRes.tested("updateString()", Status.skipped(true)) ;
378 return ;
381 try {
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 = '"
387 + getVal + "'") ;
388 } catch (SQLException e) {
389 e.printStackTrace(log) ;
390 result = false ;
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
400 * result returned.
402 public void _updateBytes() {
403 boolean result = true ;
404 int idx = findColumnOfType(byte[].class) ;
406 if (idx < 0) {
407 log.println("Required type not found") ;
408 tRes.tested("updateBytes()", Status.skipped(true)) ;
409 return ;
412 try {
413 byte[] newVal = row.getBytes(idx) ;
414 if (newVal == null || newVal.length == 0) {
415 newVal = new byte[] {34, 111, 98} ;
416 } else {
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) ;
424 result = false ;
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
434 * result returned.
436 public void _updateDate() {
437 boolean result = true ;
438 int idx = findColumnOfType(Date.class) ;
440 if (idx < 0) {
441 log.println("Required type not found") ;
442 tRes.tested("updateDate()", Status.skipped(true)) ;
443 return ;
446 try {
447 Date newVal = row.getDate(idx) ;
448 newVal.Year ++ ;
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) ;
454 result = false ;
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
464 * result returned.
466 public void _updateTime() {
467 boolean result = true ;
468 int idx = findColumnOfType(Time.class) ;
470 if (idx < 0) {
471 log.println("Required type not found") ;
472 tRes.tested("updateTime()", Status.skipped(true)) ;
473 return ;
476 try {
477 Time newVal = row.getTime(idx) ;
478 newVal.Seconds ++ ;
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) ;
484 result = false ;
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
494 * result returned.
496 public void _updateTimestamp() {
497 boolean result = true ;
498 int idx = findColumnOfType(DateTime.class) ;
500 if (idx < 0) {
501 log.println("Required type not found") ;
502 tRes.tested("updateTimestamp()", Status.skipped(true)) ;
503 return ;
506 try {
507 DateTime newVal = row.getTimestamp(idx) ;
508 newVal.Year ++ ;
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) ;
514 result = false ;
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
525 * result returned.
527 public void _updateBinaryStream() {
528 boolean result = true ;
529 int idx = findColumnOfType(XDataInputStream.class) ;
531 if (idx < 0) {
532 log.println("Required type not found") ;
533 tRes.tested("updateBinaryStream()", Status.skipped(true)) ;
534 return ;
537 try {
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) ;
548 result = false ;
549 } catch (com.sun.star.uno.Exception e) {
550 log.println("Unexpected exception:") ;
551 e.printStackTrace(log) ;
552 result = false ;
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
562 * result returned.
564 public void _updateCharacterStream() {
565 boolean result = true ;
566 int idx = findColumnOfType(XTextInputStream.class) ;
568 if (idx < 0) {
569 log.println("Required type not found") ;
570 tRes.tested("updateCharacterStream()", Status.skipped(true)) ;
571 return ;
574 try {
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) ;
585 result = false ;
586 } catch (com.sun.star.uno.Exception e) {
587 log.println("Unexpected exception:") ;
588 e.printStackTrace(log) ;
589 result = false ;
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
599 * result returned.
601 public void _updateObject() {
602 boolean result = true ;
603 int idx = findColumnOfType(Object[].class) ;
605 if (idx < 0) {
606 log.println("Required type not found") ;
607 tRes.tested("updateObject()", Status.skipped(true)) ;
608 return ;
611 try {
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) ;
618 result = false ;
619 } catch (com.sun.star.uno.Exception e) {
620 log.println("Unexpected exception:") ;
621 e.printStackTrace(log) ;
622 result = false ;
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
632 * result returned.
634 public void _updateNumericObject() {
635 boolean result = true ;
636 int idx = findColumnOfType(Object[].class) ;
638 if (idx < 0) {
639 log.println("Required type not found") ;
640 tRes.tested("updateNumericObject()", Status.skipped(true)) ;
641 return ;
644 try {
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) ;
651 result = false ;
652 } catch (com.sun.star.uno.Exception e) {
653 log.println("Unexpected exception:") ;
654 e.printStackTrace(log) ;
655 result = false ;
658 tRes.tested("updateNumericObject()", result) ;
662 * Finds in relation vector index of column of the appropriate
663 * type.
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 ;
669 return -1 ;
673 * Disposes environment.
675 @Override
676 public void after() {
677 disposeEnvironment() ;
680 } // finish class _XRow