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