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 lib.MultiMethodTest;
27 
28 import com.sun.star.sdbc.SQLException;
29 import com.sun.star.sdbc.XResultSet;
30 import com.sun.star.sdbc.XRow;
31 import com.sun.star.sdbc.XRowUpdate;
32 import com.sun.star.uno.UnoRuntime;
33 
34 /**
35 /**
36 * Testing <code>com.sun.star.sdbc.XResultSet</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> next()</code></li>
40 *  <li><code> isBeforeFirst()</code></li>
41 *  <li><code> isAfterLast()</code></li>
42 *  <li><code> isFirst()</code></li>
43 *  <li><code> isLast()</code></li>
44 *  <li><code> beforeFirst()</code></li>
45 *  <li><code> afterLast()</code></li>
46 *  <li><code> first()</code></li>
47 *  <li><code> last()</code></li>
48 *  <li><code> getRow()</code></li>
49 *  <li><code> absolute()</code></li>
50 *  <li><code> relative()</code></li>
51 *  <li><code> previous()</code></li>
52 *  <li><code> refreshRow()</code></li>
53 *  <li><code> rowUpdated()</code></li>
54 *  <li><code> rowInserted()</code></li>
55 *  <li><code> rowDeleted()</code></li>
56 *  <li><code> getStatement()</code></li>
57 * </ul> <p>
58 * This test needs the following object relations :
59 * <ul>
60 *  <li> <code>'XResultSet.hasStatement'</code> (<b>optional</b> of type
61 * <code>Object</code>):
62 *  it the relation exists than <code>getStatement</code> method
63 *  must not return <code>null</code> </li>
64 * <ul> <p>
65 * Test places DB cursor to different positions and then checks
66 * its current position. <p>
67 * Test is <b> NOT </b> multithread compilant. <p>
68 * @see com.sun.star.sdbc.XResultSet
69 */
70 public class _XResultSet extends MultiMethodTest {
71 
72     // oObj filled by MultiMethodTest
73     public XResultSet oObj = null ;
74 
75     /**
76      * Positions the cursor to the first row.
77      * Forces method tests to be executed in definite order.
78      */
before()79     public void before() {
80         try {
81             oObj.last() ;
82             log.println("Totally number of rows is " + oObj.getRow()) ;
83             oObj.first() ;
84         } catch (SQLException e) {
85             log.println("Ignored exception :") ;
86             e.printStackTrace(log);
87         }
88 
89         executeMethod("isBeforeFirst()") ;
90         executeMethod("isAfterLast()") ;
91         executeMethod("isLast()") ;
92         executeMethod("isFirst()") ;
93         executeMethod("next()") ;
94         executeMethod("previous()") ;
95     }
96 
97     /**
98     * Places the cursor before the first row. <p>
99     * Has <b>OK</b> status if no exceptions were thrown.
100     */
_beforeFirst()101     public void _beforeFirst() {
102         try {
103             oObj.beforeFirst() ;
104         } catch (SQLException e) {
105             log.println("Exception occured :") ;
106             e.printStackTrace(log) ;
107             tRes.tested("beforeFirst()", false) ;
108             return ;
109         }
110         tRes.tested("beforeFirst()", true) ;
111     }
112 
113     /**
114     * The method is called immediatly after <code>beforeFirst</code>
115     * method test. <p>
116     * Has <b>OK</b> status if method returns <code>true</code>. <p>
117     * The following method tests are to be completed successfully before :
118     * <ul>
119     *  <li> <code> beforeFirst </code> : to position cursor before
120     *   the first row. </li>
121     * </ul>
122     */
_isBeforeFirst()123     public void _isBeforeFirst() {
124         requiredMethod("beforeFirst()") ;
125 
126         boolean result = true ;
127 
128         try {
129             result = oObj.isBeforeFirst() ;
130         } catch (SQLException e) {
131             log.println("Exception occured :") ;
132             e.printStackTrace(log) ;
133             result = false ;
134         }
135         tRes.tested("isBeforeFirst()", result) ;
136     }
137 
138     /**
139     * Places the cursor after the last row. <p>
140     * Has <b>OK</b> status if no exceptions were thrown.
141     */
_afterLast()142     public void _afterLast() {
143         try {
144             oObj.afterLast() ;
145         } catch (SQLException e) {
146             log.println("Exception occured :") ;
147             e.printStackTrace(log) ;
148             tRes.tested("afterLast()", false) ;
149             return ;
150         }
151         tRes.tested("afterLast()", true) ;
152     }
153 
154     /**
155     * The method is called immediatly after <code>afterLast</code>
156     * method test. <p>
157     * Has <b>OK</b> status if method returns <code>true</code> <p>
158     * The following method tests are to be completed successfully before :
159     * <ul>
160     *  <li> <code> afterLast </code> : to position cursor after
161     *   the last row. </li>
162     * </ul>
163     */
_isAfterLast()164     public void _isAfterLast() {
165         requiredMethod("afterLast()") ;
166 
167         boolean result = true ;
168 
169         try {
170             result = oObj.isAfterLast() ;
171         } catch (SQLException e) {
172             log.println("Exception occured :") ;
173             e.printStackTrace(log) ;
174             result = false ;
175         }
176         tRes.tested("isAfterLast()", result) ;
177     }
178 
179     /**
180     * Places the cursor on the first row. <p>
181     * Has <b>OK</b> status if no exceptions were thrown.
182     */
_first()183     public void _first() {
184         try {
185             oObj.first() ;
186         } catch (SQLException e) {
187             log.println("Exception occured :") ;
188             e.printStackTrace(log) ;
189             tRes.tested("first()", false) ;
190             return ;
191         }
192         tRes.tested("first()", true) ;
193     }
194 
195     /**
196     * The method is called immediatly after <code>first</code>
197     * method test. <p>
198     * Has <b>OK</b> status if method returns <code>true</code>. <p>
199     * The following method tests are to be completed successfully before :
200     * <ul>
201     *  <li> <code> first </code> : to position cursor on
202     *   the first row. </li>
203     * </ul>
204     */
_isFirst()205     public void _isFirst() {
206         requiredMethod("first()") ;
207 
208         boolean result = true ;
209 
210         try {
211             result = oObj.isFirst() ;
212         } catch (SQLException e) {
213             log.println("Exception occured :") ;
214             e.printStackTrace(log) ;
215             result = false ;
216         }
217         tRes.tested("isFirst()", result) ;
218     }
219 
220     /**
221     * Places the cursor on the last row. <p>
222     * Has <b>OK</b> status if no exceptions were thrown.
223     */
_last()224     public void _last() {
225         try {
226             oObj.last() ;
227         } catch (SQLException e) {
228             log.println("Exception occured :") ;
229             e.printStackTrace(log) ;
230             tRes.tested("last()", false) ;
231             return ;
232         }
233         tRes.tested("last()", true) ;
234     }
235 
236     /**
237     * The method is called immediatly after <code>last</code>
238     * method test. <p>
239     * Has <b>OK</b> status if method returns <code>true</code>. <p>
240     * The following method tests are to be completed successfully before :
241     * <ul>
242     *  <li> <code> last </code> : to position cursor on
243     *   the last row. </li>
244     * </ul>
245     */
_isLast()246     public void _isLast() {
247         requiredMethod("last()") ;
248         boolean result = true ;
249 
250         try {
251             result = oObj.isLast() ;
252         } catch (SQLException e) {
253             log.println("Exception occured :") ;
254             e.printStackTrace(log) ;
255             result = false ;
256         }
257         tRes.tested("isLast()", result) ;
258     }
259 
260     /**
261     * Places the cursor on the row number 1. <p>
262     * Has <b>OK</b> status if no exceptions were thrown.
263     */
_absolute()264     public void _absolute() {
265         boolean result = true ;
266 
267         try {
268             oObj.absolute(1) ;
269         } catch (SQLException e) {
270             log.println("Exception occured :") ;
271             e.printStackTrace(log) ;
272             result = false ;
273         }
274         tRes.tested("absolute()", result) ;
275     }
276 
277     /**
278     * The method is called immediatly after <code>absolute</code>
279     * method test. <p>
280     * Has <b>OK</b> status if method returns 1. <p>
281     * The following method tests are to be completed successfully before :
282     * <ul>
283     *  <li> <code> absolute </code> : to position cursor on
284     *   the row number 1. </li>
285     * </ul>
286     */
_getRow()287     public void _getRow() {
288         requiredMethod("absolute()");
289         boolean result = true;
290 
291         try {
292             result &= oObj.getRow() == 1;
293         } catch (SQLException e) {
294             log.println("Exception occured:");
295             e.printStackTrace(log);
296             result = false;
297         }
298 
299         tRes.tested("getRow()", result);
300     }
301 
302     /**
303     * Positions the cursor to the next row. Current row
304     * number is retrieved before and after method call. <p>
305     * Has <b>OK</b> status if current row number increases
306     * by 1 after method call.
307     */
_next()308     public void _next() {
309         boolean result = true ;
310 
311         try {
312             int prevRow = oObj.getRow() ;
313             oObj.next() ;
314 
315             log.println("Row was : " + prevRow + ", row is : " + oObj.getRow());
316             result &= prevRow + 1 == oObj.getRow() ;
317         } catch (SQLException e) {
318             log.println("Exception occured :") ;
319             e.printStackTrace(log) ;
320             result = false ;
321         }
322         tRes.tested("next()", result) ;
323     }
324 
325     /**
326     * Positions the cursor to the previous row. Current row
327     * number is retrieved before and after method call. <p>
328     * Has <b>OK</b> status if current row number decreases
329     * by 1 after method call.
330     */
_previous()331     public void _previous() {
332         boolean result = true ;
333 
334         try {
335             int prevRow = oObj.getRow() ;
336             oObj.previous() ;
337 
338             log.println("Row was : " + prevRow + ", row is : " + oObj.getRow());
339             result &= prevRow - 1 == oObj.getRow() ;
340         } catch (SQLException e) {
341             log.println("Exception occured :") ;
342             e.printStackTrace(log) ;
343             result = false ;
344         }
345         tRes.tested("previous()", result) ;
346     }
347 
348     /**
349     * Positions the cursor relatively by 2 rows forward.
350     * Current row number is retrieved before and after method call. <p>
351     * Has <b>OK</b> status if current row number increases
352     * by 2 after method call.
353     */
_relative()354     public void _relative() {
355         boolean result = true ;
356 
357         try {
358             oObj.first() ;
359             int prevRow = oObj.getRow() ;
360             oObj.relative(2) ;
361 
362             log.println("Row was : " + prevRow + ", row is : " + oObj.getRow());
363 
364             result &= prevRow + 2 == oObj.getRow() ;
365         } catch (SQLException e) {
366             log.println("Exception occured :") ;
367             e.printStackTrace(log) ;
368             result = false ;
369         }
370         tRes.tested("relative()", result) ;
371     }
372 
373     /**
374     * If component supports XRow and XRowUpdate then:
375     *   test saves current value of string field, updates string,
376     *   calls refreshRow() and checks that value of
377     *   string field was refetched from DB
378     * else: just calls method.<p>
379     * Has <b>OK</b> status if no exceptions were thrown and value after
380     * refreshRow() equals to saved value.
381     */
_refreshRow()382     public void _refreshRow() {
383         XRowUpdate xRowUpdate = (XRowUpdate)
384             UnoRuntime.queryInterface(XRowUpdate.class, oObj);
385         XRow xRow = (XRow)UnoRuntime.queryInterface(XRow.class, oObj);
386 
387         if (xRowUpdate == null || xRow == null) {
388             log.println("Test must be modified because XRow or XRowUpdate is't supported");
389             log.println("Only call method");
390             try {
391                 oObj.refreshRow() ;
392                 tRes.tested("refreshRow()", true) ;
393             } catch (SQLException e) {
394                 log.println("Exception occured :") ;
395                 e.printStackTrace(log) ;
396                 tRes.tested("refreshRow()", false) ;
397             }
398         } else {
399             log.println("Testing of refreshRow()...");
400             try {
401                 String oldValue = xRow.getString(util.DBTools.TST_STRING);
402                 log.println("Old value: " + oldValue);
403                 xRowUpdate.updateString(util.DBTools.TST_STRING,
404                     "Test method refreshRow");
405                 log.println("New value: "
406                     + xRow.getString(util.DBTools.TST_STRING));
407                 oObj.refreshRow();
408                 String valAfterRefresh =
409                     xRow.getString(util.DBTools.TST_STRING);
410                 log.println("Value after refresh: " + valAfterRefresh);
411                 tRes.tested("refreshRow()", valAfterRefresh.equals(oldValue));
412             } catch(SQLException e) {
413                 log.println("Exception occured :");
414                 e.printStackTrace(log);
415                 tRes.tested("refreshRow()", false);
416             }
417         }
418     }
419 
420     /**
421     * Just the method is called. <p>
422     * Has <b>OK</b> status if no exceptions were thrown.
423     */
_rowUpdated()424     public void _rowUpdated() {
425 
426         try {
427             boolean res = oObj.rowUpdated() ;
428             tRes.tested("rowUpdated()", true) ;
429         } catch (SQLException e) {
430             log.println("Exception occured :") ;
431             e.printStackTrace(log) ;
432             tRes.tested("rowUpdated()", false) ;
433         }
434     }
435 
436     /**
437     * Just the method is called. <p>
438     * Has <b>OK</b> status if no exceptions were thrown.
439     */
_rowInserted()440     public void _rowInserted() {
441         try {
442             boolean res = oObj.rowInserted() ;
443             tRes.tested("rowInserted()", true) ;
444         } catch (SQLException e) {
445             log.println("Exception occured :") ;
446             e.printStackTrace(log) ;
447             tRes.tested("rowInserted()", false) ;
448         }
449     }
450 
451     /**
452     * Just the method is called. <p>
453     * Has <b>OK</b> status if no exceptions were thrown.
454     */
_rowDeleted()455     public void _rowDeleted() {
456         try {
457             boolean res = oObj.rowDeleted() ;
458             tRes.tested("rowDeleted()", true) ;
459         } catch (SQLException e) {
460             log.println("Exception occured :") ;
461             e.printStackTrace(log) ;
462             tRes.tested("rowDeleted()", false) ;
463         }
464     }
465 
466     /**
467     * Just the method is called. <p>
468     * Has <b>OK</b> status if the statement returned isn't null or
469     * the relation exists that informs that statement absent (e.g. for
470     * MetaData row set).
471     */
_getStatement()472     public void _getStatement() {
473         try {
474             boolean hasStatement =
475                 tEnv.getObjRelation("XResultSet.hasStatement") != null ;
476             Object res = oObj.getStatement() ;
477             tRes.tested("getStatement()",
478                 (hasStatement && res != null) || !hasStatement) ;
479         } catch (SQLException e) {
480             log.println("Exception occured :") ;
481             e.printStackTrace(log) ;
482             tRes.tested("getStatement()", false) ;
483         }
484     }
485 
486     /**
487      * Moves the cursor to the first row to avoid affection to
488      * the following interfaces tests
489      */
after()490     public void after() {
491         log.println("Finally moving cursor to the first row ...");
492         try {
493             oObj.first();
494         } catch (SQLException e) {
495             log.println("Exception occured :") ;
496             e.printStackTrace(log) ;
497         }
498     }
499 
500 }  // finish class _XResultSet
501 
502