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