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