2 * This file is part of the LibreOffice project.
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/.
8 * This file incorporates work covered by the following license notice:
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 .
19 import com
.sun
.star
.beans
.NamedValue
;
20 import com
.sun
.star
.beans
.PropertyValue
;
21 import com
.sun
.star
.container
.XIndexAccess
;
22 import java
.io
.PrintWriter
;
24 import lib
.StatusException
;
26 import lib
.TestEnvironment
;
27 import lib
.TestParameters
;
29 import util
.FormTools
;
30 import util
.WriterTools
;
32 import com
.sun
.star
.beans
.XPropertySet
;
33 import com
.sun
.star
.drawing
.XControlShape
;
34 import com
.sun
.star
.form
.XBoundComponent
;
35 import com
.sun
.star
.form
.XForm
;
36 import com
.sun
.star
.form
.XLoadable
;
37 import com
.sun
.star
.lang
.XMultiServiceFactory
;
38 import com
.sun
.star
.lang
.XComponent
;
39 import com
.sun
.star
.sdbc
.XConnection
;
40 import com
.sun
.star
.sdbc
.XResultSetUpdate
;
41 import com
.sun
.star
.sdb
.XDocumentDataSource
;
42 import com
.sun
.star
.text
.XTextDocument
;
43 import com
.sun
.star
.uno
.AnyConverter
;
44 import com
.sun
.star
.uno
.Type
;
45 import com
.sun
.star
.uno
.UnoRuntime
;
46 import com
.sun
.star
.uno
.XInterface
;
47 import com
.sun
.star
.util
.XCloseable
;
48 import java
.util
.ArrayList
;
54 * Test for object which is represented by service
55 * <code>com.sun.star.form.component.DateField</code>. <p>
56 * Object implements the following interfaces :
58 * <li> <code>com::sun::star::io::XPersistObject</code></li>
59 * <li> <code>com::sun::star::awt::UnoControlDateFieldModel</code></li>
60 * <li> <code>com::sun::star::form::XReset</code></li>
61 * <li> <code>com::sun::star::form::XBoundComponent</code></li>
62 * <li> <code>com::sun::star::form::FormComponent</code></li>
63 * <li> <code>com::sun::star::form::component::DateField</code></li>
64 * <li> <code>com::sun::star::beans::XFastPropertySet</code></li>
65 * <li> <code>com::sun::star::beans::XMultiPropertySet</code></li>
66 * <li> <code>com::sun::star::form::XUpdateBroadcaster</code></li>
67 * <li> <code>com::sun::star::form::DataAwareControlModel</code></li>
68 * <li> <code>com::sun::star::beans::XPropertyState</code></li>
69 * <li> <code>com::sun::star::form::FormControlModel</code></li>
70 * <li> <code>com::sun::star::container::XNamed</code></li>
71 * <li> <code>com::sun::star::lang::XComponent</code></li>
72 * <li> <code>com::sun::star::lang::XEventListener</code></li>
73 * <li> <code>com::sun::star::beans::XPropertyAccess</code></li>
74 * <li> <code>com::sun::star::beans::XPropertyContainer</code></li>
75 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
76 * <li> <code>com::sun::star::form::XLoadListener</code></li>
77 * <li> <code>com::sun::star::container::XChild</code></li>
79 * The following files used by this test :
81 * <li><b> TestDB </b> (directory) : directory with test database </li>
82 * <li><b> TestDB/TestDB.dbf </b> : table file. See
83 * {@link util.DBTools DBTools} class for more information.</li>
85 * This object test <b> is NOT </b> designed to be run in several
86 * threads concurently.
87 * @see com.sun.star.io.XPersistObject
88 * @see com.sun.star.awt.UnoControlDateFieldModel
89 * @see com.sun.star.form.XReset
90 * @see com.sun.star.form.XBoundComponent
91 * @see com.sun.star.form.FormComponent
92 * @see com.sun.star.form.component.DateField
93 * @see com.sun.star.beans.XFastPropertySet
94 * @see com.sun.star.beans.XMultiPropertySet
95 * @see com.sun.star.form.XUpdateBroadcaster
96 * @see com.sun.star.form.DataAwareControlModel
97 * @see com.sun.star.beans.XPropertyState
98 * @see com.sun.star.form
99 * @see com.sun.star.container.XNamed
100 * @see com.sun.star.lang.XComponent
101 * @see com.sun.star.lang.XEventListener
102 * @see com.sun.star.beans.XPropertyAccess
103 * @see com.sun.star.beans.XPropertyContainer
104 * @see com.sun.star.beans.XPropertySet
105 * @see com.sun.star.form.XLoadListener
106 * @see com.sun.star.container.XChild
107 * @see ifc.io._XPersistObject
108 * @see ifc.awt._UnoControlDateFieldModel
109 * @see ifc.form._XReset
110 * @see ifc.form._XBoundComponent
111 * @see ifc.form._FormComponent
112 * @see ifc.form.component._DateField
113 * @see ifc.beans._XFastPropertySet
114 * @see ifc.beans._XMultiPropertySet
115 * @see ifc.form._XUpdateBroadcaster
116 * @see ifc.form._DataAwareControlModel
117 * @see ifc.beans._XPropertyState
118 * @see ifc.form._FormControlModel
119 * @see ifc.container._XNamed
120 * @see ifc.lang._XComponent
121 * @see ifc.lang._XEventListener
122 * @see ifc.beans._XPropertySet
123 * @see ifc.form._XLoadListener
124 * @see ifc.container._XChild
126 public class GenericModelTest
extends TestCase
{
127 private XTextDocument m_xTextDoc
;
128 private Object m_dbSrc
= null;
129 private DBTools
.DataSourceInfo m_srcInf
= null;
131 * This is the name of the Data Base which the test uses: "APITestDatabase"
133 private final static String m_dbSourceName
= "APITestDatabase";
134 private final static String m_TestDB
= "TestDB";
135 private DBTools m_dbTools
= null;
137 private boolean m_ConnectionColsed
= false;
140 * describes the kind of the shape which should be created.
141 * Example: m_kindOfshape=DateFiled
143 protected String m_kindOfControl
= null;
146 * If your object needs some special property values you can specify them with this
147 * <CODE>ArrayList</CODE>. You have to add a <CODE>NamedValue</CODE> to this list.
149 * NamedValue myProp = new NamedValue();
150 * myProp.Name = "Test";
151 * myProp.Value = "My special Value";
152 * m_propertiesToSet.add(myProp);
154 protected ArrayList
<NamedValue
> m_propertiesToSet
= new ArrayList
<NamedValue
>();
157 * This variable contains the name of the property which should be changed while
158 * interface <CODE>com::sun::star::form::XUpdateBroadcaster</CODE> is tested. The
159 * interface test needs the <CODE>ObjectRelation</CODE>
160 * "XUpdateBroadcaster.Checker" which is a <CODE>ifc.form._XUpdateBroadcaster.UpdateChecker</CODE>.
161 * @see ifc.form._XUpdateBroadcaster.UpdateChecker
162 * @see ifc.form._XUpdateBroadcaster
164 protected String m_ChangePropertyName
= null;
166 * This variable contains the value the property should be set while
167 * interface <CODE>com::sun::star::form::XUpdateBroadcaster</CODE> is tested.
168 * The interface test needs the <CODE>ObjectRelation</CODE>
169 * "XUpdateBroadcaster.Checker" which is a <CODE>ifc.form._XUpdateBroadcaster.UpdateChecker</CODE>.
170 * Normally the <CODE>Checker</CODE> uses <CODE>util.ValueChanger</CODE> to change
171 * the value of the property. If the current of this property is NULL the
172 * <CODE>ValueChanger</CODE> is unable to change the value. In this case the value
173 * of this variable was used.
175 protected Object m_ChangePropertyValue
= null;
178 * This variable contains the implementation name of the object.
180 protected String m_ObjectName
= null;
183 * For local implementations of <CODE>Checker</CODE> this variable contains the
184 * <CODE>FormLoader</CODE>
186 protected XLoadable m_XFormLoader
= null;
188 * For local implementations of <CODE>Checker</CODE> this variable contains the
189 * <CODE>XPropertySet</CODE>
191 protected XPropertySet m_XPS
= null;
193 * For local implementations of <CODE>Checker</CODE> this variable contains the
194 * <CODE>Control</CODE>
196 protected XInterface m_XCtrl
= null;
198 * The interface test of <CODE>ifc.form._DataWareControlModel</CODE> expects an
199 * object relation <CODE>'LC'</CODE>. This is a <CODE>XControlModel</CODE> of a shape.
200 * This variable contains the kind of shape to create for the interface test,
202 * @see ifc.form._DataAwareControlModel
204 protected String m_LCShape_Type
= null;
207 * If this variable is true some more debug info was logged. It was set by the parameter variable
208 * <code>debug_is_active</code>
210 private boolean debug
= false;
213 * Creates Writer document where controls are placed.
214 * @param tParam the test paremter
215 * @param log the log writer
217 protected void initialize(TestParameters tParam
, PrintWriter log
) {
218 log
.println("creating a textdocument");
219 m_xTextDoc
= WriterTools
.createTextDoc(((XMultiServiceFactory
) tParam
.getMSF()));
220 m_ConnectionColsed
= false;
221 debug
= tParam
.getBool(util
.PropertyName
.DEBUG_IS_ACTIVE
);
222 m_propertiesToSet
.clear();
226 * close the connection
227 * close the data source
229 * revoke the data source
230 * @param tParam the test parameter
231 * @param log the log writer
233 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
234 log
.println("closing connection...");
236 // some interface tests call cleanup to reset the environment. If such
237 // a test is the last one cleanup was called twice. The second call
238 // causes then nasty exceptions...
239 if (m_ConnectionColsed
) return;
242 XIndexAccess forms
= UnoRuntime
.queryInterface( XIndexAccess
.class,
243 FormTools
.getForms( WriterTools
.getDrawPage( m_xTextDoc
) ) );
244 XForm myForm
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class),
245 forms
.getByIndex(0));
249 log
.println("ERROR: could not get 'Standard' from drawpage!");
251 log
.println("the draw page contains folowing elemtens:");
252 String
[] elements
= FormTools
.getForms(WriterTools
.getDrawPage(m_xTextDoc
)).getElementNames();
253 for (int i
= 0; i
< elements
.length
; i
++){
254 log
.println("Element[" + i
+ "] :" + elements
[i
]);
259 XPropertySet xSetProp
= UnoRuntime
.queryInterface( XPropertySet
.class, myForm
);
260 XConnection connection
= UnoRuntime
.queryInterface( XConnection
.class, xSetProp
.getPropertyValue( "ActiveConnection" ) );
261 if ( connection
== null )
264 log
.println("ERROR: could not get property 'ActiveConnection' from the XForm");
270 } catch (Exception e
) {
271 log
.println("ERROR: Can't close the connection: " + e
.toString());
272 e
.printStackTrace( log
);
275 log
.println("closing data source...");
277 XCloseable closer
= UnoRuntime
.queryInterface(
278 XCloseable
.class, m_dbSrc
);
279 if ( closer
== null )
281 XDocumentDataSource dataSource
= UnoRuntime
.queryInterface(
282 XDocumentDataSource
.class, m_dbSrc
);
283 if ( dataSource
!= null )
284 closer
= UnoRuntime
.queryInterface(
285 XCloseable
.class, dataSource
.getDatabaseDocument() );
287 if (debug
&& closer
==null){
288 log
.println("ERROR: couldn't get 'XCloseable' from DataSource");
291 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
292 log
.println("ERROR: couldn't close data source: " + e
.toString());
293 } catch (com
.sun
.star
.lang
.DisposedException e
) {
294 log
.println("ERROR: couldn't close data source: " + e
.toString());
295 } catch (Exception e
) {
296 log
.println("ERROR: couldn't close data source: " + e
.toString());
299 log
.println("disposing data source...");
301 XComponent dataSourceComp
= UnoRuntime
.queryInterface(
302 XComponent
.class, m_dbSrc
);
303 dataSourceComp
.dispose();
305 catch (Exception e
) {
306 log
.println("couldn't dispose the data source");
309 log
.println("closing document...");
312 XCloseable closer
= UnoRuntime
.queryInterface(
313 XCloseable
.class, m_xTextDoc
);
315 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
316 log
.println("ERROR: couldn't close document: " + e
.toString());
317 } catch (com
.sun
.star
.lang
.DisposedException e
) {
318 log
.println("ERROR: couldn't close document: " + e
.toString());
319 } catch (Exception e
) {
320 log
.println("ERROR: couldn't close document: " + e
.toString());
323 log
.println("revoking data source...");
325 m_dbTools
.revokeDB(m_dbSourceName
);
326 } catch (com
.sun
.star
.container
.NoSuchElementException e
){
327 } catch (com
.sun
.star
.uno
.Exception e
) {
328 log
.println("ERROR: Error while object test cleaning up: " + e
.toString());
331 m_ConnectionColsed
= true;
335 * Creating a Testenvironment for the interfaces to be tested.
336 * First <code>TestDB</code> database is registered.
337 * Creates DateField in the Form, then binds it to TestDB
338 * database and returns Field's control. <p>
339 * Object relations created :
341 * <li> <code>'OBJNAME'</code> for
342 * {@link ifc.io._XPersistObject} : name of service which is
343 * represented by this object. </li>
344 * <li> <code>'LC'</code> for {@link ifc.form._DataAwareControlModel}.
345 * Specifies the value for LabelControl property. It is
346 * <code>FixedText</code> component added to the document.</li>
347 * <li> <code>'FL'</code> for
348 * {@link ifc.form._DataAwareControlModel} interface.
349 * Specifies XLoadable implementation which connects form to
350 * the data source.</li>
351 * <li> <code>'XUpdateBroadcaster.Checker'</code> : <code>
352 * _XUpdateBroadcaster.UpdateChecker</code> interface implementation
353 * which can update, commit data and check if the data was successfully
355 * <li> <code>'DataAwareControlModel.NewFieldName'</code> : for
356 * <code>com.sun.star.form.DataAwareControlModel</code> service
357 * which contains new name of the field ('_DATE') to bind control to.
359 * <li> <code>'XFastPropertySet.ExcludeProps'</code> : for
360 * <code>com.sun.star.beans.XFastPropertySet</code> interface
361 * the property FormatKey can have only restricted set of values.
364 * @see ifc.form._XUpdateBroadcaster
365 * @param Param the test parameter
366 * @param log the log writer
367 * @return a test environment
369 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
,
371 XInterface oObj
= null;
372 XControlShape aShape
= null;
373 XMultiServiceFactory xMSF
= (XMultiServiceFactory
) Param
.getMSF();
376 log
.println("adding contol shape '" + m_kindOfControl
+ "'");
377 aShape
= FormTools
.createControlShape(m_xTextDoc
, 3000,
380 } catch (Exception e
){
381 e
.printStackTrace(log
);
382 throw new StatusException("Couldn't create following control shape (m_kindOfControl): '" +
383 m_kindOfControl
+ "': ", e
);
387 WriterTools
.getDrawPage(m_xTextDoc
).add(aShape
);
388 oObj
= aShape
.getControl();
390 log
.println("Implementation name: " + util
.utils
.getImplName(oObj
));
393 String sourceTestDB
= utils
.getFullURL(utils
.getFullTestDocName("TestDB/testDB.dbf"));
394 String destTestDB
= utils
.getOfficeTemp(xMSF
);
395 destTestDB
= utils
.getFullURL(destTestDB
+ "testDB.dbf");
397 log
.println("copy '"+sourceTestDB
+ "' -> '" + destTestDB
+ "'");
398 utils
.copyFile(xMSF
, sourceTestDB
, destTestDB
);
400 m_dbTools
= new DBTools( xMSF
, log
);
401 String tmpDir
= utils
.getOfficeTemp((xMSF
));
403 m_srcInf
= m_dbTools
.newDataSourceInfo();
404 m_srcInf
.URL
= "sdbc:dbase:" + DBTools
.dirToUrl(tmpDir
);
405 log
.println("data source: " + m_srcInf
.URL
);
407 m_dbSrc
= m_srcInf
.getDataSourceService();
408 m_dbTools
.reRegisterDB(m_dbSourceName
, m_dbSrc
);
410 m_XFormLoader
= FormTools
.bindForm(m_xTextDoc
, m_dbSourceName
,
412 } catch (com
.sun
.star
.uno
.Exception e
) {
413 log
.println("!!! Can't access TestDB !!!");
414 e
.printStackTrace(log
);
415 throw new StatusException("Can't access TestDB", e
);
418 log
.println("creating a new environment for object");
420 TestEnvironment tEnv
= new TestEnvironment(oObj
);
422 tEnv
.addObjRelation("OBJNAME", m_ObjectName
);
424 log
.println("adding shape '" + m_LCShape_Type
+"' for DataAwareControlModel test");
425 aShape
= FormTools
.createControlShape(m_xTextDoc
, 6000, 4500, 15000,
426 10000, m_LCShape_Type
);
427 WriterTools
.getDrawPage(m_xTextDoc
).add(aShape
);
429 m_XPS
= UnoRuntime
.queryInterface(
430 XPropertySet
.class, oObj
);
433 NamedValue prop
= null;
435 for (i
= 0; i
< m_propertiesToSet
.size(); i
++){
436 prop
= m_propertiesToSet
.get(i
);
438 log
.println("setting property: '"+prop
.Name
+"' to value '"+prop
.Value
.toString()+"'");
440 m_XPS
.setPropertyValue(prop
.Name
, prop
.Value
);
442 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
443 e
.printStackTrace(log
);
444 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
445 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
446 e
.printStackTrace(log
);
447 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
448 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
449 e
.printStackTrace(log
);
450 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
451 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
452 e
.printStackTrace(log
);
453 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
454 } catch (java
.lang
.ClassCastException e
) {
455 e
.printStackTrace(log
);
456 throw new StatusException("Couldn't get property on index '" + i
+ "': ", e
);
459 // added LabelControl for 'DataAwareControlModel'
460 tEnv
.addObjRelation("LC", aShape
.getControl());
462 // added FormLoader for 'DataAwareControlModel'
463 tEnv
.addObjRelation("FL", m_XFormLoader
);
465 // adding relation for XUpdateBroadcaster
468 tEnv
.addObjRelation("XUpdateBroadcaster.Checker",
469 new Checker(m_XFormLoader
, m_XPS
, m_XCtrl
, m_ChangePropertyName
, m_ChangePropertyValue
));
471 // adding relation for DataAwareControlModel service
472 tEnv
.addObjRelation("DataAwareControlModel.NewFieldName",
475 //adding ObjRelation for XPersistObject
476 tEnv
.addObjRelation("PSEUDOPERSISTENT", new Boolean(true));
478 // adding relation for XFastPropertySet
479 java
.util
.HashSet
<String
> exclude
= new java
.util
.HashSet
<String
>();
480 exclude
.add("FormatKey");
481 tEnv
.addObjRelation("XFastPropertySet.ExcludeProps", exclude
);
483 PropertyValue propVal
= new PropertyValue();
484 propVal
.Name
= "HelpText";
485 propVal
.Value
= "Text since XPropertyAccess";
486 tEnv
.addObjRelation("XPropertyAccess.propertyToChange", propVal
);
487 tEnv
.addObjRelation("XPropertyContainer.propertyNotRemovable", "HelpText");
491 } // finish method getTestEnvironment
494 static class Checker
implements ifc
.form
._XUpdateBroadcaster
.UpdateChecker
{
495 private Object lastValue
= null;
496 XLoadable formLoaderF
= null;
497 XPropertySet ps
= null;
498 XInterface ctrl
= null;
499 String ChangePropertyName
= null;
500 Object ChangePropertyValue
= null;
502 public Checker(XLoadable xl
, XPropertySet ps
, XInterface ctrl
, String ChangePropertyName
, Object ChangePropertyValue
) {
506 this.ChangePropertyName
=ChangePropertyName
;
507 this.ChangePropertyValue
=ChangePropertyValue
;
510 public void update() throws com
.sun
.star
.uno
.Exception
{
511 if (!formLoaderF
.isLoaded()) {
515 lastValue
= util
.ValueChanger
.changePValue(ps
.getPropertyValue(ChangePropertyName
));
517 if (lastValue
== null){
519 if (ChangePropertyValue
!= null){
521 lastValue
= ChangePropertyValue
;
525 String msg
= "The initial value of the property '" + ChangePropertyName
+ "' is NULL\n";
526 msg
+= "The member variable 'm_ChangePropertyValue' is NULL\n";
527 msg
+= "Could not change Property.";
529 throw new StatusException(Status
.failed(msg
));
532 ps
.setPropertyValue(ChangePropertyName
, lastValue
);
535 public void commit() throws com
.sun
.star
.sdbc
.SQLException
{
536 XBoundComponent bound
= UnoRuntime
.queryInterface(
537 XBoundComponent
.class, ctrl
);
538 XResultSetUpdate update
= UnoRuntime
.queryInterface(
539 XResultSetUpdate
.class,
546 public boolean wasCommited() throws com
.sun
.star
.uno
.Exception
{
547 formLoaderF
.reload();
549 Object newValue
= ps
.getPropertyValue(ChangePropertyName
);
551 return (newValue
!= null) && (util
.ValueComparer
.equalValue(lastValue
, newValue
));
554 } // finish class GenericModelTest