Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / sdbc / _XRowUpdate.java
blob88122513cc882441d70eb521667681e0ae755b83
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.utils;
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;
39 /**
40 * Testing <code>com.sun.star.sdbc.XRowUpdate</code>
41 * interface methods :
42 * <ul>
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>
60 * </ul> <p>
61 * Object relations required :
62 * <ul>
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
69 * in Vector :
70 * <ul>
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>
78 * </ul>
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).
82 * </li>
83 * <li> <code>'XRowUpdate.XRow'</code> : implementation of <code>
84 * com.sun.star.sdbc.XRow</code> interface for checking updated data.
85 * </li>
86 * </ul> <p>
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 ;
99 /**
100 * Gets relations.
102 @SuppressWarnings("unchecked")
103 @Override
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) + " ...") ;
126 try {
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 !!!") ;
150 result = false ;
151 } else {
152 log.println("OK") ;
154 } catch (SQLException e) {
155 log.println("FAILED") ;
156 e.printStackTrace(log) ;
157 result = false ;
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
168 * result returned.
170 public void _updateBoolean() {
171 boolean result = true ;
172 int idx = findColumnOfType(Boolean.class) ;
174 if (idx < 0) {
175 log.println("Required type not found") ;
176 tRes.tested("updateBoolean()", Status.skipped(true)) ;
177 return ;
180 try {
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) ;
187 result = false ;
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
197 * result returned.
199 public void _updateByte() {
200 boolean result = true ;
201 int idx = findColumnOfType(Byte.class) ;
203 if (idx < 0) {
204 log.println("Required type not found") ;
205 tRes.tested("updateByte()", Status.skipped(true)) ;
206 return ;
209 try {
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) ;
216 result = false ;
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
226 * result returned.
228 public void _updateShort() {
229 boolean result = true ;
230 int idx = findColumnOfType(Short.class) ;
232 if (idx < 0) {
233 log.println("Required type not found") ;
234 tRes.tested("updateShort()", Status.skipped(true)) ;
235 return ;
238 try {
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) ;
245 result = false ;
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
255 * result returned.
257 public void _updateInt() {
258 boolean result = true ;
259 int idx = findColumnOfType(Integer.class) ;
261 if (idx < 0) {
262 log.println("Required type not found") ;
263 tRes.tested("updateInt()", Status.skipped(true)) ;
264 return ;
267 try {
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) ;
274 result = false ;
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
284 * result returned.
286 public void _updateLong() {
287 boolean result = true ;
288 int idx = findColumnOfType(Long.class) ;
290 if (idx < 0) {
291 log.println("Required type not found") ;
292 tRes.tested("updateLong()", Status.skipped(true)) ;
293 return ;
296 try {
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) ;
303 result = false ;
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
313 * result returned.
315 public void _updateFloat() {
316 boolean result = true ;
317 int idx = findColumnOfType(Float.class) ;
319 if (idx < 0) {
320 log.println("Required type not found") ;
321 tRes.tested("updateFloat()", Status.skipped(true)) ;
322 return ;
325 try {
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) ;
332 result = false ;
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
342 * result returned.
344 public void _updateDouble() {
345 boolean result = true ;
346 int idx = findColumnOfType(Double.class) ;
348 if (idx < 0) {
349 log.println("Required type not found") ;
350 tRes.tested("updateDouble()", Status.skipped(true)) ;
351 return ;
354 try {
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) ;
361 result = false ;
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
371 * result returned.
373 public void _updateString() {
374 boolean result = true ;
375 int idx = findColumnOfType(String.class) ;
377 if (idx < 0) {
378 log.println("Required type not found") ;
379 tRes.tested("updateString()", Status.skipped(true)) ;
380 return ;
383 try {
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 = '"
389 + getVal + "'") ;
390 } catch (SQLException e) {
391 e.printStackTrace(log) ;
392 result = false ;
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
402 * result returned.
404 public void _updateBytes() {
405 boolean result = true ;
406 int idx = findColumnOfType(byte[].class) ;
408 if (idx < 0) {
409 log.println("Required type not found") ;
410 tRes.tested("updateBytes()", Status.skipped(true)) ;
411 return ;
414 try {
415 byte[] newVal = row.getBytes(idx) ;
416 if (newVal == null || newVal.length == 0) {
417 newVal = new byte[] {34, 111, 98} ;
418 } else {
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) ;
426 result = false ;
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
436 * result returned.
438 public void _updateDate() {
439 boolean result = true ;
440 int idx = findColumnOfType(Date.class) ;
442 if (idx < 0) {
443 log.println("Required type not found") ;
444 tRes.tested("updateDate()", Status.skipped(true)) ;
445 return ;
448 try {
449 Date newVal = row.getDate(idx) ;
450 newVal.Year ++ ;
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) ;
456 result = false ;
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
466 * result returned.
468 public void _updateTime() {
469 boolean result = true ;
470 int idx = findColumnOfType(Time.class) ;
472 if (idx < 0) {
473 log.println("Required type not found") ;
474 tRes.tested("updateTime()", Status.skipped(true)) ;
475 return ;
478 try {
479 Time newVal = row.getTime(idx) ;
480 newVal.Seconds ++ ;
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) ;
486 result = false ;
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
496 * result returned.
498 public void _updateTimestamp() {
499 boolean result = true ;
500 int idx = findColumnOfType(DateTime.class) ;
502 if (idx < 0) {
503 log.println("Required type not found") ;
504 tRes.tested("updateTimestamp()", Status.skipped(true)) ;
505 return ;
508 try {
509 DateTime newVal = row.getTimestamp(idx) ;
510 newVal.Year ++ ;
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) ;
516 result = false ;
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
527 * result returned.
529 public void _updateBinaryStream() {
530 boolean result = true ;
531 int idx = findColumnOfType(XDataInputStream.class) ;
533 if (idx < 0) {
534 log.println("Required type not found") ;
535 tRes.tested("updateBinaryStream()", Status.skipped(true)) ;
536 return ;
539 try {
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) ;
550 result = false ;
551 } catch (com.sun.star.uno.Exception e) {
552 log.println("Unexpected exception:") ;
553 e.printStackTrace(log) ;
554 result = false ;
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
564 * result returned.
566 public void _updateCharacterStream() {
567 boolean result = true ;
568 int idx = findColumnOfType(XTextInputStream.class) ;
570 if (idx < 0) {
571 log.println("Required type not found") ;
572 tRes.tested("updateCharacterStream()", Status.skipped(true)) ;
573 return ;
576 try {
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) ;
587 result = false ;
588 } catch (com.sun.star.uno.Exception e) {
589 log.println("Unexpected exception:") ;
590 e.printStackTrace(log) ;
591 result = false ;
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
601 * result returned.
603 public void _updateObject() {
604 boolean result = true ;
605 int idx = findColumnOfType(Object[].class) ;
607 if (idx < 0) {
608 log.println("Required type not found") ;
609 tRes.tested("updateObject()", Status.skipped(true)) ;
610 return ;
613 try {
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) ;
620 result = false ;
621 } catch (com.sun.star.uno.Exception e) {
622 log.println("Unexpected exception:") ;
623 e.printStackTrace(log) ;
624 result = false ;
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
634 * result returned.
636 public void _updateNumericObject() {
637 boolean result = true ;
638 int idx = findColumnOfType(Object[].class) ;
640 if (idx < 0) {
641 log.println("Required type not found") ;
642 tRes.tested("updateNumericObject()", Status.skipped(true)) ;
643 return ;
646 try {
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) ;
653 result = false ;
654 } catch (com.sun.star.uno.Exception e) {
655 log.println("Unexpected exception:") ;
656 e.printStackTrace(log) ;
657 result = false ;
660 tRes.tested("updateNumericObject()", result) ;
664 * Finds in relation vector index of column of the appropriate
665 * type.
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 ;
671 return -1 ;
675 * Disposes environment.
677 @Override
678 public void after() {
679 disposeEnvironment() ;
682 } // finish class _XRow