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