Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / mod / _dbaccess / ODatabaseSource.java
blobbff1ecdf22c778f67376c91aca42b361eb265e42
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 XOfficeDatabaseDocument xDBDoc = null;
83 /**
84 * Assigns database's name unique suffix for this object depending
85 * on static field. For names differs in different object threads.
87 @Override
88 protected void initialize ( TestParameters Param, PrintWriter log) {
89 uniqueSuffix = uniqueSuffixStat++ ;
92 @Override
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 @Override
119 protected TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
120 XInterface oObj = null;
121 Object oInterface = null;
122 XMultiServiceFactory xMSF = null ;
124 xMSF = Param.getMSF();
125 try{
126 oInterface = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
128 if (oInterface == null)
129 throw new StatusException("Could not get service 'com.sun.star.sdb.DatabaseContext'", new Exception());
131 }catch( Exception e ) {
132 log.println("Could not get service 'com.sun.star.sdb.DatabaseContext'" );
133 e.printStackTrace ();
134 throw new StatusException("Service not available", e) ;
137 XNamingService xDBContextNameServ = UnoRuntime.queryInterface(XNamingService.class, oInterface) ;
139 // retrieving temp directory for database
140 String tmpDatabaseUrl = utils.getOfficeTempDir(Param.getMSF());
142 tmpDatabaseUrl = "sdbc:dbase:file:///" + tmpDatabaseUrl ;
144 try{
145 XInterface oDatabaseDoc = (XInterface) xMSF.createInstance
146 ("com.sun.star.sdb.OfficeDatabaseDocument") ;
148 if (oDatabaseDoc == null)
149 throw new StatusException("Could not get service 'com.sun.star.sdb.OfficeDatabaseDocument'", new Exception());
151 xDBDoc = UnoRuntime.queryInterface(
152 XOfficeDatabaseDocument.class,
153 oDatabaseDoc);
155 catch( Exception e ) {
156 log.println("Could not get service 'com.sun.star.sdb.OfficeDatabaseDocument'" );
157 e.printStackTrace ();
158 throw new StatusException("Service not available", e) ;
161 oObj = xDBDoc.getDataSource();
162 log.println("ImplementationName: " + utils.getImplName(oObj));
164 // Creating new DBase data source in the TEMP directory
166 XPropertySet xSrcProp = UnoRuntime.queryInterface(XPropertySet.class, oObj);
168 try{
169 xSrcProp.setPropertyValue("URL", tmpDatabaseUrl) ;
170 } catch ( UnknownPropertyException e){
171 throw new StatusException("Could not set property 'URL' ", e) ;
172 } catch ( PropertyVetoException e){
173 throw new StatusException("Could not set property 'URL' ", e) ;
174 } catch ( IllegalArgumentException e){
175 throw new StatusException("Could not set property 'URL' ", e) ;
176 } catch ( WrappedTargetException e){
177 throw new StatusException("Could not set property 'URL' ", e) ;
180 String databaseName = "NewDatabaseSource" + uniqueSuffix ;
182 // make sure that the DatabaseContext isn't already registered
183 try {
184 xDBContextNameServ.revokeObject(databaseName) ;
185 } catch (Exception e) {
186 log.println("Nothing to be removed - OK");
189 // registering source in DatabaseContext
190 XStorable store = UnoRuntime.queryInterface(XStorable.class, xDBDoc);
191 String aFile = utils.getOfficeTemp (Param.getMSF ())+"DataSource.odb";
192 try{
193 store.storeAsURL(aFile,new PropertyValue[]{});
194 } catch (IOException e){
195 log.println("Could not store datasource 'aFile'" );
196 e.printStackTrace ();
197 throw new StatusException("Could not save ", e) ;
200 try{
201 xDBContextNameServ.registerObject(databaseName, oObj) ;
202 } catch (Exception e){
203 log.println("Could not register data source" );
204 e.printStackTrace ();
205 throw new StatusException("Could not register ", e) ;
208 log.println( " creating a new environment for object" );
209 TestEnvironment tEnv = new TestEnvironment( oObj );
211 // adding obj relation for interface XCompletedConnection
212 Object handler = null ;
213 try {
214 handler = Param.getMSF().createInstance
215 ("com.sun.star.sdb.InteractionHandler") ;
216 } catch (Exception e) {
217 log.println("Relation for XCompletedConnection wasn't created") ;
218 e.printStackTrace(log) ;
221 // dbase does not need user and password
222 tEnv.addObjRelation("UserAndPassword", new String[]{"",""}) ;
224 tEnv.addObjRelation("XCompletedConnection.Handler",
225 UnoRuntime.queryInterface(XInteractionHandler.class, handler)) ;
227 return tEnv;
228 } // finish method getTestEnvironment