1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package mod._simplereg.uno;
25 
26 import com.sun.star.lang.XMultiServiceFactory;
27 import com.sun.star.uno.XInterface;
28 import java.io.File;
29 import java.io.FileInputStream;
30 import java.io.FileOutputStream;
31 import java.io.PrintWriter;
32 import lib.TestCase;
33 import lib.TestEnvironment;
34 import lib.TestParameters;
35 import util.utils;
36 
37 /**
38  * Test for object which is represented by service
39  * <code>com.sun.star.registry.SimpleRegistry</code>. <p>
40  * Object implements the following interfaces :
41  * <ul>
42  *  <li> <code>com::sun::star::registry::XSimpleRegistry</code></li>
43  * </ul> <p>
44  * The following files used by this test :
45  * <ul>
46  *  <li><b> XSimpleRegistry.rdb </b> : a registry file with
47  *   some key set. </li>
48  * </ul> <p>
49  * This object test <b> is NOT </b> designed to be run in several
50  * threads concurently.
51  *
52  * @see com.sun.star.registry.XSimpleRegistry
53  * @see ifc.registry._XSimpleRegistry
54  */
55 public class SimpleRegistry extends TestCase {
56 
57     /**
58     * Creates a temporary copy of file, which is deleted when VM exits.
59     * @param src Source file path.
60     * @param dst Destination file path.
61     * @param log The log writer.
62     * @throws java.io.IOException If any problems occur during copiing.
63     */
copyFile(String src, String dst, PrintWriter log)64     protected void copyFile(String src, String dst, PrintWriter log)
65             throws java.io.IOException {
66         File srcF = new File(src) ;
67         File dstF = new File(dst) ;
68         System.out.println("H1");
69 
70         if (dstF.exists()) dstF.delete() ;
71         System.out.println("H2");
72         dstF.createNewFile() ;
73 
74         dstF.deleteOnExit() ;
75         System.out.println("H3");
76 
77         FileInputStream fIn = new FileInputStream(srcF) ;
78         System.out.println("H4");
79 
80         FileOutputStream fOut = new FileOutputStream(dstF) ;
81 
82         byte[] buf = new byte[1024] ;
83         int bytesRead = 0 ;
84         while ((bytesRead = fIn.read(buf)) > 0)
85             fOut.write(buf, 0, bytesRead) ;
86 
87         fIn.close() ;
88         fOut.close() ;
89     }
90     /**
91      * Creating a Testenvironment for the interfaces to be tested.
92      * Creates an instance of the service
93      * <code>com.sun.star.registry.SimpleRegistry</code>. Then
94      * makes three copies of a predefined registry file with different
95      * names in a temporary SOffice directory and passes their URLs as
96      * relations. <p>
97      *
98      *     Object relations created :
99      * <ul>
100      *  <li> <code>'XSimpleRegistry.open'</code> for
101      *      {@link ifc.registry._XSimpleRegistry} :
102      *       URL of 'XSimpleRegistry.rbd' file copy in the
103      *       temp directory. </li>
104      *  <li> <code>'XSimpleRegistry.merge'</code> for
105      *      {@link ifc.registry._XSimpleRegistry} :
106      *       URL of 'XSimpleRegistry.rbd' file copy in the
107      *       temp directory. </li>
108      *  <li> <code>'XSimpleRegistry.destroy'</code> for
109      *      {@link ifc.registry._XSimpleRegistry} :
110      *       URL of 'XSimpleRegistry.rbd' file copy in the
111      *       temp directory. </li>
112      * </ul>
113      */
createTestEnvironment(TestParameters Param, PrintWriter log)114     protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
115         XInterface oObj = null;
116         Object oInterface = null;
117         final String tmpDir = utils.getOfficeTempDirSys(
118                                 (XMultiServiceFactory)Param.getMSF()) ;
119         final String openF = "XSimpleRegistry_open.rdb" ;
120         final String destroyF = "XSimpleRegistry_destroy.rdb" ;
121         final String mergeF = "XSimpleRegistry_merge.rdb" ;
122 
123 
124         try {
125             XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
126             oInterface = xMSF.createInstance
127                 ( "com.sun.star.registry.SimpleRegistry" );
128         } catch( com.sun.star.uno.Exception e ) {
129             log.println("Service not available" );
130         }
131 
132         if (oInterface == null)
133             log.println("Service wasn't created") ;
134 
135         oObj = (XInterface) oInterface;
136 
137         log.println("creating copies of the registry for XSimpleRegistry");
138        try {
139             String source = utils.getFullTestDocName("XSimpleRegistry.rdb");
140             copyFile(source, tmpDir + openF, log);
141             copyFile(source, tmpDir + destroyF, log);
142             copyFile(source, tmpDir + mergeF, log);
143         } catch (java.io.IOException e) {
144             log.println("Exception occurred while copying files");
145             e.printStackTrace(log);
146         }
147 
148         log.println( "    creating a new environment for object" );
149         TestEnvironment tEnv = new TestEnvironment( oObj );
150 
151         tEnv.addObjRelation("XSimpleRegistry.open", tmpDir + openF);
152         tEnv.addObjRelation("XSimpleRegistry.destroy", tmpDir + destroyF);
153         tEnv.addObjRelation("XSimpleRegistry.merge", tmpDir + mergeF);
154 
155         return tEnv;
156     } // finish method getTestEnvironment
157 
158 }    // finish class SimpleRegistry
159 
160