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