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.sdbcx;
29 
30 import lib.MultiMethodTest;
31 
32 import com.sun.star.sdbc.SQLException;
33 import com.sun.star.sdbcx.XRowLocate;
34 
35 /**
36 * Testing <code>com.sun.star.sdbcx.XRowLocate</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> getBookmark()</code></li>
40 *  <li><code> moveToBookmark()</code></li>
41 *  <li><code> moveRelativeToBookmark()</code></li>
42 *  <li><code> compareBookmarks()</code></li>
43 *  <li><code> hasOrderedBookmarks()</code></li>
44 *  <li><code> hashBookmark()</code></li>
45 * </ul> <p>
46 * Object relations needed :
47 * <ul>
48 * <li> <code>XRowLocate.HasOrderedBookmarks</code> <b>(optional)</b>:
49 *  <code>Boolean</code> type. If information about bookmark ordering
50 *  of object tested exists it placed in this relation.</li>
51 * </ul> <p>
52 * @see com.sun.star.sdbcx.XRowLocate
53 */
54 public class _XRowLocate extends MultiMethodTest {
55 
56     // oObj filled by MultiMethodTest
57     public XRowLocate oObj = null ;
58 
59     private Object bookmark1 = null, bookmark2 = null ;
60 
61     /**
62     * Gets the bookmark of the current cursor position and
63     * stores it for other methods use.
64     * Has OK status if method returns not null
65     * <code>XNameAccess</code> object, FAILED otherwise.
66     */
67     public void _getBookmark() {
68 
69         try {
70             bookmark1 = oObj.getBookmark() ;
71         } catch (SQLException e) {
72             log.println("Exception occured :" + e) ;
73         }
74 
75         tRes.tested("getBookmark()", bookmark1 != null) ;
76     }
77 
78     /**
79     * Moves to bookmark previously created by method <code>_getBookmark</code>
80     * then creates new bookmark and compare it to the first one. The
81     * row difference must be equal to 0. <p>
82     * Method tests to be completed successfully before :
83     * <ul>
84     * <li> <code>getBookmark()</code> : to have a bookmark to move to.</li>
85     * </ul>
86     * Method tests to be executed before :
87     * <ul>
88     * <li> <code>moveRelativeToBookmark()</code> : to move the cursor from
89     *    the initial position, to check if it returns back.</li>
90     * </ul> <p>
91     * Has OK status difference between positions of bookmarks (where
92     * cursor was moved to and created new one) equals to 0 and no
93     * exceptions occured, FAILED otherwise.
94     */
95     public void _moveToBookmark() {
96         requiredMethod("getBookmark()") ;
97         executeMethod("moveRelativeToBookmark()") ;
98 
99         int comparison = -1 ;
100         Object tmpBookmark = null ;
101         try {
102             synchronized(oObj) {
103                 oObj.moveToBookmark(bookmark1) ;
104                 tmpBookmark = oObj.getBookmark() ;
105             }
106             comparison = oObj.compareBookmarks(bookmark1, tmpBookmark) ;
107         } catch (SQLException e) {
108             log.println("Exception occured :" + e) ;
109         }
110 
111         tRes.tested("moveToBookmark()", comparison == 0) ;
112     }
113 
114     /**
115     * Moves the cursor to the position with offset 2 from the
116     * bookmark created by <code>_getBookmark()</code> method and
117     * then creates a bookmark of new position. <p>
118     * Method tests to be completed successfully before :
119     * <ul>
120     * <li> <code>getBookmark()</code> : to have a bookmark to move to.</li>
121     * </ul> <p>
122     * Has OK status if no exceptions occured while method call.
123     */
124     public void _moveRelativeToBookmark() {
125         requiredMethod("getBookmark()") ;
126 
127         boolean result = true ;
128         try {
129             synchronized (oObj) {
130                 oObj.moveRelativeToBookmark(bookmark1, 1) ;
131                 bookmark2 = oObj.getBookmark() ;
132             }
133         } catch (SQLException e) {
134             log.println("Exception occured :" + e) ;
135             result = false ;
136         }
137 
138         tRes.tested("moveRelativeToBookmark()", result) ;
139     }
140 
141     /**
142     * Compares bookmarks created in <code>getBookmark()</code> and
143     * <code>moveRelativeToBokkmark()</code> methods.
144     * Method tests to be completed successfully before :
145     * <ul>
146     * <li> <code>getBookmark()</code> : to have first bookmark to compare.</li>
147     * <li> <code>moveRelativeToBookmark()</code> : to have second
148     *   bookmark to compare.</li>
149     * </ul> <p>
150     * Has OK status if difference in bookmark positions equals to 2.
151     */
152     public void _compareBookmarks() {
153         requiredMethod("getBookmark()") ;
154         requiredMethod("moveRelativeToBookmark()") ;
155 
156         int comparison = 0 ;
157         int comparison1 = 0 ;
158         try {
159             comparison = oObj.compareBookmarks(bookmark1, bookmark2) ;
160             comparison1 = oObj.compareBookmarks(bookmark1, bookmark1) ;
161         } catch (SQLException e) {
162             log.println("Exception occured :" + e) ;
163         }
164 
165         if (comparison != -1) {
166             log.println("! Wrong compare number :" + comparison) ;
167         }
168 
169         tRes.tested("compareBookmarks()", comparison == -1 && comparison1 == 0) ;
170     }
171 
172     /**
173     * Calls method. If relation is not found, returned result is not
174     * checked. <p>
175     * Has OK status if method returned right value (in case if relation
176     * and value to compare to exist), or just successfully returned
177     * (in case of no relation).
178     */
179     public void _hasOrderedBookmarks() {
180         boolean result = true ;
181         boolean res = true ;
182 
183         // Optional relation
184         Boolean has = (Boolean) tEnv.getObjRelation
185             ("XRowLocate.HasOrderedBookmarks") ;
186 
187         try {
188             res = oObj.hasOrderedBookmarks() ;
189         } catch (SQLException e) {
190             log.println("Exception occured :" + e) ;
191             result = false ;
192         }
193 
194         if (has == null) {
195             log.println("Optional relation not found - result not checked") ;
196         } else {
197             result &= res == has.booleanValue() ;
198         }
199 
200         tRes.tested("hasOrderedBookmarks()", result) ;
201     }
202 
203     /**
204     * Gets hash values of two previously created bookmarks of different
205     * cursor positions and compares them. <p>
206     * Method tests to be completed successfully before :
207     * <ul>
208     * <li> <code>getBookmark()</code> : to have first bookmark.</li>
209     * <li> <code>moveRelativeToBookmark()</code> : to have second
210     *   bookmark.</li>
211     * </ul> <p>
212     * Has OK status if hash values of two bookmarks are different.
213     */
214     public void _hashBookmark() {
215         requiredMethod("getBookmark()") ;
216         requiredMethod("moveRelativeToBookmark()") ;
217 
218         boolean result = true ;
219         try {
220             int hash1 = oObj.hashBookmark(bookmark1) ;
221             int hash2 = oObj.hashBookmark(bookmark2) ;
222             log.println("1st hash = " + hash1 + ", 2nd = " + hash2) ;
223 
224             result = hash1 != hash2 ;
225         } catch (SQLException e) {
226             log.println("Exception occured :" + e) ;
227             result = false ;
228         }
229 
230         tRes.tested("hashBookmark()", result) ;
231     }
232 
233 }  // finish class _XRowLocate
234 
235