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