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.registry;
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.registry.CannotRegisterImplementationException;
32 import com.sun.star.registry.XImplementationRegistration;
33 import com.sun.star.registry.XSimpleRegistry;
34 import com.sun.star.uno.RuntimeException;
35 import lib.MultiMethodTest;
36 import util.RegistryTools;
37 import util.utils;
38 
39 /**
40 * Testing <code>com.sun.star.registry.XImplementationRegistration</code>
41 * interface methods :
42 * <ul>
43 *  <li><code> registerImplementation()</code></li>
44 *  <li><code> revokeImplementation()</code></li>
45 *  <li><code> getImplementations()</code></li>
46 *  <li><code> checkInstantiation()</code></li>
47 * </ul> <p>
48 * The following predefined files needed to complete the test:
49 * <ul>
50 *  <li> <code>solibrary.jar</code> : jar file with implementation
51 *   classes. One of the required implementation must have name
52 *   <code>com.ivistaportal.solibrary.HistogramImpl</code> cause
53 *   it is checked in <code>getImplementations</code> method. </li>
54 * <ul> <p>
55 * Test is <b> NOT </b> multithread compilant. <p>
56 * After test completion object environment has to be recreated.
57 * @see com.sun.star.###
58 */
59 public class _XImplementationRegistration extends MultiMethodTest {
60 
61     public XImplementationRegistration oObj = null;
62 
63     private String url = null ;
64     private String loader = null ;
65     private XSimpleRegistry reg = null ;
66 
67     /**
68     * First a registry created and opened in the temporary directory
69     * of StarOffice. Then some implementations situated in JAR file
70     * is registered in the registry opened. <p>
71     * Has <b>OK</b> status if some information is written into registry.
72     *
73     */
74     public void _registerImplementation()
75         throws CannotRegisterImplementationException, RuntimeException
76     {
77         url = util.utils.getFullTestURL("qadevlibs/MyPersistObjectImpl.jar");
78         loader = "com.sun.star.loader.Java2";
79         boolean result = false ;
80         String name = null;
81 
82         try {
83             name = utils.getOfficeTempDir((XMultiServiceFactory)tParam.getMSF()) +
84                 "XImplementationRegistration_tmp.rdb";
85             reg = RegistryTools.openRegistry
86                 (name, (XMultiServiceFactory)tParam.getMSF()) ;
87 
88             oObj.registerImplementation(loader, url, reg) ;
89 
90             RegistryTools.printRegistryInfo(reg.getRootKey(), log) ;
91 
92             String[] subKeys = reg.getRootKey().getKeyNames() ;
93 
94             result = subKeys != null && subKeys.length > 0 ;
95 
96         } catch (com.sun.star.uno.Exception e) {
97             log.println("Can't open registry file: " + name) ;
98             e.printStackTrace(log) ;
99         }
100         tRes.tested("registerImplementation()", result) ;
101     }
102 
103     /**
104     * Retrieves an array of implementation names and check them. <p>
105     * Has <b>OK</b> status if among them an implementation name
106     * <code>com.ivistaportal.solibrary.HistogramImpl</code> exists.
107     * The following method tests are to be completed successfully before :
108     * <ul>
109     *  <li> <code> registerImplementation </code>  </li>
110     * </ul>
111     */
112     public void _getImplementations() throws RuntimeException{
113         requiredMethod("registerImplementation()") ;
114 
115         String[] impl = oObj.getImplementations(loader, url) ;
116 
117         if (impl.length == 0) log.println("getImplementations() "+
118             "returns an empty array");
119         boolean result = false ;
120         log.println("Implementations found :") ;
121         for (int i = 0; i < impl.length; i++) {
122             log.println(" '" + impl[i] + "'") ;
123             if ("com.sun.star.cmp.MyPersistObject".
124                 equals(impl[i])) {
125 
126                 result = true ;
127                 break ;
128             }
129         }
130 
131 
132 
133         tRes.tested("getImplementations()", result) ;
134     }
135 
136     /**
137     * Calls the method with
138     * <code>com.sun.star.comp.stoc.JavaComponentLoader</code>
139     * implementation name.<p>
140     * Has <b>OK</b> status if not null array returned. <p>
141     * The following method tests are to be completed successfully before :
142     * <ul>
143     *  <li> <code> registerImplementation </code>  </li>
144     * </ul>
145     */
146     public void _checkInstantiation() throws RuntimeException {
147         requiredMethod("registerImplementation()") ;
148 
149         String[] inst = oObj.checkInstantiation(
150             "com.sun.star.comp.stoc.JavaComponentLoader") ;
151 
152         tRes.tested("checkInstantiation()", inst != null) ;
153     }
154 
155     /**
156     * Revokes implementations from registry, and checks if
157     * all implementations' information is deleted. <p>
158     * Has <b>OK</b> status if registry has no key entries. <p>
159     * The following method tests are to be completed successfully before :
160     * <ul>
161     *  <li> <code> registerImplementation </code> : to have
162     *   implementation registered in registry. </li>
163     * </ul>
164     * The following method tests are to be executed before :
165     * <ul>
166     *  <li> <code> getImplementations </code>
167     *  <li> <code> checkInstantiation </code>
168     * </ul>
169     */
170     public void _revokeImplementation() throws RuntimeException{
171         boolean result = false ;
172 
173         requiredMethod("registerImplementation()") ;
174 
175         executeMethod("getImplementations()") ;
176         executeMethod("checkInstantiation()") ;
177 
178         oObj.revokeImplementation(url, reg);
179         RegistryTools.printRegistryInfo(reg, log) ;
180 
181         try {
182             String[] subKeys = reg.getRootKey().getKeyNames() ;
183 
184             result = subKeys == null || subKeys.length == 0 ;
185         } catch (com.sun.star.registry.InvalidRegistryException e) {
186             log.println("!!! Exception retrieving keys from registry :") ;
187             e.printStackTrace(log);
188         }
189 
190         tRes.tested("revokeImplementation()", true) ;
191     }
192 
193     public void after() {
194         this.disposeEnvironment() ;
195     }
196 }
197 
198 
199