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._defreg; 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.RegistryTools; 36 import util.utils; 37 38 import com.sun.star.lang.XMultiServiceFactory; 39 import com.sun.star.registry.XSimpleRegistry; 40 import com.sun.star.uno.XInterface; 41 42 /** 43 * Test for object which is represented by service 44 * <code>com.sun.star.registry.NestedRegistry</code>. <p> 45 * Object implements the following interfaces : 46 * <ul> 47 * <li> <code>com::sun::star::registry::XSimpleRegistry</code></li> 48 * <li> <code>com::sun::star::lang::XInitialization</code></li> 49 * </ul> 50 * The following files used by this test : 51 * <ul> 52 * <li><b> XSimpleRegistry.rdb </b> : Registry file created before. </li> 53 * <li><b> XSimpleRegistry_open#.rdb </b> : Temporary registry file as copy of 54 * <b> XSimpleRegistry.rdb </b> in the SOffice temp directory. 55 * ('#' - is an ordinary number) </li> 56 * <li><b> XSimpleRegistry_merge#.rdb </b> : Temporary registry file as copy of 57 * <b> XSimpleRegistry.rdb </b> in the SOffice temp directory. 58 * ('#' - is an ordinary number) </li> 59 * </ul> <p> 60 * This object test <b> is NOT </b> designed to be run in several 61 * threads concurently. 62 * @see com.sun.star.registry.XSimpleRegistry 63 * @see com.sun.star.lang.XInitialization 64 * @see ifc.registry._XSimpleRegistry 65 * @see ifc.lang._XInitialization 66 */ 67 public class NestedRegistry extends TestCase { 68 69 protected static int uniq = 0 ; 70 XSimpleRegistry reg1; 71 XSimpleRegistry reg2; 72 disposeTestEnvironment( TestEnvironment tEnv, TestParameters tParam )73 public synchronized void disposeTestEnvironment( TestEnvironment tEnv, 74 TestParameters tParam ) { 75 try { 76 reg1.destroy(); 77 reg2.destroy(); 78 } 79 catch (com.sun.star.registry.InvalidRegistryException e) {} 80 } 81 82 /** 83 * Creates a temporary copy of file, which is deleted when VM exits. 84 * @param src Source file path. 85 * @param dst Destination file path. 86 * @throws java.io.IOException If any problems occur during copiing. 87 */ copyFile(String src, String dst, PrintWriter log)88 protected void copyFile(String src, String dst, PrintWriter log) 89 throws java.io.IOException { 90 log.println("Copy File "+src+" to "+dst); 91 File srcF = new File(src) ; 92 File dstF = new File(dst) ; 93 94 if (dstF.exists()) dstF.delete() ; 95 dstF.createNewFile() ; 96 97 dstF.deleteOnExit() ; 98 99 FileInputStream fIn = new FileInputStream(srcF) ; 100 FileOutputStream fOut = new FileOutputStream(dstF) ; 101 102 byte[] buf = new byte[1024] ; 103 int bytesRead = 0 ; 104 while ((bytesRead = fIn.read(buf)) > 0) 105 fOut.write(buf, 0, bytesRead) ; 106 107 fIn.close() ; 108 fOut.close() ; 109 } 110 111 112 /** 113 * Creating a Testenvironment for the interfaces to be tested. 114 * Creates two temporary copies of registry file created before, opens 115 * them, and creates service <code>com.sun.star.comp.stoc.NestedRegistry</code> 116 * with these two registries. <p> 117 * Object relations created : 118 * <ul> 119 * <li> <code>'NR'</code> for {@link ifc.registry._XSimpleRegistry} : 120 * Just informs interface test that <code>NestedRegistry</code> 121 * service is tested. If this relation exists, than some methods 122 * are not supported. The relation is a <code>String</code> with 123 * object name.</li> 124 * <li> <code>'XSimpleRegistry.open'</code> for 125 * {@link ifc.registry._XSimpleRegistry} 126 * </li> 127 * <li> <code>'XSimpleRegistry.destroy'</code> for 128 * {@link ifc.registry._XSimpleRegistry} 129 * </li> 130 * <li> <code>'XSimpleRegistry.merge'</code> for 131 * {@link ifc.registry._XSimpleRegistry} 132 * </li> 133 * </ul> 134 */ createTestEnvironment( TestParameters Param, PrintWriter log )135 public TestEnvironment createTestEnvironment( TestParameters Param, 136 PrintWriter log ) 137 throws StatusException { 138 XInterface oObj = null; 139 Object oInterface = null; 140 141 final String tmpDir = utils.getOfficeTempDirSys((XMultiServiceFactory)Param.getMSF()) ; 142 final String openF = tmpDir + "XSimpleRegistry_open" + uniq + ".rdb" ; 143 final String destroyF = tmpDir 144 + "XSimpleRegistry_destroy" + uniq + ".rdb" ; 145 final String mergeF = tmpDir + "XSimpleRegistry_merge" + uniq + ".rdb" ; 146 uniq++ ; 147 148 log.println("creating copies of the registry for XSimpleRegistry"); 149 try { 150 String source = utils.getFullTestDocName("XSimpleRegistry.rdb"); 151 copyFile(source, openF, log) ; 152 copyFile(source, mergeF, log) ; 153 } catch (java.io.IOException e) { 154 log.println("Exception occured while copying files"); 155 e.printStackTrace(log); 156 throw new StatusException("Exception occured while copying files", e); 157 } 158 159 try { 160 XMultiServiceFactory xMSF = (XMultiServiceFactory)Param.getMSF(); 161 reg1 = RegistryTools. 162 createRegistryService(xMSF) ; 163 reg1.open(mergeF, false, true) ; 164 reg2 = RegistryTools. 165 createRegistryService(xMSF) ; 166 reg2.open(openF, false, true) ; 167 XSimpleRegistry[] arg = new XSimpleRegistry[2]; 168 arg[0]=reg1; 169 arg[1]=reg2; 170 oInterface = xMSF.createInstanceWithArguments 171 ( "com.sun.star.comp.stoc.NestedRegistry", arg ); 172 } 173 catch( Exception e ) { 174 log.println("Introspection Service not available" ); 175 } 176 oObj = (XInterface) oInterface; 177 178 179 180 log.println( " creating a new environment for Introspection object" ); 181 TestEnvironment tEnv = new TestEnvironment( oObj ); 182 183 tEnv.addObjRelation("NR","NestedRegistry"); 184 185 tEnv.addObjRelation("XSimpleRegistry.open", openF) ; 186 tEnv.addObjRelation("XSimpleRegistry.merge", mergeF) ; 187 tEnv.addObjRelation("XSimpleRegistry.destroy", destroyF) ; 188 189 return tEnv; 190 } // finish method getTestEnvironment 191 cleanup( TestParameters Param, PrintWriter log)192 protected void cleanup( TestParameters Param, PrintWriter log) { 193 194 195 } 196 197 } // finish class NestedRegistry 198 199