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 .
18 package mod
._dbaccess
;
20 //import com.sun.star.awt.XControl;
21 //import com.sun.star.awt.XControlModel;
22 import com
.sun
.star
.container
.NoSuchElementException
;
23 import com
.sun
.star
.lang
.WrappedTargetException
;
24 import com
.sun
.star
.sdbc
.SQLException
;
25 import com
.sun
.star
.sdbc
.XConnection
;
26 import com
.sun
.star
.uno
.Exception
;
27 import java
.io
.PrintWriter
;
30 import lib
.TestEnvironment
;
31 import lib
.TestParameters
;
32 import util
.DesktopTools
;
34 import com
.sun
.star
.beans
.PropertyValue
;
35 import com
.sun
.star
.container
.XNameAccess
;
36 import com
.sun
.star
.frame
.XController
;
37 import com
.sun
.star
.frame
.XDesktop
;
38 import com
.sun
.star
.frame
.XDispatch
;
39 import com
.sun
.star
.frame
.XDispatchProvider
;
40 import com
.sun
.star
.frame
.XFrame
;
41 import com
.sun
.star
.frame
.XModel
;
42 import com
.sun
.star
.lang
.XInitialization
;
43 import com
.sun
.star
.lang
.XMultiServiceFactory
;
44 import com
.sun
.star
.text
.XTextDocument
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.uno
.XInterface
;
47 import com
.sun
.star
.util
.URL
;
48 import lib
.StatusException
;
49 import util
.SOfficeFactory
;
50 import com
.sun
.star
.sdb
.XDocumentDataSource
;
51 import com
.sun
.star
.sdbc
.XDataSource
;
53 public class OQueryDesign
extends TestCase
{
55 private static XDesktop xDesk
;
56 private static XFrame xFrame
;
57 private final String sDataSourceName
= "Bibliography";
58 private static XConnection xConn
;
59 private static XTextDocument xTextDoc
;
63 * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
65 protected void initialize(TestParameters Param
, PrintWriter log
) {
66 xDesk
= UnoRuntime
.queryInterface(
67 XDesktop
.class, DesktopTools
.createDesktop((XMultiServiceFactory
)Param
.getMSF()) );
70 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
, PrintWriter log
) {
72 log
.println( "creating a test environment" );
74 XInterface oObj
= null;
76 XDispatchProvider aProv
= UnoRuntime
.queryInterface(XDispatchProvider
.class,xDesk
);
78 XDispatch getting
= null;
79 XMultiServiceFactory xMSF
= (XMultiServiceFactory
) Param
.getMSF();
81 XNameAccess xNameAccess
= null;
83 // we use the first datasource
84 XDataSource xDS
= null;
86 xNameAccess
= UnoRuntime
.queryInterface(
88 xMSF
.createInstance("com.sun.star.sdb.DatabaseContext"));
89 } catch (Exception ex
) {
90 ex
.printStackTrace( log
);
91 throw new StatusException( "Could not get Databasecontext", ex
);
94 xDS
= UnoRuntime
.queryInterface(
95 XDataSource
.class, xNameAccess
.getByName( "Bibliography" ));
96 } catch (NoSuchElementException ex
) {
97 ex
.printStackTrace( log
);
98 throw new StatusException( "Could not get XDataSource", ex
);
99 } catch (WrappedTargetException ex
) {
100 ex
.printStackTrace( log
);
101 throw new StatusException( "Could not get XDataSource", ex
);
104 xNameAccess
= UnoRuntime
.queryInterface(
106 xMSF
.createInstance("com.sun.star.sdb.DatabaseContext"));
107 } catch (Exception ex
) {
108 ex
.printStackTrace( log
);
109 throw new StatusException( "Could not get DatabaseConext", ex
);
112 log
.println("check XMultiServiceFactory");
115 xConn
= xDS
.getConnection(new String(), new String());
116 } catch (SQLException ex
) {
117 ex
.printStackTrace( log
);
118 throw new StatusException( "Could not get XConnection", ex
);
121 log
.println( "opening QueryDesign" );
122 URL the_url
= new URL();
123 the_url
.Complete
= ".component:DB/QueryDesign";
124 getting
= aProv
.queryDispatch(the_url
,"Query",12);
125 PropertyValue
[] Args
= new PropertyValue
[2];
126 PropertyValue param1
= new PropertyValue();
127 param1
.Name
= "DataSourceName";
128 param1
.Value
= "Bibliography";
130 PropertyValue param2
= new PropertyValue();
131 param2
.Name
= "QueryDesignView";
132 param2
.Value
= new Boolean(false);
134 param1
.Name
= "ActiveConnection";
135 param1
.Value
= xConn
;
137 getting
.dispatch(the_url
,Args
);
144 oDBC
= xMSF
.createInstance( "com.sun.star.sdb.DatabaseContext" );
146 catch( com
.sun
.star
.uno
.Exception e
) {
147 throw new StatusException("Could not instantiate DatabaseContext", e
) ;
150 Object oDataSource
= null;
152 XNameAccess xNA
= UnoRuntime
.queryInterface(XNameAccess
.class, oDBC
);
153 oDataSource
= xNA
.getByName(sDataSourceName
);
154 } catch ( com
.sun
.star
.container
.NoSuchElementException e
){
155 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
156 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
){
157 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
159 UnoRuntime
.queryInterface(XDocumentDataSource
.class, oDataSource
);
161 // Frame = xMod.getCurrentController().getFrame();
163 xFrame
= DesktopTools
.getCurrentFrame(xMSF
);
165 SOfficeFactory SOF
= null;
167 SOF
= SOfficeFactory
.getFactory( xMSF
);
169 log
.println( "creating a textdocument" );
170 xTextDoc
= SOF
.createTextDoc( null );
171 } catch ( com
.sun
.star
.uno
.Exception e
) {
172 e
.printStackTrace( log
);
173 throw new StatusException( "Could not create document", e
);
176 XModel xDocMod
= UnoRuntime
.queryInterface(XModel
.class, xTextDoc
);
178 XFrame xTextFrame
= xDocMod
.getCurrentController().getFrame();
180 Object
[] params
= new Object
[3];
181 param1
= new PropertyValue();
182 param1
.Name
= "DataSourceName";
183 param1
.Value
= "Bibliography";
185 param2
= new PropertyValue();
186 param2
.Name
= "Frame";
187 param2
.Value
= xTextFrame
;
189 PropertyValue param3
= new PropertyValue();
190 param3
.Name
= "QueryDesignView";
191 param3
.Value
= new Boolean(true);
195 oObj
= xFrame
.getController();
197 TestEnvironment tEnv
= new TestEnvironment(oObj
);
199 //Adding ObjRelations for XInitialization
200 tEnv
.addObjRelation("XInitialization.args", params
);
202 //Object[] ExceptionParams = new Object[3];
203 //ExceptionParams = params;
204 //((PropertyValue) ExceptionParams[1]).Value = Frame;
205 Object
[] ExceptionParams
= new Object
[3];
206 PropertyValue ExceptionParam1
= new PropertyValue();
207 ExceptionParam1
.Name
= "DataSourceName";
208 ExceptionParam1
.Value
= "Bibliography2";
209 ExceptionParams
[0] = ExceptionParam1
;
210 PropertyValue ExceptionParam2
= new PropertyValue();
211 ExceptionParam2
.Name
= "Frame";
212 ExceptionParam2
.Value
= null;
213 ExceptionParams
[1] = ExceptionParam2
;
214 PropertyValue ExceptionParam3
= new PropertyValue();
215 ExceptionParam3
.Name
= "QueryDesignView";
216 ExceptionParam3
.Value
= new Integer(17);//new Boolean(true);
217 ExceptionParams
[2] = ExceptionParam3
;
219 tEnv
.addObjRelation("XInitialization.ExceptionArgs", ExceptionParams
);
221 tEnv
.addObjRelation("Frame", xFrame
);
223 tEnv
.addObjRelation("XInitialization.xIni", getUnititializedObj(Param
));
225 log
.println("ImplementationName: "+util
.utils
.getImplName(oObj
));
229 } // finish method getTestEnvironment
231 private XInitialization
getUnititializedObj(TestParameters Param
){
232 // creating an object which ist not initialized
234 // get a model of a DataSource
236 XMultiServiceFactory xMSF
;
239 xMSF
= (XMultiServiceFactory
)Param
.getMSF();
240 oDBC
= xMSF
.createInstance( "com.sun.star.sdb.DatabaseContext" );
242 catch( com
.sun
.star
.uno
.Exception e
) {
243 throw new StatusException("Could not instantiate DatabaseContext", e
) ;
246 Object oDataSource
= null;
248 XNameAccess xNA
= UnoRuntime
.queryInterface(XNameAccess
.class, oDBC
);
249 oDataSource
= xNA
.getByName(sDataSourceName
);
250 } catch ( com
.sun
.star
.container
.NoSuchElementException e
){
251 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
252 } catch ( com
.sun
.star
.lang
.WrappedTargetException e
){
253 throw new StatusException("could not get '" + sDataSourceName
+ "'" , e
) ;
256 XDocumentDataSource xDDS
= UnoRuntime
.queryInterface(XDocumentDataSource
.class, oDataSource
);
257 XModel xMod
= UnoRuntime
.queryInterface(XModel
.class, xDDS
.getDatabaseDocument ());
259 // get an intaces of QueryDesign
260 Object oQueryDesign
= null;
262 oQueryDesign
= xMSF
.createInstance("com.sun.star.sdb.QueryDesign");
263 }catch( com
.sun
.star
.uno
.Exception e
) {
264 throw new StatusException("Could not instantiate QueryDesign", e
) ;
267 XController xCont
= UnoRuntime
.queryInterface(XController
.class, oQueryDesign
);
270 xCont
.attachModel(xMod
);
271 xMod
.connectController(xCont
);
273 xMod
.setCurrentController(xCont
);
274 } catch (com
.sun
.star
.container
.NoSuchElementException e
){
275 throw new StatusException("Could not set controller", e
) ;
278 //xCont.attachFrame(xFrame);
280 return UnoRuntime
.queryInterface(XInitialization
.class, oQueryDesign
);
285 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
288 DesktopTools
.closeDoc(xFrame
);
289 DesktopTools
.closeDoc(xTextDoc
);
290 } catch (com
.sun
.star
.uno
.Exception e
) {
291 log
.println("Can't close the connection") ;
292 e
.printStackTrace(log
) ;
293 } catch (com
.sun
.star
.lang
.DisposedException e
) {
294 log
.println("Connection was already closed. It's OK.") ;
300 * Sleeps for 0.2 sec. to allow StarOffice to react on <code>
303 private void shortWait() {
306 } catch (InterruptedException e
) {
307 System
.out
.println("While waiting :" + e
) ;
311 } // finish class oDatasourceBrowser