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.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.CachedContentResultSetStubFactory</code>. <p>
52 * Object implements the following interfaces :
53 * <ul>
54 *  <li> <code>com::sun::star::ucb::XCachedContentResultSetStubFactory</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.XCachedContentResultSetStubFactory
59 * @see com.sun.star.ucb.CachedContentResultSetStubFactory
60 * @see ifc.ucb._XCachedContentResultSetStubFactory
61 */
62 public class CachedContentResultSetStubFactory 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.CachedContentResultSetStubFactory</code>. <p>
68     *     Object relations created :
69     * <ul>
70     *  <li> <code>'ContentResultSet'</code> for
71     *      {@link ifc.ucb._XCachedContentResultSetStubFactory} : the destination
72     *   interface requires as its parameter an instance of
73     *   <code>ContentResultSet</code> service. It is created
74     *   using <code>UniversalContentBroker</code> and querying it for
75     *   <code>PackageContent</code> which represents JAR file mentioned
76     *   above. Then the dynamic list of file contents (entries) is retrieved,
77     *   and a static list is created from it. It represents
78     *   <code>ContentResultSet</code> service instance 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.CachedContentResultSetStubFactory" );
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 XCachedContentResultSetStubFactory
107         XResultSet resSet = 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             log.println("Getting Content of '" + cntUrl + "'") ;
131 
132             XContentIdentifier CI = ciFac.createContentIdentifier(cntUrl) ;
133 
134             XContentProvider cntProv = (XContentProvider)
135                 UnoRuntime.queryInterface(XContentProvider.class, oUCB) ;
136 
137             XContent cnt = cntProv.queryContent(CI) ;
138 
139             XCommandProcessor cmdProc = (XCommandProcessor)
140                 UnoRuntime.queryInterface(XCommandProcessor.class, cnt) ;
141 
142             Property prop = new Property() ;
143             prop.Name = "Title" ;
144 
145             Command cmd = new Command("open", -1, new OpenCommandArgument2
146                 (OpenMode.ALL, 10000, null, new Property[] {prop},
147                  new NumberedSortingInfo[0])) ;
148 
149             XDynamicResultSet dynResSet = null;
150             try {
151                 dynResSet = (XDynamicResultSet)
152                     AnyConverter.toObject(new Type(XDynamicResultSet.class),
153                                         cmdProc.execute(cmd, 0, null));
154             } catch (com.sun.star.lang.IllegalArgumentException iae) {
155                 throw new StatusException("Couldn't convert Any ",iae);
156             }
157 
158             resSet = dynResSet.getStaticResultSet() ;
159 
160         } catch (com.sun.star.uno.Exception e) {
161             log.println("Can't create relation." );
162             e.printStackTrace(log) ;
163             throw new StatusException( "Can't create relation", e );
164         }
165 
166         tEnv.addObjRelation("ContentResultSet", resSet) ;
167 
168         return tEnv;
169     } // finish method getTestEnvironment
170 
171 }
172 
173