1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XParameters.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import java
.util
.Vector
;
35 import lib
.MultiMethodTest
;
38 import com
.sun
.star
.io
.XDataInputStream
;
39 import com
.sun
.star
.io
.XInputStream
;
40 import com
.sun
.star
.io
.XTextInputStream
;
41 import com
.sun
.star
.lang
.XMultiServiceFactory
;
42 import com
.sun
.star
.sdbc
.DataType
;
43 import com
.sun
.star
.sdbc
.SQLException
;
44 import com
.sun
.star
.sdbc
.XParameters
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.util
.Date
;
47 import com
.sun
.star
.util
.DateTime
;
48 import com
.sun
.star
.util
.Time
;
52 * Testing <code>com.sun.star.sdbc.XParameters</code>
55 * <li><code> setNull()</code></li>
56 * <li><code> setObjectNull()</code></li>
57 * <li><code> setBoolean()</code></li>
58 * <li><code> setByte()</code></li>
59 * <li><code> setShort()</code></li>
60 * <li><code> setInt()</code></li>
61 * <li><code> setLong()</code></li>
62 * <li><code> setFloat()</code></li>
63 * <li><code> setDouble()</code></li>
64 * <li><code> setString()</code></li>
65 * <li><code> setBytes()</code></li>
66 * <li><code> setDate()</code></li>
67 * <li><code> setTime()</code></li>
68 * <li><code> setTimestamp()</code></li>
69 * <li><code> setBinaryStream()</code></li>
70 * <li><code> setCharacterStream()</code></li>
71 * <li><code> setObject()</code></li>
72 * <li><code> setObjectWithInfo()</code></li>
73 * <li><code> setRef()</code></li>
74 * <li><code> setBlob()</code></li>
75 * <li><code> setClob()</code></li>
76 * <li><code> setArray()</code></li>
77 * <li><code> clearParameters()</code></li>
79 * Object relations required :
81 * <li> <code>'XParameters.ParamValues'</code> : is a
82 * <code>java.util.Vector</code> object
83 * that contains parameter types and values of the statement. Each
84 * element of vector corresponds to appropriate parameter (element
85 * with index 0 to parameter #1, 1 -> #2, etc.). <p>
86 * The following <code>XParameters</code> methods correspond to classes
89 * <li> <code>setBinaryStream</code> -
90 * <code>com.sun.star.io.XDataInputStream</code> class. </li>
91 * <li> <code>setCharacterStream</code> -
92 * <code>com.sun.star.io.XTextInputStream</code> class. </li>
93 * <li> <code>setObject</code> -
94 * <code>java.lang.Object[]</code> class, the element with
95 * index 0 must be used. </li>
97 * Other methods uses types of their arguments (i.e.
98 * <code>java.lang.String</code>
99 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
100 * for <code>setRef</code> method).
103 * @see com.sun.star.sdbc.XParameters
105 public class _XParameters
extends MultiMethodTest
{
107 // oObj filled by MultiMethodTest
108 public XParameters oObj
= null ;
110 private Vector data
= null ;
113 * Gets object relation
115 public void before() {
116 data
= (Vector
) tEnv
.getObjRelation("XParameters.ParamValues") ;
118 log
.println("!!! Relation not found !!!") ;
123 * Sets String parameter (if exists) to SQL NULL value. <p>
124 * Has OK status if no exceptions occured.
126 public void _setNull() {
127 boolean result
= true ;
128 int idx
= findParamOfType(String
.class) ;
129 if (idx
< 0) log
.println("Type not found in relation: not tested");
132 oObj
.setNull(idx
, DataType
.VARCHAR
) ;
133 } catch (SQLException e
) {
134 log
.println("Unexpected SQL exception:") ;
140 tRes
.tested("setNull()", result
) ;
143 public void _setObjectNull() {
147 tRes
.tested("setObjectNull()", Status
.skipped(true)) ;
151 * Sets String parameter (if exists) to new value. <p>
152 * Has OK status if no exceptions occured.
154 public void _setString() {
155 boolean result
= true ;
156 int idx
= findParamOfType(String
.class) ;
157 if (idx
< 0) log
.println("Type not found in relation: not tested");
160 oObj
.setString(idx
, "XParameters") ;
161 } catch (SQLException e
) {
162 log
.println("Unexpected SQL exception:") ;
168 tRes
.tested("setString()", result
) ;
172 * Sets parameter (if exists) to new value. <p>
173 * Has OK status if no exceptions occured.
175 public void _setBoolean() {
176 boolean result
= true ;
177 int idx
= findParamOfType(Boolean
.class) ;
178 if (idx
< 0) log
.println("Type not found in relation: not tested");
181 oObj
.setBoolean(idx
, true) ;
182 } catch (SQLException e
) {
183 log
.println("Unexpected SQL exception:") ;
189 tRes
.tested("setBoolean()", result
) ;
193 * Sets parameter (if exists) to new value. <p>
194 * Has OK status if no exceptions occured.
196 public void _setByte() {
197 boolean result
= true ;
198 int idx
= findParamOfType(Byte
.class) ;
199 if (idx
< 0) log
.println("Type not found in relation: not tested");
202 oObj
.setByte(idx
, (byte)122) ;
203 } catch (SQLException e
) {
204 log
.println("Unexpected SQL exception:") ;
210 tRes
.tested("setByte()", result
) ;
214 * Sets parameter (if exists) to new value. <p>
215 * Has OK status if no exceptions occured.
217 public void _setShort() {
218 boolean result
= true ;
219 int idx
= findParamOfType(Short
.class) ;
220 if (idx
< 0) log
.println("Type not found in relation: not tested");
223 oObj
.setShort(idx
, (short)133) ;
224 } catch (SQLException e
) {
225 log
.println("Unexpected SQL exception:") ;
231 tRes
.tested("setShort()", result
) ;
235 * Sets parameter (if exists) to new value. <p>
236 * Has OK status if no exceptions occured.
238 public void _setInt() {
239 boolean result
= true ;
240 int idx
= findParamOfType(Integer
.class) ;
241 if (idx
< 0) log
.println("Type not found in relation: not tested");
244 oObj
.setInt(idx
, 13300) ;
245 } catch (SQLException e
) {
246 log
.println("Unexpected SQL exception:") ;
252 tRes
.tested("setInt()", result
) ;
256 * Sets parameter (if exists) to new value. <p>
257 * Has OK status if no exceptions occured.
259 public void _setLong() {
260 boolean result
= true ;
261 int idx
= findParamOfType(Long
.class) ;
262 if (idx
< 0) log
.println("Type not found in relation: not tested");
265 oObj
.setLong(idx
, 13362453) ;
266 } catch (SQLException e
) {
267 log
.println("Unexpected SQL exception:") ;
273 tRes
.tested("setLong()", result
) ;
277 * Sets parameter (if exists) to new value. <p>
278 * Has OK status if no exceptions occured.
280 public void _setFloat() {
281 boolean result
= true ;
282 int idx
= findParamOfType(Float
.class) ;
283 if (idx
< 0) log
.println("Type not found in relation: not tested");
286 oObj
.setFloat(idx
, (float)133.55) ;
287 } catch (SQLException e
) {
288 log
.println("Unexpected SQL exception:") ;
294 tRes
.tested("setFloat()", result
) ;
298 * Sets parameter (if exists) to new value. <p>
299 * Has OK status if no exceptions occured.
301 public void _setDouble() {
302 boolean result
= true ;
303 int idx
= findParamOfType(Double
.class) ;
304 if (idx
< 0) log
.println("Type not found in relation: not tested");
307 oObj
.setDouble(idx
, 133) ;
308 } catch (SQLException e
) {
309 log
.println("Unexpected SQL exception:") ;
315 tRes
.tested("setDouble()", result
) ;
319 * Sets parameter (if exists) to new value. <p>
320 * Has OK status if no exceptions occured.
322 public void _setBytes() {
323 boolean result
= true ;
324 int idx
= findParamOfType(byte[].class) ;
325 if (idx
< 0) log
.println("Type not found in relation: not tested");
328 oObj
.setBytes(idx
, new byte[] {5}) ;
329 } catch (SQLException e
) {
330 log
.println("Unexpected SQL exception:") ;
336 tRes
.tested("setBytes()", result
) ;
340 * Sets parameter (if exists) to new value. <p>
341 * Has OK status if no exceptions occured.
343 public void _setDate() {
344 boolean result
= true ;
345 int idx
= findParamOfType(Date
.class) ;
346 if (idx
< 0) log
.println("Type not found in relation: not tested");
350 idx
, new Date ((short)19, (short)01, (short)1979)) ;
351 } catch (SQLException e
) {
352 log
.println("Unexpected SQL exception:") ;
358 tRes
.tested("setDate()", result
) ;
362 * Sets parameter (if exists) to new value. <p>
363 * Has OK status if no exceptions occured.
365 public void _setTime() {
366 boolean result
= true ;
367 int idx
= findParamOfType(Time
.class) ;
368 if (idx
< 0) log
.println("Type not found in relation: not tested");
372 idx
, new Time((short)1,(short)2,(short)3,(short)44)) ;
373 } catch (SQLException e
) {
374 log
.println("Unexpected SQL exception:") ;
380 tRes
.tested("setTime()", result
) ;
384 * Sets parameter (if exists) to new value. <p>
385 * Has OK status if no exceptions occured.
387 public void _setTimestamp() {
388 boolean result
= true ;
389 int idx
= findParamOfType(DateTime
.class) ;
390 if (idx
< 0) log
.println("Type not found in relation: not tested");
393 oObj
.setTimestamp(idx
, new DateTime((short)1,(short)2,(short)3,
394 (short)4, (short)19, (short)01, (short)1979)) ;
395 } catch (SQLException e
) {
396 log
.println("Unexpected SQL exception:") ;
402 tRes
.tested("setTimestamp()", result
) ;
406 * Sets parameter (if exists) to new value. <p>
407 * Has OK status if no exceptions occured.
409 public void _setBinaryStream() {
410 boolean result
= true ;
411 int idx
= findParamOfType(XDataInputStream
.class) ;
412 if (idx
< 0) log
.println("Type not found in relation: not tested");
415 Object oStream
= ((XMultiServiceFactory
)tParam
.getMSF()).
416 createInstance("com.sun.star.io.DataInputStream") ;
417 XInputStream xStream
= (XInputStream
)UnoRuntime
.queryInterface
418 (XInputStream
.class, oStream
);
420 oObj
.setBinaryStream(idx
, xStream
, 2) ;
421 } catch (SQLException e
) {
422 log
.println("Unexpected SQL exception:") ;
425 } catch (com
.sun
.star
.uno
.Exception e
) {
426 log
.println("Unexpected exception:") ;
432 tRes
.tested("setBinaryStream()", result
) ;
436 * Sets parameter (if exists) to new value. <p>
437 * Has OK status if no exceptions occured.
439 public void _setCharacterStream() {
440 boolean result
= true ;
441 int idx
= findParamOfType(XTextInputStream
.class) ;
442 if (idx
< 0) log
.println("Type not found in relation: not tested");
445 Object oStream
= ((XMultiServiceFactory
)tParam
.getMSF())
446 .createInstance("com.sun.star.io.TextInputStream") ;
447 XInputStream xStream
= (XInputStream
)UnoRuntime
.queryInterface
448 (XInputStream
.class, oStream
);
450 oObj
.setCharacterStream(idx
, xStream
, 2) ;
451 } catch (SQLException e
) {
452 log
.println("Unexpected SQL exception:") ;
455 } catch (com
.sun
.star
.uno
.Exception e
) {
456 log
.println("Unexpected exception:") ;
462 tRes
.tested("setCharacterStream()", result
) ;
466 * Sets parameter (if exists) to new value. <p>
467 * Has OK status if no exceptions occured.
469 public void _setObject() {
470 boolean result
= true ;
471 int idx
= findParamOfType(Object
[].class) ;
472 if (idx
< 0) log
.println("Type not found in relation: not tested");
475 Object obj
= ((XMultiServiceFactory
)tParam
.getMSF()).
476 createInstance("com.sun.star.io.Pipe") ;
478 oObj
.setObject(idx
, obj
) ;
479 } catch (SQLException e
) {
480 log
.println("Unexpected SQL exception:") ;
483 } catch (com
.sun
.star
.uno
.Exception e
) {
484 log
.println("Unexpected exception:") ;
490 tRes
.tested("setObject()", result
) ;
494 * Sets parameter (if exists) to new value. <p>
495 * Has OK status if no exceptions occured.
497 public void _setObjectWithInfo() {
498 boolean result
= true ;
499 int idx
= findParamOfType(Object
[].class) ;
500 if (idx
< 0) log
.println("Type not found in relation: not tested");
503 Object obj
= ((XMultiServiceFactory
)tParam
.getMSF()).
504 createInstance("com.sun.star.io.Pipe") ;
506 oObj
.setObjectWithInfo(idx
, obj
, DataType
.OBJECT
, 0) ;
507 } catch (SQLException e
) {
508 log
.println("Unexpected SQL exception:") ;
511 } catch (com
.sun
.star
.uno
.Exception e
) {
512 log
.println("Unexpected exception:") ;
518 tRes
.tested("setObjectWithInfo()", result
) ;
521 public void _setRef() {
525 tRes
.tested("setRef()", Status
.skipped(true)) ;
527 public void _setBlob() {
531 tRes
.tested("setBlob()", Status
.skipped(true)) ;
533 public void _setClob() {
537 tRes
.tested("setClob()", Status
.skipped(true)) ;
539 public void _setArray() {
543 tRes
.tested("setArray()", Status
.skipped(true)) ;
548 * Has OK status if no exceptions occured.
550 public void _clearParameters() {
551 boolean result
= true ;
553 oObj
.clearParameters() ;
554 } catch (SQLException e
) {
555 log
.println("Unexpected SQL exception:") ;
560 tRes
.tested("clearParameters()", result
) ;
565 * Finds in relation vector index of parameter of the appropriate
568 private int findParamOfType(Class clz
) {
570 for (int i
= 0; i
< data
.size(); i
++)
571 if (clz
.isInstance(data
.get(i
))) return i
+ 1 ;
575 } // finish class _XParameters