1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package mod._simreg;
29 
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.FileOutputStream;
33 import java.io.PrintWriter;
34 
35 import lib.StatusException;
36 import lib.TestCase;
37 import lib.TestEnvironment;
38 import lib.TestParameters;
39 import util.utils;
40 
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.uno.XInterface;
43 
44 /**
45  * Test for object which is represented by service
46  * <code>com.sun.star.registry.SimpleRegistry</code>. <p>
47  * Object implements the following interfaces :
48  * <ul>
49  *  <li> <code>com::sun::star::registry::XSimpleRegistry</code></li>
50  * </ul> <p>
51  * The following files used by this test :
52  * <ul>
53  *  <li><b> XSimpleRegistry.rdb </b> : a registry file with
54  *   some key set. </li>
55  * </ul> <p>
56  * This object test <b> is NOT </b> designed to be run in several
57  * threads concurently.
58  *
59  * @see com.sun.star.registry.XSimpleRegistry
60  * @see ifc.registry._XSimpleRegistry
61  */
62 public class SimpleRegistry extends TestCase {
63 
64     /**
65     * Creates a temporary copy of file, which is deleted when VM exits.
66     * @param src Source file path.
67     * @param dst Destination file path.
68     * @param log The log writer.
69     * @throws java.io.IOException If any problems occur during copiing.
70     */
71     protected void copyFile(String src, String dst, PrintWriter log)
72             throws java.io.IOException {
73         File srcF = new File(src) ;
74         File dstF = new File(dst) ;
75         log.println("H1");
76 
77         if (dstF.exists()) dstF.delete() ;
78         log.println("H2");
79         dstF.createNewFile() ;
80 
81         dstF.deleteOnExit() ;
82         log.println("H3");
83 
84         FileInputStream fIn = new FileInputStream(srcF) ;
85         System.out.println("H4");
86 
87         FileOutputStream fOut = new FileOutputStream(dstF) ;
88 
89         byte[] buf = new byte[1024] ;
90         int bytesRead = 0 ;
91         while ((bytesRead = fIn.read(buf)) > 0)
92             fOut.write(buf, 0, bytesRead) ;
93 
94         fIn.close() ;
95         fOut.close() ;
96     }
97     /**
98      * Creating a Testenvironment for the interfaces to be tested.
99      * Creates an instance of the service
100      * <code>com.sun.star.registry.SimpleRegistry</code>. Then
101      * makes three copies of a predefined registry file with different
102      * names in a temporary SOffice directory and passes their URLs as
103      * relations. <p>
104      *
105      *     Object relations created :
106      * <ul>
107      *  <li> <code>'XSimpleRegistry.open'</code> for
108      *      {@link ifc.registry._XSimpleRegistry} :
109      *       URL of 'XSimpleRegistry.rbd' file copy in the
110      *       temp directory. </li>
111      *  <li> <code>'XSimpleRegistry.merge'</code> for
112      *      {@link ifc.registry._XSimpleRegistry} :
113      *       URL of 'XSimpleRegistry.rbd' file copy in the
114      *       temp directory. </li>
115      *  <li> <code>'XSimpleRegistry.destroy'</code> for
116      *      {@link ifc.registry._XSimpleRegistry} :
117      *       URL of 'XSimpleRegistry.rbd' file copy in the
118      *       temp directory. </li>
119      * </ul>
120      */
121     public TestEnvironment createTestEnvironment( TestParameters Param,
122                                                   PrintWriter log )
123                                                     throws StatusException {
124         XInterface oObj = null;
125         Object oInterface = null;
126         final String tmpDir = utils.getOfficeTempDirSys((XMultiServiceFactory)Param.getMSF()) ;
127         final String openF = "XSimpleRegistry_open.rdb" ;
128         final String destroyF = "XSimpleRegistry_destroy.rdb" ;
129         final String mergeF = "XSimpleRegistry_merge.rdb" ;
130 
131 
132         try {
133             XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF();
134             oInterface = xMSF.createInstance
135                 ( "com.sun.star.registry.SimpleRegistry" );
136         } catch( com.sun.star.uno.Exception e ) {
137             log.println("Service not available" );
138         }
139 
140         if (oInterface == null)
141             log.println("Service wasn't created") ;
142 
143         oObj = (XInterface) oInterface;
144 
145         log.println("creating copies of the registry for XSimpleRegistry");
146        try {
147             String source = utils.getFullTestDocName("XSimpleRegistry.rdb");
148             copyFile(source, tmpDir + openF, log);
149             copyFile(source, tmpDir + destroyF, log);
150             copyFile(source, tmpDir + mergeF, log);
151         } catch (java.io.IOException e) {
152             log.println("Exception occured while copying files");
153             e.printStackTrace(log);
154         }
155 
156         log.println( "    creating a new environment for object" );
157         TestEnvironment tEnv = new TestEnvironment( oObj );
158 
159         tEnv.addObjRelation("XSimpleRegistry.open", tmpDir + openF);
160         tEnv.addObjRelation("XSimpleRegistry.destroy", tmpDir + destroyF);
161         tEnv.addObjRelation("XSimpleRegistry.merge", tmpDir + mergeF);
162 
163         return tEnv;
164     } // finish method getTestEnvironment
165 
166 }    // finish class SimpleRegistry
167 
168