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