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