1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir package mod._dbaccess; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 31*cdf0e10cSrcweir import com.sun.star.beans.PropertyVetoException; 32*cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException; 33*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 34*cdf0e10cSrcweir import com.sun.star.frame.XStorable; 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir import com.sun.star.uno.Exception; 38*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 39*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 40*cdf0e10cSrcweir import com.sun.star.uno.XNamingService; 41*cdf0e10cSrcweir import java.io.PrintWriter; 42*cdf0e10cSrcweir import lib.StatusException; 43*cdf0e10cSrcweir import lib.TestCase; 44*cdf0e10cSrcweir import lib.TestEnvironment; 45*cdf0e10cSrcweir import lib.TestParameters; 46*cdf0e10cSrcweir import util.DesktopTools; 47*cdf0e10cSrcweir import util.utils; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir import com.sun.star.io.IOException; 50*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException; 51*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException; 52*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 53*cdf0e10cSrcweir import com.sun.star.sdb.XOfficeDatabaseDocument; 54*cdf0e10cSrcweir import com.sun.star.task.XInteractionHandler; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir /** 57*cdf0e10cSrcweir * Test for object which is represented by service 58*cdf0e10cSrcweir * <code>com.sun.star.sdb.DataSource</code>. <p> 59*cdf0e10cSrcweir * 60*cdf0e10cSrcweir * Object implements the following interfaces : 61*cdf0e10cSrcweir * <ul> 62*cdf0e10cSrcweir * <li> <code>com::sun::star::sdb::XQueryDefinitionsSupplier</code></li> 63*cdf0e10cSrcweir * <li> <code>com::sun::star::sdb::DataSource</code></li> 64*cdf0e10cSrcweir * <li> <code>com::sun::star::sdb::XCompletedConnection</code></li> 65*cdf0e10cSrcweir * <li> <code>com::sun::star::util::XFlushable</code></li> 66*cdf0e10cSrcweir * <li> <code>com::sun::star::sdb::XBookmarksSupplier</code></li> 67*cdf0e10cSrcweir * <li> <code>com::sun::star::beans::XPropertySet</code></li> 68*cdf0e10cSrcweir * </ul> 69*cdf0e10cSrcweir * 70*cdf0e10cSrcweir * @see com.sun.star.sdb.XQueryDefinitionSupplier 71*cdf0e10cSrcweir * @see com.sun.star.sdb.XFormDocumentsSupplier 72*cdf0e10cSrcweir * @see com.sun.star.beans.XPropertySet 73*cdf0e10cSrcweir * @see com.sun.star.sdb.DataSource 74*cdf0e10cSrcweir * @see com.sun.star.sdb.XCompletedConnection 75*cdf0e10cSrcweir * @see com.sun.star.util.XFlushable 76*cdf0e10cSrcweir * @see com.sun.star.sdb.XReportDocumentsSupplier 77*cdf0e10cSrcweir * @see ifc.sdb._XQueryDefinitionSupplier 78*cdf0e10cSrcweir * @see ifc.sdb._XFormDocumentsSupplier 79*cdf0e10cSrcweir * @see ifc.beans._XPropertySet 80*cdf0e10cSrcweir * @see ifc.sdb._DataSource 81*cdf0e10cSrcweir * @see ifc.sdb._XCompletedConnection 82*cdf0e10cSrcweir * @see ifc.util._XFlushable 83*cdf0e10cSrcweir * @see ifc.sdb._XReportDocumentsSupplier 84*cdf0e10cSrcweir */ 85*cdf0e10cSrcweir public class ODatabaseSource extends TestCase { 86*cdf0e10cSrcweir 87*cdf0e10cSrcweir private static int uniqueSuffixStat = 0 ; 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir private int uniqueSuffix = 0 ; 90*cdf0e10cSrcweir private XNamingService xDBContextNameServ = null ; 91*cdf0e10cSrcweir private String databaseName = null ; 92*cdf0e10cSrcweir private XOfficeDatabaseDocument xDBDoc = null; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir /** 95*cdf0e10cSrcweir * Assigns database's name unique suffix for this object depending 96*cdf0e10cSrcweir * on static field. For names differs in different object threads. 97*cdf0e10cSrcweir */ 98*cdf0e10cSrcweir protected void initialize ( TestParameters Param, PrintWriter log) { 99*cdf0e10cSrcweir uniqueSuffix = uniqueSuffixStat++ ; 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir protected void cleanup(TestParameters tParam, PrintWriter log) { 103*cdf0e10cSrcweir log.println(" disposing not longer needed docs... "); 104*cdf0e10cSrcweir DesktopTools.closeDoc(xDBDoc); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir /** 110*cdf0e10cSrcweir * Creating a Testenvironment for the interfaces to be tested. 111*cdf0e10cSrcweir * 112*cdf0e10cSrcweir * Creates new <code>DataSource</code> which represents DBase database 113*cdf0e10cSrcweir * located in temporary directory, and then registers it in service 114*cdf0e10cSrcweir * <code>com.sun.star.sdb.DatabaseContext</code>. 115*cdf0e10cSrcweir * 116*cdf0e10cSrcweir * Created object relations : 117*cdf0e10cSrcweir * <ul> 118*cdf0e10cSrcweir * <li> <code>'XCompletedConnection.Handler'</code> for interface test 119*cdf0e10cSrcweir * <code>XCompletedConnection</code> </li> 120*cdf0e10cSrcweir * </ul> 121*cdf0e10cSrcweir * <li> <code>'UserAndPassword'</code> for interface test 122*cdf0e10cSrcweir * <code>XIsolatedConnection</code> </li> 123*cdf0e10cSrcweir * </ul> 124*cdf0e10cSrcweir * @see com.sun.star.sdb.DatabaseContext 125*cdf0e10cSrcweir * @see com.sun.star.sdb.DataSource 126*cdf0e10cSrcweir */ 127*cdf0e10cSrcweir protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) { 128*cdf0e10cSrcweir XInterface oObj = null; 129*cdf0e10cSrcweir Object oInterface = null; 130*cdf0e10cSrcweir XMultiServiceFactory xMSF = null ; 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir xMSF = (XMultiServiceFactory)Param.getMSF(); 133*cdf0e10cSrcweir try{ 134*cdf0e10cSrcweir oInterface = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" ); 135*cdf0e10cSrcweir 136*cdf0e10cSrcweir if (oInterface == null) 137*cdf0e10cSrcweir throw new StatusException("Could not get service 'com.sun.star.sdb.DatabaseContext'", new Exception()); 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir }catch( Exception e ) { 140*cdf0e10cSrcweir log.println("Could not get service 'com.sun.star.sdb.DatabaseContext'" ); 141*cdf0e10cSrcweir e.printStackTrace (); 142*cdf0e10cSrcweir throw new StatusException("Service not available", e) ; 143*cdf0e10cSrcweir } 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir xDBContextNameServ = (XNamingService) 146*cdf0e10cSrcweir UnoRuntime.queryInterface(XNamingService.class, oInterface) ; 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir // retrieving temp directory for database 149*cdf0e10cSrcweir String tmpDatabaseUrl = utils.getOfficeTempDir((XMultiServiceFactory)Param.getMSF()); 150*cdf0e10cSrcweir 151*cdf0e10cSrcweir tmpDatabaseUrl = "sdbc:dbase:file:///" + tmpDatabaseUrl ; 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir try{ 154*cdf0e10cSrcweir XInterface oDatabaseDoc = (XInterface) xMSF.createInstance 155*cdf0e10cSrcweir ("com.sun.star.sdb.OfficeDatabaseDocument") ; 156*cdf0e10cSrcweir 157*cdf0e10cSrcweir if (oDatabaseDoc == null) 158*cdf0e10cSrcweir throw new StatusException("Could not get service 'com.sun.star.sdb.OfficeDatabaseDocument'", new Exception()); 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir xDBDoc = (XOfficeDatabaseDocument) UnoRuntime.queryInterface( 161*cdf0e10cSrcweir XOfficeDatabaseDocument.class, 162*cdf0e10cSrcweir oDatabaseDoc); 163*cdf0e10cSrcweir } 164*cdf0e10cSrcweir catch( Exception e ) { 165*cdf0e10cSrcweir log.println("Could not get service 'com.sun.star.sdb.OfficeDatabaseDocument'" ); 166*cdf0e10cSrcweir e.printStackTrace (); 167*cdf0e10cSrcweir throw new StatusException("Service not available", e) ; 168*cdf0e10cSrcweir } 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir oObj = (XInterface) xDBDoc.getDataSource(); 171*cdf0e10cSrcweir log.println("ImplementationName: " + utils.getImplName(oObj)); 172*cdf0e10cSrcweir 173*cdf0e10cSrcweir // Creating new DBase data source in the TEMP directory 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir XPropertySet xSrcProp = (XPropertySet) 176*cdf0e10cSrcweir UnoRuntime.queryInterface(XPropertySet.class, oObj); 177*cdf0e10cSrcweir 178*cdf0e10cSrcweir try{ 179*cdf0e10cSrcweir xSrcProp.setPropertyValue("URL", tmpDatabaseUrl) ; 180*cdf0e10cSrcweir } catch ( UnknownPropertyException e){ 181*cdf0e10cSrcweir throw new StatusException("Could not set property 'URL' ", e) ; 182*cdf0e10cSrcweir } catch ( PropertyVetoException e){ 183*cdf0e10cSrcweir throw new StatusException("Could not set property 'URL' ", e) ; 184*cdf0e10cSrcweir } catch ( IllegalArgumentException e){ 185*cdf0e10cSrcweir throw new StatusException("Could not set property 'URL' ", e) ; 186*cdf0e10cSrcweir } catch ( WrappedTargetException e){ 187*cdf0e10cSrcweir throw new StatusException("Could not set property 'URL' ", e) ; 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir databaseName = "NewDatabaseSource" + uniqueSuffix ; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir // make sure that the DatabaseContext isn't already registered 193*cdf0e10cSrcweir try { 194*cdf0e10cSrcweir xDBContextNameServ.revokeObject(databaseName) ; 195*cdf0e10cSrcweir } catch (Exception e) { 196*cdf0e10cSrcweir log.println("Nothing to be removed - OK"); 197*cdf0e10cSrcweir } 198*cdf0e10cSrcweir 199*cdf0e10cSrcweir // registering source in DatabaseContext 200*cdf0e10cSrcweir XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, xDBDoc); 201*cdf0e10cSrcweir String aFile = utils.getOfficeTemp ((XMultiServiceFactory) Param.getMSF ())+"DataSource.odb"; 202*cdf0e10cSrcweir try{ 203*cdf0e10cSrcweir store.storeAsURL(aFile,new PropertyValue[]{}); 204*cdf0e10cSrcweir } catch (IOException e){ 205*cdf0e10cSrcweir log.println("Could not store datasource 'aFile'" ); 206*cdf0e10cSrcweir e.printStackTrace (); 207*cdf0e10cSrcweir throw new StatusException("Could not save ", e) ; 208*cdf0e10cSrcweir } catch (Exception e){ 209*cdf0e10cSrcweir log.println("Could not store datasource 'aFile'" ); 210*cdf0e10cSrcweir e.printStackTrace (); 211*cdf0e10cSrcweir throw new StatusException("Could not save ", e) ; 212*cdf0e10cSrcweir } 213*cdf0e10cSrcweir 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir try{ 216*cdf0e10cSrcweir xDBContextNameServ.registerObject(databaseName, oObj) ; 217*cdf0e10cSrcweir } catch (Exception e){ 218*cdf0e10cSrcweir log.println("Could not register data source" ); 219*cdf0e10cSrcweir e.printStackTrace (); 220*cdf0e10cSrcweir throw new StatusException("Could not register ", e) ; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir 223*cdf0e10cSrcweir log.println( " creating a new environment for object" ); 224*cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment( oObj ); 225*cdf0e10cSrcweir 226*cdf0e10cSrcweir // adding obj relation for interface XCompletedConnection 227*cdf0e10cSrcweir Object handler = null ; 228*cdf0e10cSrcweir try { 229*cdf0e10cSrcweir handler = ((XMultiServiceFactory)Param.getMSF()).createInstance 230*cdf0e10cSrcweir ("com.sun.star.sdb.InteractionHandler") ; 231*cdf0e10cSrcweir } catch (Exception e) { 232*cdf0e10cSrcweir log.println("Relation for XCompletedConnection wasn't created") ; 233*cdf0e10cSrcweir e.printStackTrace(log) ; 234*cdf0e10cSrcweir } 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir // dbase does not need user and password 237*cdf0e10cSrcweir tEnv.addObjRelation("UserAndPassword", new String[]{"",""}) ; 238*cdf0e10cSrcweir 239*cdf0e10cSrcweir tEnv.addObjRelation("XCompletedConnection.Handler", 240*cdf0e10cSrcweir UnoRuntime.queryInterface(XInteractionHandler.class, handler)) ; 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir return tEnv; 243*cdf0e10cSrcweir } // finish method getTestEnvironment 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir } 246