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