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