1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: GenericModelTest.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 import com
.sun
.star
.beans
.NamedValue
;
32 import com
.sun
.star
.beans
.PropertyValue
;
33 import java
.io
.PrintWriter
;
35 import lib
.StatusException
;
37 import lib
.TestEnvironment
;
38 import lib
.TestParameters
;
40 import util
.FormTools
;
41 import util
.WriterTools
;
43 import com
.sun
.star
.beans
.XPropertySet
;
44 import com
.sun
.star
.drawing
.XControlShape
;
45 import com
.sun
.star
.drawing
.XShape
;
46 import com
.sun
.star
.form
.XBoundComponent
;
47 import com
.sun
.star
.form
.XForm
;
48 import com
.sun
.star
.form
.XLoadable
;
49 import com
.sun
.star
.lang
.XMultiServiceFactory
;
50 import com
.sun
.star
.lang
.XComponent
;
51 import com
.sun
.star
.sdbc
.XConnection
;
52 import com
.sun
.star
.sdbc
.XResultSetUpdate
;
53 import com
.sun
.star
.sdb
.XDocumentDataSource
;
54 import com
.sun
.star
.text
.XTextDocument
;
55 import com
.sun
.star
.uno
.AnyConverter
;
56 import com
.sun
.star
.uno
.Type
;
57 import com
.sun
.star
.uno
.UnoRuntime
;
58 import com
.sun
.star
.uno
.XInterface
;
59 import com
.sun
.star
.util
.XCloseable
;
60 import java
.util
.ArrayList
;
66 * Test for object which is represented by service
67 * <code>com.sun.star.form.component.DateField</code>. <p>
68 * Object implements the following interfaces :
70 * <li> <code>com::sun::star::io::XPersistObject</code></li>
71 * <li> <code>com::sun::star::awt::UnoControlDateFieldModel</code></li>
72 * <li> <code>com::sun::star::form::XReset</code></li>
73 * <li> <code>com::sun::star::form::XBoundComponent</code></li>
74 * <li> <code>com::sun::star::form::FormComponent</code></li>
75 * <li> <code>com::sun::star::form::component::DateField</code></li>
76 * <li> <code>com::sun::star::beans::XFastPropertySet</code></li>
77 * <li> <code>com::sun::star::beans::XMultiPropertySet</code></li>
78 * <li> <code>com::sun::star::form::XUpdateBroadcaster</code></li>
79 * <li> <code>com::sun::star::form::DataAwareControlModel</code></li>
80 * <li> <code>com::sun::star::beans::XPropertyState</code></li>
81 * <li> <code>com::sun::star::form::FormControlModel</code></li>
82 * <li> <code>com::sun::star::container::XNamed</code></li>
83 * <li> <code>com::sun::star::lang::XComponent</code></li>
84 * <li> <code>com::sun::star::lang::XEventListener</code></li>
85 * <li> <code>com::sun::star::beans::XPropertyAccess</code></li>
86 * <li> <code>com::sun::star::beans::XPropertyContainer</code></li>
87 * <li> <code>com::sun::star::beans::XPropertySet</code></li>
88 * <li> <code>com::sun::star::form::XLoadListener</code></li>
89 * <li> <code>com::sun::star::container::XChild</code></li>
91 * The following files used by this test :
93 * <li><b> TestDB </b> (directory) : directory with test database </li>
94 * <li><b> TestDB/TestDB.dbf </b> : table file. See
95 * {@link util.DBTools DBTools} class for more information.</li>
97 * This object test <b> is NOT </b> designed to be run in several
98 * threads concurently.
99 * @see com.sun.star.io.XPersistObject
100 * @see com.sun.star.awt.UnoControlDateFieldModel
101 * @see com.sun.star.form.XReset
102 * @see com.sun.star.form.XBoundComponent
103 * @see com.sun.star.form.FormComponent
104 * @see com.sun.star.form.component.DateField
105 * @see com.sun.star.beans.XFastPropertySet
106 * @see com.sun.star.beans.XMultiPropertySet
107 * @see com.sun.star.form.XUpdateBroadcaster
108 * @see com.sun.star.form.DataAwareControlModel
109 * @see com.sun.star.beans.XPropertyState
110 * @see com.sun.star.form.FormControlModel
111 * @see com.sun.star.container.XNamed
112 * @see com.sun.star.lang.XComponent
113 * @see com.sun.star.lang.XEventListener
114 * @see com.sun.star.beans.XPropertyAccess
115 * @see com.sun.star.beans.XPropertyContainer
116 * @see com.sun.star.beans.XPropertySet
117 * @see com.sun.star.form.XLoadListener
118 * @see com.sun.star.container.XChild
119 * @see ifc.io._XPersistObject
120 * @see ifc.awt._UnoControlDateFieldModel
121 * @see ifc.form._XReset
122 * @see ifc.form._XBoundComponent
123 * @see ifc.form._FormComponent
124 * @see ifc.form.component._DateField
125 * @see ifc.beans._XFastPropertySet
126 * @see ifc.beans._XMultiPropertySet
127 * @see ifc.form._XUpdateBroadcaster
128 * @see ifc.form._DataAwareControlModel
129 * @see ifc.beans._XPropertyState
130 * @see ifc.form._FormControlModel
131 * @see ifc.container._XNamed
132 * @see ifc.lang._XComponent
133 * @see ifc.lang._XEventListener
134 * @see ifc.beans._XPropertySet
135 * @see ifc.form._XLoadListener
136 * @see ifc.container._XChild
138 public class GenericModelTest
extends TestCase
{
139 private static XTextDocument m_xTextDoc
;
140 private static Object m_dbSrc
= null;
141 private static DBTools
.DataSourceInfo m_srcInf
= null;
143 * This is the name of the Data Base which the test uses: "APITestDatabase"
145 protected final static String m_dbSourceName
= "APITestDatabase";
146 protected final static String m_TestDB
= "TestDB";
147 private DBTools m_dbTools
= null;
149 private static boolean m_ConnectionColsed
= false;
152 * descibes the kind of the shape which should be created.
153 * Example: m_kindOfshape=DateFiled
155 public static String m_kindOfControl
= null;
158 * If your object needs some special propery values you can specify them with this
159 * <CODE>ArrayList</CODE>. You have to add a <CODE>NamedValue</CODE> to this list.
161 * NamedValue myProp = new NamedValue();
162 * myProp.Name = "Test";
163 * myProp.Value = "My special Value";
164 * m_propertiesToSet.add(myProp);
166 public static ArrayList m_propertiesToSet
= new ArrayList();
169 * This variable contains the name of the property which should be changed while
170 * interface <CODE>com::sun::star::form::XUpdateBroadcaster</CODE> is tested. The
171 * interface test needs the <CODE>ObjectRelation</CODE>
172 * "XUpdateBroadcaster.Checker" which is a <CODE>ifc.form._XUpdateBroadcaster.UpdateChecker</CODE>.
173 * @see ifc.form._XUpdateBroadcaster.UpdateChecker
174 * @see ifc.form._XUpdateBroadcaster
176 public static String m_ChangePropertyName
= null;
178 * This variable contains the value the property should be set while
179 * interface <CODE>com::sun::star::form::XUpdateBroadcaster</CODE> is tested.
180 * The interface test needs the <CODE>ObjectRelation</CODE>
181 * "XUpdateBroadcaster.Checker" which is a <CODE>ifc.form._XUpdateBroadcaster.UpdateChecker</CODE>.
182 * Normaly the <CODE>Checker</CODE> uses <CODE>util.ValueChanger</CODE> to change
183 * the value of the property. If the current of this property is NULL the
184 * <CODE>ValueChanger</CODE> is unable to change the value. In this case the value
185 * of this variable was used.
187 public static Object m_ChangePropertyValue
= null;
190 * This variable contains the implelemtation name of the object.
192 public static String m_ObjectName
= null;
195 * For local implementaions of <CODE>Checker</CODE> this variable contains the
196 * <CODE>FormLoader</CODE>
198 protected static XLoadable m_XFormLoader
= null;
200 * For local implementaions of <CODE>Checker</CODE> this variable contains the
201 * <CODE>XPropertySet</CODE>
203 protected static XPropertySet m_XPS
= null;
205 * For local implementaions of <CODE>Checker</CODE> this variable contains the
206 * <CODE>Control</CODE>
208 protected static XInterface m_XCtrl
= null;
210 * The insterface test of <CODE>ifc.form._DataWareControlModel</CODE> expects an
211 * object relation <CODE>'LC'</CODE>. This is a <CODE>XControlModel</CODE> of a shape.
212 * This variable contains the kind of shape to create for the interface test,
214 * @see ifc.form._DataAwareControlModel
216 protected static String m_LCShape_Type
= null;
218 protected static String m_XPropertyAccess_propertyToChange
= "HelpText";
220 protected static String m_XPropertyContainer_propertyNotRemovable
= "HelpText";
222 * If this variable is true some more debug info was logged. It was setted by the parameter variable
223 * <code>debug_is_active</code>
225 protected static boolean debug
= false;
228 * Creates Writer document where controls are placed.
229 * @param tParam the test paremter
230 * @param log the log writer
232 protected void initialize(TestParameters tParam
, PrintWriter log
) {
233 log
.println("creating a textdocument");
234 m_xTextDoc
= WriterTools
.createTextDoc(((XMultiServiceFactory
) tParam
.getMSF()));
235 m_ConnectionColsed
= false;
236 debug
= tParam
.getBool(util
.PropertyName
.DEBUG_IS_ACTIVE
);
237 m_propertiesToSet
.clear();
241 * close the connection
242 * close the data source
244 * revoke the data source
245 * @param tParam the test parameter
246 * @param log the log writer
248 protected void cleanup(TestParameters tParam
, PrintWriter log
) {
249 log
.println("closing connection...");
251 // some interface tests call cleanup to reset the environment. If such
252 // a test is the last one cleanup was called twice. The second call
253 // causes then nasty exceptions...
254 if (m_ConnectionColsed
) return;
257 XForm myForm
= (XForm
) AnyConverter
.toObject(new Type(XForm
.class),
259 WriterTools
.getDrawPage(
261 .getByName("Standard"));
265 log
.println("ERROR: could not get 'Standard' from drawpage!");
267 log
.println("the draw page contains folowing elemtens:");
268 String
[] elements
= FormTools
.getForms(WriterTools
.getDrawPage(m_xTextDoc
)).getElementNames();
269 for (int i
= 0; i
< elements
.length
; i
++){
270 log
.println("Element[" + i
+ "] :" + elements
[i
]);
275 XPropertySet xSetProp
= (XPropertySet
) UnoRuntime
.queryInterface(
276 XPropertySet
.class, myForm
);
277 XConnection connection
= (XConnection
) AnyConverter
.toObject(
278 new Type(XConnection
.class),
279 xSetProp
.getPropertyValue("ActiveConnection"));
280 if (debug
&& connection
== null){
281 log
.println("ERROR: could not get property 'ActiveConnection' from the XForm");
285 } catch (Exception e
) {
286 log
.println("ERROR: Can't close the connection: " + e
.toString());
289 log
.println("closing data source...");
291 XCloseable closer
= (XCloseable
) UnoRuntime
.queryInterface(
292 XCloseable
.class, m_dbSrc
);
293 if ( closer
== null )
295 XDocumentDataSource dataSource
= (XDocumentDataSource
)UnoRuntime
.queryInterface(
296 XDocumentDataSource
.class, m_dbSrc
);
297 if ( dataSource
!= null )
298 closer
= (XCloseable
) UnoRuntime
.queryInterface(
299 XCloseable
.class, dataSource
.getDatabaseDocument() );
301 if (debug
&& closer
==null){
302 log
.println("ERROR: couldn't get 'XCloseable' from DataSource");
305 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
306 log
.println("ERROR: couldn't close data source: " + e
.toString());
307 } catch (com
.sun
.star
.lang
.DisposedException e
) {
308 log
.println("ERROR: couldn't close data source: " + e
.toString());
309 } catch (Exception e
) {
310 log
.println("ERROR: couldn't close data source: " + e
.toString());
313 log
.println("disposing data source...");
315 XComponent dataSourceComp
= (XComponent
)UnoRuntime
.queryInterface(
316 XComponent
.class, m_dbSrc
);
317 dataSourceComp
.dispose();
319 catch (Exception e
) {
320 log
.println("couldn't dispose the data source");
323 log
.println("closing document...");
326 XCloseable closer
= (XCloseable
) UnoRuntime
.queryInterface(
327 XCloseable
.class, m_xTextDoc
);
329 } catch (com
.sun
.star
.util
.CloseVetoException e
) {
330 log
.println("ERROR: couldn't close document: " + e
.toString());
331 } catch (com
.sun
.star
.lang
.DisposedException e
) {
332 log
.println("ERROR: couldn't close document: " + e
.toString());
333 } catch (Exception e
) {
334 log
.println("ERROR: couldn't close document: " + e
.toString());
337 log
.println("revoking data source...");
339 m_dbTools
.revokeDB(m_dbSourceName
);
340 } catch (com
.sun
.star
.container
.NoSuchElementException e
){
341 } catch (com
.sun
.star
.uno
.Exception e
) {
342 log
.println("ERROR: Error while object test cleaning up: " + e
.toString());
345 m_ConnectionColsed
= true;
349 * Creating a Testenvironment for the interfaces to be tested.
350 * First <code>TestDB</code> database is registered.
351 * Creates DateField in the Form, then binds it to TestDB
352 * database and returns Field's control. <p>
353 * Object relations created :
355 * <li> <code>'OBJNAME'</code> for
356 * {@link ifc.io._XPersistObject} : name of service which is
357 * represented by this object. </li>
358 * <li> <code>'LC'</code> for {@link ifc.form._DataAwareControlModel}.
359 * Specifies the value for LabelControl property. It is
360 * <code>FixedText</code> component added to the document.</li>
361 * <li> <code>'FL'</code> for
362 * {@link ifc.form._DataAwareControlModel} interface.
363 * Specifies XLoadable implementation which connects form to
364 * the data source.</li>
365 * <li> <code>'XUpdateBroadcaster.Checker'</code> : <code>
366 * _XUpdateBroadcaster.UpdateChecker</code> interface implementation
367 * which can update, commit data and check if the data was successfully
369 * <li> <code>'DataAwareControlModel.NewFieldName'</code> : for
370 * <code>com.sun.star.form.DataAwareControlModel</code> service
371 * which contains new name of the field ('_DATE') to bind control to.
373 * <li> <code>'XFastPropertySet.ExcludeProps'</code> : for
374 * <code>com.sun.star.beans.XFastPropertySet</code> interface
375 * the property FormatKey can have only restricted set of values.
378 * @see ifc.form._XUpdateBroadcaster
379 * @param Param the test parameter
380 * @param log the log writer
381 * @return a test environment
383 protected synchronized TestEnvironment
createTestEnvironment(TestParameters Param
,
385 XInterface oObj
= null;
386 XControlShape aShape
= null;
387 XMultiServiceFactory xMSF
= (XMultiServiceFactory
) Param
.getMSF();
390 log
.println("adding contol shape '" + m_kindOfControl
+ "'");
391 aShape
= FormTools
.createControlShape(m_xTextDoc
, 3000,
394 } catch (Exception e
){
395 e
.printStackTrace(log
);
396 throw new StatusException("Couldn't create following control shape (m_kindOfControl): '" +
397 m_kindOfControl
+ "': ", e
);
401 WriterTools
.getDrawPage(m_xTextDoc
).add((XShape
) aShape
);
402 oObj
= aShape
.getControl();
404 log
.println("Implementation name: " + util
.utils
.getImplName(oObj
));
407 String sourceTestDB
= utils
.getFullURL(utils
.getFullTestDocName("TestDB/testDB.dbf"));
408 String destTestDB
= utils
.getOfficeTemp(xMSF
);
409 destTestDB
= utils
.getFullURL(destTestDB
+ "testDB.dbf");
411 log
.println("copy '"+sourceTestDB
+ "' -> '" + destTestDB
+ "'");
412 utils
.copyFile(xMSF
, sourceTestDB
, destTestDB
);
414 m_dbTools
= new DBTools((xMSF
));
415 String tmpDir
= utils
.getOfficeTemp((xMSF
));
417 m_srcInf
= m_dbTools
.newDataSourceInfo();
418 m_srcInf
.URL
= "sdbc:dbase:" + DBTools
.dirToUrl(tmpDir
);
419 log
.println("data source: " + m_srcInf
.URL
);
421 m_dbSrc
= m_srcInf
.getDataSourceService();
422 m_dbTools
.reRegisterDB(m_dbSourceName
, m_dbSrc
);
424 m_XFormLoader
= FormTools
.bindForm(m_xTextDoc
, m_dbSourceName
,
426 } catch (com
.sun
.star
.uno
.Exception e
) {
427 log
.println("!!! Can't access TestDB !!!");
428 e
.printStackTrace(log
);
429 throw new StatusException("Can't access TestDB", e
);
432 log
.println("creating a new environment for object");
434 TestEnvironment tEnv
= new TestEnvironment(oObj
);
436 tEnv
.addObjRelation("OBJNAME", m_ObjectName
);
438 log
.println("adding shape '" + m_LCShape_Type
+"' for DataAwareControlModel test");
439 aShape
= FormTools
.createControlShape(m_xTextDoc
, 6000, 4500, 15000,
440 10000, m_LCShape_Type
);
441 WriterTools
.getDrawPage(m_xTextDoc
).add((XShape
) aShape
);
443 m_XPS
= (XPropertySet
) UnoRuntime
.queryInterface(
444 XPropertySet
.class, oObj
);
447 NamedValue prop
= null;
449 for (i
= 0; i
< m_propertiesToSet
.size(); i
++){
450 prop
= (NamedValue
) m_propertiesToSet
.get(i
);
452 log
.println("setting property: '"+prop
.Name
+"' to value '"+prop
.Value
.toString()+"'");
454 m_XPS
.setPropertyValue(prop
.Name
, prop
.Value
);
456 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
457 e
.printStackTrace(log
);
458 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
459 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
460 e
.printStackTrace(log
);
461 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
462 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
463 e
.printStackTrace(log
);
464 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
465 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
466 e
.printStackTrace(log
);
467 throw new StatusException("Couldn't set property '" + prop
.Name
+ "': ", e
);
468 } catch (java
.lang
.ClassCastException e
) {
469 e
.printStackTrace(log
);
470 throw new StatusException("Couldn't get property on index '" + i
+ "': ", e
);
473 // added LabelControl for 'DataAwareControlModel'
474 tEnv
.addObjRelation("LC", aShape
.getControl());
476 // added FormLoader for 'DataAwareControlModel'
477 tEnv
.addObjRelation("FL", m_XFormLoader
);
479 // adding relation for XUpdateBroadcaster
482 tEnv
.addObjRelation("XUpdateBroadcaster.Checker",
483 new Checker(m_XFormLoader
, m_XPS
, m_XCtrl
, m_ChangePropertyName
, m_ChangePropertyValue
));
485 // adding relation for DataAwareControlModel service
486 tEnv
.addObjRelation("DataAwareControlModel.NewFieldName",
489 //adding ObjRelation for XPersistObject
490 tEnv
.addObjRelation("PSEUDOPERSISTENT", new Boolean(true));
492 // adding relation for XFastPropertySet
493 java
.util
.HashSet exclude
= new java
.util
.HashSet();
494 exclude
.add("FormatKey");
495 tEnv
.addObjRelation("XFastPropertySet.ExcludeProps", exclude
);
497 PropertyValue propVal
= new PropertyValue();
498 propVal
.Name
= m_XPropertyAccess_propertyToChange
;
499 propVal
.Value
= "Text since XPropertyAccess";
500 tEnv
.addObjRelation("XPropertyAccess.propertyToChange", propVal
);
501 tEnv
.addObjRelation("XPropertyContainer.propertyNotRemovable", m_XPropertyContainer_propertyNotRemovable
);
505 } // finish method getTestEnvironment
508 static class Checker
implements ifc
.form
._XUpdateBroadcaster
.UpdateChecker
{
509 private Object lastValue
= null;
510 XLoadable formLoaderF
= null;
511 XPropertySet ps
= null;
512 XInterface ctrl
= null;
513 String ChangePropertyName
= null;
514 Object ChangePropertyValue
= null;
516 public Checker(XLoadable xl
, XPropertySet ps
, XInterface ctrl
, String ChangePropertyName
, Object ChangePropertyValue
) {
520 this.ChangePropertyName
=ChangePropertyName
;
521 this.ChangePropertyValue
=ChangePropertyValue
;
524 public void update() throws com
.sun
.star
.uno
.Exception
{
525 if (!formLoaderF
.isLoaded()) {
529 lastValue
= util
.ValueChanger
.changePValue(ps
.getPropertyValue(ChangePropertyName
));
531 if (lastValue
== null){
533 if (ChangePropertyValue
!= null){
535 lastValue
= ChangePropertyValue
;
539 String msg
= "The initial value of the property '" + ChangePropertyName
+ "' is NULL\n";
540 msg
+= "The member variable 'm_ChangePropertyValue' is NULL\n";
541 msg
+= "Could not change Property.";
543 throw new StatusException(Status
.failed(msg
));
546 ps
.setPropertyValue(ChangePropertyName
, lastValue
);
549 public void commit() throws com
.sun
.star
.sdbc
.SQLException
{
550 XBoundComponent bound
= (XBoundComponent
) UnoRuntime
.queryInterface(
551 XBoundComponent
.class, ctrl
);
552 XResultSetUpdate update
= (XResultSetUpdate
) UnoRuntime
.queryInterface(
553 XResultSetUpdate
.class,
560 public boolean wasCommited() throws com
.sun
.star
.uno
.Exception
{
561 formLoaderF
.reload();
563 Object newValue
= ps
.getPropertyValue(ChangePropertyName
);
565 return (newValue
!= null) && (util
.ValueComparer
.equalValue(lastValue
, newValue
));
568 } // finish class GenericModelTest