bump product version to 5.0.4.1
[LibreOffice.git] / qadevOOo / tests / java / mod / _dbaccess / OQueryDesign.java
blob255b7f9d16686200152daa5e0fffcbfa2e4c00ac
1 /*
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.container.NoSuchElementException;
21 import com.sun.star.lang.WrappedTargetException;
22 import com.sun.star.sdbc.SQLException;
23 import com.sun.star.sdbc.XConnection;
24 import com.sun.star.uno.Exception;
25 import java.io.PrintWriter;
27 import lib.TestCase;
28 import lib.TestEnvironment;
29 import lib.TestParameters;
30 import util.DesktopTools;
32 import com.sun.star.beans.PropertyValue;
33 import com.sun.star.container.XNameAccess;
34 import com.sun.star.frame.XController;
35 import com.sun.star.frame.XDesktop;
36 import com.sun.star.frame.XDispatch;
37 import com.sun.star.frame.XDispatchProvider;
38 import com.sun.star.frame.XFrame;
39 import com.sun.star.frame.XModel;
40 import com.sun.star.lang.XInitialization;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.text.XTextDocument;
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.uno.XInterface;
45 import com.sun.star.util.URL;
46 import lib.StatusException;
47 import util.SOfficeFactory;
48 import com.sun.star.sdb.XDocumentDataSource;
49 import com.sun.star.sdbc.XDataSource;
51 public class OQueryDesign extends TestCase {
53 private static XDesktop xDesk;
54 private static XFrame xFrame;
55 private static final String sDataSourceName = "Bibliography";
56 private static XConnection xConn;
57 private static XTextDocument xTextDoc;
60 /**
61 * Creates the Desktop service (<code>com.sun.star.frame.Desktop</code>).
63 @Override
64 protected void initialize(TestParameters Param, PrintWriter log) {
65 xDesk = UnoRuntime.queryInterface(
66 XDesktop.class, DesktopTools.createDesktop(Param.getMSF()) );
69 @Override
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 = Param.getMSF();
81 XNameAccess xNameAccess = null;
83 // we use the first datasource
84 XDataSource xDS = null;
85 try {
86 xNameAccess = UnoRuntime.queryInterface(
87 XNameAccess.class,
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 );
93 try {
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 );
103 try {
104 xNameAccess = UnoRuntime.queryInterface(
105 XNameAccess.class,
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");
114 try {
115 xConn = xDS.getConnection("", "");
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";
129 Args[0] = param1;
130 PropertyValue param2 = new PropertyValue();
131 param2.Name = "QueryDesignView";
132 param2.Value = Boolean.FALSE;
133 Args[1] = param2;
134 param1.Name = "ActiveConnection";
135 param1.Value = xConn;
136 Args[1] = param2;
137 getting.dispatch(the_url,Args);
139 util.utils.pause(5000);
141 Object oDBC = null;
143 try {
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;
151 try{
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 xFrame = DesktopTools.getCurrentFrame(xMSF);
163 SOfficeFactory SOF = null;
165 SOF = SOfficeFactory.getFactory( xMSF );
166 try {
167 log.println( "creating a textdocument" );
168 xTextDoc = SOF.createTextDoc( null );
169 } catch ( com.sun.star.uno.Exception e ) {
170 e.printStackTrace( log );
171 throw new StatusException( "Could not create document", e );
174 XModel xDocMod = UnoRuntime.queryInterface(XModel.class, xTextDoc);
176 XFrame xTextFrame = xDocMod.getCurrentController().getFrame();
178 Object[] params = new Object[3];
179 param1 = new PropertyValue();
180 param1.Name = "DataSourceName";
181 param1.Value = "Bibliography";
182 params[0] = param1;
183 param2 = new PropertyValue();
184 param2.Name = "Frame";
185 param2.Value = xTextFrame;
186 params[1] = param2;
187 PropertyValue param3 = new PropertyValue();
188 param3.Name = "QueryDesignView";
189 param3.Value = Boolean.TRUE;
190 params[2] = param3;
193 oObj = xFrame.getController();
195 TestEnvironment tEnv = new TestEnvironment(oObj);
197 //Adding ObjRelations for XInitialization
198 tEnv.addObjRelation("XInitialization.args", params);
200 Object[] ExceptionParams = new Object[3];
201 PropertyValue ExceptionParam1 = new PropertyValue();
202 ExceptionParam1.Name = "DataSourceName";
203 ExceptionParam1.Value = "Bibliography2";
204 ExceptionParams[0] = ExceptionParam1;
205 PropertyValue ExceptionParam2 = new PropertyValue();
206 ExceptionParam2.Name = "Frame";
207 ExceptionParam2.Value = null;
208 ExceptionParams[1] = ExceptionParam2;
209 PropertyValue ExceptionParam3 = new PropertyValue();
210 ExceptionParam3.Name = "QueryDesignView";
211 ExceptionParam3.Value = Integer.valueOf(17);//Boolean.TRUE;
212 ExceptionParams[2] = ExceptionParam3;
214 tEnv.addObjRelation("XInitialization.ExceptionArgs", ExceptionParams);
216 tEnv.addObjRelation("Frame", xFrame);
218 tEnv.addObjRelation("XInitialization.xIni", getUnititializedObj(Param));
220 log.println("ImplementationName: "+util.utils.getImplName(oObj));
222 return tEnv;
224 } // finish method getTestEnvironment
226 private XInitialization getUnititializedObj(TestParameters Param){
227 // creating an object which ist not initialized
229 // get a model of a DataSource
230 Object oDBC = null;
231 XMultiServiceFactory xMSF;
233 try {
234 xMSF = Param.getMSF();
235 oDBC = xMSF.createInstance( "com.sun.star.sdb.DatabaseContext" );
237 catch( com.sun.star.uno.Exception e ) {
238 throw new StatusException("Could not instantiate DatabaseContext", e) ;
241 Object oDataSource = null;
242 try{
243 XNameAccess xNA = UnoRuntime.queryInterface(XNameAccess.class, oDBC);
244 oDataSource = xNA.getByName(sDataSourceName);
245 } catch ( com.sun.star.container.NoSuchElementException e){
246 throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
247 } catch ( com.sun.star.lang.WrappedTargetException e){
248 throw new StatusException("could not get '" + sDataSourceName + "'" , e) ;
251 XDocumentDataSource xDDS = UnoRuntime.queryInterface(XDocumentDataSource.class, oDataSource);
252 XModel xMod = UnoRuntime.queryInterface(XModel.class, xDDS.getDatabaseDocument ());
254 // get an intaces of QueryDesign
255 Object oQueryDesign = null;
256 try{
257 oQueryDesign = xMSF.createInstance("com.sun.star.sdb.QueryDesign");
258 }catch( com.sun.star.uno.Exception e ) {
259 throw new StatusException("Could not instantiate QueryDesign", e) ;
262 XController xCont = UnoRuntime.queryInterface(XController.class, oQueryDesign);
264 // marry them all
265 xCont.attachModel(xMod);
266 xMod.connectController(xCont);
267 try{
268 xMod.setCurrentController(xCont);
269 } catch (com.sun.star.container.NoSuchElementException e){
270 throw new StatusException("Could not set controller", e) ;
273 return UnoRuntime.queryInterface(XInitialization.class, oQueryDesign);
277 @Override
278 protected void cleanup(TestParameters tParam, PrintWriter log) {
279 try {
280 xConn.close() ;
281 DesktopTools.closeDoc(xFrame);
282 DesktopTools.closeDoc(xTextDoc);
283 } catch (com.sun.star.uno.Exception e) {
284 log.println("Can't close the connection") ;
285 e.printStackTrace(log) ;
286 } catch (com.sun.star.lang.DisposedException e) {
287 log.println("Connection was already closed. It's OK.") ;
292 } // finish class oDatasourceBrowser