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 import com.sun.star.beans.NamedValue; 25 import com.sun.star.beans.PropertyValue; 26 import com.sun.star.container.XIndexAccess; 27 import java.io.PrintWriter; 28 29 import lib.StatusException; 30 import lib.TestCase; 31 import lib.TestEnvironment; 32 import lib.TestParameters; 33 import util.DBTools; 34 import util.FormTools; 35 import util.WriterTools; 36 37 import com.sun.star.beans.XPropertySet; 38 import com.sun.star.drawing.XControlShape; 39 import com.sun.star.drawing.XShape; 40 import com.sun.star.form.XBoundComponent; 41 import com.sun.star.form.XForm; 42 import com.sun.star.form.XLoadable; 43 import com.sun.star.lang.XMultiServiceFactory; 44 import com.sun.star.lang.XComponent; 45 import com.sun.star.sdbc.XConnection; 46 import com.sun.star.sdbc.XResultSetUpdate; 47 import com.sun.star.sdb.XDocumentDataSource; 48 import com.sun.star.text.XTextDocument; 49 import com.sun.star.uno.AnyConverter; 50 import com.sun.star.uno.Type; 51 import com.sun.star.uno.UnoRuntime; 52 import com.sun.star.uno.XInterface; 53 import com.sun.star.util.XCloseable; 54 import java.util.ArrayList; 55 import lib.Status; 56 import util.utils; 57 58 59 /** 60 * Test for object which is represented by service 61 * <code>com.sun.star.form.component.DateField</code>. <p> 62 * Object implements the following interfaces : 63 * <ul> 64 * <li> <code>com::sun::star::io::XPersistObject</code></li> 65 * <li> <code>com::sun::star::awt::UnoControlDateFieldModel</code></li> 66 * <li> <code>com::sun::star::form::XReset</code></li> 67 * <li> <code>com::sun::star::form::XBoundComponent</code></li> 68 * <li> <code>com::sun::star::form::FormComponent</code></li> 69 * <li> <code>com::sun::star::form::component::DateField</code></li> 70 * <li> <code>com::sun::star::beans::XFastPropertySet</code></li> 71 * <li> <code>com::sun::star::beans::XMultiPropertySet</code></li> 72 * <li> <code>com::sun::star::form::XUpdateBroadcaster</code></li> 73 * <li> <code>com::sun::star::form::DataAwareControlModel</code></li> 74 * <li> <code>com::sun::star::beans::XPropertyState</code></li> 75 * <li> <code>com::sun::star::form::FormControlModel</code></li> 76 * <li> <code>com::sun::star::container::XNamed</code></li> 77 * <li> <code>com::sun::star::lang::XComponent</code></li> 78 * <li> <code>com::sun::star::lang::XEventListener</code></li> 79 * <li> <code>com::sun::star::beans::XPropertyAccess</code></li> 80 * <li> <code>com::sun::star::beans::XPropertyContainer</code></li> 81 * <li> <code>com::sun::star::beans::XPropertySet</code></li> 82 * <li> <code>com::sun::star::form::XLoadListener</code></li> 83 * <li> <code>com::sun::star::container::XChild</code></li> 84 * </ul> 85 * The following files used by this test : 86 * <ul> 87 * <li><b> TestDB </b> (directory) : directory with test database </li> 88 * <li><b> TestDB/TestDB.dbf </b> : table file. See 89 * {@link util.DBTools DBTools} class for more information.</li> 90 * </ul> <p> 91 * This object test <b> is NOT </b> designed to be run in several 92 * threads concurently. 93 * @see com.sun.star.io.XPersistObject 94 * @see com.sun.star.awt.UnoControlDateFieldModel 95 * @see com.sun.star.form.XReset 96 * @see com.sun.star.form.XBoundComponent 97 * @see com.sun.star.form.FormComponent 98 * @see com.sun.star.form.component.DateField 99 * @see com.sun.star.beans.XFastPropertySet 100 * @see com.sun.star.beans.XMultiPropertySet 101 * @see com.sun.star.form.XUpdateBroadcaster 102 * @see com.sun.star.form.DataAwareControlModel 103 * @see com.sun.star.beans.XPropertyState 104 * @see com.sun.star.form.FormControlModel 105 * @see com.sun.star.container.XNamed 106 * @see com.sun.star.lang.XComponent 107 * @see com.sun.star.lang.XEventListener 108 * @see com.sun.star.beans.XPropertyAccess 109 * @see com.sun.star.beans.XPropertyContainer 110 * @see com.sun.star.beans.XPropertySet 111 * @see com.sun.star.form.XLoadListener 112 * @see com.sun.star.container.XChild 113 * @see ifc.io._XPersistObject 114 * @see ifc.awt._UnoControlDateFieldModel 115 * @see ifc.form._XReset 116 * @see ifc.form._XBoundComponent 117 * @see ifc.form._FormComponent 118 * @see ifc.form.component._DateField 119 * @see ifc.beans._XFastPropertySet 120 * @see ifc.beans._XMultiPropertySet 121 * @see ifc.form._XUpdateBroadcaster 122 * @see ifc.form._DataAwareControlModel 123 * @see ifc.beans._XPropertyState 124 * @see ifc.form._FormControlModel 125 * @see ifc.container._XNamed 126 * @see ifc.lang._XComponent 127 * @see ifc.lang._XEventListener 128 * @see ifc.beans._XPropertySet 129 * @see ifc.form._XLoadListener 130 * @see ifc.container._XChild 131 */ 132 public class GenericModelTest extends TestCase { 133 private static XTextDocument m_xTextDoc; 134 private static Object m_dbSrc = null; 135 private static DBTools.DataSourceInfo m_srcInf = null; 136 /** 137 * This is the name of the Data Base which the test uses: "APITestDatabase" 138 */ 139 protected final static String m_dbSourceName = "APITestDatabase"; 140 protected final static String m_TestDB = "TestDB"; 141 private DBTools m_dbTools = null; 142 143 private static boolean m_ConnectionColsed = false; 144 145 /** 146 * descibes the kind of the shape which should be created. 147 * Example: m_kindOfshape=DateFiled 148 */ 149 public static String m_kindOfControl = null; 150 151 /** 152 * If your object needs some special propery values you can specify them with this 153 * <CODE>ArrayList</CODE>. You have to add a <CODE>NamedValue</CODE> to this list. 154 * Example: 155 * NamedValue myProp = new NamedValue(); 156 * myProp.Name = "Test"; 157 * myProp.Value = "My special Value"; 158 * m_propertiesToSet.add(myProp); 159 */ 160 public static ArrayList m_propertiesToSet = new ArrayList(); 161 162 /** 163 * This variable contains the name of the property which should be changed while 164 * interface <CODE>com::sun::star::form::XUpdateBroadcaster</CODE> is tested. The 165 * interface test needs the <CODE>ObjectRelation</CODE> 166 * "XUpdateBroadcaster.Checker" which is a <CODE>ifc.form._XUpdateBroadcaster.UpdateChecker</CODE>. 167 * @see ifc.form._XUpdateBroadcaster.UpdateChecker 168 * @see ifc.form._XUpdateBroadcaster 169 */ 170 public static String m_ChangePropertyName = null; 171 /** 172 * This variable contains the value the property should be set while 173 * interface <CODE>com::sun::star::form::XUpdateBroadcaster</CODE> is tested. 174 * The interface test needs the <CODE>ObjectRelation</CODE> 175 * "XUpdateBroadcaster.Checker" which is a <CODE>ifc.form._XUpdateBroadcaster.UpdateChecker</CODE>. 176 * Normaly the <CODE>Checker</CODE> uses <CODE>util.ValueChanger</CODE> to change 177 * the value of the property. If the current of this property is NULL the 178 * <CODE>ValueChanger</CODE> is unable to change the value. In this case the value 179 * of this variable was used. 180 */ 181 public static Object m_ChangePropertyValue = null; 182 183 /** 184 * This variable contains the implelemtation name of the object. 185 */ 186 public static String m_ObjectName = null; 187 188 /** 189 * For local implementaions of <CODE>Checker</CODE> this variable contains the 190 * <CODE>FormLoader</CODE> 191 */ 192 protected static XLoadable m_XFormLoader = null; 193 /** 194 * For local implementaions of <CODE>Checker</CODE> this variable contains the 195 * <CODE>XPropertySet</CODE> 196 */ 197 protected static XPropertySet m_XPS = null; 198 /** 199 * For local implementaions of <CODE>Checker</CODE> this variable contains the 200 * <CODE>Control</CODE> 201 */ 202 protected static XInterface m_XCtrl = null; 203 /** 204 * The insterface test of <CODE>ifc.form._DataWareControlModel</CODE> expects an 205 * object relation <CODE>'LC'</CODE>. This is a <CODE>XControlModel</CODE> of a shape. 206 * This variable contains the kind of shape to create for the interface test, 207 * f.e. "FixedText" 208 * @see ifc.form._DataAwareControlModel 209 */ 210 protected static String m_LCShape_Type = null; 211 212 protected static String m_XPropertyAccess_propertyToChange = "HelpText"; 213 214 protected static String m_XPropertyContainer_propertyNotRemovable = "HelpText"; 215 /** 216 * If this variable is true some more debug info was logged. It was setted by the parameter variable 217 * <code>debug_is_active</code> 218 */ 219 protected static boolean debug = false; 220 221 /** 222 * Creates Writer document where controls are placed. 223 * @param tParam the test paremter 224 * @param log the log writer 225 */ 226 protected void initialize(TestParameters tParam, PrintWriter log) { 227 log.println("creating a textdocument"); 228 m_xTextDoc = WriterTools.createTextDoc(((XMultiServiceFactory) tParam.getMSF())); 229 m_ConnectionColsed = false; 230 debug = tParam.getBool(util.PropertyName.DEBUG_IS_ACTIVE); 231 m_propertiesToSet.clear(); 232 } 233 234 /** 235 * close the connection 236 * close the data source 237 * close the document 238 * revoke the data source 239 * @param tParam the test parameter 240 * @param log the log writer 241 */ 242 protected void cleanup(TestParameters tParam, PrintWriter log) { 243 log.println("closing connection..."); 244 245 // some interface tests call cleanup to reset the environment. If such 246 // a test is the last one cleanup was called twice. The second call 247 // causes then nasty exceptions... 248 if (m_ConnectionColsed) return; 249 250 try { 251 XIndexAccess forms = UnoRuntime.queryInterface( XIndexAccess.class, 252 FormTools.getForms( WriterTools.getDrawPage( m_xTextDoc ) ) ); 253 XForm myForm = (XForm) AnyConverter.toObject(new Type(XForm.class), 254 forms.getByIndex(0)); 255 256 if (debug){ 257 if (myForm == null){ 258 log.println("ERROR: could not get 'Standard' from drawpage!"); 259 } 260 log.println("the draw page contains folowing elemtens:"); 261 String[] elements = FormTools.getForms(WriterTools.getDrawPage(m_xTextDoc)).getElementNames(); 262 for (int i = 0; i< elements.length; i++){ 263 log.println("Element[" + i + "] :" + elements[i]); 264 } 265 266 } 267 268 XPropertySet xSetProp = UnoRuntime.queryInterface( XPropertySet.class, myForm ); 269 XConnection connection = UnoRuntime.queryInterface( XConnection.class, xSetProp.getPropertyValue( "ActiveConnection" ) ); 270 if ( connection == null ) 271 { 272 if ( debug ) 273 log.println("ERROR: could not get property 'ActiveConnection' from the XForm"); 274 } 275 else 276 { 277 connection.close(); 278 } 279 } catch (Exception e) { 280 log.println("ERROR: Can't close the connection: " + e.toString()); 281 e.printStackTrace( log ); 282 } 283 284 log.println("closing data source..."); 285 try { 286 XCloseable closer = (XCloseable) UnoRuntime.queryInterface( 287 XCloseable.class, m_dbSrc); 288 if ( closer == null ) 289 { 290 XDocumentDataSource dataSource = (XDocumentDataSource)UnoRuntime.queryInterface( 291 XDocumentDataSource.class, m_dbSrc); 292 if ( dataSource != null ) 293 closer = (XCloseable) UnoRuntime.queryInterface( 294 XCloseable.class, dataSource.getDatabaseDocument() ); 295 } 296 if (debug && closer==null){ 297 log.println("ERROR: couldn't get 'XCloseable' from DataSource"); 298 } 299 closer.close(true); 300 } catch (com.sun.star.util.CloseVetoException e) { 301 log.println("ERROR: couldn't close data source: " + e.toString()); 302 } catch (com.sun.star.lang.DisposedException e) { 303 log.println("ERROR: couldn't close data source: " + e.toString()); 304 } catch (Exception e) { 305 log.println("ERROR: couldn't close data source: " + e.toString()); 306 } 307 308 log.println("disposing data source..."); 309 try { 310 XComponent dataSourceComp = (XComponent)UnoRuntime.queryInterface( 311 XComponent.class, m_dbSrc); 312 dataSourceComp.dispose(); 313 } 314 catch (Exception e) { 315 log.println("couldn't dispose the data source"); 316 } 317 318 log.println("closing document..."); 319 320 try { 321 XCloseable closer = (XCloseable) UnoRuntime.queryInterface( 322 XCloseable.class, m_xTextDoc); 323 closer.close(true); 324 } catch (com.sun.star.util.CloseVetoException e) { 325 log.println("ERROR: couldn't close document: " + e.toString()); 326 } catch (com.sun.star.lang.DisposedException e) { 327 log.println("ERROR: couldn't close document: " + e.toString()); 328 } catch (Exception e) { 329 log.println("ERROR: couldn't close document: " + e.toString()); 330 } 331 332 log.println("revoking data source..."); 333 try { 334 m_dbTools.revokeDB(m_dbSourceName); 335 } catch (com.sun.star.container.NoSuchElementException e){ 336 } catch (com.sun.star.uno.Exception e) { 337 log.println("ERROR: Error while object test cleaning up: " + e.toString()); 338 } 339 340 m_ConnectionColsed = true; 341 } 342 343 /** 344 * Creating a Testenvironment for the interfaces to be tested. 345 * First <code>TestDB</code> database is registered. 346 * Creates DateField in the Form, then binds it to TestDB 347 * database and returns Field's control. <p> 348 * Object relations created : 349 * <ul> 350 * <li> <code>'OBJNAME'</code> for 351 * {@link ifc.io._XPersistObject} : name of service which is 352 * represented by this object. </li> 353 * <li> <code>'LC'</code> for {@link ifc.form._DataAwareControlModel}. 354 * Specifies the value for LabelControl property. It is 355 * <code>FixedText</code> component added to the document.</li> 356 * <li> <code>'FL'</code> for 357 * {@link ifc.form._DataAwareControlModel} interface. 358 * Specifies XLoadable implementation which connects form to 359 * the data source.</li> 360 * <li> <code>'XUpdateBroadcaster.Checker'</code> : <code> 361 * _XUpdateBroadcaster.UpdateChecker</code> interface implementation 362 * which can update, commit data and check if the data was successfully 363 * commited.</li> 364 * <li> <code>'DataAwareControlModel.NewFieldName'</code> : for 365 * <code>com.sun.star.form.DataAwareControlModel</code> service 366 * which contains new name of the field ('_DATE') to bind control to. 367 * </li> 368 * <li> <code>'XFastPropertySet.ExcludeProps'</code> : for 369 * <code>com.sun.star.beans.XFastPropertySet</code> interface 370 * the property FormatKey can have only restricted set of values. 371 * </li> 372 * </ul> 373 * @see ifc.form._XUpdateBroadcaster 374 * @param Param the test parameter 375 * @param log the log writer 376 * @return a test environment 377 */ 378 protected synchronized TestEnvironment createTestEnvironment(TestParameters Param, 379 PrintWriter log) { 380 XInterface oObj = null; 381 XControlShape aShape = null; 382 XMultiServiceFactory xMSF = (XMultiServiceFactory) Param.getMSF(); 383 384 try{ 385 log.println("adding contol shape '" + m_kindOfControl + "'"); 386 aShape = FormTools.createControlShape(m_xTextDoc, 3000, 387 4500, 15000, 10000, 388 m_kindOfControl); 389 } catch (Exception e){ 390 e.printStackTrace(log); 391 throw new StatusException("Couldn't create following control shape (m_kindOfControl): '" + 392 m_kindOfControl + "': ", e); 393 394 } 395 396 WriterTools.getDrawPage(m_xTextDoc).add((XShape) aShape); 397 oObj = aShape.getControl(); 398 399 log.println("Implementation name: " + util.utils.getImplName(oObj)); 400 401 try { 402 String sourceTestDB = utils.getFullURL(utils.getFullTestDocName("TestDB/testDB.dbf")); 403 String destTestDB = utils.getOfficeTemp(xMSF); 404 destTestDB = utils.getFullURL(destTestDB + "testDB.dbf"); 405 406 log.println("copy '"+sourceTestDB + "' -> '" + destTestDB + "'"); 407 utils.copyFile(xMSF, sourceTestDB, destTestDB); 408 409 m_dbTools = new DBTools( xMSF, log ); 410 String tmpDir = utils.getOfficeTemp((xMSF)); 411 412 m_srcInf = m_dbTools.newDataSourceInfo(); 413 m_srcInf.URL = "sdbc:dbase:" + DBTools.dirToUrl(tmpDir); 414 log.println("data source: " + m_srcInf.URL); 415 416 m_dbSrc = m_srcInf.getDataSourceService(); 417 m_dbTools.reRegisterDB(m_dbSourceName, m_dbSrc); 418 419 m_XFormLoader = FormTools.bindForm(m_xTextDoc, m_dbSourceName, 420 m_TestDB); 421 } catch (com.sun.star.uno.Exception e) { 422 log.println("!!! Can't access TestDB !!!"); 423 e.printStackTrace(log); 424 throw new StatusException("Can't access TestDB", e); 425 } 426 427 log.println("creating a new environment for object"); 428 429 TestEnvironment tEnv = new TestEnvironment(oObj); 430 431 tEnv.addObjRelation("OBJNAME", m_ObjectName); 432 433 log.println("adding shape '" + m_LCShape_Type +"' for DataAwareControlModel test"); 434 aShape = FormTools.createControlShape(m_xTextDoc, 6000, 4500, 15000, 435 10000, m_LCShape_Type); 436 WriterTools.getDrawPage(m_xTextDoc).add((XShape) aShape); 437 438 m_XPS = (XPropertySet) UnoRuntime.queryInterface( 439 XPropertySet.class, oObj); 440 441 int i = 0; 442 NamedValue prop = null; 443 try { 444 for (i = 0; i < m_propertiesToSet.size(); i++){ 445 prop = (NamedValue) m_propertiesToSet.get(i); 446 447 log.println("setting property: '"+prop.Name+"' to value '"+prop.Value.toString()+"'"); 448 449 m_XPS.setPropertyValue(prop.Name, prop.Value); 450 } 451 } catch (com.sun.star.lang.WrappedTargetException e) { 452 e.printStackTrace(log); 453 throw new StatusException("Couldn't set property '" + prop.Name + "': ", e); 454 } catch (com.sun.star.lang.IllegalArgumentException e) { 455 e.printStackTrace(log); 456 throw new StatusException("Couldn't set property '" + prop.Name + "': ", e); 457 } catch (com.sun.star.beans.PropertyVetoException e) { 458 e.printStackTrace(log); 459 throw new StatusException("Couldn't set property '" + prop.Name + "': ", e); 460 } catch (com.sun.star.beans.UnknownPropertyException e) { 461 e.printStackTrace(log); 462 throw new StatusException("Couldn't set property '" + prop.Name + "': ", e); 463 } catch (java.lang.ClassCastException e) { 464 e.printStackTrace(log); 465 throw new StatusException("Couldn't get property on index '" + i + "': ", e); 466 } 467 468 // added LabelControl for 'DataAwareControlModel' 469 tEnv.addObjRelation("LC", aShape.getControl()); 470 471 // added FormLoader for 'DataAwareControlModel' 472 tEnv.addObjRelation("FL", m_XFormLoader); 473 474 // adding relation for XUpdateBroadcaster 475 m_XCtrl = oObj; 476 477 tEnv.addObjRelation("XUpdateBroadcaster.Checker", 478 new Checker(m_XFormLoader, m_XPS, m_XCtrl, m_ChangePropertyName, m_ChangePropertyValue)); 479 480 // adding relation for DataAwareControlModel service 481 tEnv.addObjRelation("DataAwareControlModel.NewFieldName", 482 DBTools.TST_DATE_F); 483 484 //adding ObjRelation for XPersistObject 485 tEnv.addObjRelation("PSEUDOPERSISTENT", new Boolean(true)); 486 487 // adding relation for XFastPropertySet 488 java.util.HashSet exclude = new java.util.HashSet(); 489 exclude.add("FormatKey"); 490 tEnv.addObjRelation("XFastPropertySet.ExcludeProps", exclude); 491 492 PropertyValue propVal = new PropertyValue(); 493 propVal.Name = m_XPropertyAccess_propertyToChange; 494 propVal.Value = "Text since XPropertyAccess"; 495 tEnv.addObjRelation("XPropertyAccess.propertyToChange", propVal); 496 tEnv.addObjRelation("XPropertyContainer.propertyNotRemovable", m_XPropertyContainer_propertyNotRemovable); 497 498 499 return tEnv; 500 } // finish method getTestEnvironment 501 502 503 static class Checker implements ifc.form._XUpdateBroadcaster.UpdateChecker { 504 private Object lastValue = null; 505 XLoadable formLoaderF = null; 506 XPropertySet ps = null; 507 XInterface ctrl = null; 508 String ChangePropertyName = null; 509 Object ChangePropertyValue = null; 510 511 public Checker(XLoadable xl, XPropertySet ps, XInterface ctrl, String ChangePropertyName, Object ChangePropertyValue) { 512 formLoaderF = xl; 513 this.ps = ps; 514 this.ctrl = ctrl; 515 this.ChangePropertyName=ChangePropertyName; 516 this.ChangePropertyValue=ChangePropertyValue; 517 } 518 519 public void update() throws com.sun.star.uno.Exception { 520 if (!formLoaderF.isLoaded()) { 521 formLoaderF.load(); 522 } 523 524 lastValue = util.ValueChanger.changePValue(ps.getPropertyValue(ChangePropertyName)); 525 526 if (lastValue == null){ 527 528 if (ChangePropertyValue != null){ 529 530 lastValue = ChangePropertyValue; 531 532 } else { 533 534 String msg = "The initial value of the property '" + ChangePropertyName + "' is NULL\n"; 535 msg += "The member variable 'm_ChangePropertyValue' is NULL\n"; 536 msg += "Could not change Property."; 537 538 throw new StatusException(Status.failed(msg)); 539 } 540 } 541 ps.setPropertyValue(ChangePropertyName, lastValue); 542 } 543 544 public void commit() throws com.sun.star.sdbc.SQLException { 545 XBoundComponent bound = (XBoundComponent) UnoRuntime.queryInterface( 546 XBoundComponent.class, ctrl); 547 XResultSetUpdate update = (XResultSetUpdate) UnoRuntime.queryInterface( 548 XResultSetUpdate.class, 549 formLoaderF); 550 551 bound.commit(); 552 update.updateRow(); 553 } 554 555 public boolean wasCommited() throws com.sun.star.uno.Exception { 556 formLoaderF.reload(); 557 558 Object newValue = ps.getPropertyValue(ChangePropertyName); 559 560 return (newValue != null) && (util.ValueComparer.equalValue(lastValue, newValue)); 561 } 562 } 563 } // finish class GenericModelTest 564