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 import java.util.Vector;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir import lib.Status;
34*cdf0e10cSrcweir import lib.StatusException;
35*cdf0e10cSrcweir import lib.TestCase;
36*cdf0e10cSrcweir import lib.TestEnvironment;
37*cdf0e10cSrcweir import lib.TestParameters;
38*cdf0e10cSrcweir import util.DBTools;
39*cdf0e10cSrcweir import util.utils;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
42*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
43*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
44*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
45*cdf0e10cSrcweir import com.sun.star.sdb.CommandType;
46*cdf0e10cSrcweir import com.sun.star.sdb.ParametersRequest;
47*cdf0e10cSrcweir import com.sun.star.sdb.XInteractionSupplyParameters;
48*cdf0e10cSrcweir import com.sun.star.sdbc.XConnection;
49*cdf0e10cSrcweir import com.sun.star.sdbc.XResultSet;
50*cdf0e10cSrcweir import com.sun.star.sdbc.XResultSetUpdate;
51*cdf0e10cSrcweir import com.sun.star.sdbc.XRow;
52*cdf0e10cSrcweir import com.sun.star.sdbc.XRowSet;
53*cdf0e10cSrcweir import com.sun.star.sdbc.XRowUpdate;
54*cdf0e10cSrcweir import com.sun.star.task.XInteractionAbort;
55*cdf0e10cSrcweir import com.sun.star.task.XInteractionContinuation;
56*cdf0e10cSrcweir import com.sun.star.task.XInteractionRequest;
57*cdf0e10cSrcweir import com.sun.star.ucb.AuthenticationRequest;
58*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
59*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
60*cdf0e10cSrcweir import com.sun.star.util.XCloseable;
61*cdf0e10cSrcweir import com.sun.star.frame.XModel;
62*cdf0e10cSrcweir import com.sun.star.sdb.RowChangeEvent;
63*cdf0e10cSrcweir import com.sun.star.sdbc.SQLException;
64*cdf0e10cSrcweir import com.sun.star.sdbc.XParameters;
65*cdf0e10cSrcweir import ifc.sdb._XCompletedExecution;
66*cdf0e10cSrcweir import util.db.DataSource;
67*cdf0e10cSrcweir import util.db.DataSourceDescriptor;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir /**
70*cdf0e10cSrcweir  * Test for object which is represented by service
71*cdf0e10cSrcweir  * <code>com.sun.star.sdb.RowSet</code>. <p>
72*cdf0e10cSrcweir  *
73*cdf0e10cSrcweir  * The following files used by this test :
74*cdf0e10cSrcweir  * <ul>
75*cdf0e10cSrcweir  *  <li><b> TestDB/TestDB.dbf </b> : the database file with some
76*cdf0e10cSrcweir  *    predefined fields described in <code>util.DBTools</code>.
77*cdf0e10cSrcweir  *    The copy of this file is always made in temp directory for
78*cdf0e10cSrcweir  *    testing purposes.</li>
79*cdf0e10cSrcweir  * </ul>
80*cdf0e10cSrcweir  * The following parameters in ini-file used by this test:
81*cdf0e10cSrcweir  * <ul>
82*cdf0e10cSrcweir  *   <li><code>test.db.url</code> - URL to MySQL database.
83*cdf0e10cSrcweir  *   For example: <code>mysql://mercury:3306/api_current</code></li>
84*cdf0e10cSrcweir  *   <li><code>test.db.user</code> - user for MySQL database</li>
85*cdf0e10cSrcweir  *   <li><code>test.db.password</code> - password for MySQL database</li>
86*cdf0e10cSrcweir  * </ul>
87*cdf0e10cSrcweir  *
88*cdf0e10cSrcweir  * @see com.sun.star.sdbc.RowSet
89*cdf0e10cSrcweir  * @see com.sun.star.sdbcx.XRowLocate
90*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XResultSetUpdate
91*cdf0e10cSrcweir  * @see com.sun.star.util.XCancellable
92*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XParameters
93*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XResultSetMetaDataSupplier
94*cdf0e10cSrcweir  * @see com.sun.star.sdbcx.XDeleteRows
95*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XCloseable
96*cdf0e10cSrcweir  * @see com.sun.star.sdbcx.XColumnsSupplier
97*cdf0e10cSrcweir  * @see com.sun.star.sdb.XResultSetAccess
98*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XResultSet
99*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XColumnLocate
100*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XRowSet
101*cdf0e10cSrcweir  * @see com.sun.star.sdb.RowSet
102*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XRowUpdate
103*cdf0e10cSrcweir  * @see com.sun.star.sdb.XRowSetApproveBroadcaster
104*cdf0e10cSrcweir  * @see com.sun.star.beans.XPropertySet
105*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XRow
106*cdf0e10cSrcweir  * @see com.sun.star.sdbc.XWarningsSupplier
107*cdf0e10cSrcweir  * @see com.sun.star.lang.XComponent
108*cdf0e10cSrcweir  * @see com.sun.star.sdbcx.ResultSet
109*cdf0e10cSrcweir  * @see com.sun.star.sdbc.ResultSet
110*cdf0e10cSrcweir  * @see ifc.sdbc._RowSet
111*cdf0e10cSrcweir  * @see ifc.sdbcx._XRowLocate
112*cdf0e10cSrcweir  * @see ifc.sdbc._XResultSetUpdate
113*cdf0e10cSrcweir  * @see ifc.util._XCancellable
114*cdf0e10cSrcweir  * @see ifc.sdbc._XParameters
115*cdf0e10cSrcweir  * @see ifc.sdbc._XResultSetMetaDataSupplier
116*cdf0e10cSrcweir  * @see ifc.sdbcx._XDeleteRows
117*cdf0e10cSrcweir  * @see ifc.sdbc._XCloseable
118*cdf0e10cSrcweir  * @see ifc.sdbcx._XColumnsSupplier
119*cdf0e10cSrcweir  * @see ifc.sdb._XResultSetAccess
120*cdf0e10cSrcweir  * @see ifc.sdbc._XResultSet
121*cdf0e10cSrcweir  * @see ifc.sdbc._XColumnLocate
122*cdf0e10cSrcweir  * @see ifc.sdbc._XRowSet
123*cdf0e10cSrcweir  * @see ifc.sdb._RowSet
124*cdf0e10cSrcweir  * @see ifc.sdbc._XRowUpdate
125*cdf0e10cSrcweir  * @see ifc.sdb._XRowSetApproveBroadcaster
126*cdf0e10cSrcweir  * @see ifc.beans._XPropertySet
127*cdf0e10cSrcweir  * @see ifc.sdbc._XRow
128*cdf0e10cSrcweir  * @see ifc.sdbc._XWarningsSupplier
129*cdf0e10cSrcweir  * @see ifc.lang._XComponent
130*cdf0e10cSrcweir  * @see ifc.sdbcx._ResultSet
131*cdf0e10cSrcweir  * @see ifc.sdbc._ResultSet
132*cdf0e10cSrcweir  */
133*cdf0e10cSrcweir public class ORowSet extends TestCase {
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     private static int uniqueSuffix = 0 ;
136*cdf0e10cSrcweir     private DBTools dbTools     = null ;
137*cdf0e10cSrcweir     private static String origDB = null ;
138*cdf0e10cSrcweir     String tableName = null;
139*cdf0e10cSrcweir     DataSourceDescriptor srcInf = null;
140*cdf0e10cSrcweir     boolean isMySQLDB = false;
141*cdf0e10cSrcweir     protected final static String dbSourceName = "ORowSetDataSource";
142*cdf0e10cSrcweir     public XConnection m_connection = null;
143*cdf0e10cSrcweir     private Object m_rowSet = null;
144*cdf0e10cSrcweir     private DataSource m_dataSource;
145*cdf0e10cSrcweir     private String m_tableFile;
146*cdf0e10cSrcweir     private XMultiServiceFactory m_orb = null;
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     /**
149*cdf0e10cSrcweir     * Initializes some class fields. Then creates DataSource, which serves
150*cdf0e10cSrcweir     * as a single source for all tables created in the test.
151*cdf0e10cSrcweir     * This DataSource then registered in the global
152*cdf0e10cSrcweir     * <code>DatabaseContext</code> service. This data source's URL
153*cdf0e10cSrcweir     * points to SOffice temp directory where tables are copied from
154*cdf0e10cSrcweir     * <code>TestDocuments</code> directory on every environment
155*cdf0e10cSrcweir     * creation.
156*cdf0e10cSrcweir     * To create DataSource for MySQL database next parameters required
157*cdf0e10cSrcweir     * in ini-file:
158*cdf0e10cSrcweir     * <ul>
159*cdf0e10cSrcweir     *   <li><code>test.db.url</code> - URL to MySQL database.
160*cdf0e10cSrcweir     *   For example: <code>mysql://mercury:3306/api_current</code></li>
161*cdf0e10cSrcweir     *   <li><code>test.db.user</code> - user for MySQL database</li>
162*cdf0e10cSrcweir     *   <li><code>test.db.password</code> - password for MySQL database</li>
163*cdf0e10cSrcweir     * </ul>
164*cdf0e10cSrcweir     *
165*cdf0e10cSrcweir     * @throws StatusException if DataSource can not be created or
166*cdf0e10cSrcweir     * registered.
167*cdf0e10cSrcweir     */
168*cdf0e10cSrcweir     protected void initialize ( TestParameters Param, PrintWriter _log)
169*cdf0e10cSrcweir         throws StatusException
170*cdf0e10cSrcweir     {
171*cdf0e10cSrcweir         m_orb = (XMultiServiceFactory)Param.getMSF();
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         String tmpDir = utils.getOfficeTemp( m_orb );
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir         origDB = util.utils.getFullTestDocName("TestDB/testDB.dbf");
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir         dbTools = new DBTools( m_orb, _log );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         // creating DataSource and registering it in DatabaseContext
180*cdf0e10cSrcweir         String dbURL = (String) Param.get("test.db.url");
181*cdf0e10cSrcweir         String dbUser = (String) Param.get("test.db.user");
182*cdf0e10cSrcweir         String dbPassword = (String) Param.get("test.db.password");
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         log.println("Creating and registering DataSource ...");
185*cdf0e10cSrcweir         srcInf = new DataSourceDescriptor( m_orb );
186*cdf0e10cSrcweir         if (dbURL != null && dbUser != null && dbPassword != null)
187*cdf0e10cSrcweir         {
188*cdf0e10cSrcweir             isMySQLDB = true;
189*cdf0e10cSrcweir             log.println("dbURL = " + dbURL);
190*cdf0e10cSrcweir             log.println("dbUSER = " + dbUser);
191*cdf0e10cSrcweir             log.println("dbPASSWORD = " + dbPassword);
192*cdf0e10cSrcweir             //DataSource for mysql db
193*cdf0e10cSrcweir             tableName = "soffice_test_table";
194*cdf0e10cSrcweir             srcInf.URL = "jdbc:" + dbURL;
195*cdf0e10cSrcweir             srcInf.IsPasswordRequired = new Boolean(true);
196*cdf0e10cSrcweir             srcInf.Password = dbPassword;
197*cdf0e10cSrcweir             srcInf.User = dbUser;
198*cdf0e10cSrcweir             PropertyValue[] propInfo = new PropertyValue[1];
199*cdf0e10cSrcweir             propInfo[0] = new PropertyValue();
200*cdf0e10cSrcweir             propInfo[0].Name = "JavaDriverClass";
201*cdf0e10cSrcweir             propInfo[0].Value = "org.gjt.mm.mysql.Driver";
202*cdf0e10cSrcweir             srcInf.Info = propInfo;
203*cdf0e10cSrcweir         }
204*cdf0e10cSrcweir         else
205*cdf0e10cSrcweir         {
206*cdf0e10cSrcweir             srcInf.URL = "sdbc:dbase:" + DBTools.dirToUrl(tmpDir);
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir         m_dataSource = srcInf.createDataSource();
209*cdf0e10cSrcweir         m_dataSource.registerAs( dbSourceName, true );
210*cdf0e10cSrcweir     }
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     /**
213*cdf0e10cSrcweir     * Creating a Testenvironment for the interfaces to be tested.
214*cdf0e10cSrcweir     * The database (DBF) file is copied from test document directory
215*cdf0e10cSrcweir     * into SOffice temp dir with unique name for each enviroment
216*cdf0e10cSrcweir     * creation. If the file cann't be copied (is not released)
217*cdf0e10cSrcweir     * then another unique name is used (file name suffix incremented
218*cdf0e10cSrcweir     * by 1).<p>
219*cdf0e10cSrcweir     *
220*cdf0e10cSrcweir     * <code>com.sun.star.sdb.RowSet</code> service created and its
221*cdf0e10cSrcweir     * source is all rows from the current copy of the table. Then
222*cdf0e10cSrcweir     * row set command ("select all rows from a table") is executed
223*cdf0e10cSrcweir     * and cursor is positioned to the first row. <p>
224*cdf0e10cSrcweir     *
225*cdf0e10cSrcweir     *     Object relations created :
226*cdf0e10cSrcweir     * <ul>
227*cdf0e10cSrcweir     *  <li> <code>'ORowSet.Connection'</code> for
228*cdf0e10cSrcweir     *      internal component test usage. Is used for
229*cdf0e10cSrcweir     *      closing connection when cleaning up environment. </li>
230*cdf0e10cSrcweir     *  <li> <code>'XRowSetApproveBroadcaster.ApproveChecker'</code> for
231*cdf0e10cSrcweir     *      {@link ifc.sdb._XRowSetApproveBroadcaster} interface
232*cdf0e10cSrcweir     *      implementation which made actions required </li>
233*cdf0e10cSrcweir     *  <li> <code>'CurrentRowData'</code> for
234*cdf0e10cSrcweir     *      {@link ifc.sdbc._XRow}, {@link ifc.sdbc._XRowUpdate} :
235*cdf0e10cSrcweir     *      exports types and values of the current row data.</li>
236*cdf0e10cSrcweir     *  <li> <code>'XColumnLocate.ColumnName'</code> for
237*cdf0e10cSrcweir     *      {@link ifc.sdbc._XColumnLocate} :
238*cdf0e10cSrcweir     *      the name of the first column of the table.</li>
239*cdf0e10cSrcweir     *  <li> <code>'XParameters.ParamValues'</code> for
240*cdf0e10cSrcweir     *      {@link ifc.sdbc._XParameters} :
241*cdf0e10cSrcweir     *      Collection of parameter types presented in the query. </li>
242*cdf0e10cSrcweir     *  <li> <code>'XRowUpdate.XRow'</code> for
243*cdf0e10cSrcweir     *      {@link ifc.sdbc._XRowUpdate} :
244*cdf0e10cSrcweir     *      <code>XRow</code> interface of the current component.</li>
245*cdf0e10cSrcweir     *  <li> <code>'XResultSetUpdate.UpdateTester'</code> for
246*cdf0e10cSrcweir     *      {@link ifc.sdbc._XResultSetUpdate} </li>
247*cdf0e10cSrcweir     * </ul>
248*cdf0e10cSrcweir     *
249*cdf0e10cSrcweir     * @see com.sun.star.sdb.DatabaseContext
250*cdf0e10cSrcweir     * @see com.sun.star.sdb.DataSource
251*cdf0e10cSrcweir     */
252*cdf0e10cSrcweir     protected TestEnvironment createTestEnvironment(TestParameters Param,
253*cdf0e10cSrcweir             PrintWriter log)
254*cdf0e10cSrcweir     {
255*cdf0e10cSrcweir         XMultiServiceFactory orb = (XMultiServiceFactory)Param.getMSF();
256*cdf0e10cSrcweir         uniqueSuffix++;
257*cdf0e10cSrcweir         boolean envCreatedOK = false ;
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         //initialize test table
260*cdf0e10cSrcweir         if (isMySQLDB)
261*cdf0e10cSrcweir         {
262*cdf0e10cSrcweir             try
263*cdf0e10cSrcweir             {
264*cdf0e10cSrcweir                 DBTools.DataSourceInfo legacyDescriptor = dbTools.newDataSourceInfo();
265*cdf0e10cSrcweir                 legacyDescriptor.Name = srcInf.Name;
266*cdf0e10cSrcweir                 legacyDescriptor.User = srcInf.User;
267*cdf0e10cSrcweir                 legacyDescriptor.Password = srcInf.Password;
268*cdf0e10cSrcweir                 legacyDescriptor.Info = srcInf.Info;
269*cdf0e10cSrcweir                 legacyDescriptor.URL = srcInf.URL;
270*cdf0e10cSrcweir                 legacyDescriptor.IsPasswordRequired = srcInf.IsPasswordRequired;
271*cdf0e10cSrcweir                 dbTools.initTestTableUsingJDBC(tableName, legacyDescriptor);
272*cdf0e10cSrcweir             }
273*cdf0e10cSrcweir             catch(java.sql.SQLException e)
274*cdf0e10cSrcweir             {
275*cdf0e10cSrcweir                 e.printStackTrace(log);
276*cdf0e10cSrcweir                 throw new StatusException(Status.failed("Couldn't " +
277*cdf0e10cSrcweir                     " init test table. SQLException..."));
278*cdf0e10cSrcweir             }
279*cdf0e10cSrcweir             catch(java.lang.ClassNotFoundException e)
280*cdf0e10cSrcweir             {
281*cdf0e10cSrcweir                 throw new StatusException(Status.failed("Couldn't " +
282*cdf0e10cSrcweir                     "register mysql driver"));
283*cdf0e10cSrcweir             }
284*cdf0e10cSrcweir         }
285*cdf0e10cSrcweir         else
286*cdf0e10cSrcweir         {
287*cdf0e10cSrcweir             String oldF = null ;
288*cdf0e10cSrcweir             String newF = null ;
289*cdf0e10cSrcweir             String tempFolder = utils.getOfficeTemp( orb );
290*cdf0e10cSrcweir             do
291*cdf0e10cSrcweir             {
292*cdf0e10cSrcweir                 tableName = "ORowSet_tmp" + uniqueSuffix ;
293*cdf0e10cSrcweir                 oldF = utils.getFullURL(origDB);
294*cdf0e10cSrcweir                 newF = tempFolder + tableName + ".dbf";
295*cdf0e10cSrcweir             }
296*cdf0e10cSrcweir             while ( !utils.tryOverwriteFile( orb, oldF, newF ) );
297*cdf0e10cSrcweir             m_tableFile = newF;
298*cdf0e10cSrcweir         }
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir         try
301*cdf0e10cSrcweir         {
302*cdf0e10cSrcweir             m_rowSet = orb.createInstance("com.sun.star.sdb.RowSet");
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir             XPropertySet rowSetProps = UnoRuntime.queryInterface( XPropertySet.class, m_rowSet );
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir             log.println("Trying to open: " + tableName);
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir             rowSetProps.setPropertyValue("DataSourceName", dbSourceName);
309*cdf0e10cSrcweir             rowSetProps.setPropertyValue("Command", tableName);
310*cdf0e10cSrcweir             rowSetProps.setPropertyValue("CommandType",
311*cdf0e10cSrcweir                 new Integer(CommandType.TABLE));
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir             final XRowSet rowSet = UnoRuntime.queryInterface( XRowSet.class, m_rowSet);
314*cdf0e10cSrcweir             rowSet.execute();
315*cdf0e10cSrcweir             m_connection = UnoRuntime.queryInterface( XConnection.class, rowSetProps.getPropertyValue("ActiveConnection") );
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir             XResultSet xRes = UnoRuntime.queryInterface( XResultSet.class, m_rowSet );
318*cdf0e10cSrcweir             xRes.first();
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir             log.println( "creating a new environment for object" );
321*cdf0e10cSrcweir             TestEnvironment tEnv = new TestEnvironment( (XInterface)m_rowSet );
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir             // Adding obj relation for XRowSetApproveBroadcaster test
324*cdf0e10cSrcweir             {
325*cdf0e10cSrcweir                 final XResultSet resultSet = UnoRuntime.queryInterface( XResultSet.class, m_rowSet );
326*cdf0e10cSrcweir                 final XResultSetUpdate resultSetUpdate = UnoRuntime.queryInterface( XResultSetUpdate.class, m_rowSet );
327*cdf0e10cSrcweir                 final XRowUpdate rowUpdate = UnoRuntime.queryInterface(XRowUpdate.class, m_rowSet );
328*cdf0e10cSrcweir                 final PrintWriter logF = log ;
329*cdf0e10cSrcweir                 tEnv.addObjRelation( "XRowSetApproveBroadcaster.ApproveChecker",
330*cdf0e10cSrcweir                     new ifc.sdb._XRowSetApproveBroadcaster.RowSetApproveChecker()
331*cdf0e10cSrcweir                     {
332*cdf0e10cSrcweir                         public void moveCursor()
333*cdf0e10cSrcweir                         {
334*cdf0e10cSrcweir                             try
335*cdf0e10cSrcweir                             {
336*cdf0e10cSrcweir                                 resultSet.beforeFirst();
337*cdf0e10cSrcweir                                 resultSet.afterLast();
338*cdf0e10cSrcweir                                 resultSet.first();
339*cdf0e10cSrcweir                             }
340*cdf0e10cSrcweir                             catch (com.sun.star.sdbc.SQLException e)
341*cdf0e10cSrcweir                             {
342*cdf0e10cSrcweir                                 logF.println("### _XRowSetApproveBroadcaster.RowSetApproveChecker.moveCursor() :");
343*cdf0e10cSrcweir                                 e.printStackTrace(logF);
344*cdf0e10cSrcweir                                 throw new StatusException( "RowSetApproveChecker.moveCursor failed", e );
345*cdf0e10cSrcweir                             }
346*cdf0e10cSrcweir                         }
347*cdf0e10cSrcweir                         public RowChangeEvent changeRow()
348*cdf0e10cSrcweir                         {
349*cdf0e10cSrcweir                             try
350*cdf0e10cSrcweir                             {
351*cdf0e10cSrcweir                                 resultSet.first();
352*cdf0e10cSrcweir                                 rowUpdate.updateString(1, "ORowSetTest2");
353*cdf0e10cSrcweir                                 resultSetUpdate.updateRow();
354*cdf0e10cSrcweir                             }
355*cdf0e10cSrcweir                             catch (com.sun.star.sdbc.SQLException e)
356*cdf0e10cSrcweir                             {
357*cdf0e10cSrcweir                                 logF.println("### _XRowSetApproveBroadcaster.RowSetApproveChecker.changeRow() :");
358*cdf0e10cSrcweir                                 e.printStackTrace(logF);
359*cdf0e10cSrcweir                                 throw new StatusException( "RowSetApproveChecker.changeRow failed", e );
360*cdf0e10cSrcweir                             }
361*cdf0e10cSrcweir                             RowChangeEvent ev = new RowChangeEvent();
362*cdf0e10cSrcweir                             ev.Action = com.sun.star.sdb.RowChangeAction.UPDATE ;
363*cdf0e10cSrcweir                             ev.Rows = 1 ;
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir                             return ev ;
366*cdf0e10cSrcweir                         }
367*cdf0e10cSrcweir                         public void changeRowSet()
368*cdf0e10cSrcweir                         {
369*cdf0e10cSrcweir                             try
370*cdf0e10cSrcweir                             {
371*cdf0e10cSrcweir                                 // since we gave the row set a parametrized statement, we need to ensure the
372*cdf0e10cSrcweir                                 // parameter is actually filled, otherwise we would get an empty result set,
373*cdf0e10cSrcweir                                 // which would imply some further tests failing
374*cdf0e10cSrcweir                                 XParameters rowSetParams = UnoRuntime.queryInterface( XParameters.class, resultSet );
375*cdf0e10cSrcweir                                 rowSetParams.setString( 1, "String2" );
376*cdf0e10cSrcweir                                 rowSet.execute();
377*cdf0e10cSrcweir                                 resultSet.first();
378*cdf0e10cSrcweir                             }
379*cdf0e10cSrcweir                             catch (com.sun.star.sdbc.SQLException e)
380*cdf0e10cSrcweir                             {
381*cdf0e10cSrcweir                                 logF.println("### _XRowSetApproveBroadcaster.RowSetApproveChecker.changeRowSet() :");
382*cdf0e10cSrcweir                                 e.printStackTrace(logF);
383*cdf0e10cSrcweir                                 throw new StatusException( "RowSetApproveChecker.changeRowSet failed", e );
384*cdf0e10cSrcweir                             }
385*cdf0e10cSrcweir                         }
386*cdf0e10cSrcweir                     }
387*cdf0e10cSrcweir                 );
388*cdf0e10cSrcweir             }
389*cdf0e10cSrcweir             // Adding relations for XRow as a Vector with all data
390*cdf0e10cSrcweir             // of current row of RowSet.
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir             Vector rowData = new Vector();
393*cdf0e10cSrcweir 
394*cdf0e10cSrcweir             for (int i = 0; i < DBTools.TST_TABLE_VALUES[0].length; i++) {
395*cdf0e10cSrcweir                 rowData.add(DBTools.TST_TABLE_VALUES[0][i]);
396*cdf0e10cSrcweir             }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir             // here XRef must be added
399*cdf0e10cSrcweir             // here XBlob must be added
400*cdf0e10cSrcweir             // here XClob must be added
401*cdf0e10cSrcweir             // here XArray must be added
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir             tEnv.addObjRelation("CurrentRowData", rowData);
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir             // Adding relation for XColumnLocate ifc test
406*cdf0e10cSrcweir             tEnv.addObjRelation( "XColumnLocate.ColumnName", DBTools.TST_STRING_F );
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir             // Adding relation for XCompletedExecution
409*cdf0e10cSrcweir             tEnv.addObjRelation( "InteractionHandlerChecker", new InteractionHandlerImpl() );
410*cdf0e10cSrcweir             try
411*cdf0e10cSrcweir             {
412*cdf0e10cSrcweir                 String sqlCommand = isMySQLDB
413*cdf0e10cSrcweir                     ?   "SELECT Column0  FROM soffice_test_table  WHERE ( (  Column0 = :param1 ) )"
414*cdf0e10cSrcweir                     :   "SELECT \"_TEXT\" FROM \"" + tableName + "\" WHERE ( ( \"_TEXT\" = :param1 ) )";
415*cdf0e10cSrcweir                 rowSetProps.setPropertyValue( "DataSourceName", dbSourceName );
416*cdf0e10cSrcweir                 rowSetProps.setPropertyValue( "Command", sqlCommand );
417*cdf0e10cSrcweir                 rowSetProps.setPropertyValue( "CommandType", new Integer(CommandType.COMMAND) );
418*cdf0e10cSrcweir             }
419*cdf0e10cSrcweir             catch(Exception e)
420*cdf0e10cSrcweir             {
421*cdf0e10cSrcweir                 throw new StatusException( "setting up the RowSet with a parametrized command failed", e );
422*cdf0e10cSrcweir             }
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir             // Adding relation for XParameters ifc test
425*cdf0e10cSrcweir             tEnv.addObjRelation( "XParameters.ParamValues", new Vector() );
426*cdf0e10cSrcweir 
427*cdf0e10cSrcweir             // Adding relation for XRowUpdate
428*cdf0e10cSrcweir             final XRow row = UnoRuntime.queryInterface( XRow.class, m_rowSet );
429*cdf0e10cSrcweir             tEnv.addObjRelation("XRowUpdate.XRow", row);
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir             // Adding relation for XResultSetUpdate
432*cdf0e10cSrcweir             {
433*cdf0e10cSrcweir                 final XResultSet resultSet = UnoRuntime.queryInterface( XResultSet.class, m_rowSet );
434*cdf0e10cSrcweir                 final XRowUpdate rowUpdate = UnoRuntime.queryInterface( XRowUpdate.class, m_rowSet );
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir                 tEnv.addObjRelation("XResultSetUpdate.UpdateTester",
437*cdf0e10cSrcweir                     new ifc.sdbc._XResultSetUpdate.UpdateTester()
438*cdf0e10cSrcweir                     {
439*cdf0e10cSrcweir                         String lastUpdate = null ;
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir                         public int rowCount() throws SQLException
442*cdf0e10cSrcweir                         {
443*cdf0e10cSrcweir                             int prevPos = resultSet.getRow();
444*cdf0e10cSrcweir                             resultSet.last();
445*cdf0e10cSrcweir                             int count = resultSet.getRow();
446*cdf0e10cSrcweir                             resultSet.absolute(prevPos);
447*cdf0e10cSrcweir 
448*cdf0e10cSrcweir                             return count ;
449*cdf0e10cSrcweir                         }
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir                         public void update() throws SQLException
452*cdf0e10cSrcweir                         {
453*cdf0e10cSrcweir                             lastUpdate = row.getString(1);
454*cdf0e10cSrcweir                             lastUpdate += "_" ;
455*cdf0e10cSrcweir                             rowUpdate.updateString(1, lastUpdate);
456*cdf0e10cSrcweir                         }
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir                         public boolean wasUpdated() throws SQLException
459*cdf0e10cSrcweir                         {
460*cdf0e10cSrcweir                             String getStr = row.getString(1);
461*cdf0e10cSrcweir                             return lastUpdate.equals(getStr);
462*cdf0e10cSrcweir                         }
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir                         public int currentRow() throws SQLException
465*cdf0e10cSrcweir                         {
466*cdf0e10cSrcweir                             return resultSet.getRow();
467*cdf0e10cSrcweir                         }
468*cdf0e10cSrcweir                     }
469*cdf0e10cSrcweir                 );
470*cdf0e10cSrcweir             }
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir             envCreatedOK = true ;
473*cdf0e10cSrcweir             return tEnv;
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir         }
476*cdf0e10cSrcweir         catch(com.sun.star.uno.Exception e)
477*cdf0e10cSrcweir         {
478*cdf0e10cSrcweir             log.println( "couldn't set up tes tenvironment:" );
479*cdf0e10cSrcweir             e.printStackTrace(log);
480*cdf0e10cSrcweir             try
481*cdf0e10cSrcweir             {
482*cdf0e10cSrcweir                 if ( m_connection != null )
483*cdf0e10cSrcweir                     m_connection.close();
484*cdf0e10cSrcweir             }
485*cdf0e10cSrcweir             catch(Exception ex)
486*cdf0e10cSrcweir             {
487*cdf0e10cSrcweir             }
488*cdf0e10cSrcweir             throw new StatusException( "couldn't set up tes tenvironment", e );
489*cdf0e10cSrcweir         }
490*cdf0e10cSrcweir         finally
491*cdf0e10cSrcweir         {
492*cdf0e10cSrcweir             if (!envCreatedOK)
493*cdf0e10cSrcweir             {
494*cdf0e10cSrcweir                 try
495*cdf0e10cSrcweir                 {
496*cdf0e10cSrcweir                     m_connection.close();
497*cdf0e10cSrcweir                 }
498*cdf0e10cSrcweir                 catch(Exception ex)
499*cdf0e10cSrcweir                 {
500*cdf0e10cSrcweir                 }
501*cdf0e10cSrcweir             }
502*cdf0e10cSrcweir         }
503*cdf0e10cSrcweir 
504*cdf0e10cSrcweir     } // finish method getTestEnvironment
505*cdf0e10cSrcweir 
506*cdf0e10cSrcweir     /**
507*cdf0e10cSrcweir     * Closes connection of <code>RowSet</code> instance created.
508*cdf0e10cSrcweir     */
509*cdf0e10cSrcweir     protected void cleanup( TestParameters Param, PrintWriter log)
510*cdf0e10cSrcweir     {
511*cdf0e10cSrcweir         String doing = null;
512*cdf0e10cSrcweir         try
513*cdf0e10cSrcweir         {
514*cdf0e10cSrcweir             doing = "revoking data source registration";
515*cdf0e10cSrcweir             log.println( doing );
516*cdf0e10cSrcweir             m_dataSource.revokeRegistration();
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir             doing = "closing database document";
519*cdf0e10cSrcweir             log.println( doing );
520*cdf0e10cSrcweir             XModel databaseDocModel = UnoRuntime.queryInterface( XModel.class,
521*cdf0e10cSrcweir                 m_dataSource.getDatabaseDocument().getDatabaseDocument() );
522*cdf0e10cSrcweir             String documentFile = databaseDocModel.getURL();
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir             XCloseable closeModel = UnoRuntime.queryInterface( XCloseable.class,
525*cdf0e10cSrcweir                 m_dataSource.getDatabaseDocument().getDatabaseDocument() );
526*cdf0e10cSrcweir             closeModel.close( true );
527*cdf0e10cSrcweir 
528*cdf0e10cSrcweir             if ( m_rowSet != null )
529*cdf0e10cSrcweir             {
530*cdf0e10cSrcweir                 doing = "disposing row set";
531*cdf0e10cSrcweir                 log.println( doing );
532*cdf0e10cSrcweir                 XComponent rowSetComponent = UnoRuntime.queryInterface( XComponent.class, m_rowSet );
533*cdf0e10cSrcweir                 rowSetComponent.dispose();
534*cdf0e10cSrcweir             }
535*cdf0e10cSrcweir 
536*cdf0e10cSrcweir             try
537*cdf0e10cSrcweir             {
538*cdf0e10cSrcweir                 doing = "closing connection";
539*cdf0e10cSrcweir                 log.println( doing );
540*cdf0e10cSrcweir                 m_connection.close();
541*cdf0e10cSrcweir             }
542*cdf0e10cSrcweir             catch (com.sun.star.lang.DisposedException e)
543*cdf0e10cSrcweir             {
544*cdf0e10cSrcweir                 log.println( "already closed - okay." );
545*cdf0e10cSrcweir             }
546*cdf0e10cSrcweir 
547*cdf0e10cSrcweir             doing = "deleting database file ("  + documentFile + ")";
548*cdf0e10cSrcweir             log.println( doing );
549*cdf0e10cSrcweir             impl_deleteFile( documentFile );
550*cdf0e10cSrcweir 
551*cdf0e10cSrcweir             if ( m_tableFile != null )
552*cdf0e10cSrcweir             {
553*cdf0e10cSrcweir                 doing = "deleting dBase table file (" + m_tableFile + ")";
554*cdf0e10cSrcweir                 log.println( doing );
555*cdf0e10cSrcweir                 impl_deleteFile( m_tableFile );
556*cdf0e10cSrcweir             }
557*cdf0e10cSrcweir         }
558*cdf0e10cSrcweir         catch (com.sun.star.uno.Exception e)
559*cdf0e10cSrcweir         {
560*cdf0e10cSrcweir             log.println( "error: ");
561*cdf0e10cSrcweir             e.printStackTrace(log);
562*cdf0e10cSrcweir         }
563*cdf0e10cSrcweir     }
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir     private final void impl_deleteFile( final String _file )
566*cdf0e10cSrcweir     {
567*cdf0e10cSrcweir         java.io.File file = new java.io.File( _file );
568*cdf0e10cSrcweir         file.delete();
569*cdf0e10cSrcweir         if ( file.exists() )
570*cdf0e10cSrcweir             file.deleteOnExit();
571*cdf0e10cSrcweir     }
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir     /**
574*cdf0e10cSrcweir      * Implementation of interface _XCompletedExecution.CheckInteractionHandler
575*cdf0e10cSrcweir      * for the XCompletedExecution test
576*cdf0e10cSrcweir      * @see ifc.sdb._XCompletedExecution
577*cdf0e10cSrcweir      */
578*cdf0e10cSrcweir     public class InteractionHandlerImpl implements _XCompletedExecution.CheckInteractionHandler {
579*cdf0e10cSrcweir         private boolean handlerWasUsed = false;
580*cdf0e10cSrcweir         private PrintWriter log = new PrintWriter(System.out);
581*cdf0e10cSrcweir 
582*cdf0e10cSrcweir         public boolean checkInteractionHandler() {
583*cdf0e10cSrcweir             return handlerWasUsed;
584*cdf0e10cSrcweir         }
585*cdf0e10cSrcweir 
586*cdf0e10cSrcweir         public void handle(XInteractionRequest xInteractionRequest) {
587*cdf0e10cSrcweir             log.println("### _XCompletedExecution.InteractionHandlerImpl: handle called.");
588*cdf0e10cSrcweir             ParametersRequest req = null;
589*cdf0e10cSrcweir             boolean abort = false;
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir             Object o = xInteractionRequest.getRequest();
592*cdf0e10cSrcweir             if (o instanceof ParametersRequest) {
593*cdf0e10cSrcweir                 req = (ParametersRequest)o;
594*cdf0e10cSrcweir             }
595*cdf0e10cSrcweir             else if (o instanceof AuthenticationRequest) {
596*cdf0e10cSrcweir                 log.println("### The request in XCompletedExecution is of type 'AuthenticationRequest'");
597*cdf0e10cSrcweir                 log.println("### This is not implemented in ORowSet.InteractionHandlerImpl test -> abort.");
598*cdf0e10cSrcweir                 abort = true;
599*cdf0e10cSrcweir             }
600*cdf0e10cSrcweir             else {
601*cdf0e10cSrcweir                 log.println("### Unknown request:" + o.toString());
602*cdf0e10cSrcweir                 log.println("### This is not implemented in ORowSet.InteractionHandlerImpl test -> abort.");
603*cdf0e10cSrcweir                 abort = true;
604*cdf0e10cSrcweir             }
605*cdf0e10cSrcweir 
606*cdf0e10cSrcweir             XInteractionContinuation[]xCont = xInteractionRequest.getContinuations();
607*cdf0e10cSrcweir             XInteractionSupplyParameters xParamCallback = null;
608*cdf0e10cSrcweir             for(int i=0; i<xCont.length; i++) {
609*cdf0e10cSrcweir                 if (abort) {
610*cdf0e10cSrcweir                     XInteractionAbort xAbort = null;
611*cdf0e10cSrcweir                     xAbort = UnoRuntime.queryInterface(XInteractionAbort.class, xCont[i]);
612*cdf0e10cSrcweir                     if (xAbort != null)
613*cdf0e10cSrcweir                         xAbort.select();
614*cdf0e10cSrcweir                         return;
615*cdf0e10cSrcweir                 }
616*cdf0e10cSrcweir                 else {
617*cdf0e10cSrcweir                     xParamCallback = UnoRuntime.queryInterface(XInteractionSupplyParameters.class, xCont[i]);
618*cdf0e10cSrcweir                     if (xParamCallback != null)
619*cdf0e10cSrcweir                         break;
620*cdf0e10cSrcweir                 }
621*cdf0e10cSrcweir             }
622*cdf0e10cSrcweir             if (xParamCallback != null) {
623*cdf0e10cSrcweir                 log.println("### _XCompletedExecution.InteractionHandlerImpl: supplying parameters.");
624*cdf0e10cSrcweir                 handlerWasUsed = true;
625*cdf0e10cSrcweir                 PropertyValue[] prop = new PropertyValue[1];
626*cdf0e10cSrcweir                 prop[0] = new PropertyValue();
627*cdf0e10cSrcweir                 prop[0].Name = "param1";
628*cdf0e10cSrcweir                 prop[0].Value = "Hi.";
629*cdf0e10cSrcweir 
630*cdf0e10cSrcweir                 xParamCallback.setParameters(prop);
631*cdf0e10cSrcweir                 xParamCallback.select();
632*cdf0e10cSrcweir             }
633*cdf0e10cSrcweir             else { // we should never reach this: abort has to be true first.
634*cdf0e10cSrcweir                 log.println("### _XCompletedExecution.InteractionHandlerImpl: Got no " +
635*cdf0e10cSrcweir                             "'XInteractionSupplyParameters' and no 'XInteractionAbort'.");
636*cdf0e10cSrcweir             }
637*cdf0e10cSrcweir         }
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir         public void setLog(PrintWriter log) {
640*cdf0e10cSrcweir             this.log = log;
641*cdf0e10cSrcweir         }
642*cdf0e10cSrcweir 
643*cdf0e10cSrcweir     }
644*cdf0e10cSrcweir }
645