1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.sdbc;
25 
26 import java.util.Vector;
27 
28 import lib.MultiMethodTest;
29 import lib.Status;
30 
31 import com.sun.star.io.XDataInputStream;
32 import com.sun.star.io.XInputStream;
33 import com.sun.star.io.XTextInputStream;
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.sdbc.DataType;
36 import com.sun.star.sdbc.SQLException;
37 import com.sun.star.sdbc.XParameters;
38 import com.sun.star.uno.UnoRuntime;
39 import com.sun.star.util.Date;
40 import com.sun.star.util.DateTime;
41 import com.sun.star.util.Time;
42 
43 /**
44 /**
45 * Testing <code>com.sun.star.sdbc.XParameters</code>
46 * interface methods :
47 * <ul>
48 *  <li><code> setNull()</code></li>
49 *  <li><code> setObjectNull()</code></li>
50 *  <li><code> setBoolean()</code></li>
51 *  <li><code> setByte()</code></li>
52 *  <li><code> setShort()</code></li>
53 *  <li><code> setInt()</code></li>
54 *  <li><code> setLong()</code></li>
55 *  <li><code> setFloat()</code></li>
56 *  <li><code> setDouble()</code></li>
57 *  <li><code> setString()</code></li>
58 *  <li><code> setBytes()</code></li>
59 *  <li><code> setDate()</code></li>
60 *  <li><code> setTime()</code></li>
61 *  <li><code> setTimestamp()</code></li>
62 *  <li><code> setBinaryStream()</code></li>
63 *  <li><code> setCharacterStream()</code></li>
64 *  <li><code> setObject()</code></li>
65 *  <li><code> setObjectWithInfo()</code></li>
66 *  <li><code> setRef()</code></li>
67 *  <li><code> setBlob()</code></li>
68 *  <li><code> setClob()</code></li>
69 *  <li><code> setArray()</code></li>
70 *  <li><code> clearParameters()</code></li>
71 * </ul> <p>
72 * Object relations required :
73 * <ul>
74 * <li> <code>'XParameters.ParamValues'</code> :  is a
75 * <code>java.util.Vector</code> object
76 * that contains parameter types and values of the statement. Each
77 * element of vector corresponds to appropriate parameter (element
78 * with index 0 to parameter #1, 1 -> #2, etc.). <p>
79 * The following <code>XParameters</code> methods correspond to classes
80 * in Vector :
81 * <ul>
82 *   <li> <code>setBinaryStream</code> -
83 *        <code>com.sun.star.io.XDataInputStream</code> class. </li>
84 *   <li> <code>setCharacterStream</code> -
85 *        <code>com.sun.star.io.XTextInputStream</code> class. </li>
86 *   <li> <code>setObject</code> -
87 *        <code>java.lang.Object[]</code> class, the element with
88 *         index 0 must be used. </li>
89 * </ul>
90 * Other methods uses types of their arguments (i.e.
91 * <code>java.lang.String</code>
92 * for <code>setString</code> method, <code>com.sun.star.sdbc.XRef</code>
93 * for <code>setRef</code> method).
94 * </li>
95 * </ul>
96 * @see com.sun.star.sdbc.XParameters
97 */
98 public class _XParameters extends MultiMethodTest {
99 
100     // oObj filled by MultiMethodTest
101     public XParameters oObj = null ;
102 
103     private Vector data = null ;
104 
105     /**
106     * Gets object relation
107     */
before()108     public void before() {
109         data = (Vector) tEnv.getObjRelation("XParameters.ParamValues") ;
110         if (data == null) {
111             log.println("!!! Relation not found !!!") ;
112         }
113     }
114 
115     /**
116     * Sets String parameter (if exists) to SQL NULL value. <p>
117     * Has OK status if no exceptions occurred.
118     */
_setNull()119     public void _setNull() {
120         boolean result = true ;
121         int idx = findParamOfType(String.class) ;
122         if (idx < 0) log.println("Type not found in relation: not tested");
123         else {
124             try {
125                 oObj.setNull(idx, DataType.VARCHAR) ;
126             } catch (SQLException e) {
127                 log.println("Unexpected SQL exception:") ;
128                 log.println(e) ;
129                 result = false ;
130             }
131         }
132 
133         tRes.tested("setNull()", result) ;
134     }
135 
_setObjectNull()136     public void _setObjectNull() {
137         /*
138             !!! TO DO !!!
139         */
140         tRes.tested("setObjectNull()", Status.skipped(true)) ;
141     }
142 
143     /**
144     * Sets String parameter (if exists) to new value. <p>
145     * Has OK status if no exceptions occurred.
146     */
_setString()147     public void _setString() {
148         boolean result = true ;
149         int idx = findParamOfType(String.class) ;
150         if (idx < 0) log.println("Type not found in relation: not tested");
151         else {
152             try {
153                 oObj.setString(idx, "XParameters") ;
154             } catch (SQLException e) {
155                 log.println("Unexpected SQL exception:") ;
156                 log.println(e) ;
157                 result = false ;
158             }
159         }
160 
161         tRes.tested("setString()", result) ;
162     }
163 
164     /**
165     * Sets parameter (if exists) to new value. <p>
166     * Has OK status if no exceptions occurred.
167     */
_setBoolean()168     public void _setBoolean() {
169         boolean result = true ;
170         int idx = findParamOfType(Boolean.class) ;
171         if (idx < 0) log.println("Type not found in relation: not tested");
172         else {
173             try {
174                 oObj.setBoolean(idx, true) ;
175             } catch (SQLException e) {
176                 log.println("Unexpected SQL exception:") ;
177                 log.println(e) ;
178                 result = false ;
179             }
180         }
181 
182         tRes.tested("setBoolean()", result) ;
183     }
184 
185     /**
186     * Sets parameter (if exists) to new value. <p>
187     * Has OK status if no exceptions occurred.
188     */
_setByte()189     public void _setByte() {
190         boolean result = true ;
191         int idx = findParamOfType(Byte.class) ;
192         if (idx < 0) log.println("Type not found in relation: not tested");
193         else {
194             try {
195                 oObj.setByte(idx, (byte)122) ;
196             } catch (SQLException e) {
197                 log.println("Unexpected SQL exception:") ;
198                 log.println(e) ;
199                 result = false ;
200             }
201         }
202 
203         tRes.tested("setByte()", result) ;
204     }
205 
206     /**
207     * Sets parameter (if exists) to new value. <p>
208     * Has OK status if no exceptions occurred.
209     */
_setShort()210     public void _setShort() {
211         boolean result = true ;
212         int idx = findParamOfType(Short.class) ;
213         if (idx < 0) log.println("Type not found in relation: not tested");
214         else {
215             try {
216                 oObj.setShort(idx, (short)133) ;
217             } catch (SQLException e) {
218                 log.println("Unexpected SQL exception:") ;
219                 log.println(e) ;
220                 result = false ;
221             }
222         }
223 
224         tRes.tested("setShort()", result) ;
225     }
226 
227     /**
228     * Sets parameter (if exists) to new value. <p>
229     * Has OK status if no exceptions occurred.
230     */
_setInt()231     public void _setInt() {
232         boolean result = true ;
233         int idx = findParamOfType(Integer.class) ;
234         if (idx < 0) log.println("Type not found in relation: not tested");
235         else {
236             try {
237                 oObj.setInt(idx, 13300) ;
238             } catch (SQLException e) {
239                 log.println("Unexpected SQL exception:") ;
240                 log.println(e) ;
241                 result = false ;
242             }
243         }
244 
245         tRes.tested("setInt()", result) ;
246     }
247 
248     /**
249     * Sets parameter (if exists) to new value. <p>
250     * Has OK status if no exceptions occurred.
251     */
_setLong()252     public void _setLong() {
253         boolean result = true ;
254         int idx = findParamOfType(Long.class) ;
255         if (idx < 0) log.println("Type not found in relation: not tested");
256         else {
257             try {
258                 oObj.setLong(idx, 13362453) ;
259             } catch (SQLException e) {
260                 log.println("Unexpected SQL exception:") ;
261                 log.println(e) ;
262                 result = false ;
263             }
264         }
265 
266         tRes.tested("setLong()", result) ;
267     }
268 
269     /**
270     * Sets parameter (if exists) to new value. <p>
271     * Has OK status if no exceptions occurred.
272     */
_setFloat()273     public void _setFloat() {
274         boolean result = true ;
275         int idx = findParamOfType(Float.class) ;
276         if (idx < 0) log.println("Type not found in relation: not tested");
277         else {
278             try {
279                 oObj.setFloat(idx, (float)133.55) ;
280             } catch (SQLException e) {
281                 log.println("Unexpected SQL exception:") ;
282                 log.println(e) ;
283                 result = false ;
284             }
285         }
286 
287         tRes.tested("setFloat()", result) ;
288     }
289 
290     /**
291     * Sets parameter (if exists) to new value. <p>
292     * Has OK status if no exceptions occurred.
293     */
_setDouble()294     public void _setDouble() {
295         boolean result = true ;
296         int idx = findParamOfType(Double.class) ;
297         if (idx < 0) log.println("Type not found in relation: not tested");
298         else {
299             try {
300                 oObj.setDouble(idx, 133) ;
301             } catch (SQLException e) {
302                 log.println("Unexpected SQL exception:") ;
303                 log.println(e) ;
304                 result = false ;
305             }
306         }
307 
308         tRes.tested("setDouble()", result) ;
309     }
310 
311     /**
312     * Sets parameter (if exists) to new value. <p>
313     * Has OK status if no exceptions occurred.
314     */
_setBytes()315     public void _setBytes() {
316         boolean result = true ;
317         int idx = findParamOfType(byte[].class) ;
318         if (idx < 0) log.println("Type not found in relation: not tested");
319         else {
320             try {
321                 oObj.setBytes(idx, new byte[] {5}) ;
322             } catch (SQLException e) {
323                 log.println("Unexpected SQL exception:") ;
324                 log.println(e) ;
325                 result = false ;
326             }
327         }
328 
329         tRes.tested("setBytes()", result) ;
330     }
331 
332     /**
333     * Sets parameter (if exists) to new value. <p>
334     * Has OK status if no exceptions occurred.
335     */
_setDate()336     public void _setDate() {
337         boolean result = true ;
338         int idx = findParamOfType(Date.class) ;
339         if (idx < 0) log.println("Type not found in relation: not tested");
340         else {
341             try {
342                 oObj.setDate(
343                     idx, new Date ((short)19, (short)01, (short)1979)) ;
344             } catch (SQLException e) {
345                 log.println("Unexpected SQL exception:") ;
346                 log.println(e) ;
347                 result = false ;
348             }
349         }
350 
351         tRes.tested("setDate()", result) ;
352     }
353 
354     /**
355     * Sets parameter (if exists) to new value. <p>
356     * Has OK status if no exceptions occurred.
357     */
_setTime()358     public void _setTime() {
359         boolean result = true ;
360         int idx = findParamOfType(Time.class) ;
361         if (idx < 0) log.println("Type not found in relation: not tested");
362         else {
363             try {
364                 oObj.setTime(
365                     idx, new Time((short)1,(short)2,(short)3,(short)44)) ;
366             } catch (SQLException e) {
367                 log.println("Unexpected SQL exception:") ;
368                 log.println(e) ;
369                 result = false ;
370             }
371         }
372 
373         tRes.tested("setTime()", result) ;
374     }
375 
376     /**
377     * Sets parameter (if exists) to new value. <p>
378     * Has OK status if no exceptions occurred.
379     */
_setTimestamp()380     public void _setTimestamp() {
381         boolean result = true ;
382         int idx = findParamOfType(DateTime.class) ;
383         if (idx < 0) log.println("Type not found in relation: not tested");
384         else {
385             try {
386                 oObj.setTimestamp(idx, new DateTime((short)1,(short)2,(short)3,
387                     (short)4, (short)19, (short)01, (short)1979)) ;
388             } catch (SQLException e) {
389                 log.println("Unexpected SQL exception:") ;
390                 log.println(e) ;
391                 result = false ;
392             }
393         }
394 
395         tRes.tested("setTimestamp()", result) ;
396     }
397 
398     /**
399     * Sets parameter (if exists) to new value. <p>
400     * Has OK status if no exceptions occurred.
401     */
_setBinaryStream()402     public void _setBinaryStream() {
403         boolean result = true ;
404         int idx = findParamOfType(XDataInputStream.class) ;
405         if (idx < 0) log.println("Type not found in relation: not tested");
406         else {
407             try {
408                 Object oStream = ((XMultiServiceFactory)tParam.getMSF()).
409                         createInstance("com.sun.star.io.DataInputStream") ;
410                 XInputStream xStream = (XInputStream)UnoRuntime.queryInterface
411                     (XInputStream.class, oStream);
412 
413                 oObj.setBinaryStream(idx, xStream, 2) ;
414             } catch (SQLException e) {
415                 log.println("Unexpected SQL exception:") ;
416                 log.println(e) ;
417                 result = false ;
418             } catch (com.sun.star.uno.Exception e) {
419                 log.println("Unexpected exception:") ;
420                 log.println(e) ;
421                 result = false ;
422             }
423         }
424 
425         tRes.tested("setBinaryStream()", result) ;
426     }
427 
428     /**
429     * Sets parameter (if exists) to new value. <p>
430     * Has OK status if no exceptions occurred.
431     */
_setCharacterStream()432     public void _setCharacterStream() {
433         boolean result = true ;
434         int idx = findParamOfType(XTextInputStream.class) ;
435         if (idx < 0) log.println("Type not found in relation: not tested");
436         else {
437             try {
438                 Object oStream = ((XMultiServiceFactory)tParam.getMSF())
439                         .createInstance("com.sun.star.io.TextInputStream") ;
440                 XInputStream xStream = (XInputStream)UnoRuntime.queryInterface
441                     (XInputStream.class, oStream);
442 
443                 oObj.setCharacterStream(idx, xStream, 2) ;
444             } catch (SQLException e) {
445                 log.println("Unexpected SQL exception:") ;
446                 log.println(e) ;
447                 result = false ;
448             } catch (com.sun.star.uno.Exception e) {
449                 log.println("Unexpected exception:") ;
450                 log.println(e) ;
451                 result = false ;
452             }
453         }
454 
455         tRes.tested("setCharacterStream()", result) ;
456     }
457 
458     /**
459     * Sets parameter (if exists) to new value. <p>
460     * Has OK status if no exceptions occurred.
461     */
_setObject()462     public void _setObject() {
463         boolean result = true ;
464         int idx = findParamOfType(Object[].class) ;
465         if (idx < 0) log.println("Type not found in relation: not tested");
466         else {
467             try {
468                 Object obj = ((XMultiServiceFactory)tParam.getMSF()).
469                                 createInstance("com.sun.star.io.Pipe") ;
470 
471                 oObj.setObject(idx, obj) ;
472             } catch (SQLException e) {
473                 log.println("Unexpected SQL exception:") ;
474                 log.println(e) ;
475                 result = false ;
476             } catch (com.sun.star.uno.Exception e) {
477                 log.println("Unexpected exception:") ;
478                 log.println(e) ;
479                 result = false ;
480             }
481         }
482 
483         tRes.tested("setObject()", result) ;
484     }
485 
486     /**
487     * Sets parameter (if exists) to new value. <p>
488     * Has OK status if no exceptions occurred.
489     */
_setObjectWithInfo()490     public void _setObjectWithInfo() {
491         boolean result = true ;
492         int idx = findParamOfType(Object[].class) ;
493         if (idx < 0) log.println("Type not found in relation: not tested");
494         else {
495             try {
496                 Object obj = ((XMultiServiceFactory)tParam.getMSF()).
497                     createInstance("com.sun.star.io.Pipe") ;
498 
499                 oObj.setObjectWithInfo(idx, obj, DataType.OBJECT, 0) ;
500             } catch (SQLException e) {
501                 log.println("Unexpected SQL exception:") ;
502                 log.println(e) ;
503                 result = false ;
504             } catch (com.sun.star.uno.Exception e) {
505                 log.println("Unexpected exception:") ;
506                 log.println(e) ;
507                 result = false ;
508             }
509         }
510 
511         tRes.tested("setObjectWithInfo()", result) ;
512     }
513 
_setRef()514     public void _setRef() {
515         /*
516             !!! TO DO !!!
517         */
518         tRes.tested("setRef()", Status.skipped(true)) ;
519     }
_setBlob()520     public void _setBlob() {
521         /*
522             !!! TO DO !!!
523         */
524         tRes.tested("setBlob()", Status.skipped(true)) ;
525     }
_setClob()526     public void _setClob() {
527         /*
528             !!! TO DO !!!
529         */
530         tRes.tested("setClob()", Status.skipped(true)) ;
531     }
_setArray()532     public void _setArray() {
533         /*
534             !!! TO DO !!!
535         */
536         tRes.tested("setArray()", Status.skipped(true)) ;
537     }
538 
539     /**
540     * Calls method. <p>
541     * Has OK status if no exceptions occurred.
542     */
_clearParameters()543     public void _clearParameters() {
544         boolean result = true ;
545         try {
546             oObj.clearParameters() ;
547         } catch (SQLException e) {
548             log.println("Unexpected SQL exception:") ;
549             log.println(e) ;
550             result = false ;
551         }
552 
553         tRes.tested("clearParameters()", result) ;
554     }
555 
556 
557     /**
558     * Finds in relation vector index of parameter of the appropriate
559     * type.
560     */
findParamOfType(Class clz)561     private int findParamOfType(Class clz) {
562 
563         for (int i = 0; i < data.size(); i++)
564             if (clz.isInstance(data.get(i))) return i + 1 ;
565         return -1 ;
566     }
567 
568 }  // finish class _XParameters
569 
570 
571