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
103 public void before() {
104 data
= (List
<Object
>) tEnv
.getObjRelation("XParameters.ParamValues") ;
106 log
.println("!!! Relation not found !!!") ;
111 * Sets String parameter (if exists) to SQL NULL value. <p>
112 * Has OK status if no exceptions occurred.
114 public void _setNull() {
115 boolean result
= true ;
116 int idx
= findParamOfType(String
.class) ;
117 if (idx
< 0) log
.println("Type not found in relation: not tested");
120 oObj
.setNull(idx
, DataType
.VARCHAR
) ;
121 } catch (SQLException e
) {
122 log
.println("Unexpected SQL exception:") ;
128 tRes
.tested("setNull()", result
) ;
131 public void _setObjectNull() {
135 tRes
.tested("setObjectNull()", Status
.skipped(true)) ;
139 * Sets String parameter (if exists) to new value. <p>
140 * Has OK status if no exceptions occurred.
142 public void _setString() {
143 boolean result
= true ;
144 int idx
= findParamOfType(String
.class) ;
145 if (idx
< 0) log
.println("Type not found in relation: not tested");
148 oObj
.setString(idx
, "XParameters") ;
149 } catch (SQLException e
) {
150 log
.println("Unexpected SQL exception:") ;
156 tRes
.tested("setString()", result
) ;
160 * Sets parameter (if exists) to new value. <p>
161 * Has OK status if no exceptions occurred.
163 public void _setBoolean() {
164 boolean result
= true ;
165 int idx
= findParamOfType(Boolean
.class) ;
166 if (idx
< 0) log
.println("Type not found in relation: not tested");
169 oObj
.setBoolean(idx
, true) ;
170 } catch (SQLException e
) {
171 log
.println("Unexpected SQL exception:") ;
177 tRes
.tested("setBoolean()", result
) ;
181 * Sets parameter (if exists) to new value. <p>
182 * Has OK status if no exceptions occurred.
184 public void _setByte() {
185 boolean result
= true ;
186 int idx
= findParamOfType(Byte
.class) ;
187 if (idx
< 0) log
.println("Type not found in relation: not tested");
190 oObj
.setByte(idx
, (byte)122) ;
191 } catch (SQLException e
) {
192 log
.println("Unexpected SQL exception:") ;
198 tRes
.tested("setByte()", result
) ;
202 * Sets parameter (if exists) to new value. <p>
203 * Has OK status if no exceptions occurred.
205 public void _setShort() {
206 boolean result
= true ;
207 int idx
= findParamOfType(Short
.class) ;
208 if (idx
< 0) log
.println("Type not found in relation: not tested");
211 oObj
.setShort(idx
, (short)133) ;
212 } catch (SQLException e
) {
213 log
.println("Unexpected SQL exception:") ;
219 tRes
.tested("setShort()", result
) ;
223 * Sets parameter (if exists) to new value. <p>
224 * Has OK status if no exceptions occurred.
226 public void _setInt() {
227 boolean result
= true ;
228 int idx
= findParamOfType(Integer
.class) ;
229 if (idx
< 0) log
.println("Type not found in relation: not tested");
232 oObj
.setInt(idx
, 13300) ;
233 } catch (SQLException e
) {
234 log
.println("Unexpected SQL exception:") ;
240 tRes
.tested("setInt()", result
) ;
244 * Sets parameter (if exists) to new value. <p>
245 * Has OK status if no exceptions occurred.
247 public void _setLong() {
248 boolean result
= true ;
249 int idx
= findParamOfType(Long
.class) ;
250 if (idx
< 0) log
.println("Type not found in relation: not tested");
253 oObj
.setLong(idx
, 13362453) ;
254 } catch (SQLException e
) {
255 log
.println("Unexpected SQL exception:") ;
261 tRes
.tested("setLong()", result
) ;
265 * Sets parameter (if exists) to new value. <p>
266 * Has OK status if no exceptions occurred.
268 public void _setFloat() {
269 boolean result
= true ;
270 int idx
= findParamOfType(Float
.class) ;
271 if (idx
< 0) log
.println("Type not found in relation: not tested");
274 oObj
.setFloat(idx
, (float)133.55) ;
275 } catch (SQLException e
) {
276 log
.println("Unexpected SQL exception:") ;
282 tRes
.tested("setFloat()", result
) ;
286 * Sets parameter (if exists) to new value. <p>
287 * Has OK status if no exceptions occurred.
289 public void _setDouble() {
290 boolean result
= true ;
291 int idx
= findParamOfType(Double
.class) ;
292 if (idx
< 0) log
.println("Type not found in relation: not tested");
295 oObj
.setDouble(idx
, 133) ;
296 } catch (SQLException e
) {
297 log
.println("Unexpected SQL exception:") ;
303 tRes
.tested("setDouble()", result
) ;
307 * Sets parameter (if exists) to new value. <p>
308 * Has OK status if no exceptions occurred.
310 public void _setBytes() {
311 boolean result
= true ;
312 int idx
= findParamOfType(byte[].class) ;
313 if (idx
< 0) log
.println("Type not found in relation: not tested");
316 oObj
.setBytes(idx
, new byte[] {5}) ;
317 } catch (SQLException e
) {
318 log
.println("Unexpected SQL exception:") ;
324 tRes
.tested("setBytes()", result
) ;
328 * Sets parameter (if exists) to new value. <p>
329 * Has OK status if no exceptions occurred.
331 public void _setDate() {
332 boolean result
= true ;
333 int idx
= findParamOfType(Date
.class) ;
334 if (idx
< 0) log
.println("Type not found in relation: not tested");
338 idx
, new Date ((short)19, (short)1, (short)1979)) ;
339 } catch (SQLException e
) {
340 log
.println("Unexpected SQL exception:") ;
346 tRes
.tested("setDate()", result
) ;
350 * Sets parameter (if exists) to new value. <p>
351 * Has OK status if no exceptions occurred.
353 public void _setTime() {
354 boolean result
= true ;
355 int idx
= findParamOfType(Time
.class) ;
356 if (idx
< 0) log
.println("Type not found in relation: not tested");
360 idx
, new Time((short)1,(short)2,(short)3,(short)44, false));
361 } catch (SQLException e
) {
362 log
.println("Unexpected SQL exception:") ;
368 tRes
.tested("setTime()", result
) ;
372 * Sets parameter (if exists) to new value. <p>
373 * Has OK status if no exceptions occurred.
375 public void _setTimestamp() {
376 boolean result
= true ;
377 int idx
= findParamOfType(DateTime
.class) ;
378 if (idx
< 0) log
.println("Type not found in relation: not tested");
381 oObj
.setTimestamp(idx
, new DateTime((short)1,(short)2,(short)3,
382 (short)4, (short)19, (short)1, (short)1979, false)) ;
383 } catch (SQLException e
) {
384 log
.println("Unexpected SQL exception:") ;
390 tRes
.tested("setTimestamp()", result
) ;
394 * Sets parameter (if exists) to new value. <p>
395 * Has OK status if no exceptions occurred.
397 public void _setBinaryStream() {
398 boolean result
= true ;
399 int idx
= findParamOfType(XDataInputStream
.class) ;
400 if (idx
< 0) log
.println("Type not found in relation: not tested");
403 Object oStream
= tParam
.getMSF().
404 createInstance("com.sun.star.io.DataInputStream") ;
405 XInputStream xStream
= UnoRuntime
.queryInterface
406 (XInputStream
.class, oStream
);
408 oObj
.setBinaryStream(idx
, xStream
, 2) ;
409 } catch (SQLException e
) {
410 log
.println("Unexpected SQL exception:") ;
413 } catch (com
.sun
.star
.uno
.Exception e
) {
414 log
.println("Unexpected exception:") ;
420 tRes
.tested("setBinaryStream()", result
) ;
424 * Sets parameter (if exists) to new value. <p>
425 * Has OK status if no exceptions occurred.
427 public void _setCharacterStream() {
428 boolean result
= true ;
429 int idx
= findParamOfType(XTextInputStream
.class) ;
430 if (idx
< 0) log
.println("Type not found in relation: not tested");
433 Object oStream
= tParam
.getMSF()
434 .createInstance("com.sun.star.io.TextInputStream") ;
435 XInputStream xStream
= UnoRuntime
.queryInterface
436 (XInputStream
.class, oStream
);
438 oObj
.setCharacterStream(idx
, xStream
, 2) ;
439 } catch (SQLException e
) {
440 log
.println("Unexpected SQL exception:") ;
443 } catch (com
.sun
.star
.uno
.Exception e
) {
444 log
.println("Unexpected exception:") ;
450 tRes
.tested("setCharacterStream()", result
) ;
454 * Sets parameter (if exists) to new value. <p>
455 * Has OK status if no exceptions occurred.
457 public void _setObject() {
458 boolean result
= true ;
459 int idx
= findParamOfType(Object
[].class) ;
460 if (idx
< 0) log
.println("Type not found in relation: not tested");
463 Object obj
= tParam
.getMSF().
464 createInstance("com.sun.star.io.Pipe") ;
466 oObj
.setObject(idx
, obj
) ;
467 } catch (SQLException e
) {
468 log
.println("Unexpected SQL exception:") ;
471 } catch (com
.sun
.star
.uno
.Exception e
) {
472 log
.println("Unexpected exception:") ;
478 tRes
.tested("setObject()", result
) ;
482 * Sets parameter (if exists) to new value. <p>
483 * Has OK status if no exceptions occurred.
485 public void _setObjectWithInfo() {
486 boolean result
= true ;
487 int idx
= findParamOfType(Object
[].class) ;
488 if (idx
< 0) log
.println("Type not found in relation: not tested");
491 Object obj
= tParam
.getMSF().
492 createInstance("com.sun.star.io.Pipe") ;
494 oObj
.setObjectWithInfo(idx
, obj
, DataType
.OBJECT
, 0) ;
495 } catch (SQLException e
) {
496 log
.println("Unexpected SQL exception:") ;
499 } catch (com
.sun
.star
.uno
.Exception e
) {
500 log
.println("Unexpected exception:") ;
506 tRes
.tested("setObjectWithInfo()", result
) ;
509 public void _setRef() {
513 tRes
.tested("setRef()", Status
.skipped(true)) ;
515 public void _setBlob() {
519 tRes
.tested("setBlob()", Status
.skipped(true)) ;
521 public void _setClob() {
525 tRes
.tested("setClob()", Status
.skipped(true)) ;
527 public void _setArray() {
531 tRes
.tested("setArray()", Status
.skipped(true)) ;
536 * Has OK status if no exceptions occurred.
538 public void _clearParameters() {
539 boolean result
= true ;
541 oObj
.clearParameters() ;
542 } catch (SQLException e
) {
543 log
.println("Unexpected SQL exception:") ;
548 tRes
.tested("clearParameters()", result
) ;
553 * Finds in relation vector index of parameter of the appropriate
556 private int findParamOfType(Class
<?
> clz
) {
557 for (int i
= 0; i
< data
.size(); i
++)
558 if (clz
.isInstance(data
.get(i
))) return i
+ 1 ;
562 } // finish class _XParameters