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