1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.loader;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.StatusException;
32*cdf0e10cSrcweir import util.RegistryTools;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
36*cdf0e10cSrcweir import com.sun.star.loader.CannotActivateFactoryException;
37*cdf0e10cSrcweir import com.sun.star.loader.XImplementationLoader;
38*cdf0e10cSrcweir import com.sun.star.registry.CannotRegisterImplementationException;
39*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
40*cdf0e10cSrcweir import com.sun.star.registry.XSimpleRegistry;
41*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
42*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /**
45*cdf0e10cSrcweir  * Testing <code>com.sun.star.loader.XImplementationLoader</code>
46*cdf0e10cSrcweir  * interface methods :
47*cdf0e10cSrcweir  * <ul>
48*cdf0e10cSrcweir  *  <li><code> activate()</code></li>
49*cdf0e10cSrcweir  *  <li><code> writeRegistryInfo()</code></li>
50*cdf0e10cSrcweir  * </ul> <p>
51*cdf0e10cSrcweir  *
52*cdf0e10cSrcweir  * The following object relations required :
53*cdf0e10cSrcweir  * <ul>
54*cdf0e10cSrcweir  *  <li> <code>'ImplementationLoader'</code> : service which is
55*cdf0e10cSrcweir  *    responsible for loading implementations. </li>
56*cdf0e10cSrcweir  *  <li> <code>'ImplementationUrl'</code> : implementation file location. </li>
57*cdf0e10cSrcweir  *  <li> <code>'ImplementationName'</code> : Name of the implementation.</li>
58*cdf0e10cSrcweir  * </ul> <p>
59*cdf0e10cSrcweir  * Object has to be recreated after this test. <p>
60*cdf0e10cSrcweir  * Test is <b> Not </b> multithread compilant.
61*cdf0e10cSrcweir  */
62*cdf0e10cSrcweir public class _XImplementationLoader extends MultiMethodTest {
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     public XImplementationLoader oObj = null;
65*cdf0e10cSrcweir     private String implLoader = null ;
66*cdf0e10cSrcweir     private String implUrl = null ;
67*cdf0e10cSrcweir     private String implName = null ;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /**
70*cdf0e10cSrcweir     * Retrieves object relations.
71*cdf0e10cSrcweir     * @throws StatusException If one of relations not found.
72*cdf0e10cSrcweir     */
73*cdf0e10cSrcweir     public void before() {
74*cdf0e10cSrcweir         implLoader = (String) tEnv.getObjRelation("ImplementationLoader") ;
75*cdf0e10cSrcweir         implUrl = (String) tEnv.getObjRelation("ImplementationUrl") ;
76*cdf0e10cSrcweir         implName = (String) tEnv.getObjRelation("ImplementationName") ;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         if (implLoader == null || implUrl == null || implName == null)
79*cdf0e10cSrcweir             throw new StatusException("One of object relations not found",
80*cdf0e10cSrcweir                 new NullPointerException()) ;
81*cdf0e10cSrcweir     }
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     /**
84*cdf0e10cSrcweir     * First registry file created, and the root key retrieved.
85*cdf0e10cSrcweir     * Then method <code>writeRegistryInfo</code> called and it must
86*cdf0e10cSrcweir     * write some info into the registry root key. After all registry
87*cdf0e10cSrcweir     * is destroyed.<p>
88*cdf0e10cSrcweir     * Has OK status if some info was written into registry.
89*cdf0e10cSrcweir     */
90*cdf0e10cSrcweir     public void _writeRegistryInfo() {
91*cdf0e10cSrcweir         XRegistryKey key ;
92*cdf0e10cSrcweir         XSimpleRegistry xReg = null ;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         String tmpDir = util.utils.getOfficeTempDir((XMultiServiceFactory)tParam.getMSF());
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir         try {
97*cdf0e10cSrcweir             xReg = RegistryTools.createRegistryService
98*cdf0e10cSrcweir                 ((XMultiServiceFactory)tParam.getMSF()) ;
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir             xReg.open(tmpDir + "XImpLoader_tmp.rdb", false, true) ;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir             key = xReg.getRootKey() ;
103*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
104*cdf0e10cSrcweir             log.println("Can not create registry for writing") ;
105*cdf0e10cSrcweir             e.printStackTrace(log) ;
106*cdf0e10cSrcweir             tRes.tested("writeRegistryInfo()", false) ;
107*cdf0e10cSrcweir             return ;
108*cdf0e10cSrcweir         }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir         boolean rc ;
111*cdf0e10cSrcweir         try {
112*cdf0e10cSrcweir             rc = oObj.writeRegistryInfo(key, implLoader, implUrl) ;
113*cdf0e10cSrcweir         } catch (CannotRegisterImplementationException e) {
114*cdf0e10cSrcweir             throw new StatusException("Can not register implementation", e) ;
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         if (rc == false)
118*cdf0e10cSrcweir             log.println("Method returned false value") ;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         String[] keys ;
121*cdf0e10cSrcweir         try {
122*cdf0e10cSrcweir             keys = key.getKeyNames() ;
123*cdf0e10cSrcweir         } catch (com.sun.star.uno.Exception e) {
124*cdf0e10cSrcweir             log.println("Error retrieving key names from registry") ;
125*cdf0e10cSrcweir             tRes.tested("writeRegistryInfo()", false) ;
126*cdf0e10cSrcweir             return ;
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir         // destroying registry file
130*cdf0e10cSrcweir         try {
131*cdf0e10cSrcweir             xReg.close() ;
132*cdf0e10cSrcweir             xReg.destroy() ;
133*cdf0e10cSrcweir         } catch (com.sun.star.registry.InvalidRegistryException e) {
134*cdf0e10cSrcweir             log.println("Can't destroy registry file.") ;
135*cdf0e10cSrcweir         }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir         tRes.tested("writeRegistryInfo()", rc && keys.length > 0) ;
138*cdf0e10cSrcweir     }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     /**
141*cdf0e10cSrcweir     * Tries to activate the implementation. <p>
142*cdf0e10cSrcweir     *
143*cdf0e10cSrcweir     * Has OK status if not <code>null</code> value returned by method,
144*cdf0e10cSrcweir     * if its implementation name is the same as expected.
145*cdf0e10cSrcweir     */
146*cdf0e10cSrcweir     public void _activate() {
147*cdf0e10cSrcweir         boolean ok = true ;
148*cdf0e10cSrcweir         XInterface factory = null ;
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir         try {
151*cdf0e10cSrcweir             factory = (XInterface) oObj.activate
152*cdf0e10cSrcweir                 (implName, implLoader, implUrl, null) ;
153*cdf0e10cSrcweir         } catch (CannotActivateFactoryException e) {
154*cdf0e10cSrcweir             throw new StatusException("Can not activate factory", e) ;
155*cdf0e10cSrcweir         }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         XServiceInfo xServInf = (XServiceInfo) UnoRuntime.queryInterface
158*cdf0e10cSrcweir             (XServiceInfo.class, factory) ;
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         if (xServInf == null) {
161*cdf0e10cSrcweir             if (factory == null) {
162*cdf0e10cSrcweir                 log.println("activate() returns null - FAILED.");
163*cdf0e10cSrcweir             } else {
164*cdf0e10cSrcweir                 log.println("Activated impementation doesn't support "+
165*cdf0e10cSrcweir                     "XServiceInfo - FAILED.");
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir             ok = false ;
168*cdf0e10cSrcweir         } else {
169*cdf0e10cSrcweir             String gImpName = xServInf.getImplementationName() ;
170*cdf0e10cSrcweir             log.println("Implementation name returned :" + gImpName);
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             if (!gImpName.equals(implName)) {
173*cdf0e10cSrcweir                 log.println("!!! But other name was expected :" + implName);
174*cdf0e10cSrcweir                 ok = false ;
175*cdf0e10cSrcweir             }
176*cdf0e10cSrcweir         }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir         tRes.tested("activate()", ok) ;
179*cdf0e10cSrcweir     }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir     /**
182*cdf0e10cSrcweir     * Forces object recreation.
183*cdf0e10cSrcweir     */
184*cdf0e10cSrcweir     public void after() {
185*cdf0e10cSrcweir         this.disposeEnvironment() ;
186*cdf0e10cSrcweir     }
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189