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