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 package mod._forms;
24 
25 import java.io.PrintWriter;
26 import java.util.Vector;
27 
28 import lib.Status;
29 import lib.StatusException;
30 import lib.TestCase;
31 import lib.TestEnvironment;
32 import lib.TestParameters;
33 import util.DBTools;
34 import util.DrawTools;
35 import util.FormTools;
36 import util.WriterTools;
37 import util.utils;
38 
39 import com.sun.star.awt.XControl;
40 import com.sun.star.awt.XControlModel;
41 import com.sun.star.beans.PropertyValue;
42 import com.sun.star.beans.XPropertySet;
43 import com.sun.star.container.XIndexAccess;
44 import com.sun.star.container.XNameContainer;
45 import com.sun.star.container.XNamed;
46 import com.sun.star.drawing.XControlShape;
47 import com.sun.star.drawing.XShape;
48 import com.sun.star.drawing.XShapes;
49 import com.sun.star.form.DatabaseParameterEvent;
50 import com.sun.star.form.XForm;
51 import com.sun.star.form.XLoadable;
52 import com.sun.star.lang.EventObject;
53 import com.sun.star.lang.XMultiServiceFactory;
54 import com.sun.star.sdb.CommandType;
55 import com.sun.star.sdb.ParametersRequest;
56 import com.sun.star.sdb.RowChangeEvent;
57 import com.sun.star.sdbc.SQLException;
58 import com.sun.star.sdbc.XConnection;
59 import com.sun.star.sdbc.XResultSet;
60 import com.sun.star.sdbc.XResultSetUpdate;
61 import com.sun.star.sdbc.XRow;
62 import com.sun.star.sdbc.XRowSet;
63 import com.sun.star.sdbc.XRowUpdate;
64 import com.sun.star.task.XInteractionRequest;
65 import com.sun.star.text.XTextDocument;
66 import com.sun.star.uno.Any;
67 import com.sun.star.uno.AnyConverter;
68 import com.sun.star.uno.Type;
69 import com.sun.star.uno.UnoRuntime;
70 import com.sun.star.uno.XInterface;
71 import com.sun.star.util.Date;
72 import com.sun.star.util.DateTime;
73 import com.sun.star.util.Time;
74 import com.sun.star.util.XCloseable;
75 import com.sun.star.view.XControlAccess;
76 import ifc.form._XDatabaseParameterBroadcaster;
77 import ifc.sdb._XCompletedExecution;
78 
79 
80 /**
81  * Test for object which is represented by service
82  * <code>com.sun.star.form.component.DatabaseForm</code>. <p>
83  *
84  * Object implements the following interfaces :
85  * <ul>
86  *  <li> <code>com::sun::star::script::XEventAttacherManager</code></li>
87  *  <li> <code>com::sun::star::container::XElementAccess</code></li>
88  *  <li> <code>com::sun::star::sdbcx::ResultSet</code></li>
89  *  <li> <code>com::sun::star::container::XChild</code></li>
90  *  <li> <code>com::sun::star::sdbc::XResultSetUpdate</code></li>
91  *  <li> <code>com::sun::star::sdb::XResultSetAccess</code></li>
92  *  <li> <code>com::sun::star::form::FormComponent</code></li>
93  *  <li> <code>com::sun::star::form::component::DataForm</code></li>
94  *  <li> <code>com::sun::star::sdbc::XResultSetMetaDataSupplier</code></li>
95  *  <li> <code>com::sun::star::container::XEnumerationAccess</code></li>
96  *  <li> <code>com::sun::star::sdbcx::XDeleteRows</code></li>
97  *  <li> <code>com::sun::star::sdb::RowSet</code></li>
98  *  <li> <code>com::sun::star::lang::XComponent</code></li>
99  *  <li> <code>com::sun::star::sdbc::XRowSet</code></li>
100  *  <li> <code>com::sun::star::sdbc::XRowUpdate</code></li>
101  *  <li> <code>com::sun::star::form::XLoadable</code></li>
102  *  <li> <code>com::sun::star::container::XNamed</code></li>
103  *  <li> <code>com::sun::star::container::XIndexReplace</code></li>
104  *  <li> <code>com::sun::star::io::XPersistObject</code></li>
105  *  <li> <code>com::sun::star::container::XNameReplace</code></li>
106  *  <li> <code>com::sun::star::container::XIndexContainer</code></li>
107  *  <li> <code>com::sun::star::container::XNameAccess</code></li>
108  *  <li> <code>com::sun::star::sdbc::XParameters</code></li>
109  *  <li> <code>com::sun::star::util::XCancellable</code></li>
110  *  <li> <code>com::sun::star::form::XReset</code></li>
111  *  <li> <code>com::sun::star::sdbc::XCloseable</code></li>
112  *  <li> <code>com::sun::star::sdbcx::XColumnsSupplier</code></li>
113  *  <li> <code>com::sun::star::sdb::XRowSetApproveBroadcaster</code></li>
114  *  <li> <code>com::sun::star::sdbc::ResultSet</code></li>
115  *  <li> <code>com::sun::star::sdbc::XResultSet</code></li>
116  *  <li> <code>com::sun::star::sdbc::XRow</code></li>
117  *  <li> <code>com::sun::star::sdbc::XColumnLocate</code></li>
118  *  <li> <code>com::sun::star::awt::XTabControllerModel</code></li>
119  *  <li> <code>com::sun::star::container::XIndexAccess</code></li>
120  *  <li> <code>com::sun::star::form::XSubmit</code></li>
121  *  <li> <code>com::sun::star::form::component::HTMLForm</code></li>
122  *  <li> <code>com::sun::star::sdbcx::XRowLocate</code></li>
123  *  <li> <code>com::sun::star::sdbc::XWarningsSupplier</code></li>
124  *  <li> <code>com::sun::star::container::XNameContainer</code></li>
125  *  <li> <code>com::sun::star::beans::XPropertyAccess</code></li>
126  *  <li> <code>com::sun::star::beans::XPropertyContainer</code></li>
127  *  <li> <code>com::sun::star::beans::XPropertySet</code></li>
128  *  <li> <code>com::sun::star::sdbc::RowSet</code></li>
129  * </ul> <p>
130  *
131  * This object test <b> is NOT </b> designed to be run in several
132  * threads concurently.
133  * The following parameters in ini-file used by this test:
134  * <ul>
135  *   <li><code>test.db.url</code> - URL to MySQL database.
136  *   For example: <code>mysql://mercury:3306/api_current</code></li>
137  *   <li><code>test.db.user</code> - user for MySQL database</li>
138  *   <li><code>test.db.password</code> - password for MySQL database</li>
139  * </ul><p>
140  *
141  * @see com.sun.star.script.XEventAttacherManager
142  * @see com.sun.star.container.XElementAccess
143  * @see com.sun.star.sdbcx.ResultSet
144  * @see com.sun.star.container.XChild
145  * @see com.sun.star.sdbc.XResultSetUpdate
146  * @see com.sun.star.sdb.XResultSetAccess
147  * @see com.sun.star.form.FormComponent
148  * @see com.sun.star.form.component.DataForm
149  * @see com.sun.star.sdbc.XResultSetMetaDataSupplier
150  * @see com.sun.star.container.XEnumerationAccess
151  * @see com.sun.star.sdbcx.XDeleteRows
152  * @see com.sun.star.sdb.RowSet
153  * @see com.sun.star.lang.XComponent
154  * @see com.sun.star.sdbc.XRowSet
155  * @see com.sun.star.sdbc.XRowUpdate
156  * @see com.sun.star.form.XLoadable
157  * @see com.sun.star.container.XNamed
158  * @see com.sun.star.container.XIndexReplace
159  * @see com.sun.star.io.XPersistObject
160  * @see com.sun.star.container.XNameReplace
161  * @see com.sun.star.container.XIndexContainer
162  * @see com.sun.star.container.XNameAccess
163  * @see com.sun.star.sdbc.XParameters
164  * @see com.sun.star.util.XCancellable
165  * @see com.sun.star.form.XReset
166  * @see com.sun.star.sdbc.XCloseable
167  * @see com.sun.star.sdbcx.XColumnsSupplier
168  * @see com.sun.star.sdb.XRowSetApproveBroadcaster
169  * @see com.sun.star.sdbc.ResultSet
170  * @see com.sun.star.sdbc.XResultSet
171  * @see com.sun.star.sdbc.XRow
172  * @see com.sun.star.sdbc.XColumnLocate
173  * @see com.sun.star.awt.XTabControllerModel
174  * @see com.sun.star.container.XIndexAccess
175  * @see com.sun.star.form.XSubmit
176  * @see com.sun.star.form.component.HTMLForm
177  * @see com.sun.star.sdbcx.XRowLocate
178  * @see com.sun.star.sdbc.XWarningsSupplier
179  * @see com.sun.star.container.XNameContainer
180  * @see com.sun.star.beans.XPropertySet
181  * @see com.sun.star.sdbc.RowSet
182  * @see ifc.script._XEventAttacherManager
183  * @see ifc.container._XElementAccess
184  * @see ifc.sdbcx._ResultSet
185  * @see ifc.container._XChild
186  * @see ifc.sdbc._XResultSetUpdate
187  * @see ifc.sdb._XResultSetAccess
188  * @see ifc.form._FormComponent
189  * @see ifc.form.component._DataForm
190  * @see ifc.sdbc._XResultSetMetaDataSupplier
191  * @see ifc.container._XEnumerationAccess
192  * @see ifc.sdbcx._XDeleteRows
193  * @see ifc.sdb._RowSet
194  * @see ifc.lang._XComponent
195  * @see ifc.sdbc._XRowSet
196  * @see ifc.sdbc._XRowUpdate
197  * @see ifc.form._XLoadable
198  * @see ifc.container._XNamed
199  * @see ifc.container._XIndexReplace
200  * @see ifc.io._XPersistObject
201  * @see ifc.container._XNameReplace
202  * @see ifc.container._XIndexContainer
203  * @see ifc.container._XNameAccess
204  * @see ifc.sdbc._XParameters
205  * @see ifc.util._XCancellable
206  * @see ifc.form._XReset
207  * @see ifc.sdbc._XCloseable
208  * @see ifc.sdbcx._XColumnsSupplier
209  * @see ifc.sdb._XRowSetApproveBroadcaster
210  * @see ifc.sdbc._ResultSet
211  * @see ifc.sdbc._XResultSet
212  * @see ifc.sdbc._XRow
213  * @see ifc.sdbc._XColumnLocate
214  * @see ifc.awt._XTabControllerModel
215  * @see ifc.container._XIndexAccess
216  * @see ifc.form._XSubmit
217  * @see ifc.form.component._HTMLForm
218  * @see ifc.sdbcx._XRowLocate
219  * @see ifc.sdbc._XWarningsSupplier
220  * @see ifc.container._XNameContainer
221  * @see ifc.beans._XPropertyAccess
222  * @see ifc.beans._XPropertyContainer
223  * @see ifc.beans._XPropertySet
224  * @see ifc.sdbc._RowSet
225  */
226 public class ODatabaseForm extends TestCase {
227     protected final static String dbSourceName = "ODatabaseFormDataSource";
228     private static int uniqueSuffix = 0;
229     private static String origDB = null;
230     private static String tmpDir = null;
231     protected XTextDocument xTextDoc = null;
232     private DBTools dbTools = null;
233     String tableName = null;
234     DBTools.DataSourceInfo srcInf = null;
235     boolean isMySQLDB = false;
236     protected XConnection conn = null;
237     private Object dbSrc = null;
238 
initialize(TestParameters tParam, PrintWriter log)239     protected void initialize(TestParameters tParam, PrintWriter log) {
240         //log.println( "creating a draw document" );
241         //xTextDoc = WriterTools.createTextDoc(t((XMultiServiceFactory) Param.getMSF));
242         tmpDir = utils.getOfficeTemp(((XMultiServiceFactory) tParam.getMSF()));
243 
244         origDB = util.utils.getFullTestDocName("TestDB/testDB.dbf");
245 
246         dbTools = new DBTools( (XMultiServiceFactory)tParam.getMSF(), log );
247 
248         // creating DataSource and registering it in DatabaseContext
249         String dbURL = (String) tParam.get("test.db.url");
250         String dbUser = (String) tParam.get("test.db.user");
251         String dbPassword = (String) tParam.get("test.db.password");
252 
253         log.println("Creating and registering DataSource ...");
254         srcInf = dbTools.newDataSourceInfo();
255 
256         if ((dbURL != null) && (dbUser != null) && (dbPassword != null)) {
257             isMySQLDB = true;
258             log.println("dbURL = " + dbURL);
259             log.println("dbUSER = " + dbUser);
260             log.println("dbPASSWORD = " + dbPassword);
261 
262             //DataSource for mysql db
263             try {
264                 tableName = "soffice_test_table";
265                 srcInf.URL = "jdbc:" + dbURL;
266                 srcInf.IsPasswordRequired = new Boolean(true);
267                 srcInf.Password = dbPassword;
268                 srcInf.User = dbUser;
269 
270                 PropertyValue[] propInfo = new PropertyValue[1];
271                 propInfo[0] = new PropertyValue();
272                 propInfo[0].Name = "JavaDriverClass";
273 //                propInfo[0].Value = "org.gjt.mm.mysql.Driver";
274                 propInfo[0].Value = "util.dddriver.Driver";
275                 srcInf.Info = propInfo;
276 
277                 dbSrc = srcInf.getDataSourceService();
278                 dbTools.reRegisterDB(dbSourceName, dbSrc);
279             } catch (com.sun.star.uno.Exception e) {
280                 log.println("Error while object test initialization :");
281                 e.printStackTrace(log);
282                 throw new StatusException("Error while object test" +
283                                           " initialization", e);
284             }
285         } else {
286             //DataSource for sdbc db
287             try {
288                 String myDbUrl = "sdbc:dbase:" + DBTools.dirToUrl(tmpDir);
289                 srcInf.URL = myDbUrl;
290 
291                 log.println("try to register '"+myDbUrl+"' as '"+dbSourceName+"'");
292 
293                 dbSrc = srcInf.getDataSourceService();
294                 dbTools.reRegisterDB(dbSourceName, dbSrc);
295             } catch (com.sun.star.uno.Exception e) {
296                 log.println("Error while object test initialization :");
297                 e.printStackTrace(log);
298                 throw new StatusException(
299                         "Error while object test initialization", e);
300             }
301 
302             String oldF = null;
303             String newF = null;
304 
305             do {
306                 tableName = "ODatabaseForm_tmp" + uniqueSuffix;
307                 oldF = utils.getFullURL(origDB);
308                 newF = utils.getOfficeTemp((XMultiServiceFactory) tParam.getMSF()) + tableName +
309                        ".dbf";
310             } while (!utils.tryOverwriteFile(((XMultiServiceFactory) tParam.getMSF()), oldF, newF) &&
311                      (uniqueSuffix++ < 50));
312         }
313     }
314 
315     /**
316      *  *    creating a Testenvironment for the interfaces to be tested
317      */
createTestEnvironment(TestParameters Param, PrintWriter log)318     protected synchronized TestEnvironment createTestEnvironment(TestParameters Param,
319                                                                  PrintWriter log) {
320         if (xTextDoc != null) {
321             try {
322                 XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
323                                             XCloseable.class, xTextDoc);
324                 closer.close(true);
325             } catch (com.sun.star.util.CloseVetoException e) {
326                 log.println("couldn't close document");
327             } catch (com.sun.star.lang.DisposedException e) {
328                 log.println("couldn't close document");
329             }
330 
331             log.println("Existing document disposed");
332         }
333 
334         log.println("creating a text document");
335         xTextDoc = WriterTools.createTextDoc(((XMultiServiceFactory) Param.getMSF()));
336 
337         //initialize test table
338         if (isMySQLDB) {
339             try {
340                 dbTools.initTestTableUsingJDBC(tableName, srcInf);
341             } catch (java.sql.SQLException e) {
342                 e.printStackTrace(log);
343                 throw new StatusException(Status.failed("Couldn't " + " init test table. SQLException..."));
344             } catch (java.lang.ClassNotFoundException e) {
345                 throw new StatusException(Status.failed("Couldn't " + "register mysql driver"));
346             }
347         }
348 
349         XInterface oObj = null;
350         XShapes oShapes = null;
351         XInterface oInstance = null;
352         XConnection connection = null;
353 
354 
355         // creation of testobject here
356         // first we write what we are intend to do to log file
357         log.println("creating a test environment");
358 
359         XNameContainer forms = FormTools.getForms(WriterTools.getDrawPage(
360                                                           xTextDoc));
361 
362         try {
363             String[] formNames = forms.getElementNames();
364 
365             for (int i = 0; i < formNames.length; i++) {
366                 log.println("Removing form '" + formNames[i] + "' ...");
367                 forms.removeByName(formNames[i]);
368             }
369         } catch (com.sun.star.lang.WrappedTargetException e) {
370             e.printStackTrace(log);
371         } catch (com.sun.star.container.NoSuchElementException e) {
372             e.printStackTrace(log);
373         }
374 
375         String[] formNames = forms.getElementNames();
376         FormTools.insertForm(xTextDoc, forms, "MyForm");
377         formNames = forms.getElementNames();
378 
379         XLoadable formLoader = null;
380 
381         try {
382             formLoader = FormTools.bindForm(xTextDoc, "MyForm", dbSourceName,
383                                             tableName);
384         } catch (com.sun.star.uno.Exception e) {
385             log.println("Cann't bind the form to source '" + dbSourceName +
386                         "', table '" + tableName + "' :");
387             e.printStackTrace(log);
388             throw new StatusException("Cann't bind a form", e);
389         }
390 
391 
392         // DEBUG
393         log.println("Forms before adding controls : ");
394         formNames = forms.getElementNames();
395 
396         for (int i = 0; i < formNames.length; i++) {
397             log.println("    '" + formNames[i] + "'");
398         }
399 
400         XControlShape shape1 = null;
401         XControlShape shape2 = null;
402 
403         try {
404 
405             log.println("Elements in the 'MyForm' :");
406 
407             XIndexAccess formElements1 = (XIndexAccess) UnoRuntime.queryInterface(
408                                                  XIndexAccess.class,
409                                                  forms.getByName("MyForm"));
410 
411             for (int i = 0; i < formElements1.getCount(); i++) {
412                 XNamed elemName = (XNamed) UnoRuntime.queryInterface(
413                                           XNamed.class,
414                                           formElements1.getByIndex(i));
415                 log.println("   '" + elemName.getName() + "'");
416             }
417 
418 
419             // END DEBUG
420             //put something on the drawpage
421             log.println("inserting some ControlShapes");
422             oShapes = DrawTools.getShapes(WriterTools.getDrawPage(xTextDoc));
423             shape1 = FormTools.createControlShape(xTextDoc, 3000, 4500, 15000,
424                                                   1000, "CommandButton");
425             shape2 = FormTools.createControlShape(xTextDoc, 5000, 3500, 7500,
426                                                   5000, "TextField");
427 
428             XControlShape shape3 = FormTools.createControlShape(xTextDoc, 2000,
429                                                                 1500, 1000,
430                                                                 1000,
431                                                                 "CheckBox");
432             oShapes.add((XShape) shape1);
433             oShapes.add((XShape) shape2);
434             oShapes.add(shape3);
435         } catch (Exception e) {
436             e.printStackTrace(log);
437         }
438 
439         log.println("Forms after adding controls : ");
440         formNames = forms.getElementNames();
441 
442         for (int i = 0; i < formNames.length; i++) {
443             log.println("    '" + formNames[i] + "'");
444         }
445 
446         try {
447             log.println("Elements in the 'MyForm' :");
448 
449             XIndexAccess formElements1 = (XIndexAccess) UnoRuntime.queryInterface(
450                                                  XIndexAccess.class,
451                                                  forms.getByName("MyForm"));
452 
453             for (int i = 0; i < formElements1.getCount(); i++) {
454                 XNamed elemName = (XNamed) UnoRuntime.queryInterface(
455                                           XNamed.class,
456                                           formElements1.getByIndex(i));
457                 log.println("   '" + elemName.getName() + "'");
458             }
459         } catch (Exception e) {
460             e.printStackTrace(log);
461         }
462 
463         formLoader.load();
464 
465         try {
466             oObj = (XForm) AnyConverter.toObject(new Type(XForm.class),
467                                                  (FormTools.getForms(
468                                                          WriterTools.getDrawPage(
469                                                                  xTextDoc)))
470                                                      .getByName("MyForm"));
471 
472             XPropertySet xSetProp = (XPropertySet) UnoRuntime.queryInterface(
473                                             XPropertySet.class, oObj);
474             connection = (XConnection) AnyConverter.toObject(
475                                  new Type(XConnection.class),
476                                  xSetProp.getPropertyValue("ActiveConnection"));
477         } catch (com.sun.star.uno.Exception e) {
478             log.println("Couldn't get Form");
479             e.printStackTrace(log);
480         }
481 
482 
483         // get a control
484         oInstance = FormTools.createControl(xTextDoc, "TextField");
485 
486         log.println("creating a new environment for drawpage object");
487 
488         TestEnvironment tEnv = new TestEnvironment(oObj);
489 
490 
491         // adding relation for closing connection while environment disposing.
492         this.conn = connection;
493 
494         // adding relation for XSubmit
495         XControlModel the_Model = shape2.getControl();
496         XControlAccess the_access = (XControlAccess) UnoRuntime.queryInterface(
497                                             XControlAccess.class,
498                                             xTextDoc.getCurrentController());
499         XControl cntrl = null;
500 
501         //now get the OEditControl
502         try {
503             cntrl = the_access.getControl(the_Model);
504             log.println(cntrl.getClass().getName());
505         } catch (com.sun.star.container.NoSuchElementException e) {
506             log.println("Couldn't get OEditControl");
507             e.printStackTrace(log);
508             throw new StatusException("Couldn't get OEditControl", e);
509         }
510 
511         XResultSet the_set = (XResultSet) UnoRuntime.queryInterface(
512                                      XResultSet.class, oObj);
513 
514         try {
515             the_set.first();
516         } catch (SQLException e) {
517             log.println("Cann't move cursor to the first row.");
518             e.printStackTrace();
519             throw new StatusException("Can't move cursor to the first row.", e);
520         }
521 
522         tEnv.addObjRelation("Model1", shape1.getControl());
523         tEnv.addObjRelation("Model2", shape2.getControl());
524 
525 
526         // adding an object for XNameReplace testing
527         log.println("adding oInstace as obj relation to environment");
528         tEnv.addObjRelation("INSTANCE", oInstance);
529 
530 
531         // INDEX : _XNameContainer
532         log.println("adding INDEX as obj relation to environment");
533         tEnv.addObjRelation("INDEX", "0");
534 
535 
536         // INDEX : _XNameReplace
537         log.println("adding NameReplaceIndex as obj relation to environment");
538         tEnv.addObjRelation("XNameReplaceINDEX", "2");
539 
540 
541         // INSTANCEn : _XNameContainer; _XNameReplace
542         log.println("adding INSTANCEn as obj relation to environment");
543 
544         //XComponent xComp = (XComponent)
545         //    UnoRuntime.queryInterface(XComponent.class, xDrawDoc);
546         String tc = (String) Param.get("THRCNT");
547         int THRCNT = 1;
548 
549         if (tc != null) {
550             THRCNT = Integer.parseInt(tc);
551         }
552 
553         for (int n = 1; n < (2 * (THRCNT + 1)); n++) {
554             log.println("adding INSTANCE" + n +
555                         " as obj relation to environment");
556             tEnv.addObjRelation("INSTANCE" + n,
557                                 FormTools.createControl(xTextDoc, "CheckBox"));
558         }
559 
560 
561         // adding relation for XNameContainer
562         tEnv.addObjRelation("XNameContainer.AllowDuplicateNames", new Object());
563 
564 
565         // adding relation for XPersistObject
566         tEnv.addObjRelation("OBJNAME", "stardiv.one.form.component.Form");
567 
568         if (the_set != null) {
569             log.println("The Form has a not empty ResultSet");
570         }
571 
572         // Adding obj relation for XRowSetApproveBroadcaster test
573         final XResultSet xResSet = (XResultSet) UnoRuntime.queryInterface(
574                                            XResultSet.class, oObj);
575         final XResultSetUpdate xResSetUpdate = (XResultSetUpdate) UnoRuntime.queryInterface(
576                                                        XResultSetUpdate.class,
577                                                        oObj);
578         final XRowSet xRowSet = (XRowSet) UnoRuntime.queryInterface(
579                                         XRowSet.class, oObj);
580         final PrintWriter logF = log;
581         tEnv.addObjRelation("XRowSetApproveBroadcaster.ApproveChecker",
582                             new ifc.sdb._XRowSetApproveBroadcaster.RowSetApproveChecker() {
583             public void moveCursor() {
584                 try {
585                     xResSet.beforeFirst();
586                     xResSet.afterLast();
587                 } catch (com.sun.star.sdbc.SQLException e) {
588                     logF.println("### _XRowSetApproveBroadcaster." + "RowSetApproveChecker.moveCursor() :");
589                     e.printStackTrace(logF);
590                 }
591             }
592 
593             public RowChangeEvent changeRow() {
594                 try {
595                     xResSet.first();
596 
597                     XRowUpdate row = (XRowUpdate) UnoRuntime.queryInterface(
598                                              XRowUpdate.class, xResSet);
599                     row.updateString(1, "1");
600                     xResSetUpdate.updateRow();
601                 } catch (com.sun.star.sdbc.SQLException e) {
602                     logF.println("### _XRowSetApproveBroadcaster." + "RowSetApproveChecker.changeRow() :");
603                     e.printStackTrace(logF);
604                 }
605 
606                 RowChangeEvent ev = new RowChangeEvent();
607                 ev.Action = com.sun.star.sdb.RowChangeAction.UPDATE;
608                 ev.Rows = 1;
609 
610                 return ev;
611             }
612 
613             public void changeRowSet() {
614                 try {
615                     xRowSet.execute();
616                 } catch (com.sun.star.sdbc.SQLException e) {
617                     logF.println("### _XRowSetApproveBroadcaster." + "RowSetApproveChecker.changeRowSet() :");
618                     e.printStackTrace(logF);
619                 }
620             }
621         });
622 
623 
624         // Adding relation for XColumnLocate test
625         tEnv.addObjRelation("XColumnLocate.ColumnName", DBTools.TST_STRING_F);
626 
627         // Adding relation for XParameters ifc test
628         Vector params = new Vector();
629 
630 
631         /*****  statement parameter types and their initial
632                 values must be added here as relation. */
633         params.add(new String("SAU99")) ;
634         params.add(new Boolean(false)) ;
635         params.add(new Byte((byte) 123)) ;
636         params.add(new Short((short) 234)) ;
637         params.add(new Integer(12345)) ;
638         params.add(new Long(23456)) ;
639         params.add(new Float(1.234)) ;
640         params.add(new Double(2.345)) ;
641         params.add(new byte[] {1, 2, 3}) ;
642         Date d = new Date();
643         d.Day = 26; d.Month = 1; d.Year = 2001;
644         params.add(d) ;
645         Time t = new Time();
646         t.Hours = 1; t.HundredthSeconds = 12; t.Minutes = 25; t.Seconds = 14;
647         params.add(t) ;
648         DateTime dt = new DateTime();
649         dt.Day = 26; dt.Month = 1; dt.Year = 2001; dt.Hours = 1;
650         dt.HundredthSeconds = 12; dt.Minutes = 25; dt.Seconds = 14;
651         params.add(dt) ;
652         tEnv.addObjRelation("XParameters.ParamValues", params);
653 
654         // Adding relation for XCompletedExecution
655         tEnv.addObjRelation("InteractionHandlerChecker", new InteractionHandlerImpl());
656 
657         // Adding for XWarningSupplier
658         tEnv.addObjRelation("CheckWarningsSupplier", new Boolean(isMySQLDB));
659 
660         // Adding relation for XDatabaseParameterBroadcaster
661         tEnv.addObjRelation("ParameterListenerChecker", new ODatabaseForm.ParameterListenerImpl());
662         XPropertySet xSetProp = (XPropertySet) UnoRuntime.queryInterface
663             (XPropertySet.class, oObj) ;
664         try {
665             xSetProp.setPropertyValue("DataSourceName", dbSourceName) ;
666             if(isMySQLDB) {
667                 xSetProp.setPropertyValue("Command", "SELECT Column0  FROM soffice_test_table  WHERE ( (  Column0 = :param1 ) )");
668             }
669             else {
670                 xSetProp.setPropertyValue("Command", "SELECT \"_TEXT\" FROM \"ODatabaseForm_tmp0\" WHERE ( ( \"_TEXT\" = :param1 ) )");
671             }
672 
673             xSetProp.setPropertyValue("CommandType",
674                 new Integer(CommandType.COMMAND)) ;
675         }
676         catch(Exception e) {
677         }
678 
679         // Adding relation for XResultSetUpdate
680         final XRowUpdate xRowUpdate = (XRowUpdate) UnoRuntime.queryInterface(
681                                               XRowUpdate.class, oObj);
682         final XRow xRow = (XRow) UnoRuntime.queryInterface(XRow.class, oObj);
683 
684         tEnv.addObjRelation("XResultSetUpdate.UpdateTester",
685                             new ifc.sdbc._XResultSetUpdate.UpdateTester() {
686             String lastUpdate = null;
687 
688             public int rowCount() throws SQLException {
689                 int prevPos = xResSet.getRow();
690                 xResSet.last();
691 
692                 int count = xResSet.getRow();
693                 xResSet.absolute(prevPos);
694 
695                 return count;
696             }
697 
698             public void update() throws SQLException {
699                 lastUpdate = xRow.getString(1);
700                 lastUpdate += "_";
701                 xRowUpdate.updateString(1, lastUpdate);
702             }
703 
704             public boolean wasUpdated() throws SQLException {
705                 String getStr = xRow.getString(1);
706 
707                 return lastUpdate.equals(getStr);
708             }
709 
710             public int currentRow() throws SQLException {
711                 return xResSet.getRow();
712             }
713         });
714 
715         // Adding relations for XRow as a Vector with all data
716         // of current row of RowSet.
717 
718         Vector rowData = new Vector();
719 
720         for (int i = 0; i < DBTools.TST_TABLE_VALUES[0].length; i++) {
721             rowData.add(DBTools.TST_TABLE_VALUES[0][i]);
722         }
723 
724         tEnv.addObjRelation("CurrentRowData", rowData);
725 
726         // Adding relation for XRowUpdate
727         XRow row = (XRow) UnoRuntime.queryInterface(XRow.class, oObj);
728         tEnv.addObjRelation("XRowUpdate.XRow", row);
729 
730 
731         tEnv.addObjRelation("XPropertyContainer.propertyNotRemovable", "Cycle");
732 
733         PropertyValue propVal = new PropertyValue();
734         propVal.Name = "Name";
735         propVal.Value = "Text since XPropertyAccess";
736 
737         tEnv.addObjRelation("XPropertyAccess.propertyToChange", propVal);
738 
739         return tEnv;
740     } // finish method getTestEnvironment
741 
742     /**
743     * Closes connection of <code>RowSet</code> instance created.
744     */
cleanup(TestParameters Param, PrintWriter log)745     protected void cleanup(TestParameters Param, PrintWriter log) {
746         log.println("closing connection...");
747         try {
748             conn.close();
749         } catch (com.sun.star.uno.Exception e) {
750             log.println("Can't close the connection");
751             e.printStackTrace(log);
752         } catch (com.sun.star.lang.DisposedException e) {
753             log.println("Connection was already closed. It's OK.");
754         }
755 
756 
757         log.println("closing data source...");
758         try {
759             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
760                                         XCloseable.class, dbSrc);
761             closer.close(true);
762         } catch (com.sun.star.util.CloseVetoException e) {
763             log.println("couldn't close data source");
764         } catch (com.sun.star.lang.DisposedException e) {
765             log.println("couldn't close data source");
766         }
767 
768 
769         log.println("closing document...");
770 
771         try {
772             XCloseable closer = (XCloseable) UnoRuntime.queryInterface(
773                                         XCloseable.class, xTextDoc);
774             closer.close(true);
775         } catch (com.sun.star.util.CloseVetoException e) {
776             log.println("couldn't close document");
777         } catch (com.sun.star.lang.DisposedException e) {
778             log.println("couldn't close document");
779         }
780 
781         log.println("revoking data source...");
782         try {
783             dbTools.revokeDB(dbSourceName);
784         } catch (com.sun.star.container.NoSuchElementException e){
785         } catch (com.sun.star.uno.Exception e) {
786             log.println("Error while object test cleaning up :");
787             e.printStackTrace(log);
788             throw new StatusException("Error while object test cleaning up", e);
789         }
790     }
791 
792     /**
793      * Implementation of interface _XDatabaseParameterBroadcaster.CheckParameterListener
794      * for the XDatabaseParameterBroadcaster test
795      * @see ifc.form._XDatabaseParameterBroadcaster
796      */
797     public class ParameterListenerImpl implements _XDatabaseParameterBroadcaster.CheckParameterListener {
798         boolean listenerWasCalled = false;
799         PrintWriter log = new PrintWriter(System.out);
800 
801         /**
802          * Return true, if the listener was called, false otherwise.
803          * @return True, if any other method of the listener was called.
804          */
checkListener()805         public boolean checkListener() {
806             return listenerWasCalled;
807         }
808 
809         /**
810          * Take the DataBaseParameterEvent and fill it with a meaningful value.
811          * @param e The database parameter that will be filled with a value.
812          * @return True, if the value could be filled.
813          */
approveParameter(DatabaseParameterEvent e)814         public boolean approveParameter(DatabaseParameterEvent e) {
815             log.println("### ParameterListenerImpl: approve called.");
816             XIndexAccess params = e.Parameters;
817             int count = params.getCount();
818             try {
819                 for(int i=0; i<count; i++) {
820                     log.println("### _XDatabaseParameterBroadcaster.ParameterListenerImpl: Parameter "+i+": "+params.getByIndex(i));
821                     XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, params.getByIndex(i));
822                     log.println("### _XDatabaseParameterBroadcaster.ParameterListenerImpl: Parameter Name: '"+xProp.getPropertyValue("Name") + "' is set to Value '1'");
823                     xProp.setPropertyValue("Value", new Integer(1));
824                     listenerWasCalled = true;
825                 }
826             }
827             catch(Exception eI) {
828                 log.println("### _XDatabaseParameterBroadcaster.ParameterListenerImpl: Exception!");
829                 eI.printStackTrace(log);
830                 return false;
831             }
832             return true;
833         }
834 
835         /**
836          * Dummy implemetnation. Do nothing, just log
837          * @param o Ignore.
838          */
disposing(EventObject o)839         public void disposing(EventObject o) {
840             log.println("### _XDatabaseParameterBroadcaster.ParameterListenerImpl: disposing");
841         }
842 
843         /**
844          * Set a log writer, so messages go to log instead of Standard.out
845          * @param log The log messages get printed to.
846          */
setLog(PrintWriter log)847         public void setLog(PrintWriter log) {
848             this.log = log;
849         }
850 
851     }
852 
853 
854     /**
855      * Implementation of interface _XCompletedExecution.CheckInteractionHandler
856      * for the XCompletedExecution test
857      * @see ifc.sdb._XCompletedExecution
858      */
859     public class InteractionHandlerImpl implements _XCompletedExecution.CheckInteractionHandler {
860         private boolean handlerWasUsed = false;
861         private PrintWriter log = new PrintWriter(System.out);
862 
checkInteractionHandler()863         public boolean checkInteractionHandler() {
864             return handlerWasUsed;
865         }
866 
handle(XInteractionRequest xInteractionRequest)867         public void handle(XInteractionRequest xInteractionRequest) {
868             log.println("### _XCompletedExecution.InteractionHandlerImpl: handle called.");
869             handlerWasUsed = true;
870 
871             Object o = xInteractionRequest.getRequest();
872             ParametersRequest req = (ParametersRequest)o;
873             XIndexAccess params = req.Parameters;
874             int count = params.getCount();
875             try {
876                 for(int i=0; i<count; i++) {
877                     Object aObject = params.getByIndex(i);
878                     Any any = (Any)aObject;
879                     log.println("### _XCompletedExecution.InteractionHandlerImpl: Parameter "+i+": "+params.getByIndex(i));
880                     XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, params.getByIndex(i));
881                     log.println("### _XCompletedExecution.InteractionHandlerImpl: Parameter Name: '"+xProp.getPropertyValue("Name") + "' is set to Value '1'");
882                     xProp.setPropertyValue("Value", new Integer(1));
883                     handlerWasUsed = true;
884                 }
885             }
886             catch(Exception eI) {
887                 log.println("### _XCompletedExecution.InteractionHandlerImpl: Exception!");
888                 eI.printStackTrace(log);
889             }
890         }
891 
setLog(PrintWriter log)892         public void setLog(PrintWriter log) {
893             this.log = log;
894         }
895 
896     }
897 } // finish class ODatabaseForm
898