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