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