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 mod._nestedreg.uno;
25 
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.registry.XSimpleRegistry;
28 import com.sun.star.uno.XInterface;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.PrintWriter;
33 import lib.StatusException;
34 import lib.TestCase;
35 import lib.TestEnvironment;
36 import lib.TestParameters;
37 import util.RegistryTools;
38 import util.utils;
39 
40 /**
41 * Test for object which is represented by service
42 * <code>com.sun.star.registry.NestedRegistry</code>. <p>
43 * Object implements the following interfaces :
44 * <ul>
45 *  <li> <code>com::sun::star::registry::XSimpleRegistry</code></li>
46 *  <li> <code>com::sun::star::lang::XInitialization</code></li>
47 * </ul>
48 * The following files used by this test :
49 * <ul>
50 *  <li><b> XSimpleRegistry.rdb </b> : Registry file created before. </li>
51 *  <li><b> XSimpleRegistry_open#.rdb </b> : Temporary registry file as copy of
52 *     <b> XSimpleRegistry.rdb </b> in the SOffice temp directory.
53 *     ('#' - is an ordinary number) </li>
54 *  <li><b> XSimpleRegistry_merge#.rdb </b> : Temporary registry file as copy of
55 *     <b> XSimpleRegistry.rdb </b> in the SOffice temp directory.
56 *     ('#' - is an ordinary number) </li>
57 * </ul> <p>
58 * This object test <b> is NOT </b> designed to be run in several
59 * threads concurently.
60 * @see com.sun.star.registry.XSimpleRegistry
61 * @see com.sun.star.lang.XInitialization
62 * @see ifc.registry._XSimpleRegistry
63 * @see ifc.lang._XInitialization
64 */
65 public class NestedRegistry extends TestCase {
66 
67     protected static int uniq = 0 ;
68     XSimpleRegistry reg1;
69     XSimpleRegistry reg2;
70 
disposeTestEnvironment( TestEnvironment tEnv, TestParameters tParam )71     public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
72         TestParameters tParam ) {
73         try {
74             reg1.destroy();
75             reg2.destroy();
76         }
77         catch (com.sun.star.registry.InvalidRegistryException e) {}
78     }
79 
80     /**
81     * Creates a temporary copy of file, which is deleted when VM exits.
82     * @param src Source file path.
83     * @param dst Destination file path.
84     * @throws java.io.IOException If any problems occur during copiing.
85     */
copyFile(String src, String dst, PrintWriter log)86     protected void copyFile(String src, String dst, PrintWriter log)
87             throws java.io.IOException {
88         File srcF = new File(src) ;
89         File dstF = new File(dst) ;
90 
91         if (dstF.exists()) dstF.delete() ;
92         dstF.createNewFile() ;
93 
94         dstF.deleteOnExit() ;
95 
96         FileInputStream fIn = new FileInputStream(srcF) ;
97         FileOutputStream fOut = new FileOutputStream(dstF) ;
98 
99         byte[] buf = new byte[1024] ;
100         int bytesRead = 0 ;
101         while ((bytesRead = fIn.read(buf)) > 0)
102             fOut.write(buf, 0, bytesRead) ;
103 
104         fIn.close() ;
105         fOut.close() ;
106     }
107 
108 
109     /**
110     * Creating a Testenvironment for the interfaces to be tested.
111     * Creates two temporary copies of registry file created before, opens
112     * them, and creates service <code>com.sun.star.comp.stoc.NestedRegistry</code>
113     * with these two registries. <p>
114     * Object relations created :
115     * <ul>
116     *  <li> <code>'NR'</code> for {@link ifc.registry._XSimpleRegistry} :
117     *     Just informs interface test that <code>NestedRegistry</code>
118     *     service is tested. If this relation exists, than some methods
119     *     are not supported. The relation is a <code>String</code> with
120     *     object name.</li>
121     *  <li> <code>'XSimpleRegistry.open'</code> for
122     *    {@link ifc.registry._XSimpleRegistry}
123     *  </li>
124     *  <li> <code>'XSimpleRegistry.destroy'</code> for
125     *    {@link ifc.registry._XSimpleRegistry}
126     *  </li>
127     *  <li> <code>'XSimpleRegistry.merge'</code> for
128     *    {@link ifc.registry._XSimpleRegistry}
129     *  </li>
130     * </ul>
131     */
createTestEnvironment(TestParameters Param, PrintWriter log)132     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
133         XInterface oObj = null;
134         Object oInterface = null;
135 
136         final String tmpDir = utils.getOfficeTempDirSys(
137                                     (XMultiServiceFactory)Param.getMSF()) ;
138         final String openF = tmpDir + "XSimpleRegistry_open" + uniq + ".rdb" ;
139         final String destroyF = tmpDir
140             + "XSimpleRegistry_destroy" + uniq + ".rdb" ;
141         final String mergeF = tmpDir + "XSimpleRegistry_merge" + uniq + ".rdb" ;
142         uniq++ ;
143 
144         log.println("creating copies of the registry for XSimpleRegistry");
145         try {
146             String source = utils.getFullTestDocName("XSimpleRegistry.rdb");
147             copyFile(source, openF, log) ;
148             copyFile(source, mergeF, log) ;
149         } catch (java.io.IOException e) {
150             log.println("Exception occurred while copying files");
151             e.printStackTrace(log);
152             throw new StatusException("Exception occurred while copying files", e);
153         }
154 
155         try {
156             XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
157             reg1 = RegistryTools.
158                     createRegistryService(xMSF) ;
159             reg1.open(mergeF, false, true) ;
160             reg2 = RegistryTools.
161                     createRegistryService(xMSF) ;
162             reg2.open(openF, false, true) ;
163             XSimpleRegistry[] arg = new XSimpleRegistry[2];
164             arg[0]=reg1;
165             arg[1]=reg2;
166             oInterface = xMSF.createInstanceWithArguments
167                 ( "com.sun.star.comp.stoc.NestedRegistry", arg );
168         }
169         catch( Exception e ) {
170             log.println("Introspection Service not available" );
171         }
172         oObj = (XInterface) oInterface;
173 
174 
175 
176         log.println( "    creating a new environment for Introspection object" );
177         TestEnvironment tEnv = new TestEnvironment( oObj );
178 
179         tEnv.addObjRelation("NR","NestedRegistry");
180 
181         tEnv.addObjRelation("XSimpleRegistry.open", openF) ;
182         tEnv.addObjRelation("XSimpleRegistry.merge", mergeF) ;
183         tEnv.addObjRelation("XSimpleRegistry.destroy", destroyF) ;
184 
185         return tEnv;
186     } // finish method getTestEnvironment
187 
cleanup( TestParameters Param, PrintWriter log)188     protected void cleanup( TestParameters Param, PrintWriter log) {
189 
190 
191     }
192 
193 }    // finish class NestedRegistry
194 
195