1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package ifc.loader;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import lib.MultiMethodTest;
27cdf0e10cSrcweir import lib.StatusException;
28cdf0e10cSrcweir import util.RegistryTools;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
31cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
32cdf0e10cSrcweir import com.sun.star.loader.CannotActivateFactoryException;
33cdf0e10cSrcweir import com.sun.star.loader.XImplementationLoader;
34cdf0e10cSrcweir import com.sun.star.registry.CannotRegisterImplementationException;
35cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
36cdf0e10cSrcweir import com.sun.star.registry.XSimpleRegistry;
37cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
38cdf0e10cSrcweir import com.sun.star.uno.XInterface;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /**
41cdf0e10cSrcweir  * Testing <code>com.sun.star.loader.XImplementationLoader</code>
42cdf0e10cSrcweir  * interface methods :
43cdf0e10cSrcweir  * <ul>
44cdf0e10cSrcweir  *  <li><code> activate()</code></li>
45cdf0e10cSrcweir  *  <li><code> writeRegistryInfo()</code></li>
46cdf0e10cSrcweir  * </ul> <p>
47cdf0e10cSrcweir  *
48cdf0e10cSrcweir  * The following object relations required :
49cdf0e10cSrcweir  * <ul>
50cdf0e10cSrcweir  *  <li> <code>'ImplementationLoader'</code> : service which is
51cdf0e10cSrcweir  *    responsible for loading implementations. </li>
52cdf0e10cSrcweir  *  <li> <code>'ImplementationUrl'</code> : implementation file location. </li>
53cdf0e10cSrcweir  *  <li> <code>'ImplementationName'</code> : Name of the implementation.</li>
54cdf0e10cSrcweir  * </ul> <p>
55cdf0e10cSrcweir  * Object has to be recreated after this test. <p>
56cdf0e10cSrcweir  * Test is <b> Not </b> multithread compilant.
57cdf0e10cSrcweir  */
58cdf0e10cSrcweir public class _XImplementationLoader extends MultiMethodTest {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     public XImplementationLoader oObj = null;
61cdf0e10cSrcweir     private String implLoader = null ;
62cdf0e10cSrcweir     private String implUrl = null ;
63cdf0e10cSrcweir     private String implName = null ;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /**
66cdf0e10cSrcweir     * Retrieves object relations.
67cdf0e10cSrcweir     * @throws StatusException If one of relations not found.
68cdf0e10cSrcweir     */
before()69cdf0e10cSrcweir     public void before() {
70cdf0e10cSrcweir         implLoader = (String) tEnv.getObjRelation("ImplementationLoader") ;
71cdf0e10cSrcweir         implUrl = (String) tEnv.getObjRelation("ImplementationUrl") ;
72cdf0e10cSrcweir         implName = (String) tEnv.getObjRelation("ImplementationName") ;
73cdf0e10cSrcweir 
74cdf0e10cSrcweir         if (implLoader == null || implUrl == null || implName == null)
75cdf0e10cSrcweir             throw new StatusException("One of object relations not found",
76cdf0e10cSrcweir                 new NullPointerException()) ;
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /**
80cdf0e10cSrcweir     * First registry file created, and the root key retrieved.
81cdf0e10cSrcweir     * Then method <code>writeRegistryInfo</code> called and it must
82cdf0e10cSrcweir     * write some info into the registry root key. After all registry
83cdf0e10cSrcweir     * is destroyed.<p>
84cdf0e10cSrcweir     * Has OK status if some info was written into registry.
85cdf0e10cSrcweir     */
_writeRegistryInfo()86cdf0e10cSrcweir     public void _writeRegistryInfo() {
87cdf0e10cSrcweir         XRegistryKey key ;
88cdf0e10cSrcweir         XSimpleRegistry xReg = null ;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         String tmpDir = util.utils.getOfficeTempDir((XMultiServiceFactory)tParam.getMSF());
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         try {
93cdf0e10cSrcweir             xReg = RegistryTools.createRegistryService
94cdf0e10cSrcweir                 ((XMultiServiceFactory)tParam.getMSF()) ;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir             xReg.open(tmpDir + "XImpLoader_tmp.rdb", false, true) ;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             key = xReg.getRootKey() ;
99cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
100cdf0e10cSrcweir             log.println("Can not create registry for writing") ;
101cdf0e10cSrcweir             e.printStackTrace(log) ;
102cdf0e10cSrcweir             tRes.tested("writeRegistryInfo()", false) ;
103cdf0e10cSrcweir             return ;
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         boolean rc ;
107cdf0e10cSrcweir         try {
108cdf0e10cSrcweir             rc = oObj.writeRegistryInfo(key, implLoader, implUrl) ;
109cdf0e10cSrcweir         } catch (CannotRegisterImplementationException e) {
110cdf0e10cSrcweir             throw new StatusException("Can not register implementation", e) ;
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir         if (rc == false)
114cdf0e10cSrcweir             log.println("Method returned false value") ;
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         String[] keys ;
117cdf0e10cSrcweir         try {
118cdf0e10cSrcweir             keys = key.getKeyNames() ;
119cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
120cdf0e10cSrcweir             log.println("Error retrieving key names from registry") ;
121cdf0e10cSrcweir             tRes.tested("writeRegistryInfo()", false) ;
122cdf0e10cSrcweir             return ;
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir         // destroying registry file
126cdf0e10cSrcweir         try {
127cdf0e10cSrcweir             xReg.close() ;
128cdf0e10cSrcweir             xReg.destroy() ;
129cdf0e10cSrcweir         } catch (com.sun.star.registry.InvalidRegistryException e) {
130cdf0e10cSrcweir             log.println("Can't destroy registry file.") ;
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         tRes.tested("writeRegistryInfo()", rc && keys.length > 0) ;
134cdf0e10cSrcweir     }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     /**
137cdf0e10cSrcweir     * Tries to activate the implementation. <p>
138cdf0e10cSrcweir     *
139cdf0e10cSrcweir     * Has OK status if not <code>null</code> value returned by method,
140cdf0e10cSrcweir     * if its implementation name is the same as expected.
141cdf0e10cSrcweir     */
_activate()142cdf0e10cSrcweir     public void _activate() {
143cdf0e10cSrcweir         boolean ok = true ;
144cdf0e10cSrcweir         XInterface factory = null ;
145cdf0e10cSrcweir 
146cdf0e10cSrcweir         try {
147cdf0e10cSrcweir             factory = (XInterface) oObj.activate
148cdf0e10cSrcweir                 (implName, implLoader, implUrl, null) ;
149cdf0e10cSrcweir         } catch (CannotActivateFactoryException e) {
150cdf0e10cSrcweir             throw new StatusException("Can not activate factory", e) ;
151cdf0e10cSrcweir         }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir         XServiceInfo xServInf = (XServiceInfo) UnoRuntime.queryInterface
154cdf0e10cSrcweir             (XServiceInfo.class, factory) ;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         if (xServInf == null) {
157cdf0e10cSrcweir             if (factory == null) {
158cdf0e10cSrcweir                 log.println("activate() returns null - FAILED.");
159cdf0e10cSrcweir             } else {
160cdf0e10cSrcweir                 log.println("Activated impementation doesn't support "+
161cdf0e10cSrcweir                     "XServiceInfo - FAILED.");
162cdf0e10cSrcweir             }
163cdf0e10cSrcweir             ok = false ;
164cdf0e10cSrcweir         } else {
165cdf0e10cSrcweir             String gImpName = xServInf.getImplementationName() ;
166cdf0e10cSrcweir             log.println("Implementation name returned :" + gImpName);
167cdf0e10cSrcweir 
168cdf0e10cSrcweir             if (!gImpName.equals(implName)) {
169cdf0e10cSrcweir                 log.println("!!! But other name was expected :" + implName);
170cdf0e10cSrcweir                 ok = false ;
171cdf0e10cSrcweir             }
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         tRes.tested("activate()", ok) ;
175cdf0e10cSrcweir     }
176cdf0e10cSrcweir 
177cdf0e10cSrcweir     /**
178cdf0e10cSrcweir     * Forces object recreation.
179cdf0e10cSrcweir     */
after()180cdf0e10cSrcweir     public void after() {
181cdf0e10cSrcweir         this.disposeEnvironment() ;
182cdf0e10cSrcweir     }
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
185