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