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