xref: /AOO42X/main/qadevOOo/tests/java/mod/_dbaccess/OQueryDesign.java (revision fdd107f4c8c7f3abbb61e9cedafdd70e212e016c)
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.awt.XControl;
27 //import com.sun.star.awt.XControlModel;
28 import com.sun.star.container.NoSuchElementException;
29 import com.sun.star.lang.WrappedTargetException;
30 import com.sun.star.sdbc.SQLException;
31 import com.sun.star.sdbc.XConnection;
32 import com.sun.star.uno.Exception;
33 import java.io.PrintWriter;
34 
35 import lib.TestCase;
36 import lib.TestEnvironment;
37 import lib.TestParameters;
38 import util.DesktopTools;
39 
40 import com.sun.star.beans.PropertyValue;
41 import com.sun.star.container.XNameAccess;
42 import com.sun.star.frame.XController;
43 import com.sun.star.frame.XDesktop;
44 import com.sun.star.frame.XDispatch;
45 import com.sun.star.frame.XDispatchProvider;
46 import com.sun.star.frame.XFrame;
47 import com.sun.star.frame.XModel;
48 import com.sun.star.lang.XInitialization;
49 import com.sun.star.lang.XMultiServiceFactory;
50 import com.sun.star.text.XTextDocument;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XInterface;
53 import com.sun.star.util.URL;
54 import lib.StatusException;
55 import util.SOfficeFactory;
56 import com.sun.star.sdb.XDocumentDataSource;
57 import com.sun.star.sdbc.XDataSource;
58 
59 public class OQueryDesign extends TestCase {
60 
61     private static XDesktop xDesk;
62     private static XFrame xFrame;
63     private final String sDataSourceName = "Bibliography";
64     private static XConnection xConn;
65     private static XTextDocument xTextDoc;
66 
67 
68     /**
69      * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
70      */
71     protected void initialize(TestParameters Param, PrintWriter log) {
72         xDesk = (XDesktop) UnoRuntime.queryInterface(
73                     XDesktop.class, DesktopTools.createDesktop((XMultiServiceFactory)Param.getMSF()) );
74     }
75 
76     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, PrintWriter log) {
77 
78         log.println( "creating a test environment" );
79 
80         XInterface oObj = null;
81 
82         XDispatchProvider aProv = (XDispatchProvider)
83                 UnoRuntime.queryInterface(XDispatchProvider.class,xDesk);
84 
85         XDispatch getting = null;
86         XMultiServiceFactory xMSF = (XMultiServiceFactory) Param.getMSF();
87 
88             XNameAccess xNameAccess = null;
89 
90             // we use the first DataSource
91             XDataSource xDS = null;
92         try {
93             xNameAccess = (XNameAccess) UnoRuntime.queryInterface(
94                         XNameAccess.class,
95                         xMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
96         } catch (Exception ex) {
97             ex.printStackTrace( log );
98             throw new StatusException( "Could not get Databasecontext", ex );
99         }
100         try {
101             xDS = (XDataSource) UnoRuntime.queryInterface(
102                     XDataSource.class, xNameAccess.getByName( "Bibliography" ));
103         } catch (NoSuchElementException ex) {
104             ex.printStackTrace( log );
105             throw new StatusException( "Could not get XDataSource", ex );
106         } catch (WrappedTargetException ex) {
107             ex.printStackTrace( log );
108             throw new StatusException( "Could not get XDataSource", ex );
109         }
110         try {
111             xNameAccess = (XNameAccess) UnoRuntime.queryInterface(
112                         XNameAccess.class,
113                         xMSF.createInstance("com.sun.star.sdb.DatabaseContext"));
114         } catch (Exception ex) {
115             ex.printStackTrace( log );
116             throw new StatusException( "Could not get DatabaseConext", ex );
117         }
118 
119             log.println("check XMultiServiceFactory");
120 
121         try {
122             xConn = xDS.getConnection(new String(), new String());
123         } catch (SQLException ex) {
124             ex.printStackTrace( log );
125             throw new StatusException( "Could not get XConnection", ex );
126         }
127 
128         log.println( "opening QueryDesign" );
129         URL the_url = new URL();
130         the_url.Complete = ".component:DB/QueryDesign";
131         getting = aProv.queryDispatch(the_url,"Query",12);
132         PropertyValue[] Args = new PropertyValue[2];
133         PropertyValue param1 = new PropertyValue();
134         param1.Name = "DataSourceName";
135         param1.Value = "Bibliography";
136         Args[0] = param1;
137         PropertyValue param2 = new PropertyValue();
138         param2.Name = "QueryDesignView";
139         param2.Value = new Boolean(false);
140         Args[1] = param2;
141         param1.Name = "ActiveConnection";
142         param1.Value = xConn;
143         Args[1] = param2;
144         getting.dispatch(the_url,Args);
145 
146         shortWait();
147 
148         Object oDBC = null;
149 
150         try {
151             oDBC = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
152         }
153         catch( com.sun.star.uno.Exception e ) {
154             throw new StatusException("Could not instantiate DatabaseContext", e) ;
155         }
156 
157         Object oDataSource = null;
158         try{
159             XNameAccess xNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oDBC);
160             oDataSource = xNA.getByName(sDataSourceName);
161         } catch ( com.sun.star.container.NoSuchElementException e){
162             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
163         } catch ( com.sun.star.lang.WrappedTargetException e){
164             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
165         }
166         XDocumentDataSource xDDS = (XDocumentDataSource) UnoRuntime.queryInterface(XDocumentDataSource.class, oDataSource);
167 //      XModel xMod = (XModel) UnoRuntime.queryInterface(XModel.class, xDDS.getDatabaseDocument ());
168 
169 //      Frame = xMod.getCurrentController().getFrame();
170 
171         xFrame = DesktopTools.getCurrentFrame(xMSF);
172 
173          // get an instance of Frame
174         Object oFrame = null;
175         SOfficeFactory SOF = null;
176 
177         SOF = SOfficeFactory.getFactory( xMSF );
178         try {
179             log.println( "creating a textdocument" );
180             xTextDoc = SOF.createTextDoc( null );
181         } catch ( com.sun.star.uno.Exception e ) {
182             e.printStackTrace( log );
183             throw new StatusException( "Could not create document", e );
184         }
185 
186         XModel xDocMod = (XModel) UnoRuntime.queryInterface(XModel.class, xTextDoc);
187 
188         XFrame xTextFrame = xDocMod.getCurrentController().getFrame();
189 
190         Object[] params = new Object[3];
191         param1 = new PropertyValue();
192         param1.Name = "DataSourceName";
193         param1.Value = "Bibliography";
194         params[0] = param1;
195         param2 = new PropertyValue();
196         param2.Name = "Frame";
197         param2.Value = xTextFrame;
198         params[1] = param2;
199         PropertyValue param3 = new PropertyValue();
200         param3.Name = "QueryDesignView";
201         param3.Value = new Boolean(true);
202         params[2] = param3;
203 
204 
205         oObj = xFrame.getController();
206 
207         TestEnvironment tEnv = new TestEnvironment(oObj);
208 
209         //Adding ObjRelations for XInitialization
210         tEnv.addObjRelation("XInitialization.args", params);
211 
212         //Object[] ExceptionParams = new Object[3];
213         //ExceptionParams = params;
214         //((PropertyValue) ExceptionParams[1]).Value = Frame;
215         Object[] ExceptionParams = new Object[3];
216         PropertyValue ExceptionParam1 = new PropertyValue();
217         ExceptionParam1.Name = "DataSourceName";
218         ExceptionParam1.Value = "Bibliography2";
219         ExceptionParams[0] = ExceptionParam1;
220         PropertyValue ExceptionParam2 = new PropertyValue();
221         ExceptionParam2.Name = "Frame";
222         ExceptionParam2.Value = null;
223         ExceptionParams[1] = ExceptionParam2;
224         PropertyValue ExceptionParam3 = new PropertyValue();
225         ExceptionParam3.Name = "QueryDesignView";
226         ExceptionParam3.Value = new Integer(17); // new Boolean(true);
227         ExceptionParams[2] = ExceptionParam3;
228 
229         tEnv.addObjRelation("XInitialization.ExceptionArgs", ExceptionParams);
230 
231         tEnv.addObjRelation("Frame", xFrame);
232 
233         tEnv.addObjRelation("XInitialization.xIni", getUninitializedObj(Param));
234 
235         log.println("ImplementationName: "+util.utils.getImplName(oObj));
236 
237         return tEnv;
238 
239     } // finish method getTestEnvironment
240 
241     private XInitialization getUninitializedObj(TestParameters Param){
242         // create an object which is not initialized
243 
244         // get a model of a DataSource
245         Object oDBC = null;
246         XMultiServiceFactory xMSF;
247 
248         try {
249             xMSF = (XMultiServiceFactory)Param.getMSF();
250             oDBC = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
251         }
252         catch( com.sun.star.uno.Exception e ) {
253             throw new StatusException("Could not instantiate DatabaseContext", e) ;
254         }
255 
256         Object oDataSource = null;
257         try{
258             XNameAccess xNA = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oDBC);
259             oDataSource = xNA.getByName(sDataSourceName);
260         } catch ( com.sun.star.container.NoSuchElementException e){
261             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
262         } catch ( com.sun.star.lang.WrappedTargetException e){
263             throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
264         }
265 
266         XDocumentDataSource xDDS = (XDocumentDataSource) UnoRuntime.queryInterface(XDocumentDataSource.class, oDataSource);
267         XModel xMod = (XModel) UnoRuntime.queryInterface(XModel.class, xDDS.getDatabaseDocument ());
268 
269         // get an instance of QueryDesign
270         Object oQueryDesign = null;
271         try{
272             oQueryDesign = xMSF.createInstance("com.sun.star.sdb.QueryDesign");
273         }catch( com.sun.star.uno.Exception e ) {
274             throw new StatusException("Could not instantiate QueryDesign", e) ;
275         }
276 
277         XController xCont = (XController) UnoRuntime.queryInterface(XController.class, oQueryDesign);
278 
279         // marry them all
280         xCont.attachModel(xMod);
281         xMod.connectController(xCont);
282         try{
283             xMod.setCurrentController(xCont);
284         } catch (com.sun.star.container.NoSuchElementException e){
285             throw new StatusException("Could not set controller", e) ;
286         }
287 
288         //xCont.attachFrame(xFrame);
289 
290         return (XInitialization) UnoRuntime.queryInterface(XInitialization.class, oQueryDesign);
291 
292     }
293 
294     @Override
295     protected void cleanup(TestParameters tParam, PrintWriter log) {
296         try {
297             xConn.close() ;
298             DesktopTools.closeDoc(xFrame);
299             DesktopTools.closeDoc(xTextDoc);
300         } catch (com.sun.star.uno.Exception e) {
301             log.println("Can't close the connection") ;
302             e.printStackTrace(log) ;
303         } catch (com.sun.star.lang.DisposedException e) {
304             log.println("Connection was already closed. It's OK.") ;
305         }
306 
307     }
308 
309     /**
310     * Sleeps for 0.2 sec. to allow OpenOffice to react on <code>
311     * reset</code> call.
312     */
313     private void shortWait() {
314         try {
315             Thread.sleep(5000) ;
316         } catch (InterruptedException e) {
317             System.out.println("While waiting :" + e) ;
318         }
319     }
320 
321 }   // finish class oDatasourceBrowser
322