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