1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import java
.util
.Vector
;
32 import lib
.MultiMethodTest
;
35 import com
.sun
.star
.io
.XDataInputStream
;
36 import com
.sun
.star
.io
.XInputStream
;
37 import com
.sun
.star
.io
.XTextInputStream
;
38 import com
.sun
.star
.lang
.XMultiServiceFactory
;
39 import com
.sun
.star
.sdbc
.DataType
;
40 import com
.sun
.star
.sdbc
.SQLException
;
41 import com
.sun
.star
.sdbc
.XParameters
;
42 import com
.sun
.star
.uno
.UnoRuntime
;
43 import com
.sun
.star
.util
.Date
;
44 import com
.sun
.star
.util
.DateTime
;
45 import com
.sun
.star
.util
.Time
;
49 * Testing <code>com.sun.star.sdbc.XParameters</code>
52 * <li><code> setNull()</code></li>
53 * <li><code> setObjectNull()</code></li>
54 * <li><code> setBoolean()</code></li>
55 * <li><code> setByte()</code></li>
56 * <li><code> setShort()</code></li>
57 * <li><code> setInt()</code></li>
58 * <li><code> setLong()</code></li>
59 * <li><code> setFloat()</code></li>
60 * <li><code> setDouble()</code></li>
61 * <li><code> setString()</code></li>
62 * <li><code> setBytes()</code></li>
63 * <li><code> setDate()</code></li>
64 * <li><code> setTime()</code></li>
65 * <li><code> setTimestamp()</code></li>
66 * <li><code> setBinaryStream()</code></li>
67 * <li><code> setCharacterStream()</code></li>
68 * <li><code> setObject()</code></li>
69 * <li><code> setObjectWithInfo()</code></li>
70 * <li><code> setRef()</code></li>
71 * <li><code> setBlob()</code></li>
72 * <li><code> setClob()</code></li>
73 * <li><code> setArray()</code></li>
74 * <li><code> clearParameters()</code></li>
76 * Object relations required :
78 * <li> <code>'XParameters.ParamValues'</code> : is a
79 * <code>java.util.Vector</code> object
80 * that contains parameter types and values of the statement. Each
81 * element of vector corresponds to appropriate parameter (element
82 * with index 0 to parameter #1, 1 -> #2, etc.). <p>
83 * The following <code>XParameters</code> methods correspond to classes
86 * <li> <code>setBinaryStream</code> -
87 * <code>com.sun.star.io.XDataInputStream</code> class. </li>
88 * <li> <code>setCharacterStream</code> -
89 * <code>com.sun.star.io.XTextInputStream</code> class. </li>
90 * <li> <code>setObject</code> -
91 * <code>java.lang.Object[]</code> class, the element with
92 * index 0 must be used. </li>
94 * Other methods uses types of their arguments (i.e.
95 * <code>java.lang.String</code>
96 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
97 * for <code>setRef</code> method).
100 * @see com.sun.star.sdbc.XParameters
102 public class _XParameters
extends MultiMethodTest
{
104 // oObj filled by MultiMethodTest
105 public XParameters oObj
= null ;
107 private Vector data
= null ;
110 * Gets object relation
112 public void before() {
113 data
= (Vector
) tEnv
.getObjRelation("XParameters.ParamValues") ;
115 log
.println("!!! Relation not found !!!") ;
120 * Sets String parameter (if exists) to SQL NULL value. <p>
121 * Has OK status if no exceptions occurred.
123 public void _setNull() {
124 boolean result
= true ;
125 int idx
= findParamOfType(String
.class) ;
126 if (idx
< 0) log
.println("Type not found in relation: not tested");
129 oObj
.setNull(idx
, DataType
.VARCHAR
) ;
130 } catch (SQLException e
) {
131 log
.println("Unexpected SQL exception:") ;
137 tRes
.tested("setNull()", result
) ;
140 public void _setObjectNull() {
144 tRes
.tested("setObjectNull()", Status
.skipped(true)) ;
148 * Sets String parameter (if exists) to new value. <p>
149 * Has OK status if no exceptions occurred.
151 public void _setString() {
152 boolean result
= true ;
153 int idx
= findParamOfType(String
.class) ;
154 if (idx
< 0) log
.println("Type not found in relation: not tested");
157 oObj
.setString(idx
, "XParameters") ;
158 } catch (SQLException e
) {
159 log
.println("Unexpected SQL exception:") ;
165 tRes
.tested("setString()", result
) ;
169 * Sets parameter (if exists) to new value. <p>
170 * Has OK status if no exceptions occurred.
172 public void _setBoolean() {
173 boolean result
= true ;
174 int idx
= findParamOfType(Boolean
.class) ;
175 if (idx
< 0) log
.println("Type not found in relation: not tested");
178 oObj
.setBoolean(idx
, true) ;
179 } catch (SQLException e
) {
180 log
.println("Unexpected SQL exception:") ;
186 tRes
.tested("setBoolean()", result
) ;
190 * Sets parameter (if exists) to new value. <p>
191 * Has OK status if no exceptions occurred.
193 public void _setByte() {
194 boolean result
= true ;
195 int idx
= findParamOfType(Byte
.class) ;
196 if (idx
< 0) log
.println("Type not found in relation: not tested");
199 oObj
.setByte(idx
, (byte)122) ;
200 } catch (SQLException e
) {
201 log
.println("Unexpected SQL exception:") ;
207 tRes
.tested("setByte()", result
) ;
211 * Sets parameter (if exists) to new value. <p>
212 * Has OK status if no exceptions occurred.
214 public void _setShort() {
215 boolean result
= true ;
216 int idx
= findParamOfType(Short
.class) ;
217 if (idx
< 0) log
.println("Type not found in relation: not tested");
220 oObj
.setShort(idx
, (short)133) ;
221 } catch (SQLException e
) {
222 log
.println("Unexpected SQL exception:") ;
228 tRes
.tested("setShort()", result
) ;
232 * Sets parameter (if exists) to new value. <p>
233 * Has OK status if no exceptions occurred.
235 public void _setInt() {
236 boolean result
= true ;
237 int idx
= findParamOfType(Integer
.class) ;
238 if (idx
< 0) log
.println("Type not found in relation: not tested");
241 oObj
.setInt(idx
, 13300) ;
242 } catch (SQLException e
) {
243 log
.println("Unexpected SQL exception:") ;
249 tRes
.tested("setInt()", result
) ;
253 * Sets parameter (if exists) to new value. <p>
254 * Has OK status if no exceptions occurred.
256 public void _setLong() {
257 boolean result
= true ;
258 int idx
= findParamOfType(Long
.class) ;
259 if (idx
< 0) log
.println("Type not found in relation: not tested");
262 oObj
.setLong(idx
, 13362453) ;
263 } catch (SQLException e
) {
264 log
.println("Unexpected SQL exception:") ;
270 tRes
.tested("setLong()", result
) ;
274 * Sets parameter (if exists) to new value. <p>
275 * Has OK status if no exceptions occurred.
277 public void _setFloat() {
278 boolean result
= true ;
279 int idx
= findParamOfType(Float
.class) ;
280 if (idx
< 0) log
.println("Type not found in relation: not tested");
283 oObj
.setFloat(idx
, (float)133.55) ;
284 } catch (SQLException e
) {
285 log
.println("Unexpected SQL exception:") ;
291 tRes
.tested("setFloat()", result
) ;
295 * Sets parameter (if exists) to new value. <p>
296 * Has OK status if no exceptions occurred.
298 public void _setDouble() {
299 boolean result
= true ;
300 int idx
= findParamOfType(Double
.class) ;
301 if (idx
< 0) log
.println("Type not found in relation: not tested");
304 oObj
.setDouble(idx
, 133) ;
305 } catch (SQLException e
) {
306 log
.println("Unexpected SQL exception:") ;
312 tRes
.tested("setDouble()", result
) ;
316 * Sets parameter (if exists) to new value. <p>
317 * Has OK status if no exceptions occurred.
319 public void _setBytes() {
320 boolean result
= true ;
321 int idx
= findParamOfType(byte[].class) ;
322 if (idx
< 0) log
.println("Type not found in relation: not tested");
325 oObj
.setBytes(idx
, new byte[] {5}) ;
326 } catch (SQLException e
) {
327 log
.println("Unexpected SQL exception:") ;
333 tRes
.tested("setBytes()", result
) ;
337 * Sets parameter (if exists) to new value. <p>
338 * Has OK status if no exceptions occurred.
340 public void _setDate() {
341 boolean result
= true ;
342 int idx
= findParamOfType(Date
.class) ;
343 if (idx
< 0) log
.println("Type not found in relation: not tested");
347 idx
, new Date ((short)19, (short)01, (short)1979)) ;
348 } catch (SQLException e
) {
349 log
.println("Unexpected SQL exception:") ;
355 tRes
.tested("setDate()", result
) ;
359 * Sets parameter (if exists) to new value. <p>
360 * Has OK status if no exceptions occurred.
362 public void _setTime() {
363 boolean result
= true ;
364 int idx
= findParamOfType(Time
.class) ;
365 if (idx
< 0) log
.println("Type not found in relation: not tested");
369 idx
, new Time((short)1,(short)2,(short)3,(short)44)) ;
370 } catch (SQLException e
) {
371 log
.println("Unexpected SQL exception:") ;
377 tRes
.tested("setTime()", result
) ;
381 * Sets parameter (if exists) to new value. <p>
382 * Has OK status if no exceptions occurred.
384 public void _setTimestamp() {
385 boolean result
= true ;
386 int idx
= findParamOfType(DateTime
.class) ;
387 if (idx
< 0) log
.println("Type not found in relation: not tested");
390 oObj
.setTimestamp(idx
, new DateTime((short)1,(short)2,(short)3,
391 (short)4, (short)19, (short)01, (short)1979)) ;
392 } catch (SQLException e
) {
393 log
.println("Unexpected SQL exception:") ;
399 tRes
.tested("setTimestamp()", result
) ;
403 * Sets parameter (if exists) to new value. <p>
404 * Has OK status if no exceptions occurred.
406 public void _setBinaryStream() {
407 boolean result
= true ;
408 int idx
= findParamOfType(XDataInputStream
.class) ;
409 if (idx
< 0) log
.println("Type not found in relation: not tested");
412 Object oStream
= ((XMultiServiceFactory
)tParam
.getMSF()).
413 createInstance("com.sun.star.io.DataInputStream") ;
414 XInputStream xStream
= (XInputStream
)UnoRuntime
.queryInterface
415 (XInputStream
.class, oStream
);
417 oObj
.setBinaryStream(idx
, xStream
, 2) ;
418 } catch (SQLException e
) {
419 log
.println("Unexpected SQL exception:") ;
422 } catch (com
.sun
.star
.uno
.Exception e
) {
423 log
.println("Unexpected exception:") ;
429 tRes
.tested("setBinaryStream()", result
) ;
433 * Sets parameter (if exists) to new value. <p>
434 * Has OK status if no exceptions occurred.
436 public void _setCharacterStream() {
437 boolean result
= true ;
438 int idx
= findParamOfType(XTextInputStream
.class) ;
439 if (idx
< 0) log
.println("Type not found in relation: not tested");
442 Object oStream
= ((XMultiServiceFactory
)tParam
.getMSF())
443 .createInstance("com.sun.star.io.TextInputStream") ;
444 XInputStream xStream
= (XInputStream
)UnoRuntime
.queryInterface
445 (XInputStream
.class, oStream
);
447 oObj
.setCharacterStream(idx
, xStream
, 2) ;
448 } catch (SQLException e
) {
449 log
.println("Unexpected SQL exception:") ;
452 } catch (com
.sun
.star
.uno
.Exception e
) {
453 log
.println("Unexpected exception:") ;
459 tRes
.tested("setCharacterStream()", result
) ;
463 * Sets parameter (if exists) to new value. <p>
464 * Has OK status if no exceptions occurred.
466 public void _setObject() {
467 boolean result
= true ;
468 int idx
= findParamOfType(Object
[].class) ;
469 if (idx
< 0) log
.println("Type not found in relation: not tested");
472 Object obj
= ((XMultiServiceFactory
)tParam
.getMSF()).
473 createInstance("com.sun.star.io.Pipe") ;
475 oObj
.setObject(idx
, obj
) ;
476 } catch (SQLException e
) {
477 log
.println("Unexpected SQL exception:") ;
480 } catch (com
.sun
.star
.uno
.Exception e
) {
481 log
.println("Unexpected exception:") ;
487 tRes
.tested("setObject()", result
) ;
491 * Sets parameter (if exists) to new value. <p>
492 * Has OK status if no exceptions occurred.
494 public void _setObjectWithInfo() {
495 boolean result
= true ;
496 int idx
= findParamOfType(Object
[].class) ;
497 if (idx
< 0) log
.println("Type not found in relation: not tested");
500 Object obj
= ((XMultiServiceFactory
)tParam
.getMSF()).
501 createInstance("com.sun.star.io.Pipe") ;
503 oObj
.setObjectWithInfo(idx
, obj
, DataType
.OBJECT
, 0) ;
504 } catch (SQLException e
) {
505 log
.println("Unexpected SQL exception:") ;
508 } catch (com
.sun
.star
.uno
.Exception e
) {
509 log
.println("Unexpected exception:") ;
515 tRes
.tested("setObjectWithInfo()", result
) ;
518 public void _setRef() {
522 tRes
.tested("setRef()", Status
.skipped(true)) ;
524 public void _setBlob() {
528 tRes
.tested("setBlob()", Status
.skipped(true)) ;
530 public void _setClob() {
534 tRes
.tested("setClob()", Status
.skipped(true)) ;
536 public void _setArray() {
540 tRes
.tested("setArray()", Status
.skipped(true)) ;
545 * Has OK status if no exceptions occurred.
547 public void _clearParameters() {
548 boolean result
= true ;
550 oObj
.clearParameters() ;
551 } catch (SQLException e
) {
552 log
.println("Unexpected SQL exception:") ;
557 tRes
.tested("clearParameters()", result
) ;
562 * Finds in relation vector index of parameter of the appropriate
565 private int findParamOfType(Class clz
) {
567 for (int i
= 0; i
< data
.size(); i
++)
568 if (clz
.isInstance(data
.get(i
))) return i
+ 1 ;
572 } // finish class _XParameters