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 com.sun.star.beans.Property;
27 import com.sun.star.lang.XMultiServiceFactory;
28 import com.sun.star.sdbc.XResultSet;
29 import com.sun.star.ucb.Command;
30 import com.sun.star.ucb.NumberedSortingInfo;
31 import com.sun.star.ucb.OpenCommandArgument2;
32 import com.sun.star.ucb.OpenMode;
33 import com.sun.star.ucb.XCommandProcessor;
34 import com.sun.star.ucb.XContent;
35 import com.sun.star.ucb.XContentIdentifier;
36 import com.sun.star.ucb.XContentIdentifierFactory;
37 import com.sun.star.ucb.XContentProvider;
38 import com.sun.star.ucb.XDynamicResultSet;
39 import com.sun.star.ucb.XSortedDynamicResultSetFactory;
40 import com.sun.star.uno.UnoRuntime;
41 import lib.MultiMethodTest;
42 
43 import com.sun.star.uno.AnyConverter;
44 import com.sun.star.uno.Type;
45 
46 /**
47 * Testing <code>com.sun.star.ucb.XSortedDynamicResultSetFactory</code>
48 * interface methods :
49 * <ul>
50 *  <li><code> createSortedDynamicResultSet()</code></li>
51 * </ul> <p>
52 * The following predefined files needed to complete the test:
53 * <ul>
54 *  <li> <code>solibrary.jar</code> : is used to retrieve
55 *   content of its root directory.</li>
56 * <ul> <p>
57 * Test is <b> NOT </b> multithread compilant. <p>
58 * @see com.sun.star.ucb.XSortedDynamicResultSetFactory
59 */
60 public class _XSortedDynamicResultSetFactory extends MultiMethodTest {
61 
62     /**
63      * Conatins the tested object.
64      */
65     public XSortedDynamicResultSetFactory oObj;
66 
67     /**
68      * Creates sorted dynamic result set from result set. For this
69      * a dynamic result set is to be created. It is created by
70      * retrieving content list from JAR archive.
71      * Has <b>OK</b> status if numbers of rows are equal and they are
72      * greater then 0 (because JAR file contains at least one entry).
73      */
_createSortedDynamicResultSet()74     public void _createSortedDynamicResultSet() {
75         boolean result = true ;
76 
77         XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
78         XDynamicResultSet dynResSet = null ;
79         try {
80             Object oUCB = xMSF.createInstanceWithArguments
81                 ("com.sun.star.ucb.UniversalContentBroker",
82                 new Object[] {"Local", "Office"}) ;
83 
84             XContentIdentifierFactory ciFac = (XContentIdentifierFactory)
85                 UnoRuntime.queryInterface
86                     (XContentIdentifierFactory.class,oUCB) ;
87 
88             String url = util.utils.getFullTestURL("SwXTextEmbeddedObject.sxw") ;
89             String escUrl = "" ;
90 
91             // In base URL of a JAR file in content URL all directory
92             // separators ('/') must be replaced with escape symbol '%2F'.
93             int idx = url.indexOf("/") ;
94             int lastIdx = -1 ;
95             while (idx >= 0) {
96                 escUrl += url.substring(lastIdx + 1, idx) + "%2F" ;
97                 lastIdx = idx ;
98                 idx = url.indexOf("/", idx + 1) ;
99             }
100             escUrl += url.substring(lastIdx + 1) ;
101             String cntUrl = "vnd.sun.star.pkg://" + escUrl + "/" ;
102 
103             XContentIdentifier CI = ciFac.createContentIdentifier(cntUrl) ;
104 
105             XContentProvider cntProv = (XContentProvider)
106                 UnoRuntime.queryInterface(XContentProvider.class, oUCB) ;
107 
108             XContent cnt = cntProv.queryContent(CI) ;
109 
110             XCommandProcessor cmdProc = (XCommandProcessor)
111                 UnoRuntime.queryInterface(XCommandProcessor.class, cnt) ;
112 
113             Property prop = new Property() ;
114             prop.Name = "Title" ;
115 
116             Command cmd = new Command("open", -1, new OpenCommandArgument2
117                 (OpenMode.ALL, 10000, null, new Property[] {prop},
118                  new NumberedSortingInfo[0])) ;
119 
120             dynResSet = (XDynamicResultSet) AnyConverter.toObject(
121                 new Type(XDynamicResultSet.class),cmdProc.execute(cmd, 0, null));
122         } catch (com.sun.star.uno.Exception e) {
123             e.printStackTrace(log);
124         }
125 
126         XDynamicResultSet sortedSet = oObj.createSortedDynamicResultSet
127             (dynResSet, new NumberedSortingInfo[0], null) ;
128 
129         int rowCount = -1 ;
130         if (sortedSet != null) {
131             XResultSet set = null ;
132             try {
133                 set = sortedSet.getStaticResultSet() ;
134             } catch (com.sun.star.ucb.ListenerAlreadySetException e) {
135                 e.printStackTrace(log);
136             }
137 
138             try {
139                 set.last() ;
140                 rowCount = set.getRow();
141                 log.println("Number of rows in result set: " + rowCount);
142             } catch (com.sun.star.sdbc.SQLException e) {
143                 log.println("Exception occurred while accessing "+
144                     "sorted result set :");
145                 e.printStackTrace(log);
146             }
147         } else {
148             log.println("Null returned !!!");
149             result &= false ;
150         }
151 
152         result &= rowCount > 1 ;
153 
154         tRes.tested("createSortedDynamicResultSet()", result) ;
155     }
156 
157 
158 }
159