Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / sdbc / _XParameters.java
blob31a5e6d4b0ad97bb1d99a9297f1da8d767090bae
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;
26 import com.sun.star.io.XDataInputStream;
27 import com.sun.star.io.XInputStream;
28 import com.sun.star.io.XTextInputStream;
29 import com.sun.star.sdbc.DataType;
30 import com.sun.star.sdbc.SQLException;
31 import com.sun.star.sdbc.XParameters;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.util.Date;
34 import com.sun.star.util.DateTime;
35 import com.sun.star.util.Time;
37 /**
38 /**
39 * Testing <code>com.sun.star.sdbc.XParameters</code>
40 * interface methods :
41 * <ul>
42 * <li><code> setNull()</code></li>
43 * <li><code> setObjectNull()</code></li>
44 * <li><code> setBoolean()</code></li>
45 * <li><code> setByte()</code></li>
46 * <li><code> setShort()</code></li>
47 * <li><code> setInt()</code></li>
48 * <li><code> setLong()</code></li>
49 * <li><code> setFloat()</code></li>
50 * <li><code> setDouble()</code></li>
51 * <li><code> setString()</code></li>
52 * <li><code> setBytes()</code></li>
53 * <li><code> setDate()</code></li>
54 * <li><code> setTime()</code></li>
55 * <li><code> setTimestamp()</code></li>
56 * <li><code> setBinaryStream()</code></li>
57 * <li><code> setCharacterStream()</code></li>
58 * <li><code> setObject()</code></li>
59 * <li><code> setObjectWithInfo()</code></li>
60 * <li><code> setRef()</code></li>
61 * <li><code> setBlob()</code></li>
62 * <li><code> setClob()</code></li>
63 * <li><code> setArray()</code></li>
64 * <li><code> clearParameters()</code></li>
65 * </ul> <p>
66 * Object relations required :
67 * <ul>
68 * <li> <code>'XParameters.ParamValues'</code> : is a
69 * <code>java.util.Vector</code> object
70 * that contains parameter types and values of the statement. Each
71 * element of vector corresponds to appropriate parameter (element
72 * with index 0 to parameter #1, 1 -> #2, etc.). <p>
73 * The following <code>XParameters</code> methods correspond to classes
74 * in Vector :
75 * <ul>
76 * <li> <code>setBinaryStream</code> -
77 * <code>com.sun.star.io.XDataInputStream</code> class. </li>
78 * <li> <code>setCharacterStream</code> -
79 * <code>com.sun.star.io.XTextInputStream</code> class. </li>
80 * <li> <code>setObject</code> -
81 * <code>java.lang.Object[]</code> class, the element with
82 * index 0 must be used. </li>
83 * </ul>
84 * Other methods uses types of their arguments (i.e.
85 * <code>String</code>
86 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
87 * for <code>setRef</code> method).
88 * </li>
89 * </ul>
90 * @see com.sun.star.sdbc.XParameters
92 public class _XParameters extends MultiMethodTest {
94 // oObj filled by MultiMethodTest
95 public XParameters oObj = null ;
97 private List<Object> data = null ;
99 /**
100 * Gets object relation
102 @SuppressWarnings("unchecked")
103 @Override
104 public void before() {
105 data = (List<Object>) tEnv.getObjRelation("XParameters.ParamValues") ;
106 if (data == null) {
107 log.println("!!! Relation not found !!!") ;
112 * Sets String parameter (if exists) to SQL NULL value. <p>
113 * Has OK status if no exceptions occurred.
115 public void _setNull() {
116 boolean result = true ;
117 int idx = findParamOfType(String.class) ;
118 if (idx < 0) log.println("Type not found in relation: not tested");
119 else {
120 try {
121 oObj.setNull(idx, DataType.VARCHAR) ;
122 } catch (SQLException e) {
123 log.println("Unexpected SQL exception:") ;
124 log.println(e) ;
125 result = false ;
129 tRes.tested("setNull()", result) ;
132 public void _setObjectNull() {
134 !!! TO DO !!!
136 tRes.tested("setObjectNull()", Status.skipped(true)) ;
140 * Sets String parameter (if exists) to new value. <p>
141 * Has OK status if no exceptions occurred.
143 public void _setString() {
144 boolean result = true ;
145 int idx = findParamOfType(String.class) ;
146 if (idx < 0) log.println("Type not found in relation: not tested");
147 else {
148 try {
149 oObj.setString(idx, "XParameters") ;
150 } catch (SQLException e) {
151 log.println("Unexpected SQL exception:") ;
152 log.println(e) ;
153 result = false ;
157 tRes.tested("setString()", result) ;
161 * Sets parameter (if exists) to new value. <p>
162 * Has OK status if no exceptions occurred.
164 public void _setBoolean() {
165 boolean result = true ;
166 int idx = findParamOfType(Boolean.class) ;
167 if (idx < 0) log.println("Type not found in relation: not tested");
168 else {
169 try {
170 oObj.setBoolean(idx, true) ;
171 } catch (SQLException e) {
172 log.println("Unexpected SQL exception:") ;
173 log.println(e) ;
174 result = false ;
178 tRes.tested("setBoolean()", result) ;
182 * Sets parameter (if exists) to new value. <p>
183 * Has OK status if no exceptions occurred.
185 public void _setByte() {
186 boolean result = true ;
187 int idx = findParamOfType(Byte.class) ;
188 if (idx < 0) log.println("Type not found in relation: not tested");
189 else {
190 try {
191 oObj.setByte(idx, (byte)122) ;
192 } catch (SQLException e) {
193 log.println("Unexpected SQL exception:") ;
194 log.println(e) ;
195 result = false ;
199 tRes.tested("setByte()", result) ;
203 * Sets parameter (if exists) to new value. <p>
204 * Has OK status if no exceptions occurred.
206 public void _setShort() {
207 boolean result = true ;
208 int idx = findParamOfType(Short.class) ;
209 if (idx < 0) log.println("Type not found in relation: not tested");
210 else {
211 try {
212 oObj.setShort(idx, (short)133) ;
213 } catch (SQLException e) {
214 log.println("Unexpected SQL exception:") ;
215 log.println(e) ;
216 result = false ;
220 tRes.tested("setShort()", result) ;
224 * Sets parameter (if exists) to new value. <p>
225 * Has OK status if no exceptions occurred.
227 public void _setInt() {
228 boolean result = true ;
229 int idx = findParamOfType(Integer.class) ;
230 if (idx < 0) log.println("Type not found in relation: not tested");
231 else {
232 try {
233 oObj.setInt(idx, 13300) ;
234 } catch (SQLException e) {
235 log.println("Unexpected SQL exception:") ;
236 log.println(e) ;
237 result = false ;
241 tRes.tested("setInt()", result) ;
245 * Sets parameter (if exists) to new value. <p>
246 * Has OK status if no exceptions occurred.
248 public void _setLong() {
249 boolean result = true ;
250 int idx = findParamOfType(Long.class) ;
251 if (idx < 0) log.println("Type not found in relation: not tested");
252 else {
253 try {
254 oObj.setLong(idx, 13362453) ;
255 } catch (SQLException e) {
256 log.println("Unexpected SQL exception:") ;
257 log.println(e) ;
258 result = false ;
262 tRes.tested("setLong()", result) ;
266 * Sets parameter (if exists) to new value. <p>
267 * Has OK status if no exceptions occurred.
269 public void _setFloat() {
270 boolean result = true ;
271 int idx = findParamOfType(Float.class) ;
272 if (idx < 0) log.println("Type not found in relation: not tested");
273 else {
274 try {
275 oObj.setFloat(idx, (float)133.55) ;
276 } catch (SQLException e) {
277 log.println("Unexpected SQL exception:") ;
278 log.println(e) ;
279 result = false ;
283 tRes.tested("setFloat()", result) ;
287 * Sets parameter (if exists) to new value. <p>
288 * Has OK status if no exceptions occurred.
290 public void _setDouble() {
291 boolean result = true ;
292 int idx = findParamOfType(Double.class) ;
293 if (idx < 0) log.println("Type not found in relation: not tested");
294 else {
295 try {
296 oObj.setDouble(idx, 133) ;
297 } catch (SQLException e) {
298 log.println("Unexpected SQL exception:") ;
299 log.println(e) ;
300 result = false ;
304 tRes.tested("setDouble()", result) ;
308 * Sets parameter (if exists) to new value. <p>
309 * Has OK status if no exceptions occurred.
311 public void _setBytes() {
312 boolean result = true ;
313 int idx = findParamOfType(byte[].class) ;
314 if (idx < 0) log.println("Type not found in relation: not tested");
315 else {
316 try {
317 oObj.setBytes(idx, new byte[] {5}) ;
318 } catch (SQLException e) {
319 log.println("Unexpected SQL exception:") ;
320 log.println(e) ;
321 result = false ;
325 tRes.tested("setBytes()", result) ;
329 * Sets parameter (if exists) to new value. <p>
330 * Has OK status if no exceptions occurred.
332 public void _setDate() {
333 boolean result = true ;
334 int idx = findParamOfType(Date.class) ;
335 if (idx < 0) log.println("Type not found in relation: not tested");
336 else {
337 try {
338 oObj.setDate(
339 idx, new Date ((short)19, (short)1, (short)1979)) ;
340 } catch (SQLException e) {
341 log.println("Unexpected SQL exception:") ;
342 log.println(e) ;
343 result = false ;
347 tRes.tested("setDate()", result) ;
351 * Sets parameter (if exists) to new value. <p>
352 * Has OK status if no exceptions occurred.
354 public void _setTime() {
355 boolean result = true ;
356 int idx = findParamOfType(Time.class) ;
357 if (idx < 0) log.println("Type not found in relation: not tested");
358 else {
359 try {
360 oObj.setTime(
361 idx, new Time((short)1,(short)2,(short)3,(short)44, false));
362 } catch (SQLException e) {
363 log.println("Unexpected SQL exception:") ;
364 log.println(e) ;
365 result = false ;
369 tRes.tested("setTime()", result) ;
373 * Sets parameter (if exists) to new value. <p>
374 * Has OK status if no exceptions occurred.
376 public void _setTimestamp() {
377 boolean result = true ;
378 int idx = findParamOfType(DateTime.class) ;
379 if (idx < 0) log.println("Type not found in relation: not tested");
380 else {
381 try {
382 oObj.setTimestamp(idx, new DateTime((short)1,(short)2,(short)3,
383 (short)4, (short)19, (short)1, (short)1979, false)) ;
384 } catch (SQLException e) {
385 log.println("Unexpected SQL exception:") ;
386 log.println(e) ;
387 result = false ;
391 tRes.tested("setTimestamp()", result) ;
395 * Sets parameter (if exists) to new value. <p>
396 * Has OK status if no exceptions occurred.
398 public void _setBinaryStream() {
399 boolean result = true ;
400 int idx = findParamOfType(XDataInputStream.class) ;
401 if (idx < 0) log.println("Type not found in relation: not tested");
402 else {
403 try {
404 Object oStream = tParam.getMSF().
405 createInstance("com.sun.star.io.DataInputStream") ;
406 XInputStream xStream = UnoRuntime.queryInterface
407 (XInputStream.class, oStream);
409 oObj.setBinaryStream(idx, xStream, 2) ;
410 } catch (SQLException e) {
411 log.println("Unexpected SQL exception:") ;
412 log.println(e) ;
413 result = false ;
414 } catch (com.sun.star.uno.Exception e) {
415 log.println("Unexpected exception:") ;
416 log.println(e) ;
417 result = false ;
421 tRes.tested("setBinaryStream()", result) ;
425 * Sets parameter (if exists) to new value. <p>
426 * Has OK status if no exceptions occurred.
428 public void _setCharacterStream() {
429 boolean result = true ;
430 int idx = findParamOfType(XTextInputStream.class) ;
431 if (idx < 0) log.println("Type not found in relation: not tested");
432 else {
433 try {
434 Object oStream = tParam.getMSF()
435 .createInstance("com.sun.star.io.TextInputStream") ;
436 XInputStream xStream = UnoRuntime.queryInterface
437 (XInputStream.class, oStream);
439 oObj.setCharacterStream(idx, xStream, 2) ;
440 } catch (SQLException e) {
441 log.println("Unexpected SQL exception:") ;
442 log.println(e) ;
443 result = false ;
444 } catch (com.sun.star.uno.Exception e) {
445 log.println("Unexpected exception:") ;
446 log.println(e) ;
447 result = false ;
451 tRes.tested("setCharacterStream()", result) ;
455 * Sets parameter (if exists) to new value. <p>
456 * Has OK status if no exceptions occurred.
458 public void _setObject() {
459 boolean result = true ;
460 int idx = findParamOfType(Object[].class) ;
461 if (idx < 0) log.println("Type not found in relation: not tested");
462 else {
463 try {
464 Object obj = tParam.getMSF().
465 createInstance("com.sun.star.io.Pipe") ;
467 oObj.setObject(idx, obj) ;
468 } catch (SQLException e) {
469 log.println("Unexpected SQL exception:") ;
470 log.println(e) ;
471 result = false ;
472 } catch (com.sun.star.uno.Exception e) {
473 log.println("Unexpected exception:") ;
474 log.println(e) ;
475 result = false ;
479 tRes.tested("setObject()", result) ;
483 * Sets parameter (if exists) to new value. <p>
484 * Has OK status if no exceptions occurred.
486 public void _setObjectWithInfo() {
487 boolean result = true ;
488 int idx = findParamOfType(Object[].class) ;
489 if (idx < 0) log.println("Type not found in relation: not tested");
490 else {
491 try {
492 Object obj = tParam.getMSF().
493 createInstance("com.sun.star.io.Pipe") ;
495 oObj.setObjectWithInfo(idx, obj, DataType.OBJECT, 0) ;
496 } catch (SQLException e) {
497 log.println("Unexpected SQL exception:") ;
498 log.println(e) ;
499 result = false ;
500 } catch (com.sun.star.uno.Exception e) {
501 log.println("Unexpected exception:") ;
502 log.println(e) ;
503 result = false ;
507 tRes.tested("setObjectWithInfo()", result) ;
510 public void _setRef() {
512 !!! TO DO !!!
514 tRes.tested("setRef()", Status.skipped(true)) ;
516 public void _setBlob() {
518 !!! TO DO !!!
520 tRes.tested("setBlob()", Status.skipped(true)) ;
522 public void _setClob() {
524 !!! TO DO !!!
526 tRes.tested("setClob()", Status.skipped(true)) ;
528 public void _setArray() {
530 !!! TO DO !!!
532 tRes.tested("setArray()", Status.skipped(true)) ;
536 * Calls method. <p>
537 * Has OK status if no exceptions occurred.
539 public void _clearParameters() {
540 boolean result = true ;
541 try {
542 oObj.clearParameters() ;
543 } catch (SQLException e) {
544 log.println("Unexpected SQL exception:") ;
545 log.println(e) ;
546 result = false ;
549 tRes.tested("clearParameters()", result) ;
554 * Finds in relation vector index of parameter of the appropriate
555 * type.
557 private int findParamOfType(Class<?> clz) {
558 for (int i = 0; i < data.size(); i++)
559 if (clz.isInstance(data.get(i))) return i + 1 ;
560 return -1 ;
563 } // finish class _XParameters