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.ucb;
29 
30 import lib.MultiMethodTest;
31 import lib.Status;
32 import lib.StatusException;
33 
34 import com.sun.star.lang.XMultiServiceFactory;
35 import com.sun.star.sdbc.XResultSet;
36 import com.sun.star.ucb.XCachedDynamicResultSetFactory;
37 import com.sun.star.ucb.XCachedDynamicResultSetStubFactory;
38 import com.sun.star.ucb.XDynamicResultSet;
39 import com.sun.star.uno.UnoRuntime;
40 
41 /**
42 * Testing <code>com.sun.star.ucb.XCachedDynamicResultSetStubFactory</code>
43 * interface methods :
44 * <ul>
45 *  <li><code> createCachedDynamicResultSetStub()</code></li>
46 *  <li><code> connectToCache()</code></li>
47 * </ul> <p>
48 * This test needs the following object relations :
49 * <ul>
50 *  <li> <code>'DynamicResultSet'</code> (of type
51 *   <code>com.sun.star.sdbc.XDynamicResultSet</code>):
52 *   this must be an imlementation of <code>
53 *   com.sun.star.ucb.DynamicResultSet</code> service.</li>
54 * <ul> <p>
55 * Test is <b> NOT </b> multithread compilant. <p>
56 * @see com.sun.star.ucb.XCachedDynamicResultSetStubFactory
57 */
58 public class _XCachedDynamicResultSetStubFactory extends MultiMethodTest {
59 
60     /**
61      * Conatins the tested object.
62      */
63     public XCachedDynamicResultSetStubFactory oObj;
64     private XDynamicResultSet resSet = null ;
65     private XDynamicResultSet resSetStub = null ;
66 
67     /**
68     * Retrieves object relation.
69     * @throws StatusException If relation not found.
70     */
71     public void before() {
72         resSet = (XDynamicResultSet) tEnv.getObjRelation("DynamicResultSet") ;
73         if (resSet == null) {
74             log.println("!!! Relation not found !!!") ;
75             throw new StatusException(Status.failed("!!! Relation not found !!!")) ;
76         }
77     }
78 
79     /**
80     * Creates result set stub from result set. After that number
81     * of rows from result set stub created and its source set are retrieved
82     * using their static representations and compared. <p>
83     * Has <b>OK</b> status if numbers of rows are equal and they are
84     * greater then 0 (because JAR file contains at least one entry).
85     */
86     public void _createCachedDynamicResultSetStub() {
87         boolean result = true ;
88 
89         resSetStub = oObj.createCachedDynamicResultSetStub(resSet) ;
90 
91         if (resSetStub == null) {
92             log.println("!!! Method returned null !!!") ;
93             result = false ;
94         } else {
95             try {
96                 XResultSet resSetS = resSet.getStaticResultSet() ;
97                 XResultSet resSetStubS = resSetStub.getStaticResultSet() ;
98 
99                 resSetStubS.last() ;
100                 int stubRowNum = resSetStubS.getRow() ;
101 
102                 resSetS.last() ;
103                 int setRowNum = resSetS.getRow() ;
104 
105                 result = stubRowNum == setRowNum && setRowNum > 0 ;
106 
107                 log.println("Number of rows : stub=" + stubRowNum +
108                     " set=" + setRowNum) ;
109             } catch (com.sun.star.sdbc.SQLException e) {
110                 log.println("!!! Something wrong with result sets :") ;
111                 e.printStackTrace(log) ;
112                 result = false ;
113             } catch (com.sun.star.ucb.ListenerAlreadySetException e) {
114                 log.println("!!! Can't get static result sets :") ;
115                 e.printStackTrace(log) ;
116                 result = false ;
117             }
118         }
119 
120         tRes.tested("createCachedDynamicResultSetStub()", result) ;
121     }
122 
123     /**
124     * Creates an instance of <code>CachedDynamicResultSet</code> service
125     * which is not connected to any stub. Then tries to connect it to
126     * <code>DynaminResultSet</code> created and passed as relation.
127     * Connection is checked by retrieving and comparing of row numbers
128     * of connected set and its source set. <p>
129     * Has <b>OK</b> status if row numbers are equal and they are
130     * greater then 0 (because JAR file contains at least one entry).
131     */
132     public void _connectToCache() {
133         boolean result = true ;
134 
135         XCachedDynamicResultSetFactory setFac = null ;
136 
137         try {
138             Object fac = ((XMultiServiceFactory)tParam.getMSF()).createInstance
139                 ("com.sun.star.ucb.CachedDynamicResultSetFactory") ;
140 
141             setFac = (XCachedDynamicResultSetFactory) UnoRuntime.queryInterface
142                 (XCachedDynamicResultSetFactory.class, fac) ;
143         } catch (com.sun.star.uno.Exception e) {
144             log.println("Cant instantiate a service") ;
145             e.printStackTrace(log) ;
146             result = false ;
147         }
148 
149         XDynamicResultSet rmtSet = setFac.createCachedDynamicResultSet(null, null) ;
150 
151         try {
152             oObj.connectToCache(resSet, rmtSet, null, null) ;
153         } catch (com.sun.star.ucb.ListenerAlreadySetException e) {
154             log.println("!!! Unexpected exception :" + e) ;
155             result = false ;
156         } catch (com.sun.star.ucb.AlreadyInitializedException e) {
157             log.println("!!! Unexpected exception :" + e) ;
158             result = false ;
159         }
160 
161         if (result) {
162             // checking connection to the source
163             try {
164                 XResultSet statRmtSet = rmtSet.getStaticResultSet() ;
165                 XResultSet statResSet = resSet.getStaticResultSet() ;
166 
167                 statRmtSet.last() ;
168                 int rmtRowNum = statRmtSet.getRow() ;
169 
170                 statResSet.last() ;
171                 int resRowNum = statResSet.getRow() ;
172 
173                 result = rmtRowNum == resRowNum && resRowNum > 0 ;
174 
175                 log.println("Number of rows : destination=" + rmtRowNum +
176                     " source=" + resRowNum) ;
177             } catch (com.sun.star.sdbc.SQLException e) {
178                 log.println("!!! Something wrong with result sets :") ;
179                 e.printStackTrace(log) ;
180                 result = false ;
181             } catch (com.sun.star.ucb.ListenerAlreadySetException e) {
182                 log.println("!!! Something wrong with result sets :") ;
183                 e.printStackTrace(log) ;
184                 result = false ;
185             }
186         }
187 
188         tRes.tested("connectToCache()", result) ;
189     }
190 
191 }
192 
193