Update ooo320-m1
[ooovba.git] / sc / qa / complex / dataPilot / interfaceTests / beans / _XPropertySet.java
blob1bc02b7d5afa569167e0b9936553520991af920e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XPropertySet.java,v $
10 * $Revision: 1.4 $
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 package complex.dataPilot.interfaceTests.beans;
33 import com.sun.star.beans.Property;
34 import com.sun.star.beans.PropertyAttribute;
35 import com.sun.star.beans.PropertyChangeEvent;
36 import com.sun.star.beans.XPropertyChangeListener;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.beans.XPropertySetInfo;
39 import com.sun.star.beans.XVetoableChangeListener;
40 import com.sun.star.lang.EventObject;
41 import java.util.Random;
42 import java.util.StringTokenizer;
43 import lib.TestParameters;
44 import share.LogWriter;
45 //import lib.MultiMethodTest;
46 import util.ValueChanger;
47 import util.utils;
49 /**
50 * Testing <code>com.sun.star.beans.XPropertySet</code>
51 * interface methods :
52 * <ul>
53 * <li><code>getPropertySetInfo()</code></li>
54 * <li><code>setPropertyValue()</code></li>
55 * <li><code>getPropertyValue()</code></li>
56 * <li><code>addPropertyChangeListener()</code></li>
57 * <li><code>removePropertyChangeListener()</code></li>
58 * <li><code>addVetoableChangeListener()</code></li>
59 * <li><code>removeVetoableChangeListener()</code></li>
60 * </ul>
61 * @see com.sun.star.beans.XPropertySet
63 public class _XPropertySet {
65 /**
66 * The object that is testsed.
68 private XPropertySet oObj = null;
70 /**
71 * The test parameters
73 private TestParameters param = null;
75 /**
76 * The log writer
78 private LogWriter log = null;
80 /**
81 * Flag that indicates change listener was called.
83 boolean propertyChanged = false;
86 /**
87 * The own property change listener
89 XPropertyChangeListener PClistener = new MyChangeListener();
91 /**
92 * Listener that must be called on bound property changing.
94 public class MyChangeListener implements XPropertyChangeListener {
95 /**
96 * Just set <code>propertyChanged</code> flag to true.
98 public void propertyChange(PropertyChangeEvent e) {
99 propertyChanged = true;
101 public void disposing (EventObject obj) {}
106 * Flag that indicates veto listener was called.
108 boolean vetoableChanged = false;
111 * The own vetoable change listener
113 XVetoableChangeListener VClistener = new MyVetoListener();
116 * Listener that must be called on constrained property changing.
118 public class MyVetoListener implements XVetoableChangeListener {
120 * Just set <code>vetoableChanged</code> flag to true.
122 public void vetoableChange(PropertyChangeEvent e) {
123 vetoableChanged = true;
125 public void disposing (EventObject obj) {}
130 * Properties to test
132 PropsToTest PTT = new PropsToTest();
135 * Structure that collects three properties of each type to test :
136 * Constrained, Bound and Normal.
138 public class PropsToTest {
139 String constrained = null;
140 String bound = null;
141 String normal = null;
145 * Constructor: gets the object to test, a logger and the test parameters
146 * @param xObj The test object
147 * @param log A log writer
148 * @param param The test parameters
150 public _XPropertySet(XPropertySet xObj, LogWriter log, TestParameters param) {
151 oObj = xObj;
152 this.log = log;
153 this.param = param;
157 * Tests method <code>getPropertySetInfo</code>. After test completed
158 * call {@link #getPropsToTest} method to retrieve different kinds
159 * of properties to test then. <p>
160 * Has OK status if not null <code>XPropertySetInfo</code>
161 * object returned.<p>
162 * Since <code>getPropertySetInfo</code> is optional, it may return null,
163 * if it is not implemented. This method uses then an object relation
164 * <code>PTT</code> (Properties To Test) to determine available properties.
165 * All tests for services without <code>getPropertySetInfo</code> must
166 * provide this object relation.
168 public boolean _getPropertySetInfo() {
169 XPropertySetInfo propertySetInfo = oObj.getPropertySetInfo();
171 if (propertySetInfo == null) {
172 log.println("getPropertySetInfo() method returned null");
173 String[] ptt = (String[]) param.get("PTT");
174 PTT.normal=ptt[0];
175 PTT.bound=ptt[1];
176 PTT.constrained=ptt[2];
177 } else {
178 getPropsToTest(propertySetInfo);
181 return true;
183 } // end of getPropertySetInfo()
186 * Tests change listener which added for bound properties.
187 * Adds listener to bound property (if it exists), then changes
188 * its value and check if listener was called. <p>
189 * Method tests to be successfully completed before :
190 * <ul>
191 * <li> <code>getPropertySetInfo</code> : in this method test
192 * one of bound properties is retrieved. </li>
193 * </ul> <p>
194 * Has OK status if NO bound properties exist or if listener
195 * was successfully called.
197 public boolean _addPropertyChangeListener() {
199 propertyChanged = false;
200 boolean result = true;
202 if ( PTT.bound.equals("none") ) {
203 log.println("*** No bound properties found ***");
204 } else {
205 try {
206 oObj.addPropertyChangeListener(PTT.bound,PClistener);
207 Object gValue = oObj.getPropertyValue(PTT.bound);
208 oObj.setPropertyValue(PTT.bound,
209 ValueChanger.changePValue(gValue));
210 } catch (com.sun.star.beans.PropertyVetoException e) {
211 log.println("Exception occured while trying to change "+
212 "property '"+ PTT.bound+"'");
213 e.printStackTrace((java.io.PrintWriter)log);
214 } catch (com.sun.star.lang.IllegalArgumentException e) {
215 log.println("Exception occured while trying to change "+
216 "property '"+ PTT.bound+"'");
217 e.printStackTrace((java.io.PrintWriter)log);
218 } catch (com.sun.star.beans.UnknownPropertyException e) {
219 log.println("Exception occured while trying to change "+
220 "property '"+ PTT.bound+"'");
221 e.printStackTrace((java.io.PrintWriter)log);
222 } catch (com.sun.star.lang.WrappedTargetException e) {
223 log.println("Exception occured while trying to change "+
224 "property '"+ PTT.bound+"'");
225 e.printStackTrace((java.io.PrintWriter)log);
226 } // end of try-catch
227 result = propertyChanged;
228 if (!propertyChanged) {
229 log.println("propertyChangeListener wasn't called for '"+
230 PTT.bound+"'");
232 } //endif
234 return result;
236 } // end of addPropertyChangeListener()
239 * Tests vetoable listener which added for constrained properties.
240 * Adds listener to constrained property (if it exists), then changes
241 * its value and check if listener was called. <p>
242 * Method tests to be successfully completed before :
243 * <ul>
244 * <li> <code>getPropertySetInfo</code> : in this method test
245 * one of constrained properties is retrieved. </li>
246 * </ul> <p>
247 * Has OK status if NO constrained properties exist or if listener
248 * was successfully called.
250 public boolean _addVetoableChangeListener() {
252 // requiredMethod("getPropertySetInfo()");
254 vetoableChanged = false;
255 boolean result = true;
257 if ( PTT.constrained.equals("none") ) {
258 log.println("*** No constrained properties found ***");
259 } else {
260 try {
261 oObj.addVetoableChangeListener(PTT.constrained,VClistener);
262 Object gValue = oObj.getPropertyValue(PTT.constrained);
263 oObj.setPropertyValue(PTT.constrained,
264 ValueChanger.changePValue(gValue));
265 } catch (com.sun.star.beans.PropertyVetoException e) {
266 log.println("Exception occured while trying to change "+
267 "property '"+ PTT.constrained+"'");
268 e.printStackTrace((java.io.PrintWriter)log);
269 } catch (com.sun.star.lang.IllegalArgumentException e) {
270 log.println("Exception occured while trying to change "+
271 "property '"+ PTT.constrained+"'");
272 e.printStackTrace((java.io.PrintWriter)log);
273 } catch (com.sun.star.beans.UnknownPropertyException e) {
274 log.println("Exception occured while trying to change "+
275 "property '"+ PTT.constrained+"'");
276 e.printStackTrace((java.io.PrintWriter)log);
277 } catch (com.sun.star.lang.WrappedTargetException e) {
278 log.println("Exception occured while trying to change "+
279 "property '"+ PTT.constrained+"'");
280 e.printStackTrace((java.io.PrintWriter)log);
281 } // end of try-catch
282 result = vetoableChanged;
283 if (!vetoableChanged) {
284 log.println("vetoableChangeListener wasn't called for '"+
285 PTT.constrained+"'");
287 } //endif
289 return result;
291 } // end of addVetoableChangeListener()
295 * Tests <code>setPropertyValue</code> method.
296 * Stores value before call, and compares it with value after
297 * call. <p>
298 * Method tests to be successfully completed before :
299 * <ul>
300 * <li> <code>getPropertySetInfo</code> : in this method test
301 * one of normal properties is retrieved. </li>
302 * </ul> <p>
303 * Has OK status if NO normal properties exist or if value before
304 * method call is not equal to value after.
306 public boolean _setPropertyValue() {
308 // requiredMethod("getPropertySetInfo()");
310 Object gValue = null;
311 Object sValue = null;
313 boolean result = true;
315 if ( PTT.normal.equals("none") ) {
316 log.println("*** No changeable properties found ***");
317 } else {
318 try {
319 gValue = oObj.getPropertyValue(PTT.normal);
320 sValue = ValueChanger.changePValue(gValue);
321 oObj.setPropertyValue(PTT.normal, sValue);
322 sValue = oObj.getPropertyValue(PTT.normal);
323 } catch (com.sun.star.beans.PropertyVetoException e) {
324 log.println("Exception occured while trying to change "+
325 "property '"+ PTT.normal+"'");
326 e.printStackTrace((java.io.PrintWriter)log);
327 } catch (com.sun.star.lang.IllegalArgumentException e) {
328 log.println("Exception occured while trying to change "+
329 "property '"+ PTT.normal+"'");
330 e.printStackTrace((java.io.PrintWriter)log);
331 } catch (com.sun.star.beans.UnknownPropertyException e) {
332 log.println("Exception occured while trying to change "+
333 "property '"+ PTT.normal+"'");
334 e.printStackTrace((java.io.PrintWriter)log);
335 } catch (com.sun.star.lang.WrappedTargetException e) {
336 log.println("Exception occured while trying to change "+
337 "property '"+ PTT.normal+"'");
338 e.printStackTrace((java.io.PrintWriter)log);
339 } // end of try-catch
340 result = !gValue.equals(sValue);
341 } //endif
343 return result;
345 } // end of setPropertyValue()
348 * Tests <code>getPropertyValue</code> method.
349 * Just call this method and checks for no exceptions <p>
350 * Method tests to be successfully completed before :
351 * <ul>
352 * <li> <code>getPropertySetInfo</code> : in this method test
353 * one of normal properties is retrieved. </li>
354 * </ul> <p>
355 * Has OK status if NO normal properties exist or if no
356 * exceptions were thrown.
358 public boolean _getPropertyValue() {
360 // requiredMethod("getPropertySetInfo()");
362 boolean result = true;
363 String toCheck = PTT.normal;
365 if ( PTT.normal.equals("none") ) {
366 toCheck = oObj.getPropertySetInfo().getProperties()[0].Name;
367 log.println("All properties are Read Only");
368 log.println("Using: "+toCheck);
371 try {
372 Object gValue = oObj.getPropertyValue(toCheck);
373 } catch (com.sun.star.beans.UnknownPropertyException e) {
374 log.println("Exception occured while trying to get property '"+
375 PTT.normal+"'");
376 e.printStackTrace((java.io.PrintWriter)log);
377 result = false;
378 } catch (com.sun.star.lang.WrappedTargetException e) {
379 log.println("Exception occured while trying to get property '"+
380 PTT.normal+"'");
381 e.printStackTrace((java.io.PrintWriter)log);
382 result = false;
383 } // end of try-catch
385 return result;
389 * Tests <code>removePropertyChangeListener</code> method.
390 * Removes change listener, then changes bound property value
391 * and checks if the listener was NOT called.
392 * Method tests to be successfully completed before :
393 * <ul>
394 * <li> <code>addPropertyChangeListener</code> : here listener
395 * was added. </li>
396 * </ul> <p>
397 * Has OK status if NO bound properties exist or if listener
398 * was not called and no exceptions arose.
400 public boolean _removePropertyChangeListener() {
402 // requiredMethod("addPropertyChangeListener()");
404 propertyChanged = false;
405 boolean result = true;
407 if ( PTT.bound.equals("none") ) {
408 log.println("*** No bound properties found ***");
409 } else {
410 try {
411 propertyChanged = false;
412 oObj.removePropertyChangeListener(PTT.bound,PClistener);
413 Object gValue = oObj.getPropertyValue(PTT.bound);
414 oObj.setPropertyValue(PTT.bound,
415 ValueChanger.changePValue(gValue));
416 } catch (com.sun.star.beans.PropertyVetoException e) {
417 log.println("Exception occured while trying to change "+
418 "property '"+ PTT.bound+"'");
419 e.printStackTrace((java.io.PrintWriter)log);
420 } catch (com.sun.star.lang.IllegalArgumentException e) {
421 log.println("Exception occured while trying to change "+
422 "property '"+ PTT.bound+"'");
423 e.printStackTrace((java.io.PrintWriter)log);
424 } catch (com.sun.star.beans.UnknownPropertyException e) {
425 log.println("Exception occured while trying to change "+
426 "property '"+ PTT.bound+"'");
427 e.printStackTrace((java.io.PrintWriter)log);
428 } catch (com.sun.star.lang.WrappedTargetException e) {
429 log.println("Exception occured while trying to change "+
430 "property '"+ PTT.bound+"'");
431 e.printStackTrace((java.io.PrintWriter)log);
432 } // end of try-catch
434 result = !propertyChanged;
435 if (propertyChanged) {
436 log.println("propertyChangeListener was called after removing"+
437 " for '"+PTT.bound+"'");
439 } //endif
441 return result;
443 } // end of removePropertyChangeListener()
447 * Tests <code>removeVetoableChangeListener</code> method.
448 * Removes vetoable listener, then changes constrained property value
449 * and checks if the listener was NOT called.
450 * Method tests to be successfully completed before :
451 * <ul>
452 * <li> <code>addPropertyChangeListener</code> : here vetoable listener
453 * was added. </li>
454 * </ul> <p>
455 * Has OK status if NO constrained properties exist or if listener
456 * was NOT called and no exceptions arose.
458 public boolean _removeVetoableChangeListener() {
460 // requiredMethod("addVetoableChangeListener()");
462 vetoableChanged = false;
463 boolean result = true;
465 if ( PTT.constrained.equals("none") ) {
466 log.println("*** No constrained properties found ***");
467 } else {
468 try {
469 oObj.removeVetoableChangeListener(PTT.constrained,VClistener);
470 Object gValue = oObj.getPropertyValue(PTT.constrained);
471 oObj.setPropertyValue(PTT.constrained,
472 ValueChanger.changePValue(gValue));
473 } catch (com.sun.star.beans.PropertyVetoException e) {
474 log.println("Exception occured while trying to change "+
475 "property '"+ PTT.constrained+"'");
476 e.printStackTrace((java.io.PrintWriter)log);
477 } catch (com.sun.star.lang.IllegalArgumentException e) {
478 log.println("Exception occured while trying to change "+
479 "property '"+ PTT.constrained+"'");
480 e.printStackTrace((java.io.PrintWriter)log);
481 } catch (com.sun.star.beans.UnknownPropertyException e) {
482 log.println("Exception occured while trying to change "+
483 "property '"+ PTT.constrained+"'");
484 e.printStackTrace((java.io.PrintWriter)log);
485 } catch (com.sun.star.lang.WrappedTargetException e) {
486 log.println("Exception occured while trying to change "+
487 "property '"+ PTT.constrained+"'");
488 e.printStackTrace((java.io.PrintWriter)log);
489 } // end of try-catch
491 result = !vetoableChanged;
492 if (vetoableChanged) {
493 log.println("vetoableChangeListener was called after "+
494 "removing for '"+PTT.constrained+"'");
496 } //endif
498 return result;
500 } // end of removeVetoableChangeListener()
504 * Gets the properties being tested. Searches and stores by one
505 * property of each kind (Bound, Vetoable, Normal).
507 public PropsToTest getPropsToTest(XPropertySetInfo xPSI) {
509 Property[] properties = xPSI.getProperties();
510 String bound = "";
511 String constrained = "";
512 String normal = "";
514 for (int i = 0; i < properties.length; i++) {
516 Property property = properties[i];
517 String name = property.Name;
518 log.println("Checking '"+name+"'");
519 boolean isWritable = ((property.Attributes &
520 PropertyAttribute.READONLY) == 0);
521 boolean isNotNull = ((property.Attributes &
522 PropertyAttribute.MAYBEVOID) == 0);
523 boolean isBound = ((property.Attributes &
524 PropertyAttribute.BOUND) != 0);
525 boolean isConstr = ((property.Attributes &
526 PropertyAttribute.CONSTRAINED) != 0);
527 boolean canChange = false;
529 if ( !isWritable ) log.println("Property '"+name+"' is READONLY");
531 if (name.endsWith("URL")) isWritable = false;
532 if (name.startsWith("Fill")) isWritable = false;
533 if (name.startsWith("Font")) isWritable = false;
534 if (name.startsWith("IsNumbering")) isWritable = false;
535 if (name.startsWith("LayerName")) isWritable = false;
536 if (name.startsWith("Line")) isWritable = false;
538 //if (name.equals("xinterfaceA") || name.equals("xtypeproviderA")
539 //|| name.equals("arAnyA")) isWritable=false;
541 if ( isWritable && isNotNull ) canChange = isChangeable(name);
543 if ( isWritable && isNotNull && isBound && canChange) {
544 bound+=name+";";
547 if ( isWritable && isNotNull && isConstr && canChange) {
548 constrained+=name+";";
551 if ( isWritable && isNotNull && canChange) normal+=name+";";
554 } // endfor
556 //get a random bound property
557 PTT.bound=getRandomString(bound);
558 log.println("Bound: "+PTT.bound);
560 //get a random constrained property
561 PTT.constrained=getRandomString(constrained);
562 log.println("Constrained: "+PTT.constrained);
564 //get a random normal property
565 PTT.normal=getRandomString(normal);
567 return PTT;
572 * Retrieves one random property name from list (property names separated
573 * by ';') of property names.
575 public String getRandomString(String str) {
577 String gRS = "none";
578 Random rnd = new Random();
580 if (str.equals("")) str = "none";
581 StringTokenizer ST=new StringTokenizer(str,";");
582 int nr = rnd.nextInt(ST.countTokens());
583 if (nr < 1) nr+=1;
584 for (int i=1; i<nr+1; i++) gRS = ST.nextToken();
586 return gRS;
590 public boolean isChangeable(String name) {
591 boolean hasChanged = false;
592 try {
593 Object getProp = oObj.getPropertyValue(name);
594 log.println("Getting: "+getProp);
596 Object setValue = null;
597 if (getProp != null) {
598 if (!utils.isVoid(getProp))
599 setValue = ValueChanger.changePValue(getProp);
600 else log.println("Property '"+name+
601 "' is void but MAYBEVOID isn't set");
602 } else log.println("Property '"+name+"'is null and can't be changed");
603 if (name.equals("LineStyle")) setValue = null;
604 if (setValue != null) {
605 oObj.setPropertyValue(name, setValue);
606 log.println("Setting to :"+setValue);
607 hasChanged = (! getProp.equals(oObj.getPropertyValue(name)));
608 } else log.println("Couldn't change Property '"+name+"'");
609 } catch (com.sun.star.beans.PropertyVetoException e) {
610 log.println("'" + name + "' throws exception '" + e + "'");
611 e.printStackTrace((java.io.PrintWriter)log);
612 } catch (com.sun.star.lang.IllegalArgumentException e) {
613 log.println("'" + name + "' throws exception '" + e + "'");
614 e.printStackTrace((java.io.PrintWriter)log);
615 } catch (com.sun.star.beans.UnknownPropertyException e) {
616 log.println("'" + name + "' throws exception '" + e + "'");
617 e.printStackTrace((java.io.PrintWriter)log);
618 } catch (com.sun.star.lang.WrappedTargetException e) {
619 log.println("'" + name + "' throws exception '" + e + "'");
620 e.printStackTrace((java.io.PrintWriter)log);
621 } catch (com.sun.star.uno.RuntimeException e) {
622 log.println("'" + name + "' throws exception '" + e + "'");
623 e.printStackTrace((java.io.PrintWriter)log);
624 } catch (java.lang.ArrayIndexOutOfBoundsException e) {
625 log.println("'" + name + "' throws exception '" + e + "'");
626 e.printStackTrace((java.io.PrintWriter)log);
629 return hasChanged;
633 } // finish class _XPropertySet