1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package mod._cached;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.beans.Property;
27cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
28cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet;
29cdf0e10cSrcweir import com.sun.star.ucb.Command;
30cdf0e10cSrcweir import com.sun.star.ucb.NumberedSortingInfo;
31cdf0e10cSrcweir import com.sun.star.ucb.OpenCommandArgument2;
32cdf0e10cSrcweir import com.sun.star.ucb.OpenMode;
33cdf0e10cSrcweir import com.sun.star.ucb.XCachedContentResultSetStubFactory;
34cdf0e10cSrcweir import com.sun.star.ucb.XCommandProcessor;
35cdf0e10cSrcweir import com.sun.star.ucb.XContent;
36cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifier;
37cdf0e10cSrcweir import com.sun.star.ucb.XContentIdentifierFactory;
38cdf0e10cSrcweir import com.sun.star.ucb.XContentProvider;
39cdf0e10cSrcweir import com.sun.star.ucb.XDynamicResultSet;
40cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
41cdf0e10cSrcweir import com.sun.star.uno.XInterface;
42cdf0e10cSrcweir import com.sun.star.uno.Type;
43cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
44cdf0e10cSrcweir import java.io.PrintWriter;
45cdf0e10cSrcweir import lib.StatusException;
46cdf0e10cSrcweir import lib.TestCase;
47cdf0e10cSrcweir import lib.TestEnvironment;
48cdf0e10cSrcweir import lib.TestParameters;
49cdf0e10cSrcweir 
50cdf0e10cSrcweir /**
51cdf0e10cSrcweir * Test for object which is represented by service
52cdf0e10cSrcweir * <code>com.sun.star.ucb.CachedContentResultSetFactory</code>. <p>
53cdf0e10cSrcweir * Object implements the following interfaces :
54cdf0e10cSrcweir * <ul>
55cdf0e10cSrcweir *  <li> <code>com::sun::star::ucb::XCachedContentResultSetFactory</code></li>
56cdf0e10cSrcweir * </ul> <p>
57cdf0e10cSrcweir * This object test <b> is NOT </b> designed to be run in several
58cdf0e10cSrcweir * threads concurently.
59cdf0e10cSrcweir * @see com.sun.star.ucb.XCachedContentResultSetFactory
60cdf0e10cSrcweir * @see com.sun.star.ucb.CachedContentResultSetFactory
61cdf0e10cSrcweir * @see ifc.ucb._XCachedContentResultSetFactory
62cdf0e10cSrcweir */
63cdf0e10cSrcweir public class CachedContentResultSetFactory extends TestCase {
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /**
66cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
67cdf0e10cSrcweir     * Creates an instance of the service
68cdf0e10cSrcweir     * <code>com.sun.star.ucb.CachedContentResultSetFactory</code>. <p>
69cdf0e10cSrcweir     *     Object relations created :
70cdf0e10cSrcweir     * <ul>
71cdf0e10cSrcweir     *  <li> <code>'CachedContentResultSetStub'</code> for
72cdf0e10cSrcweir     *      {@link ifc.XCachedContentResultSetFactory} : the destination
73cdf0e10cSrcweir     *   interface requires as its parameter an instance of
74cdf0e10cSrcweir     *   <code>CachedContentResultSetStub</code> service. It is created
75*242829adSPedro Giffuni     *   using <code>UniversalContentBroker</code> and querying it for
76cdf0e10cSrcweir     *   <code>PackageContent</code> which represents JAR file mentioned
77cdf0e10cSrcweir     *   above. Then the dynamic list of file contents (entries) is retrieved,
78cdf0e10cSrcweir     *   and a static list is created from it. Using
79cdf0e10cSrcweir     *   <code>CachedContentResultSetStubFactory</code> service a stub of
80cdf0e10cSrcweir     *   Cached result set is created which is relation required.
81cdf0e10cSrcweir     *  </li>
82cdf0e10cSrcweir     * </ul>
83cdf0e10cSrcweir     */
createTestEnvironment( TestParameters Param, PrintWriter log )84cdf0e10cSrcweir     public TestEnvironment createTestEnvironment( TestParameters Param,
85cdf0e10cSrcweir                                                   PrintWriter log )
86cdf0e10cSrcweir                                                     throws StatusException {
87cdf0e10cSrcweir         XInterface oObj = null;
88cdf0e10cSrcweir         Object oInterface = null;
89cdf0e10cSrcweir         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
90cdf0e10cSrcweir         Object relationContainer = null ;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         try {
94cdf0e10cSrcweir             oInterface = xMSF.createInstance
95cdf0e10cSrcweir                 ( "com.sun.star.ucb.CachedContentResultSetFactory" );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             // adding one child container
98cdf0e10cSrcweir         }
99cdf0e10cSrcweir         catch( com.sun.star.uno.Exception e ) {
100cdf0e10cSrcweir             log.println("Can't create an object." );
101cdf0e10cSrcweir             throw new StatusException( "Can't create an object", e );
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         oObj = (XInterface) oInterface;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         // creating relation for XCachedContentResultSetFactory
109cdf0e10cSrcweir         XResultSet resSetStub = null ;
110cdf0e10cSrcweir         try {
111cdf0e10cSrcweir             Object oUCB = xMSF.createInstanceWithArguments
112cdf0e10cSrcweir                 ("com.sun.star.ucb.UniversalContentBroker",
113cdf0e10cSrcweir                 new Object[] {"Local", "Office"}) ;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir             XContentIdentifierFactory ciFac = (XContentIdentifierFactory)
116cdf0e10cSrcweir                 UnoRuntime.queryInterface(XContentIdentifierFactory.class, oUCB) ;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir             String url = util.utils.getFullTestURL("SwXTextEmbeddedObject.sxw") ;
119cdf0e10cSrcweir             String escUrl = "" ;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir             // In base URL of a JAR file in content URL all directory
122cdf0e10cSrcweir             // separators ('/') must be replaced with escape symbol '%2F'.
123cdf0e10cSrcweir             int idx = url.indexOf("/") ;
124cdf0e10cSrcweir             int lastIdx = -1 ;
125cdf0e10cSrcweir             while (idx >= 0) {
126cdf0e10cSrcweir                 escUrl += url.substring(lastIdx + 1, idx) + "%2F" ;
127cdf0e10cSrcweir                 lastIdx = idx ;
128cdf0e10cSrcweir                 idx = url.indexOf("/", idx + 1) ;
129cdf0e10cSrcweir             }
130cdf0e10cSrcweir             escUrl += url.substring(lastIdx + 1) ;
131cdf0e10cSrcweir             String cntUrl = "vnd.sun.star.pkg://" + escUrl + "/" ;
132cdf0e10cSrcweir 
133cdf0e10cSrcweir             XContentIdentifier CI = ciFac.createContentIdentifier(cntUrl) ;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir             XContentProvider cntProv = (XContentProvider)
136cdf0e10cSrcweir                 UnoRuntime.queryInterface(XContentProvider.class, oUCB) ;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir             XContent cnt = cntProv.queryContent(CI) ;
139cdf0e10cSrcweir 
140cdf0e10cSrcweir             XCommandProcessor cmdProc = (XCommandProcessor)
141cdf0e10cSrcweir                 UnoRuntime.queryInterface(XCommandProcessor.class, cnt) ;
142cdf0e10cSrcweir 
143cdf0e10cSrcweir             Property prop = new Property() ;
144cdf0e10cSrcweir             prop.Name = "Title" ;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir             Command cmd = new Command("open", -1, new OpenCommandArgument2
147cdf0e10cSrcweir                 (OpenMode.ALL, 10000, null, new Property[] {prop},
148cdf0e10cSrcweir                  new NumberedSortingInfo[0])) ;
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             XDynamicResultSet dynResSet = null;
151cdf0e10cSrcweir             try {
152cdf0e10cSrcweir                 dynResSet = (XDynamicResultSet)
153cdf0e10cSrcweir                     AnyConverter.toObject(new Type(XDynamicResultSet.class),
154cdf0e10cSrcweir                                         cmdProc.execute(cmd, 0, null));
155cdf0e10cSrcweir             } catch (com.sun.star.lang.IllegalArgumentException iae) {
156cdf0e10cSrcweir                 throw new StatusException("Couldn't convert Any ",iae);
157cdf0e10cSrcweir             }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir             XResultSet resSet = dynResSet.getStaticResultSet() ;
160cdf0e10cSrcweir 
161cdf0e10cSrcweir             Object oStubFactory = xMSF.createInstance
162cdf0e10cSrcweir                 ("com.sun.star.ucb.CachedContentResultSetStubFactory") ;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir             XCachedContentResultSetStubFactory xStubFactory =
165cdf0e10cSrcweir                 (XCachedContentResultSetStubFactory) UnoRuntime.queryInterface
166cdf0e10cSrcweir                 (XCachedContentResultSetStubFactory.class, oStubFactory) ;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir             resSetStub = xStubFactory.createCachedContentResultSetStub(resSet) ;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
171cdf0e10cSrcweir             log.println("Can't create relation." );
172cdf0e10cSrcweir             e.printStackTrace(log) ;
173cdf0e10cSrcweir             throw new StatusException( "Can't create relation", e );
174cdf0e10cSrcweir         }
175cdf0e10cSrcweir 
176cdf0e10cSrcweir         tEnv.addObjRelation("CachedContentResultSetStub", resSetStub) ;
177cdf0e10cSrcweir 
178cdf0e10cSrcweir         return tEnv;
179cdf0e10cSrcweir     } // finish method getTestEnvironment
180cdf0e10cSrcweir 
181cdf0e10cSrcweir }
182cdf0e10cSrcweir 
183