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