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