1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._cached;
29 
30 import com.sun.star.beans.Property;
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.sdbc.XResultSet;
33 import com.sun.star.ucb.Command;
34 import com.sun.star.ucb.NumberedSortingInfo;
35 import com.sun.star.ucb.OpenCommandArgument2;
36 import com.sun.star.ucb.OpenMode;
37 import com.sun.star.ucb.XCachedContentResultSetStubFactory;
38 import com.sun.star.ucb.XCommandProcessor;
39 import com.sun.star.ucb.XContent;
40 import com.sun.star.ucb.XContentIdentifier;
41 import com.sun.star.ucb.XContentIdentifierFactory;
42 import com.sun.star.ucb.XContentProvider;
43 import com.sun.star.ucb.XDynamicResultSet;
44 import com.sun.star.uno.UnoRuntime;
45 import com.sun.star.uno.XInterface;
46 import com.sun.star.uno.Type;
47 import com.sun.star.uno.AnyConverter;
48 import java.io.PrintWriter;
49 import lib.StatusException;
50 import lib.TestCase;
51 import lib.TestEnvironment;
52 import lib.TestParameters;
53 
54 /**
55 * Test for object which is represented by service
56 * <code>com.sun.star.ucb.CachedContentResultSetFactory</code>. <p>
57 * Object implements the following interfaces :
58 * <ul>
59 *  <li> <code>com::sun::star::ucb::XCachedContentResultSetFactory</code></li>
60 * </ul> <p>
61 * This object test <b> is NOT </b> designed to be run in several
62 * threads concurently.
63 * @see com.sun.star.ucb.XCachedContentResultSetFactory
64 * @see com.sun.star.ucb.CachedContentResultSetFactory
65 * @see ifc.ucb._XCachedContentResultSetFactory
66 */
67 public class CachedContentResultSetFactory extends TestCase {
68 
69     /**
70     * Creating a Testenvironment for the interfaces to be tested.
71     * Creates an instance of the service
72     * <code>com.sun.star.ucb.CachedContentResultSetFactory</code>. <p>
73     *     Object relations created :
74     * <ul>
75     *  <li> <code>'CachedContentResultSetStub'</code> for
76     *      {@link ifc.XCachedContentResultSetFactory} : the destination
77     *   interface requires as its parameter an instance of
78     *   <code>CachedContentResultSetStub</code> service. It is created
79     *   using <code>UniversalContentBroker</code> and queriing it for
80     *   <code>PackageContent</code> which represents JAR file mentioned
81     *   above. Then the dynamic list of file contents (entries) is retrieved,
82     *   and a static list is created from it. Using
83     *   <code>CachedContentResultSetStubFactory</code> service a stub of
84     *   Cached result set is created which is relation required.
85     *  </li>
86     * </ul>
87     */
88     public TestEnvironment createTestEnvironment( TestParameters Param,
89                                                   PrintWriter log )
90                                                     throws StatusException {
91         XInterface oObj = null;
92         Object oInterface = null;
93         XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
94         Object relationContainer = null ;
95 
96 
97         try {
98             oInterface = xMSF.createInstance
99                 ( "com.sun.star.ucb.CachedContentResultSetFactory" );
100 
101             // adding one child container
102         }
103         catch( com.sun.star.uno.Exception e ) {
104             log.println("Can't create an object." );
105             throw new StatusException( "Can't create an object", e );
106         }
107 
108         oObj = (XInterface) oInterface;
109 
110         TestEnvironment tEnv = new TestEnvironment( oObj );
111 
112         // creating relation for XCachedContentResultSetFactory
113         XResultSet resSetStub = null ;
114         try {
115             Object oUCB = xMSF.createInstanceWithArguments
116                 ("com.sun.star.ucb.UniversalContentBroker",
117                 new Object[] {"Local", "Office"}) ;
118 
119             XContentIdentifierFactory ciFac = (XContentIdentifierFactory)
120                 UnoRuntime.queryInterface(XContentIdentifierFactory.class, oUCB) ;
121 
122             String url = util.utils.getFullTestURL("SwXTextEmbeddedObject.sxw") ;
123             String escUrl = "" ;
124 
125             // In base URL of a JAR file in content URL all directory
126             // separators ('/') must be replaced with escape symbol '%2F'.
127             int idx = url.indexOf("/") ;
128             int lastIdx = -1 ;
129             while (idx >= 0) {
130                 escUrl += url.substring(lastIdx + 1, idx) + "%2F" ;
131                 lastIdx = idx ;
132                 idx = url.indexOf("/", idx + 1) ;
133             }
134             escUrl += url.substring(lastIdx + 1) ;
135             String cntUrl = "vnd.sun.star.pkg://" + escUrl + "/" ;
136 
137             XContentIdentifier CI = ciFac.createContentIdentifier(cntUrl) ;
138 
139             XContentProvider cntProv = (XContentProvider)
140                 UnoRuntime.queryInterface(XContentProvider.class, oUCB) ;
141 
142             XContent cnt = cntProv.queryContent(CI) ;
143 
144             XCommandProcessor cmdProc = (XCommandProcessor)
145                 UnoRuntime.queryInterface(XCommandProcessor.class, cnt) ;
146 
147             Property prop = new Property() ;
148             prop.Name = "Title" ;
149 
150             Command cmd = new Command("open", -1, new OpenCommandArgument2
151                 (OpenMode.ALL, 10000, null, new Property[] {prop},
152                  new NumberedSortingInfo[0])) ;
153 
154             XDynamicResultSet dynResSet = null;
155             try {
156                 dynResSet = (XDynamicResultSet)
157                     AnyConverter.toObject(new Type(XDynamicResultSet.class),
158                                         cmdProc.execute(cmd, 0, null));
159             } catch (com.sun.star.lang.IllegalArgumentException iae) {
160                 throw new StatusException("Couldn't convert Any ",iae);
161             }
162 
163             XResultSet resSet = dynResSet.getStaticResultSet() ;
164 
165             Object oStubFactory = xMSF.createInstance
166                 ("com.sun.star.ucb.CachedContentResultSetStubFactory") ;
167 
168             XCachedContentResultSetStubFactory xStubFactory =
169                 (XCachedContentResultSetStubFactory) UnoRuntime.queryInterface
170                 (XCachedContentResultSetStubFactory.class, oStubFactory) ;
171 
172             resSetStub = xStubFactory.createCachedContentResultSetStub(resSet) ;
173 
174         } catch (com.sun.star.uno.Exception e) {
175             log.println("Can't create relation." );
176             e.printStackTrace(log) ;
177             throw new StatusException( "Can't create relation", e );
178         }
179 
180         tEnv.addObjRelation("CachedContentResultSetStub", resSetStub) ;
181 
182         return tEnv;
183     } // finish method getTestEnvironment
184 
185 }
186 
187