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 ifc.loader;
25 
26 import lib.MultiMethodTest;
27 import lib.StatusException;
28 import util.RegistryTools;
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.lang.XServiceInfo;
32 import com.sun.star.loader.CannotActivateFactoryException;
33 import com.sun.star.loader.XImplementationLoader;
34 import com.sun.star.registry.CannotRegisterImplementationException;
35 import com.sun.star.registry.XRegistryKey;
36 import com.sun.star.registry.XSimpleRegistry;
37 import com.sun.star.uno.UnoRuntime;
38 import com.sun.star.uno.XInterface;
39 
40 /**
41  * Testing <code>com.sun.star.loader.XImplementationLoader</code>
42  * interface methods :
43  * <ul>
44  *  <li><code> activate()</code></li>
45  *  <li><code> writeRegistryInfo()</code></li>
46  * </ul> <p>
47  *
48  * The following object relations required :
49  * <ul>
50  *  <li> <code>'ImplementationLoader'</code> : service which is
51  *    responsible for loading implementations. </li>
52  *  <li> <code>'ImplementationUrl'</code> : implementation file location. </li>
53  *  <li> <code>'ImplementationName'</code> : Name of the implementation.</li>
54  * </ul> <p>
55  * Object has to be recreated after this test. <p>
56  * Test is <b> Not </b> multithread compilant.
57  */
58 public class _XImplementationLoader extends MultiMethodTest {
59 
60     public XImplementationLoader oObj = null;
61     private String implLoader = null ;
62     private String implUrl = null ;
63     private String implName = null ;
64 
65     /**
66     * Retrieves object relations.
67     * @throws StatusException If one of relations not found.
68     */
before()69     public void before() {
70         implLoader = (String) tEnv.getObjRelation("ImplementationLoader") ;
71         implUrl = (String) tEnv.getObjRelation("ImplementationUrl") ;
72         implName = (String) tEnv.getObjRelation("ImplementationName") ;
73 
74         if (implLoader == null || implUrl == null || implName == null)
75             throw new StatusException("One of object relations not found",
76                 new NullPointerException()) ;
77     }
78 
79     /**
80     * First registry file created, and the root key retrieved.
81     * Then method <code>writeRegistryInfo</code> called and it must
82     * write some info into the registry root key. After all registry
83     * is destroyed.<p>
84     * Has OK status if some info was written into registry.
85     */
_writeRegistryInfo()86     public void _writeRegistryInfo() {
87         XRegistryKey key ;
88         XSimpleRegistry xReg = null ;
89 
90         String tmpDir = util.utils.getOfficeTempDir((XMultiServiceFactory)tParam.getMSF());
91 
92         try {
93             xReg = RegistryTools.createRegistryService
94                 ((XMultiServiceFactory)tParam.getMSF()) ;
95 
96             xReg.open(tmpDir + "XImpLoader_tmp.rdb", false, true) ;
97 
98             key = xReg.getRootKey() ;
99         } catch (com.sun.star.uno.Exception e) {
100             log.println("Can not create registry for writing") ;
101             e.printStackTrace(log) ;
102             tRes.tested("writeRegistryInfo()", false) ;
103             return ;
104         }
105 
106         boolean rc ;
107         try {
108             rc = oObj.writeRegistryInfo(key, implLoader, implUrl) ;
109         } catch (CannotRegisterImplementationException e) {
110             throw new StatusException("Can not register implementation", e) ;
111         }
112 
113         if (rc == false)
114             log.println("Method returned false value") ;
115 
116         String[] keys ;
117         try {
118             keys = key.getKeyNames() ;
119         } catch (com.sun.star.uno.Exception e) {
120             log.println("Error retrieving key names from registry") ;
121             tRes.tested("writeRegistryInfo()", false) ;
122             return ;
123         }
124 
125         // destroying registry file
126         try {
127             xReg.close() ;
128             xReg.destroy() ;
129         } catch (com.sun.star.registry.InvalidRegistryException e) {
130             log.println("Can't destroy registry file.") ;
131         }
132 
133         tRes.tested("writeRegistryInfo()", rc && keys.length > 0) ;
134     }
135 
136     /**
137     * Tries to activate the implementation. <p>
138     *
139     * Has OK status if not <code>null</code> value returned by method,
140     * if its implementation name is the same as expected.
141     */
_activate()142     public void _activate() {
143         boolean ok = true ;
144         XInterface factory = null ;
145 
146         try {
147             factory = (XInterface) oObj.activate
148                 (implName, implLoader, implUrl, null) ;
149         } catch (CannotActivateFactoryException e) {
150             throw new StatusException("Can not activate factory", e) ;
151         }
152 
153         XServiceInfo xServInf = (XServiceInfo) UnoRuntime.queryInterface
154             (XServiceInfo.class, factory) ;
155 
156         if (xServInf == null) {
157             if (factory == null) {
158                 log.println("activate() returns null - FAILED.");
159             } else {
160                 log.println("Activated impementation doesn't support "+
161                     "XServiceInfo - FAILED.");
162             }
163             ok = false ;
164         } else {
165             String gImpName = xServInf.getImplementationName() ;
166             log.println("Implementation name returned :" + gImpName);
167 
168             if (!gImpName.equals(implName)) {
169                 log.println("!!! But other name was expected :" + implName);
170                 ok = false ;
171             }
172         }
173 
174         tRes.tested("activate()", ok) ;
175     }
176 
177     /**
178     * Forces object recreation.
179     */
after()180     public void after() {
181         this.disposeEnvironment() ;
182     }
183 }
184 
185