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._dbaccess; 29 30 import com.sun.star.beans.PropertyValue; 31 import com.sun.star.beans.PropertyVetoException; 32 import com.sun.star.beans.UnknownPropertyException; 33 import com.sun.star.beans.XPropertySet; 34 import com.sun.star.frame.XStorable; 35 36 37 import com.sun.star.uno.Exception; 38 import com.sun.star.uno.UnoRuntime; 39 import com.sun.star.uno.XInterface; 40 import com.sun.star.uno.XNamingService; 41 import java.io.PrintWriter; 42 import lib.StatusException; 43 import lib.TestCase; 44 import lib.TestEnvironment; 45 import lib.TestParameters; 46 import util.DesktopTools; 47 import util.utils; 48 49 import com.sun.star.io.IOException; 50 import com.sun.star.lang.IllegalArgumentException; 51 import com.sun.star.lang.WrappedTargetException; 52 import com.sun.star.lang.XMultiServiceFactory; 53 import com.sun.star.sdb.XOfficeDatabaseDocument; 54 import com.sun.star.task.XInteractionHandler; 55 56 /** 57 * Test for object which is represented by service 58 * <code>com.sun.star.sdb.DataSource</code>. <p> 59 * 60 * Object implements the following interfaces : 61 * <ul> 62 * <li> <code>com::sun::star::sdb::XQueryDefinitionsSupplier</code></li> 63 * <li> <code>com::sun::star::sdb::DataSource</code></li> 64 * <li> <code>com::sun::star::sdb::XCompletedConnection</code></li> 65 * <li> <code>com::sun::star::util::XFlushable</code></li> 66 * <li> <code>com::sun::star::sdb::XBookmarksSupplier</code></li> 67 * <li> <code>com::sun::star::beans::XPropertySet</code></li> 68 * </ul> 69 * 70 * @see com.sun.star.sdb.XQueryDefinitionSupplier 71 * @see com.sun.star.sdb.XFormDocumentsSupplier 72 * @see com.sun.star.beans.XPropertySet 73 * @see com.sun.star.sdb.DataSource 74 * @see com.sun.star.sdb.XCompletedConnection 75 * @see com.sun.star.util.XFlushable 76 * @see com.sun.star.sdb.XReportDocumentsSupplier 77 * @see ifc.sdb._XQueryDefinitionSupplier 78 * @see ifc.sdb._XFormDocumentsSupplier 79 * @see ifc.beans._XPropertySet 80 * @see ifc.sdb._DataSource 81 * @see ifc.sdb._XCompletedConnection 82 * @see ifc.util._XFlushable 83 * @see ifc.sdb._XReportDocumentsSupplier 84 */ 85 public class ODatabaseSource extends TestCase { 86 87 private static int uniqueSuffixStat = 0 ; 88 89 private int uniqueSuffix = 0 ; 90 private XNamingService xDBContextNameServ = null ; 91 private String databaseName = null ; 92 private XOfficeDatabaseDocument xDBDoc = null; 93 94 /** 95 * Assigns database's name unique suffix for this object depending 96 * on static field. For names differs in different object threads. 97 */ 98 protected void initialize ( TestParameters Param, PrintWriter log) { 99 uniqueSuffix = uniqueSuffixStat++ ; 100 } 101 102 protected void cleanup(TestParameters tParam, PrintWriter log) { 103 log.println(" disposing not longer needed docs... "); 104 DesktopTools.closeDoc(xDBDoc); 105 } 106 107 108 109 /** 110 * Creating a Testenvironment for the interfaces to be tested. 111 * 112 * Creates new <code>DataSource</code> which represents DBase database 113 * located in temporary directory, and then registers it in service 114 * <code>com.sun.star.sdb.DatabaseContext</code>. 115 * 116 * Created object relations : 117 * <ul> 118 * <li> <code>'XCompletedConnection.Handler'</code> for interface test 119 * <code>XCompletedConnection</code> </li> 120 * </ul> 121 * <li> <code>'UserAndPassword'</code> for interface test 122 * <code>XIsolatedConnection</code> </li> 123 * </ul> 124 * @see com.sun.star.sdb.DatabaseContext 125 * @see com.sun.star.sdb.DataSource 126 */ 127 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 128 XInterface oObj = null; 129 Object oInterface = null; 130 XMultiServiceFactory xMSF = null ; 131 132 xMSF = (XMultiServiceFactory)Param.getMSF(); 133 try{ 134 oInterface = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" ); 135 136 if (oInterface == null) 137 throw new StatusException("Could not get service 'com.sun.star.sdb.DatabaseContext'", new Exception()); 138 139 }catch( Exception e ) { 140 log.println("Could not get service 'com.sun.star.sdb.DatabaseContext'" ); 141 e.printStackTrace (); 142 throw new StatusException("Service not available", e) ; 143 } 144 145 xDBContextNameServ = (XNamingService) 146 UnoRuntime.queryInterface(XNamingService.class, oInterface) ; 147 148 // retrieving temp directory for database 149 String tmpDatabaseUrl = utils.getOfficeTempDir((XMultiServiceFactory)Param.getMSF()); 150 151 tmpDatabaseUrl = "sdbc:dbase:file:///" + tmpDatabaseUrl ; 152 153 try{ 154 XInterface oDatabaseDoc = (XInterface) xMSF.createInstance 155 ("com.sun.star.sdb.OfficeDatabaseDocument") ; 156 157 if (oDatabaseDoc == null) 158 throw new StatusException("Could not get service 'com.sun.star.sdb.OfficeDatabaseDocument'", new Exception()); 159 160 xDBDoc = (XOfficeDatabaseDocument) UnoRuntime.queryInterface( 161 XOfficeDatabaseDocument.class, 162 oDatabaseDoc); 163 } 164 catch( Exception e ) { 165 log.println("Could not get service 'com.sun.star.sdb.OfficeDatabaseDocument'" ); 166 e.printStackTrace (); 167 throw new StatusException("Service not available", e) ; 168 } 169 170 oObj = (XInterface) xDBDoc.getDataSource(); 171 log.println("ImplementationName: " + utils.getImplName(oObj)); 172 173 // Creating new DBase data source in the TEMP directory 174 175 XPropertySet xSrcProp = (XPropertySet) 176 UnoRuntime.queryInterface(XPropertySet.class, oObj); 177 178 try{ 179 xSrcProp.setPropertyValue("URL", tmpDatabaseUrl) ; 180 } catch ( UnknownPropertyException e){ 181 throw new StatusException("Could not set property 'URL' ", e) ; 182 } catch ( PropertyVetoException e){ 183 throw new StatusException("Could not set property 'URL' ", e) ; 184 } catch ( IllegalArgumentException e){ 185 throw new StatusException("Could not set property 'URL' ", e) ; 186 } catch ( WrappedTargetException e){ 187 throw new StatusException("Could not set property 'URL' ", e) ; 188 } 189 190 databaseName = "NewDatabaseSource" + uniqueSuffix ; 191 192 // make sure that the DatabaseContext isn't already registered 193 try { 194 xDBContextNameServ.revokeObject(databaseName) ; 195 } catch (Exception e) { 196 log.println("Nothing to be removed - OK"); 197 } 198 199 // registering source in DatabaseContext 200 XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, xDBDoc); 201 String aFile = utils.getOfficeTemp ((XMultiServiceFactory) Param.getMSF ())+"DataSource.odb"; 202 try{ 203 store.storeAsURL(aFile,new PropertyValue[]{}); 204 } catch (IOException e){ 205 log.println("Could not store datasource 'aFile'" ); 206 e.printStackTrace (); 207 throw new StatusException("Could not save ", e) ; 208 } catch (Exception e){ 209 log.println("Could not store datasource 'aFile'" ); 210 e.printStackTrace (); 211 throw new StatusException("Could not save ", e) ; 212 } 213 214 215 try{ 216 xDBContextNameServ.registerObject(databaseName, oObj) ; 217 } catch (Exception e){ 218 log.println("Could not register data source" ); 219 e.printStackTrace (); 220 throw new StatusException("Could not register ", e) ; 221 } 222 223 log.println( " creating a new environment for object" ); 224 TestEnvironment tEnv = new TestEnvironment( oObj ); 225 226 // adding obj relation for interface XCompletedConnection 227 Object handler = null ; 228 try { 229 handler = ((XMultiServiceFactory)Param.getMSF()).createInstance 230 ("com.sun.star.sdb.InteractionHandler") ; 231 } catch (Exception e) { 232 log.println("Relation for XCompletedConnection wasn't created") ; 233 e.printStackTrace(log) ; 234 } 235 236 // dbase does not need user and password 237 tEnv.addObjRelation("UserAndPassword", new String[]{"",""}) ; 238 239 tEnv.addObjRelation("XCompletedConnection.Handler", 240 UnoRuntime.queryInterface(XInteractionHandler.class, handler)) ; 241 242 return tEnv; 243 } // finish method getTestEnvironment 244 245 } 246