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._simreg;
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.utils;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
38cdf0e10cSrcweir import com.sun.star.uno.XInterface;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir /**
41cdf0e10cSrcweir  * Test for object which is represented by service
42cdf0e10cSrcweir  * <code>com.sun.star.registry.SimpleRegistry</code>. <p>
43cdf0e10cSrcweir  * Object implements the following interfaces :
44cdf0e10cSrcweir  * <ul>
45cdf0e10cSrcweir  *  <li> <code>com::sun::star::registry::XSimpleRegistry</code></li>
46cdf0e10cSrcweir  * </ul> <p>
47cdf0e10cSrcweir  * The following files used by this test :
48cdf0e10cSrcweir  * <ul>
49cdf0e10cSrcweir  *  <li><b> XSimpleRegistry.rdb </b> : a registry file with
50cdf0e10cSrcweir  *   some key set. </li>
51cdf0e10cSrcweir  * </ul> <p>
52cdf0e10cSrcweir  * This object test <b> is NOT </b> designed to be run in several
53cdf0e10cSrcweir  * threads concurently.
54cdf0e10cSrcweir  *
55cdf0e10cSrcweir  * @see com.sun.star.registry.XSimpleRegistry
56cdf0e10cSrcweir  * @see ifc.registry._XSimpleRegistry
57cdf0e10cSrcweir  */
58cdf0e10cSrcweir public class SimpleRegistry extends TestCase {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     /**
61cdf0e10cSrcweir     * Creates a temporary copy of file, which is deleted when VM exits.
62cdf0e10cSrcweir     * @param src Source file path.
63cdf0e10cSrcweir     * @param dst Destination file path.
64cdf0e10cSrcweir     * @param log The log writer.
65cdf0e10cSrcweir     * @throws java.io.IOException If any problems occur during copiing.
66cdf0e10cSrcweir     */
copyFile(String src, String dst, PrintWriter log)67cdf0e10cSrcweir     protected void copyFile(String src, String dst, PrintWriter log)
68cdf0e10cSrcweir             throws java.io.IOException {
69cdf0e10cSrcweir         File srcF = new File(src) ;
70cdf0e10cSrcweir         File dstF = new File(dst) ;
71cdf0e10cSrcweir         log.println("H1");
72cdf0e10cSrcweir 
73cdf0e10cSrcweir         if (dstF.exists()) dstF.delete() ;
74cdf0e10cSrcweir         log.println("H2");
75cdf0e10cSrcweir         dstF.createNewFile() ;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         dstF.deleteOnExit() ;
78cdf0e10cSrcweir         log.println("H3");
79cdf0e10cSrcweir 
80cdf0e10cSrcweir         FileInputStream fIn = new FileInputStream(srcF) ;
81cdf0e10cSrcweir         System.out.println("H4");
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         FileOutputStream fOut = new FileOutputStream(dstF) ;
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         byte[] buf = new byte[1024] ;
86cdf0e10cSrcweir         int bytesRead = 0 ;
87cdf0e10cSrcweir         while ((bytesRead = fIn.read(buf)) > 0)
88cdf0e10cSrcweir             fOut.write(buf, 0, bytesRead) ;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         fIn.close() ;
91cdf0e10cSrcweir         fOut.close() ;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir     /**
94cdf0e10cSrcweir      * Creating a Testenvironment for the interfaces to be tested.
95cdf0e10cSrcweir      * Creates an instance of the service
96cdf0e10cSrcweir      * <code>com.sun.star.registry.SimpleRegistry</code>. Then
97cdf0e10cSrcweir      * makes three copies of a predefined registry file with different
98cdf0e10cSrcweir      * names in a temporary SOffice directory and passes their URLs as
99cdf0e10cSrcweir      * relations. <p>
100cdf0e10cSrcweir      *
101cdf0e10cSrcweir      *     Object relations created :
102cdf0e10cSrcweir      * <ul>
103cdf0e10cSrcweir      *  <li> <code>'XSimpleRegistry.open'</code> for
104cdf0e10cSrcweir      *      {@link ifc.registry._XSimpleRegistry} :
105cdf0e10cSrcweir      *       URL of 'XSimpleRegistry.rbd' file copy in the
106cdf0e10cSrcweir      *       temp directory. </li>
107cdf0e10cSrcweir      *  <li> <code>'XSimpleRegistry.merge'</code> for
108cdf0e10cSrcweir      *      {@link ifc.registry._XSimpleRegistry} :
109cdf0e10cSrcweir      *       URL of 'XSimpleRegistry.rbd' file copy in the
110cdf0e10cSrcweir      *       temp directory. </li>
111cdf0e10cSrcweir      *  <li> <code>'XSimpleRegistry.destroy'</code> for
112cdf0e10cSrcweir      *      {@link ifc.registry._XSimpleRegistry} :
113cdf0e10cSrcweir      *       URL of 'XSimpleRegistry.rbd' file copy in the
114cdf0e10cSrcweir      *       temp directory. </li>
115cdf0e10cSrcweir      * </ul>
116cdf0e10cSrcweir      */
createTestEnvironment( TestParameters Param, PrintWriter log )117cdf0e10cSrcweir     public TestEnvironment createTestEnvironment( TestParameters Param,
118cdf0e10cSrcweir                                                   PrintWriter log )
119cdf0e10cSrcweir                                                     throws StatusException {
120cdf0e10cSrcweir         XInterface oObj = null;
121cdf0e10cSrcweir         Object oInterface = null;
122cdf0e10cSrcweir         final String tmpDir = utils.getOfficeTempDirSys((XMultiServiceFactory)Param.getMSF()) ;
123cdf0e10cSrcweir         final String openF = "XSimpleRegistry_open.rdb" ;
124cdf0e10cSrcweir         final String destroyF = "XSimpleRegistry_destroy.rdb" ;
125cdf0e10cSrcweir         final String mergeF = "XSimpleRegistry_merge.rdb" ;
126cdf0e10cSrcweir 
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         try {
129cdf0e10cSrcweir             XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
130cdf0e10cSrcweir             oInterface = xMSF.createInstance
131cdf0e10cSrcweir                 ( "com.sun.star.registry.SimpleRegistry" );
132cdf0e10cSrcweir         } catch( com.sun.star.uno.Exception e ) {
133cdf0e10cSrcweir             log.println("Service not available" );
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         if (oInterface == null)
137cdf0e10cSrcweir             log.println("Service wasn't created") ;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         oObj = (XInterface) oInterface;
140cdf0e10cSrcweir 
141cdf0e10cSrcweir         log.println("creating copies of the registry for XSimpleRegistry");
142cdf0e10cSrcweir        try {
143cdf0e10cSrcweir             String source = utils.getFullTestDocName("XSimpleRegistry.rdb");
144cdf0e10cSrcweir             copyFile(source, tmpDir + openF, log);
145cdf0e10cSrcweir             copyFile(source, tmpDir + destroyF, log);
146cdf0e10cSrcweir             copyFile(source, tmpDir + mergeF, log);
147cdf0e10cSrcweir         } catch (java.io.IOException e) {
148cdf0e10cSrcweir             log.println("Exception occured while copying files");
149cdf0e10cSrcweir             e.printStackTrace(log);
150cdf0e10cSrcweir         }
151cdf0e10cSrcweir 
152cdf0e10cSrcweir         log.println( "    creating a new environment for object" );
153cdf0e10cSrcweir         TestEnvironment tEnv = new TestEnvironment( oObj );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         tEnv.addObjRelation("XSimpleRegistry.open", tmpDir + openF);
156cdf0e10cSrcweir         tEnv.addObjRelation("XSimpleRegistry.destroy", tmpDir + destroyF);
157cdf0e10cSrcweir         tEnv.addObjRelation("XSimpleRegistry.merge", tmpDir + mergeF);
158cdf0e10cSrcweir 
159cdf0e10cSrcweir         return tEnv;
160cdf0e10cSrcweir     } // finish method getTestEnvironment
161cdf0e10cSrcweir 
162cdf0e10cSrcweir }    // finish class SimpleRegistry
163cdf0e10cSrcweir 
164