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