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