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