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