1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package mod._defreg;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.PrintWriter;
25 
26 import lib.TestCase;
27 import lib.TestEnvironment;
28 import lib.TestParameters;
29 import util.RegistryTools;
30 import util.utils;
31 
32 import com.sun.star.lang.XMultiServiceFactory;
33 import com.sun.star.registry.XSimpleRegistry;
34 import com.sun.star.uno.XInterface;
35 
36 /**
37 * Test for object which is represented by service
38 * <code>com.sun.star.registry.NestedRegistry</code>. <p>
39 * Object implements the following interfaces :
40 * <ul>
41 *  <li> <code>com::sun::star::registry::XSimpleRegistry</code></li>
42 *  <li> <code>com::sun::star::lang::XInitialization</code></li>
43 * </ul>
44 * The following files used by this test :
45 * <ul>
46 *  <li><b> XSimpleRegistry.rdb </b> : Registry file created before. </li>
47 *  <li><b> XSimpleRegistry_open#.rdb </b> : Temporary registry file as copy of
48 *     <b> XSimpleRegistry.rdb </b> in the SOffice temp directory.
49 *     ('#' - is an ordinary number) </li>
50 *  <li><b> XSimpleRegistry_merge#.rdb </b> : Temporary registry file as copy of
51 *     <b> XSimpleRegistry.rdb </b> in the SOffice temp directory.
52 *     ('#' - is an ordinary number) </li>
53 * </ul> <p>
54 * This object test <b> is NOT </b> designed to be run in several
55 * threads concurrently.
56 * @see com.sun.star.registry.XSimpleRegistry
57 * @see com.sun.star.lang.XInitialization
58 * @see ifc.registry._XSimpleRegistry
59 * @see ifc.lang._XInitialization
60 */
61 public class NestedRegistry extends TestCase {
62 
63     protected static int uniq = 0 ;
64     XSimpleRegistry reg1;
65     XSimpleRegistry reg2;
66 
67     @Override
disposeTestEnvironment( TestEnvironment tEnv, TestParameters tParam )68     public synchronized void disposeTestEnvironment( TestEnvironment tEnv,
69         TestParameters tParam ) {
70         try {
71             reg1.destroy();
72             reg2.destroy();
73         }
74         catch (com.sun.star.registry.InvalidRegistryException e) {
75             System.out.println("caught exception: " + e);
76         }
77     }
78 
79     /**
80     * Creates a temporary copy of file, which is deleted when VM exits.
81     * @param src Source file path.
82     * @param dst Destination file path.
83     * @throws java.io.IOException If any problems occur during copying.
84     */
copyFile(String src, String dst, PrintWriter log)85     protected void copyFile(String src, String dst, PrintWriter log)
86             throws java.io.IOException {
87         log.println("Copy File "+src+" to "+dst);
88         File srcF = new File(src) ;
89         File dstF = new File(dst) ;
90 
91         if (dstF.exists()) {
92             boolean bDeleteOk = dstF.delete();
93             if (!bDeleteOk) {
94                 System.out.println("delete failed");
95             }
96         }
97 
98         boolean bCreateOk = dstF.createNewFile();
99         if (!bCreateOk) {
100             System.out.println("create failed");
101         }
102 
103         dstF.deleteOnExit() ;
104 
105         FileInputStream fIn = new FileInputStream(srcF) ;
106         FileOutputStream fOut = new FileOutputStream(dstF) ;
107 
108         byte[] buf = new byte[1024] ;
109         int bytesRead = 0 ;
110         while ((bytesRead = fIn.read(buf)) > 0) {
111             fOut.write(buf, 0, bytesRead) ;
112         }
113 
114         fIn.close() ;
115         fOut.close() ;
116     }
117 
118 
119     /**
120     * Creating a TestEnvironment for the interfaces to be tested.
121     * Creates two temporary copies of registry file created before, opens
122     * them, and creates service <code>com.sun.star.comp.stoc.NestedRegistry</code>
123     * with these two registries. <p>
124     * Object relations created :
125     * <ul>
126     *  <li> <code>'NR'</code> for {@link ifc.registry._XSimpleRegistry} :
127     *     Just informs interface test that <code>NestedRegistry</code>
128     *     service is tested. If this relation exists, then some methods
129     *     are not supported. The relation is a <code>String</code> with
130     *     object name.</li>
131     *  <li> <code>'XSimpleRegistry.open'</code> for
132     *    {@link ifc.registry._XSimpleRegistry}
133     *  </li>
134     *  <li> <code>'XSimpleRegistry.destroy'</code> for
135     *    {@link ifc.registry._XSimpleRegistry}
136     *  </li>
137     *  <li> <code>'XSimpleRegistry.merge'</code> for
138     *    {@link ifc.registry._XSimpleRegistry}
139     *  </li>
140     * </ul>
141     */
142     @Override
createTestEnvironment( TestParameters Param, PrintWriter log )143     public TestEnvironment createTestEnvironment( TestParameters Param,
144                                                   PrintWriter log )
145                                                     throws Exception {
146         XInterface oObj = null;
147         Object oInterface = null;
148 
149         final String tmpDir = utils.getOfficeTempDirSys(Param.getMSF()) ;
150         final String openF = tmpDir + "XSimpleRegistry_open" + uniq + ".rdb" ;
151         final String destroyF = tmpDir
152             + "XSimpleRegistry_destroy" + uniq + ".rdb" ;
153         final String mergeF = tmpDir + "XSimpleRegistry_merge" + uniq + ".rdb" ;
154         uniq++ ;
155 
156         log.println("creating copies of the registry for XSimpleRegistry");
157         String source = utils.getFullTestDocName("XSimpleRegistry.rdb");
158         copyFile(source, openF, log) ;
159         copyFile(source, mergeF, log) ;
160 
161         try {
162             XMultiServiceFactory xMSF = Param.getMSF();
163             reg1 = RegistryTools.
164                     createRegistryService(xMSF) ;
165             reg1.open(mergeF, false, true) ;
166             reg2 = RegistryTools.
167                     createRegistryService(xMSF) ;
168             reg2.open(openF, false, true) ;
169             XSimpleRegistry[] arg = new XSimpleRegistry[2];
170             arg[0]=reg1;
171             arg[1]=reg2;
172             oInterface = xMSF.createInstanceWithArguments
173                 ( "com.sun.star.comp.stoc.NestedRegistry", arg );
174         }
175         catch( Exception e ) {
176             log.println("Introspection Service not available" );
177         }
178         oObj = (XInterface) oInterface;
179 
180 
181 
182         log.println( "    creating a new environment for Introspection object" );
183         TestEnvironment tEnv = new TestEnvironment( oObj );
184 
185         tEnv.addObjRelation("NR","NestedRegistry");
186 
187         tEnv.addObjRelation("XSimpleRegistry.open", openF) ;
188         tEnv.addObjRelation("XSimpleRegistry.merge", mergeF) ;
189         tEnv.addObjRelation("XSimpleRegistry.destroy", destroyF) ;
190 
191         return tEnv;
192     } // finish method getTestEnvironment
193 
194     @Override
cleanup( TestParameters Param, PrintWriter log)195     protected void cleanup( TestParameters Param, PrintWriter log) {
196 
197 
198     }
199 
200 }    // finish class NestedRegistry
201 
202