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