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 .
21 import java
.util
.List
;
23 import lib
.MultiMethodTest
;
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
;
39 * Testing <code>com.sun.star.sdbc.XParameters</code>
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>
66 * Object relations required :
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
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>
84 * Other methods uses types of their arguments (i.e.
86 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
87 * for <code>setRef</code> method).
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 ;
100 * Gets object relation
102 @SuppressWarnings("unchecked")
104 public void before() {
105 data
= (List
<Object
>) tEnv
.getObjRelation("XParameters.ParamValues") ;
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");
121 oObj
.setNull(idx
, DataType
.VARCHAR
) ;
122 } catch (SQLException e
) {
123 log
.println("Unexpected SQL exception:") ;
129 tRes
.tested("setNull()", result
) ;
132 public void _setObjectNull() {
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");
149 oObj
.setString(idx
, "XParameters") ;
150 } catch (SQLException e
) {
151 log
.println("Unexpected SQL exception:") ;
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");
170 oObj
.setBoolean(idx
, true) ;
171 } catch (SQLException e
) {
172 log
.println("Unexpected SQL exception:") ;
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");
191 oObj
.setByte(idx
, (byte)122) ;
192 } catch (SQLException e
) {
193 log
.println("Unexpected SQL exception:") ;
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");
212 oObj
.setShort(idx
, (short)133) ;
213 } catch (SQLException e
) {
214 log
.println("Unexpected SQL exception:") ;
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");
233 oObj
.setInt(idx
, 13300) ;
234 } catch (SQLException e
) {
235 log
.println("Unexpected SQL exception:") ;
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");
254 oObj
.setLong(idx
, 13362453) ;
255 } catch (SQLException e
) {
256 log
.println("Unexpected SQL exception:") ;
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");
275 oObj
.setFloat(idx
, (float)133.55) ;
276 } catch (SQLException e
) {
277 log
.println("Unexpected SQL exception:") ;
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");
296 oObj
.setDouble(idx
, 133) ;
297 } catch (SQLException e
) {
298 log
.println("Unexpected SQL exception:") ;
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");
317 oObj
.setBytes(idx
, new byte[] {5}) ;
318 } catch (SQLException e
) {
319 log
.println("Unexpected SQL exception:") ;
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");
339 idx
, new Date ((short)19, (short)1, (short)1979)) ;
340 } catch (SQLException e
) {
341 log
.println("Unexpected SQL exception:") ;
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");
361 idx
, new Time((short)1,(short)2,(short)3,(short)44, false));
362 } catch (SQLException e
) {
363 log
.println("Unexpected SQL exception:") ;
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");
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:") ;
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");
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:") ;
414 } catch (com
.sun
.star
.uno
.Exception e
) {
415 log
.println("Unexpected exception:") ;
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");
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:") ;
444 } catch (com
.sun
.star
.uno
.Exception e
) {
445 log
.println("Unexpected exception:") ;
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");
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:") ;
472 } catch (com
.sun
.star
.uno
.Exception e
) {
473 log
.println("Unexpected exception:") ;
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");
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:") ;
500 } catch (com
.sun
.star
.uno
.Exception e
) {
501 log
.println("Unexpected exception:") ;
507 tRes
.tested("setObjectWithInfo()", result
) ;
510 public void _setRef() {
514 tRes
.tested("setRef()", Status
.skipped(true)) ;
516 public void _setBlob() {
520 tRes
.tested("setBlob()", Status
.skipped(true)) ;
522 public void _setClob() {
526 tRes
.tested("setClob()", Status
.skipped(true)) ;
528 public void _setArray() {
532 tRes
.tested("setArray()", Status
.skipped(true)) ;
537 * Has OK status if no exceptions occurred.
539 public void _clearParameters() {
540 boolean result
= true ;
542 oObj
.clearParameters() ;
543 } catch (SQLException e
) {
544 log
.println("Unexpected SQL exception:") ;
549 tRes
.tested("clearParameters()", result
) ;
554 * Finds in relation vector index of parameter of the appropriate
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 ;
563 } // finish class _XParameters