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