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