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 10ef39d40dSAndrew Rist * 11ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12ef39d40dSAndrew Rist * 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. 19ef39d40dSAndrew Rist * 20ef39d40dSAndrew Rist *************************************************************/ 21ef39d40dSAndrew Rist 22ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package mod._dbaccess; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.io.PrintWriter; 27cdf0e10cSrcweir 28cdf0e10cSrcweir import lib.StatusException; 29cdf0e10cSrcweir import lib.TestCase; 30cdf0e10cSrcweir import lib.TestEnvironment; 31cdf0e10cSrcweir import lib.TestParameters; 32cdf0e10cSrcweir import util.DBTools; 33cdf0e10cSrcweir import util.utils; 34cdf0e10cSrcweir 35cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 36cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 37cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 38cdf0e10cSrcweir import com.sun.star.sdb.CommandType; 39cdf0e10cSrcweir import com.sun.star.sdb.XSingleSelectQueryAnalyzer; 40cdf0e10cSrcweir import com.sun.star.sdb.XSingleSelectQueryComposer; 41cdf0e10cSrcweir import com.sun.star.sdbc.XConnection; 42cdf0e10cSrcweir import com.sun.star.sdbc.XDataSource; 43cdf0e10cSrcweir import com.sun.star.sdbcx.XColumnsSupplier; 44cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 45cdf0e10cSrcweir import com.sun.star.uno.Type; 46cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 47cdf0e10cSrcweir import com.sun.star.uno.XInterface; 48cdf0e10cSrcweir 49cdf0e10cSrcweir /** 50cdf0e10cSrcweir * Test for object which is represented by service 51cdf0e10cSrcweir * <code>com.sun.star.sdb.DataSource</code>. <p> 52cdf0e10cSrcweir * 53cdf0e10cSrcweir * Object implements the following interfaces : 54cdf0e10cSrcweir * <ul> 55cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::RowSet</code></li> 56cdf0e10cSrcweir * <li> <code>com::sun::star::sdbcx::XRowLocate</code></li> 57cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XResultSetUpdate</code></li> 58cdf0e10cSrcweir * <li> <code>com::sun::star::util::XCancellable</code></li> 59cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XParameters</code></li> 60cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XResultSetMetaDataSupplier</code></li> 61cdf0e10cSrcweir * <li> <code>com::sun::star::sdbcx::XDeleteRows</code></li> 62cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XCloseable</code></li> 63cdf0e10cSrcweir * <li> <code>com::sun::star::sdbcx::XColumnsSupplier</code></li> 64cdf0e10cSrcweir * <li> <code>com::sun::star::sdb::XResultSetAccess</code></li> 65cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XResultSet</code></li> 66cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XColumnLocate</code></li> 67cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XRowSet</code></li> 68cdf0e10cSrcweir * <li> <code>com::sun::star::sdb::RowSet</code></li> 69cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XRowUpdate</code></li> 70cdf0e10cSrcweir * <li> <code>com::sun::star::sdb::XRowSetApproveBroadcaster</code></li> 71cdf0e10cSrcweir * <li> <code>com::sun::star::beans::XPropertySet</code></li> 72cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XRow</code></li> 73cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::XWarningsSupplier</code></li> 74cdf0e10cSrcweir * <li> <code>com::sun::star::lang::XComponent</code></li> 75cdf0e10cSrcweir * <li> <code>com::sun::star::sdbcx::ResultSet</code></li> 76cdf0e10cSrcweir * <li> <code>com::sun::star::sdbc::ResultSet</code></li> 77cdf0e10cSrcweir * </ul> <p> 78cdf0e10cSrcweir * The following files used by this test : 79cdf0e10cSrcweir * <ul> 80cdf0e10cSrcweir * <li><b> TestDB/TestDB.dbf </b> : the database file with some 81cdf0e10cSrcweir * predefined fields described in <code>util.DBTools</code>. 82cdf0e10cSrcweir * The copy of this file is always made in temp directory for 83cdf0e10cSrcweir * testing purposes.</li> 84cdf0e10cSrcweir * </ul> <p> 85cdf0e10cSrcweir * The following parameters in ini-file used by this test: 86cdf0e10cSrcweir * <ul> 87cdf0e10cSrcweir * <li><code>test.db.url</code> - URL to MySQL database. 88cdf0e10cSrcweir * For example: <code>mysql://mercury:3306/api_current</code></li> 89cdf0e10cSrcweir * <li><code>test.db.user</code> - user for MySQL database</li> 90cdf0e10cSrcweir * <li><code>test.db.password</code> - password for MySQL database</li> 91cdf0e10cSrcweir * </ul><p> 92cdf0e10cSrcweir * 93cdf0e10cSrcweir * @see com.sun.star.sdbc.RowSet 94cdf0e10cSrcweir * @see com.sun.star.sdbcx.XRowLocate 95cdf0e10cSrcweir * @see com.sun.star.sdbc.XResultSetUpdate 96cdf0e10cSrcweir * @see com.sun.star.util.XCancellable 97cdf0e10cSrcweir * @see com.sun.star.sdbc.XParameters 98cdf0e10cSrcweir * @see com.sun.star.sdbc.XResultSetMetaDataSupplier 99cdf0e10cSrcweir * @see com.sun.star.sdbcx.XDeleteRows 100cdf0e10cSrcweir * @see com.sun.star.sdbc.XCloseable 101cdf0e10cSrcweir * @see com.sun.star.sdbcx.XColumnsSupplier 102cdf0e10cSrcweir * @see com.sun.star.sdb.XResultSetAccess 103cdf0e10cSrcweir * @see com.sun.star.sdbc.XResultSet 104cdf0e10cSrcweir * @see com.sun.star.sdbc.XColumnLocate 105cdf0e10cSrcweir * @see com.sun.star.sdbc.XRowSet 106cdf0e10cSrcweir * @see com.sun.star.sdb.RowSet 107cdf0e10cSrcweir * @see com.sun.star.sdbc.XRowUpdate 108cdf0e10cSrcweir * @see com.sun.star.sdb.XRowSetApproveBroadcaster 109cdf0e10cSrcweir * @see com.sun.star.beans.XPropertySet 110cdf0e10cSrcweir * @see com.sun.star.sdbc.XRow 111cdf0e10cSrcweir * @see com.sun.star.sdbc.XWarningsSupplier 112cdf0e10cSrcweir * @see com.sun.star.lang.XComponent 113cdf0e10cSrcweir * @see com.sun.star.sdbcx.ResultSet 114cdf0e10cSrcweir * @see com.sun.star.sdbc.ResultSet 115cdf0e10cSrcweir * @see ifc.sdbc._RowSet 116cdf0e10cSrcweir * @see ifc.sdbcx._XRowLocate 117cdf0e10cSrcweir * @see ifc.sdbc._XResultSetUpdate 118cdf0e10cSrcweir * @see ifc.util._XCancellable 119cdf0e10cSrcweir * @see ifc.sdbc._XParameters 120cdf0e10cSrcweir * @see ifc.sdbc._XResultSetMetaDataSupplier 121cdf0e10cSrcweir * @see ifc.sdbcx._XDeleteRows 122cdf0e10cSrcweir * @see ifc.sdbc._XCloseable 123cdf0e10cSrcweir * @see ifc.sdbcx._XColumnsSupplier 124cdf0e10cSrcweir * @see ifc.sdb._XResultSetAccess 125cdf0e10cSrcweir * @see ifc.sdbc._XResultSet 126cdf0e10cSrcweir * @see ifc.sdbc._XColumnLocate 127cdf0e10cSrcweir * @see ifc.sdbc._XRowSet 128cdf0e10cSrcweir * @see ifc.sdb._RowSet 129cdf0e10cSrcweir * @see ifc.sdbc._XRowUpdate 130cdf0e10cSrcweir * @see ifc.sdb._XRowSetApproveBroadcaster 131cdf0e10cSrcweir * @see ifc.beans._XPropertySet 132cdf0e10cSrcweir * @see ifc.sdbc._XRow 133cdf0e10cSrcweir * @see ifc.sdbc._XWarningsSupplier 134cdf0e10cSrcweir * @see ifc.lang._XComponent 135cdf0e10cSrcweir * @see ifc.sdbcx._ResultSet 136cdf0e10cSrcweir * @see ifc.sdbc._ResultSet 137cdf0e10cSrcweir */ 138cdf0e10cSrcweir public class OSingleSelectQueryComposer extends TestCase { 139cdf0e10cSrcweir 140cdf0e10cSrcweir private static int uniqueSuffix = 0 ; 141cdf0e10cSrcweir private DBTools dbTools = null ; 142cdf0e10cSrcweir private static String origDB = null ; 143cdf0e10cSrcweir private PrintWriter log = null ; 144cdf0e10cSrcweir private static String tmpDir = null ; 145cdf0e10cSrcweir String tableName = null; 146cdf0e10cSrcweir DBTools.DataSourceInfo srcInf = null; 147cdf0e10cSrcweir boolean isMySQLDB = false; 148cdf0e10cSrcweir protected final static String dbSourceName = "OSingleSelectQueryComposerDataSource"; 149cdf0e10cSrcweir public XConnection conn = null; 150cdf0e10cSrcweir 151cdf0e10cSrcweir 152cdf0e10cSrcweir protected void initialize ( TestParameters Param, PrintWriter log) 153cdf0e10cSrcweir throws StatusException { 154cdf0e10cSrcweir 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir /** 158cdf0e10cSrcweir * Creating a Testenvironment for the interfaces to be tested. 159cdf0e10cSrcweir * 160cdf0e10cSrcweir * Object relations created : 161cdf0e10cSrcweir * <ul>SingleSelectQueryAnalyzer 162cdf0e10cSrcweir 163cdf0e10cSrcweir * <li> <code>'xComposer'</code> for 164*e6b649b5SPedro Giffuni * {@link ifc.sdb._XSingleSelectQueryAnalyzer} interface 165cdf0e10cSrcweir * <li> <code>'xQueryAna'</code> for 166*e6b649b5SPedro Giffuni * {@link ifc.sdb._XSingleSelectQueryComposer} interface 167cdf0e10cSrcweir * <li> <code>'xProp'</code> for 168*e6b649b5SPedro Giffuni * {@link ifc.sdb._XSingleSelectQueryComposer} interface 169cdf0e10cSrcweir * <li> <code>'colName'</code> for 170*e6b649b5SPedro Giffuni * {@link ifc.sdb._XSingleSelectQueryComposer} interface 171cdf0e10cSrcweir * </ul> 172cdf0e10cSrcweir * 173cdf0e10cSrcweir */ 174cdf0e10cSrcweir protected TestEnvironment createTestEnvironment(TestParameters Param, 175cdf0e10cSrcweir PrintWriter log) { 176cdf0e10cSrcweir 177cdf0e10cSrcweir XInterface oObj = null; 178cdf0e10cSrcweir Object oInterface = null; 179cdf0e10cSrcweir XMultiServiceFactory xMSF = null ; 180cdf0e10cSrcweir boolean envCreatedOK = false ; 181cdf0e10cSrcweir 182cdf0e10cSrcweir 183cdf0e10cSrcweir try { 184cdf0e10cSrcweir xMSF = (XMultiServiceFactory)Param.getMSF(); 185cdf0e10cSrcweir 186cdf0e10cSrcweir XNameAccess xNameAccess = (XNameAccess)UnoRuntime.queryInterface( 187cdf0e10cSrcweir XNameAccess.class, 188cdf0e10cSrcweir xMSF.createInstance("com.sun.star.sdb.DatabaseContext")); 189cdf0e10cSrcweir // we use the first datasource 190cdf0e10cSrcweir XDataSource xDS = (XDataSource)UnoRuntime.queryInterface( 191cdf0e10cSrcweir XDataSource.class, xNameAccess.getByName( "Bibliography" )); 192cdf0e10cSrcweir 193cdf0e10cSrcweir log.println("check XMultiServiceFactory"); 194cdf0e10cSrcweir XMultiServiceFactory xConn = (XMultiServiceFactory) 195cdf0e10cSrcweir UnoRuntime.queryInterface(XMultiServiceFactory.class, 196cdf0e10cSrcweir xDS.getConnection(new String(),new String())); 197cdf0e10cSrcweir 198cdf0e10cSrcweir log.println("check getAvailableServiceNames"); 199cdf0e10cSrcweir String[] sServiceNames = xConn.getAvailableServiceNames(); 200cdf0e10cSrcweir if (! sServiceNames[0].equals("com.sun.star.sdb.SingleSelectQueryComposer")) 201cdf0e10cSrcweir { 202cdf0e10cSrcweir log.println("Service 'SingleSelectQueryComposer' not supported"); 203cdf0e10cSrcweir } 204cdf0e10cSrcweir 205cdf0e10cSrcweir oInterface = (XInterface) xConn.createInstance( sServiceNames[0]); 206cdf0e10cSrcweir 207cdf0e10cSrcweir if (oInterface == null) { 208cdf0e10cSrcweir log.println("Service wasn't created") ; 209cdf0e10cSrcweir throw new StatusException("Service wasn't created", 210cdf0e10cSrcweir new NullPointerException()) ; 211cdf0e10cSrcweir } 212cdf0e10cSrcweir 213cdf0e10cSrcweir Object oRowSet = xMSF.createInstance("com.sun.star.sdb.RowSet") ; 214cdf0e10cSrcweir 215cdf0e10cSrcweir XPropertySet xSetProp = (XPropertySet) UnoRuntime.queryInterface 216cdf0e10cSrcweir (XPropertySet.class, oRowSet) ; 217cdf0e10cSrcweir 218cdf0e10cSrcweir xSetProp.setPropertyValue("DataSourceName", "Bibliography") ; 219cdf0e10cSrcweir xSetProp.setPropertyValue("Command", "biblio") ; 220cdf0e10cSrcweir xSetProp.setPropertyValue("CommandType", 221cdf0e10cSrcweir new Integer(CommandType.TABLE)) ; 222cdf0e10cSrcweir 223cdf0e10cSrcweir com.sun.star.sdbc.XRowSet xORowSet = (com.sun.star.sdbc.XRowSet) 224cdf0e10cSrcweir UnoRuntime.queryInterface(com.sun.star.sdbc.XRowSet.class, 225cdf0e10cSrcweir oRowSet) ; 226cdf0e10cSrcweir 227cdf0e10cSrcweir xORowSet.execute() ; 228cdf0e10cSrcweir 229cdf0e10cSrcweir XColumnsSupplier xColSup = (XColumnsSupplier) 230cdf0e10cSrcweir UnoRuntime.queryInterface(XColumnsSupplier.class, oRowSet); 231cdf0e10cSrcweir 232cdf0e10cSrcweir XNameAccess xCols = xColSup.getColumns(); 233cdf0e10cSrcweir 234cdf0e10cSrcweir XPropertySet xCol = (XPropertySet) AnyConverter.toObject( 235cdf0e10cSrcweir new Type(XPropertySet.class), 236cdf0e10cSrcweir xCols.getByName(xCols.getElementNames()[0])); 237cdf0e10cSrcweir 238cdf0e10cSrcweir XSingleSelectQueryAnalyzer xQueryAna = (XSingleSelectQueryAnalyzer) 239cdf0e10cSrcweir UnoRuntime.queryInterface(XSingleSelectQueryAnalyzer.class, 240cdf0e10cSrcweir oInterface); 241cdf0e10cSrcweir 242cdf0e10cSrcweir // XSingleSelectQueryComposer 243cdf0e10cSrcweir XSingleSelectQueryComposer xComposer = (XSingleSelectQueryComposer) 244cdf0e10cSrcweir UnoRuntime.queryInterface(XSingleSelectQueryComposer.class, 245cdf0e10cSrcweir xQueryAna); 246cdf0e10cSrcweir xQueryAna.setQuery("SELECT * FROM \"biblio\""); 247cdf0e10cSrcweir 248cdf0e10cSrcweir oObj = (XInterface) oInterface; 249cdf0e10cSrcweir log.println("ImplementationName: " + utils.getImplName(oObj)); 250cdf0e10cSrcweir 251cdf0e10cSrcweir log.println( " creating a new environment for object" ); 252cdf0e10cSrcweir TestEnvironment tEnv = new TestEnvironment( oObj ); 253cdf0e10cSrcweir 254cdf0e10cSrcweir // for XSingleSelectQueryAnalyzer 255cdf0e10cSrcweir tEnv.addObjRelation("xComposer", xComposer); 256cdf0e10cSrcweir 257cdf0e10cSrcweir // for XSingleSelectQueryComposer 258cdf0e10cSrcweir tEnv.addObjRelation("xQueryAna", xQueryAna); 259cdf0e10cSrcweir 260cdf0e10cSrcweir tEnv.addObjRelation("xProp", xCol); 261cdf0e10cSrcweir tEnv.addObjRelation("colName", xCols.getElementNames()[0]); 262cdf0e10cSrcweir 263cdf0e10cSrcweir envCreatedOK = true ; 264cdf0e10cSrcweir return tEnv; 265cdf0e10cSrcweir 266cdf0e10cSrcweir } catch(com.sun.star.uno.Exception e) { 267cdf0e10cSrcweir log.println("Can't create object" ); 268cdf0e10cSrcweir e.printStackTrace(log) ; 269cdf0e10cSrcweir throw new StatusException("Can't create object", e) ; 270cdf0e10cSrcweir } 271cdf0e10cSrcweir 272cdf0e10cSrcweir } // finish method getTestEnvironment 273cdf0e10cSrcweir 274cdf0e10cSrcweir /** 275cdf0e10cSrcweir * Closes connection of <code>RowSet</code> instance created. 276cdf0e10cSrcweir */ 277cdf0e10cSrcweir protected void cleanup( TestParameters Param, PrintWriter log) { 278cdf0e10cSrcweir } 279cdf0e10cSrcweir } 280