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 mod._cached;
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.XCachedContentResultSetStubFactory;
34 import com.sun.star.ucb.XCommandProcessor;
35 import com.sun.star.ucb.XContent;
36 import com.sun.star.ucb.XContentIdentifier;
37 import com.sun.star.ucb.XContentIdentifierFactory;
38 import com.sun.star.ucb.XContentProvider;
39 import com.sun.star.ucb.XDynamicResultSet;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.uno.XInterface;
42 import com.sun.star.uno.Type;
43 import com.sun.star.uno.AnyConverter;
44 import java.io.PrintWriter;
45 import lib.StatusException;
46 import lib.TestCase;
47 import lib.TestEnvironment;
48 import lib.TestParameters;
49 
50 /**
51 * Test for object which is represented by service
52 * <code>com.sun.star.ucb.CachedContentResultSetFactory</code>. <p>
53 * Object implements the following interfaces :
54 * <ul>
55 *  <li> <code>com::sun::star::ucb::XCachedContentResultSetFactory</code></li>
56 * </ul> <p>
57 * This object test <b> is NOT </b> designed to be run in several
58 * threads concurently.
59 * @see com.sun.star.ucb.XCachedContentResultSetFactory
60 * @see com.sun.star.ucb.CachedContentResultSetFactory
61 * @see ifc.ucb._XCachedContentResultSetFactory
62 */
63 public class CachedContentResultSetFactory extends TestCase {
64 
65     /**
66     * Creating a Testenvironment for the interfaces to be tested.
67     * Creates an instance of the service
68     * <code>com.sun.star.ucb.CachedContentResultSetFactory</code>. <p>
69     *     Object relations created :
70     * <ul>
71     *  <li> <code>'CachedContentResultSetStub'</code> for
72     *      {@link ifc.XCachedContentResultSetFactory} : the destination
73     *   interface requires as its parameter an instance of
74     *   <code>CachedContentResultSetStub</code> service. It is created
75     *   using <code>UniversalContentBroker</code> and querying it for
76     *   <code>PackageContent</code> which represents JAR file mentioned
77     *   above. Then the dynamic list of file contents (entries) is retrieved,
78     *   and a static list is created from it. Using
79     *   <code>CachedContentResultSetStubFactory</code> service a stub of
80     *   Cached result set is created which is relation required.
81     *  </li>
82     * </ul>
83     */
createTestEnvironment( TestParameters Param, PrintWriter log )84     public TestEnvironment createTestEnvironment( TestParameters Param,
85                                                   PrintWriter log )
86                                                     throws StatusException {
87         XInterface oObj = null;
88         Object oInterface = null;
89         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
90         Object relationContainer = null ;
91 
92 
93         try {
94             oInterface = xMSF.createInstance
95                 ( "com.sun.star.ucb.CachedContentResultSetFactory" );
96 
97             // adding one child container
98         }
99         catch( com.sun.star.uno.Exception e ) {
100             log.println("Can't create an object." );
101             throw new StatusException( "Can't create an object", e );
102         }
103 
104         oObj = (XInterface) oInterface;
105 
106         TestEnvironment tEnv = new TestEnvironment( oObj );
107 
108         // creating relation for XCachedContentResultSetFactory
109         XResultSet resSetStub = null ;
110         try {
111             Object oUCB = xMSF.createInstanceWithArguments
112                 ("com.sun.star.ucb.UniversalContentBroker",
113                 new Object[] {"Local", "Office"}) ;
114 
115             XContentIdentifierFactory ciFac = (XContentIdentifierFactory)
116                 UnoRuntime.queryInterface(XContentIdentifierFactory.class, oUCB) ;
117 
118             String url = util.utils.getFullTestURL("SwXTextEmbeddedObject.sxw") ;
119             String escUrl = "" ;
120 
121             // In base URL of a JAR file in content URL all directory
122             // separators ('/') must be replaced with escape symbol '%2F'.
123             int idx = url.indexOf("/") ;
124             int lastIdx = -1 ;
125             while (idx >= 0) {
126                 escUrl += url.substring(lastIdx + 1, idx) + "%2F" ;
127                 lastIdx = idx ;
128                 idx = url.indexOf("/", idx + 1) ;
129             }
130             escUrl += url.substring(lastIdx + 1) ;
131             String cntUrl = "vnd.sun.star.pkg://" + escUrl + "/" ;
132 
133             XContentIdentifier CI = ciFac.createContentIdentifier(cntUrl) ;
134 
135             XContentProvider cntProv = (XContentProvider)
136                 UnoRuntime.queryInterface(XContentProvider.class, oUCB) ;
137 
138             XContent cnt = cntProv.queryContent(CI) ;
139 
140             XCommandProcessor cmdProc = (XCommandProcessor)
141                 UnoRuntime.queryInterface(XCommandProcessor.class, cnt) ;
142 
143             Property prop = new Property() ;
144             prop.Name = "Title" ;
145 
146             Command cmd = new Command("open", -1, new OpenCommandArgument2
147                 (OpenMode.ALL, 10000, null, new Property[] {prop},
148                  new NumberedSortingInfo[0])) ;
149 
150             XDynamicResultSet dynResSet = null;
151             try {
152                 dynResSet = (XDynamicResultSet)
153                     AnyConverter.toObject(new Type(XDynamicResultSet.class),
154                                         cmdProc.execute(cmd, 0, null));
155             } catch (com.sun.star.lang.IllegalArgumentException iae) {
156                 throw new StatusException("Couldn't convert Any ",iae);
157             }
158 
159             XResultSet resSet = dynResSet.getStaticResultSet() ;
160 
161             Object oStubFactory = xMSF.createInstance
162                 ("com.sun.star.ucb.CachedContentResultSetStubFactory") ;
163 
164             XCachedContentResultSetStubFactory xStubFactory =
165                 (XCachedContentResultSetStubFactory) UnoRuntime.queryInterface
166                 (XCachedContentResultSetStubFactory.class, oStubFactory) ;
167 
168             resSetStub = xStubFactory.createCachedContentResultSetStub(resSet) ;
169 
170         } catch (com.sun.star.uno.Exception e) {
171             log.println("Can't create relation." );
172             e.printStackTrace(log) ;
173             throw new StatusException( "Can't create relation", e );
174         }
175 
176         tEnv.addObjRelation("CachedContentResultSetStub", resSetStub) ;
177 
178         return tEnv;
179     } // finish method getTestEnvironment
180 
181 }
182 
183