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
._dbaccess
;
21 import com
.sun
.star
.beans
.PropertyValue
;
22 import com
.sun
.star
.beans
.XPropertySet
;
23 import com
.sun
.star
.frame
.XStorable
;
26 import com
.sun
.star
.uno
.Exception
;
27 import com
.sun
.star
.uno
.UnoRuntime
;
28 import com
.sun
.star
.uno
.XInterface
;
29 import com
.sun
.star
.uno
.XNamingService
;
30 import java
.io
.PrintWriter
;
31 import lib
.StatusException
;
33 import lib
.TestEnvironment
;
34 import lib
.TestParameters
;
35 import util
.DesktopTools
;
38 import com
.sun
.star
.lang
.XMultiServiceFactory
;
39 import com
.sun
.star
.sdb
.XOfficeDatabaseDocument
;
40 import com
.sun
.star
.task
.XInteractionHandler
;
43 * Test for object which is represented by service
44 * <code>com.sun.star.sdb.DataSource</code>. <p>
46 * Object implements the following interfaces :
48 * <li> <code>com::sun::star::sdb::XQueryDefinitionsSupplier</code></li>
49 * <li> <code>com::sun::star::sdb::DataSource</code></li>
50 * <li> <code>com::sun::star::sdb::XCompletedConnection</code></li>
51 * <li> <code>com::sun::star::util::XFlushable</code></li>
52 * <li> <code>com::sun::star::sdb::XBookmarksSupplier</code></li>
53 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
56 * @see com.sun.star.sdb.XQueryDefinitionsSupplier
57 * @see com.sun.star.sdb.XFormDocumentsSupplier
58 * @see com.sun.star.beans.XPropertySet
59 * @see com.sun.star.sdb.DataSource
60 * @see com.sun.star.sdb.XCompletedConnection
61 * @see com.sun.star.util.XFlushable
62 * @see com.sun.star.sdb.XReportDocumentsSupplier
63 * @see ifc.sdb._XQueryDefinitionsSupplier
64 * @see ifc.sdb._XFormDocumentsSupplier
65 * @see ifc.beans._XPropertySet
66 * @see ifc.sdb._DataSource
67 * @see ifc.sdb._XCompletedConnection
68 * @see ifc.util._XFlushable
69 * @see ifc.sdb._XReportDocumentsSupplier
71 public class ODatabaseSource
extends TestCase
{
73 private static int uniqueSuffixStat
= 0 ;
75 private int uniqueSuffix
= 0 ;
76 private XOfficeDatabaseDocument xDBDoc
= null;
79 * Assigns database's name unique suffix for this object depending
80 * on static field. For names differs in different object threads.
83 protected void initialize ( TestParameters Param
, PrintWriter log
) throws Exception
{
84 uniqueSuffix
= uniqueSuffixStat
++ ;
88 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
89 log
.println(" disposing no longer needed docs... ");
90 DesktopTools
.closeDoc(xDBDoc
);
96 * Creating a TestEnvironment for the interfaces to be tested.
98 * Creates new <code>DataSource</code> which represents DBase database
99 * located in temporary directory, and then registers it in service
100 * <code>com.sun.star.sdb.DatabaseContext</code>.
102 * Created object relations :
104 * <li> <code>'XCompletedConnection.Handler'</code> for interface test
105 * <code>XCompletedConnection</code> </li>
107 * <li> <code>'UserAndPassword'</code> for interface test
108 * <code>XIsolatedConnection</code> </li>
110 * @see com.sun.star.sdb.DatabaseContext
111 * @see com.sun.star.sdb.DataSource
114 protected TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) throws Exception
{
115 XMultiServiceFactory xMSF
= Param
.getMSF();
116 Object oInterface
= xMSF
.createInstance( "com.sun.star.sdb.DatabaseContext" );
118 XNamingService xDBContextNameServ
= UnoRuntime
.queryInterface(XNamingService
.class, oInterface
) ;
120 // retrieving temp directory for database
121 String tmpDatabaseUrl
= utils
.getOfficeTempDir(Param
.getMSF());
123 tmpDatabaseUrl
= "sdbc:dbase:file:///" + tmpDatabaseUrl
;
125 XInterface oDatabaseDoc
= (XInterface
) xMSF
.createInstance
126 ("com.sun.star.sdb.OfficeDatabaseDocument") ;
128 if (oDatabaseDoc
== null)
129 throw new StatusException("Could not get service 'com.sun.star.sdb.OfficeDatabaseDocument'", new Exception());
131 xDBDoc
= UnoRuntime
.queryInterface(
132 XOfficeDatabaseDocument
.class,
134 XInterface oObj
= xDBDoc
.getDataSource();
135 log
.println("ImplementationName: " + utils
.getImplName(oObj
));
137 // Creating new DBase data source in the TEMP directory
139 XPropertySet xSrcProp
= UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
141 xSrcProp
.setPropertyValue("URL", tmpDatabaseUrl
) ;
143 String databaseName
= "NewDatabaseSource" + uniqueSuffix
;
145 // make sure that the DatabaseContext isn't already registered
147 xDBContextNameServ
.revokeObject(databaseName
) ;
148 } catch (Exception e
) {
149 log
.println("Nothing to be removed - OK");
152 // registering source in DatabaseContext
153 XStorable store
= UnoRuntime
.queryInterface(XStorable
.class, xDBDoc
);
154 String aFile
= utils
.getOfficeTemp (Param
.getMSF ())+"DataSource.odb";
155 store
.storeAsURL(aFile
,new PropertyValue
[]{});
157 xDBContextNameServ
.registerObject(databaseName
, oObj
) ;
159 log
.println( " creating a new environment for object" );
160 TestEnvironment tEnv
= new TestEnvironment( oObj
);
162 // adding obj relation for interface XCompletedConnection
163 Object handler
= Param
.getMSF().createInstance
164 ("com.sun.star.sdb.InteractionHandler") ;
166 // dbase does not need user and password
167 tEnv
.addObjRelation("UserAndPassword", new String
[]{"",""}) ;
169 tEnv
.addObjRelation("XCompletedConnection.Handler",
170 UnoRuntime
.queryInterface(XInteractionHandler
.class, handler
)) ;
173 } // finish method getTestEnvironment