Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / beans / _XPropertySet.java
blob19e84e4df3eb3c5d0e6c4f53fe96e56af74aded3
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.7 $
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 ifc.beans;
33 import java.util.Random;
34 import java.util.StringTokenizer;
36 import lib.MultiMethodTest;
37 import util.ValueChanger;
38 import util.utils;
40 import com.sun.star.beans.Property;
41 import com.sun.star.beans.PropertyAttribute;
42 import com.sun.star.beans.PropertyChangeEvent;
43 import com.sun.star.beans.XPropertyChangeListener;
44 import com.sun.star.beans.XPropertySet;
45 import com.sun.star.beans.XPropertySetInfo;
46 import com.sun.star.beans.XVetoableChangeListener;
47 import com.sun.star.lang.EventObject;
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 extends MultiMethodTest {
65 public XPropertySet oObj = null;
67 /**
68 * Flag that indicates change listener was called.
70 boolean propertyChanged = false;
72 /**
73 * Listener that must be called on bound property changing.
75 public class MyChangeListener implements XPropertyChangeListener {
76 /**
77 * Just set <code>propertyChanged</code> flag to true.
79 public void propertyChange(PropertyChangeEvent e) {
80 propertyChanged = true;
82 public void disposing (EventObject obj) {}
85 XPropertyChangeListener PClistener = new MyChangeListener();
87 /**
88 * Flag that indicates veto listener was called.
90 boolean vetoableChanged = false;
92 /**
93 * Listener that must be called on constrained property changing.
95 public class MyVetoListener implements XVetoableChangeListener {
96 /**
97 * Just set <code>vetoableChanged</code> flag to true.
99 public void vetoableChange(PropertyChangeEvent e) {
100 vetoableChanged = true;
102 public void disposing (EventObject obj) {}
105 XVetoableChangeListener VClistener = new MyVetoListener();
108 * Structure that collects three properties of each type to test :
109 * Constrained, Bound and Normal.
111 public class PropsToTest {
112 String constrained = null;
113 String bound = null;
114 String normal = null;
117 PropsToTest PTT = new PropsToTest();
120 * Tests method <code>getPropertySetInfo</code>. After test completed
121 * call {@link #getPropsToTest} method to retrieve different kinds
122 * of properties to test then. <p>
123 * Has OK status if not null <code>XPropertySetInfo</code>
124 * object returned.<p>
125 * Since <code>getPropertySetInfo</code> is optional, it may return null,
126 * if it is not implemented. This method uses then an object relation
127 * <code>PTT</code> (Properties To Test) to determine available properties.
128 * All tests for services without <code>getPropertySetInfo</code> must
129 * provide this object relation.
131 public void _getPropertySetInfo() {
133 XPropertySetInfo propertySetInfo = oObj.getPropertySetInfo();
135 if (propertySetInfo == null) {
136 log.println("getPropertySetInfo() method returned null");
137 tRes.tested("getPropertySetInfo()", true) ;
138 String[] ptt = (String[]) tEnv.getObjRelation("PTT");
139 PTT.normal=ptt[0];
140 PTT.bound=ptt[1];
141 PTT.constrained=ptt[2];
142 } else {
143 tRes.tested("getPropertySetInfo()", true );
144 getPropsToTest(propertySetInfo);
147 return;
149 } // end of getPropertySetInfo()
152 * Tests change listener which added for bound properties.
153 * Adds listener to bound property (if it exists), then changes
154 * its value and check if listener was called. <p>
155 * Method tests to be successfully completed before :
156 * <ul>
157 * <li> <code>getPropertySetInfo</code> : in this method test
158 * one of bound properties is retrieved. </li>
159 * </ul> <p>
160 * Has OK status if NO bound properties exist or if listener
161 * was successfully called.
163 public void _addPropertyChangeListener() {
165 requiredMethod("getPropertySetInfo()");
167 propertyChanged = false;
170 if ( PTT.bound.equals("none") ) {
171 log.println("*** No bound properties found ***");
172 tRes.tested("addPropertyChangeListener()", true) ;
173 } else {
174 try {
175 oObj.addPropertyChangeListener(PTT.bound,PClistener);
176 Object gValue = oObj.getPropertyValue(PTT.bound);
177 oObj.setPropertyValue(PTT.bound,
178 ValueChanger.changePValue(gValue));
179 } catch (com.sun.star.beans.PropertyVetoException e) {
180 log.println("Exception occured while trying to change "+
181 "property '"+ PTT.bound+"'");
182 e.printStackTrace(log);
183 } catch (com.sun.star.lang.IllegalArgumentException e) {
184 log.println("Exception occured while trying to change "+
185 "property '"+ PTT.bound+"'");
186 e.printStackTrace(log);
187 } catch (com.sun.star.beans.UnknownPropertyException e) {
188 log.println("Exception occured while trying to change "+
189 "property '"+ PTT.bound+"'");
190 e.printStackTrace(log);
191 } catch (com.sun.star.lang.WrappedTargetException e) {
192 log.println("Exception occured while trying to change "+
193 "property '"+ PTT.bound+"'");
194 e.printStackTrace(log);
195 } // end of try-catch
196 tRes.tested("addPropertyChangeListener()", propertyChanged);
197 if (!propertyChanged) {
198 log.println("propertyChangeListener wasn't called for '"+
199 PTT.bound+"'");
201 } //endif
203 return;
205 } // end of addPropertyChangeListener()
208 * Tests vetoable listener which added for constrained properties.
209 * Adds listener to constrained property (if it exists), then changes
210 * its value and check if listener was called. <p>
211 * Method tests to be successfully completed before :
212 * <ul>
213 * <li> <code>getPropertySetInfo</code> : in this method test
214 * one of constrained properties is retrieved. </li>
215 * </ul> <p>
216 * Has OK status if NO constrained properties exist or if listener
217 * was successfully called.
219 public void _addVetoableChangeListener() {
221 requiredMethod("getPropertySetInfo()");
223 vetoableChanged = false;
225 if ( PTT.constrained.equals("none") ) {
226 log.println("*** No constrained properties found ***");
227 tRes.tested("addVetoableChangeListener()", true) ;
228 } else {
229 try {
230 oObj.addVetoableChangeListener(PTT.constrained,VClistener);
231 Object gValue = oObj.getPropertyValue(PTT.constrained);
232 oObj.setPropertyValue(PTT.constrained,
233 ValueChanger.changePValue(gValue));
234 } catch (com.sun.star.beans.PropertyVetoException e) {
235 log.println("Exception occured while trying to change "+
236 "property '"+ PTT.constrained+"'");
237 e.printStackTrace(log);
238 } catch (com.sun.star.lang.IllegalArgumentException e) {
239 log.println("Exception occured while trying to change "+
240 "property '"+ PTT.constrained+"'");
241 e.printStackTrace(log);
242 } catch (com.sun.star.beans.UnknownPropertyException e) {
243 log.println("Exception occured while trying to change "+
244 "property '"+ PTT.constrained+"'");
245 e.printStackTrace(log);
246 } catch (com.sun.star.lang.WrappedTargetException e) {
247 log.println("Exception occured while trying to change "+
248 "property '"+ PTT.constrained+"'");
249 e.printStackTrace(log);
250 } // end of try-catch
251 tRes.tested("addVetoableChangeListener()",vetoableChanged);
252 if (!vetoableChanged) {
253 log.println("vetoableChangeListener wasn't called for '"+
254 PTT.constrained+"'");
256 } //endif
258 return;
260 } // end of addVetoableChangeListener()
264 * Tests <code>setPropertyValue</code> method.
265 * Stores value before call, and compares it with value after
266 * call. <p>
267 * Method tests to be successfully completed before :
268 * <ul>
269 * <li> <code>getPropertySetInfo</code> : in this method test
270 * one of normal properties is retrieved. </li>
271 * </ul> <p>
272 * Has OK status if NO normal properties exist or if value before
273 * method call is not equal to value after.
275 public void _setPropertyValue() {
277 requiredMethod("getPropertySetInfo()");
279 Object gValue = null;
280 Object sValue = null;
282 if ( PTT.normal.equals("none") ) {
283 log.println("*** No changeable properties found ***");
284 tRes.tested("setPropertyValue()", true) ;
285 } else {
286 try {
287 log.println("try to cheange value of property '" + PTT.normal + "'" );
288 gValue = oObj.getPropertyValue(PTT.normal);
289 sValue = ValueChanger.changePValue(gValue);
290 oObj.setPropertyValue(PTT.normal, sValue);
291 sValue = oObj.getPropertyValue(PTT.normal);
292 } catch (com.sun.star.beans.PropertyVetoException e) {
293 log.println("Exception occured while trying to change "+
294 "property '"+ PTT.normal+"'");
295 e.printStackTrace(log);
296 } catch (com.sun.star.lang.IllegalArgumentException e) {
297 log.println("Exception occured while trying to change "+
298 "property '"+ PTT.normal+"'");
299 e.printStackTrace(log);
300 } catch (com.sun.star.beans.UnknownPropertyException e) {
301 log.println("Exception occured while trying to change "+
302 "property '"+ PTT.normal+"'");
303 e.printStackTrace(log);
304 } catch (com.sun.star.lang.WrappedTargetException e) {
305 log.println("Exception occured while trying to change "+
306 "property '"+ PTT.normal+"'");
307 e.printStackTrace(log);
308 } // end of try-catch
309 tRes.tested("setPropertyValue()",(! gValue.equals(sValue)));
310 } //endif
312 return;
314 } // end of setPropertyValue()
317 * Tests <code>getPropertyValue</code> method.
318 * Just call this method and checks for no exceptions <p>
319 * Method tests to be successfully completed before :
320 * <ul>
321 * <li> <code>getPropertySetInfo</code> : in this method test
322 * one of normal properties is retrieved. </li>
323 * </ul> <p>
324 * Has OK status if NO normal properties exist or if no
325 * exceptions were thrown.
327 public void _getPropertyValue() {
329 requiredMethod("getPropertySetInfo()");
331 String toCheck = PTT.normal;
333 if ( PTT.normal.equals("none") ) {
334 toCheck = oObj.getPropertySetInfo().getProperties()[0].Name;
335 log.println("All properties are Read Only");
336 log.println("Using: "+toCheck);
339 try {
340 oObj.getPropertyValue(toCheck);
341 tRes.tested("getPropertyValue()",true);
342 } catch (com.sun.star.beans.UnknownPropertyException e) {
343 log.println("Exception occured while trying to get property '"+
344 PTT.normal+"'");
345 e.printStackTrace(log);
346 tRes.tested("getPropertyValue()",false);
347 } catch (com.sun.star.lang.WrappedTargetException e) {
348 log.println("Exception occured while trying to get property '"+
349 PTT.normal+"'");
350 e.printStackTrace(log);
351 tRes.tested("getPropertyValue()",false);
352 } // end of try-catch
354 return;
358 * Tests <code>removePropertyChangeListener</code> method.
359 * Removes change listener, then changes bound property value
360 * and checks if the listener was NOT called.
361 * Method tests to be successfully completed before :
362 * <ul>
363 * <li> <code>addPropertyChangeListener</code> : here listener
364 * was added. </li>
365 * </ul> <p>
366 * Has OK status if NO bound properties exist or if listener
367 * was not called and no exceptions arose.
369 public void _removePropertyChangeListener() {
371 requiredMethod("addPropertyChangeListener()");
373 propertyChanged = false;
375 if ( PTT.bound.equals("none") ) {
376 log.println("*** No bound properties found ***");
377 tRes.tested("removePropertyChangeListener()", true) ;
378 } else {
379 try {
380 propertyChanged = false;
381 oObj.removePropertyChangeListener(PTT.bound,PClistener);
382 Object gValue = oObj.getPropertyValue(PTT.bound);
383 oObj.setPropertyValue(PTT.bound,
384 ValueChanger.changePValue(gValue));
385 } catch (com.sun.star.beans.PropertyVetoException e) {
386 log.println("Exception occured while trying to change "+
387 "property '"+ PTT.bound+"'");
388 e.printStackTrace(log);
389 } catch (com.sun.star.lang.IllegalArgumentException e) {
390 log.println("Exception occured while trying to change "+
391 "property '"+ PTT.bound+"'");
392 e.printStackTrace(log);
393 } catch (com.sun.star.beans.UnknownPropertyException e) {
394 log.println("Exception occured while trying to change "+
395 "property '"+ PTT.bound+"'");
396 e.printStackTrace(log);
397 } catch (com.sun.star.lang.WrappedTargetException e) {
398 log.println("Exception occured while trying to change "+
399 "property '"+ PTT.bound+"'");
400 e.printStackTrace(log);
401 } // end of try-catch
403 tRes.tested("removePropertyChangeListener()",!propertyChanged);
404 if (propertyChanged) {
405 log.println("propertyChangeListener was called after removing"+
406 " for '"+PTT.bound+"'");
408 } //endif
410 return;
412 } // end of removePropertyChangeListener()
416 * Tests <code>removeVetoableChangeListener</code> method.
417 * Removes vetoable listener, then changes constrained property value
418 * and checks if the listener was NOT called.
419 * Method tests to be successfully completed before :
420 * <ul>
421 * <li> <code>addPropertyChangeListener</code> : here vetoable listener
422 * was added. </li>
423 * </ul> <p>
424 * Has OK status if NO constrained properties exist or if listener
425 * was NOT called and no exceptions arose.
427 public void _removeVetoableChangeListener() {
429 requiredMethod("addVetoableChangeListener()");
431 vetoableChanged = false;
433 if ( PTT.constrained.equals("none") ) {
434 log.println("*** No constrained properties found ***");
435 tRes.tested("removeVetoableChangeListener()", true) ;
436 } else {
437 try {
438 oObj.removeVetoableChangeListener(PTT.constrained,VClistener);
439 Object gValue = oObj.getPropertyValue(PTT.constrained);
440 oObj.setPropertyValue(PTT.constrained,
441 ValueChanger.changePValue(gValue));
442 } catch (com.sun.star.beans.PropertyVetoException e) {
443 log.println("Exception occured while trying to change "+
444 "property '"+ PTT.constrained+"'");
445 e.printStackTrace(log);
446 } catch (com.sun.star.lang.IllegalArgumentException e) {
447 log.println("Exception occured while trying to change "+
448 "property '"+ PTT.constrained+"'");
449 e.printStackTrace(log);
450 } catch (com.sun.star.beans.UnknownPropertyException e) {
451 log.println("Exception occured while trying to change "+
452 "property '"+ PTT.constrained+"'");
453 e.printStackTrace(log);
454 } catch (com.sun.star.lang.WrappedTargetException e) {
455 log.println("Exception occured while trying to change "+
456 "property '"+ PTT.constrained+"'");
457 e.printStackTrace(log);
458 } // end of try-catch
460 tRes.tested("removeVetoableChangeListener()",!vetoableChanged);
461 if (vetoableChanged) {
462 log.println("vetoableChangeListener was called after "+
463 "removing for '"+PTT.constrained+"'");
465 } //endif
467 return;
469 } // end of removeVetoableChangeListener()
473 * Gets the properties being tested. Searches and stores by one
474 * property of each kind (Bound, Vetoable, Normal).
476 public PropsToTest getPropsToTest(XPropertySetInfo xPSI) {
478 Property[] properties = xPSI.getProperties();
479 String bound = "";
480 String constrained = "";
481 String normal = "";
482 // some properties should not be changed in a unspecific way
483 String[] skip = {"PrinterName", "CharRelief", "IsLayerMode"};
485 for (int i = 0; i < properties.length; i++) {
487 Property property = properties[i];
488 String name = property.Name;
490 boolean cont = false;
491 for (int j = 0; j < skip.length; j++) {
492 if (name.equals(skip[j])){
493 log.println("skipping '" + name + "'");
494 cont = true;
498 if (cont) continue;
500 if (name.equals(oObj))
501 log.println("Checking '"+name+"'");
502 boolean isWritable = ((property.Attributes &
503 PropertyAttribute.READONLY) == 0);
504 boolean isNotNull = ((property.Attributes &
505 PropertyAttribute.MAYBEVOID) == 0);
506 boolean isBound = ((property.Attributes &
507 PropertyAttribute.BOUND) != 0);
508 boolean isConstr = ((property.Attributes &
509 PropertyAttribute.CONSTRAINED) != 0);
510 boolean canChange = false;
512 if ( !isWritable ) log.println("Property '"+name+"' is READONLY");
514 if (name.endsWith("URL")) isWritable = false;
515 if (name.startsWith("Fill")) isWritable = false;
516 if (name.startsWith("Font")) isWritable = false;
517 if (name.startsWith("IsNumbering")) isWritable = false;
518 if (name.startsWith("LayerName")) isWritable = false;
519 if (name.startsWith("Line")) isWritable = false;
520 if (name.startsWith("TextWriting")) isWritable = false;
522 //if (name.equals("xinterfaceA") || name.equals("xtypeproviderA")
523 //|| name.equals("arAnyA")) isWritable=false;
525 if ( isWritable && isNotNull ) canChange = isChangeable(name);
527 if ( isWritable && isNotNull && isBound && canChange) {
528 bound+=name+";";
531 if ( isWritable && isNotNull && isConstr && canChange) {
532 constrained+=name+";";
535 if ( isWritable && isNotNull && canChange) normal+=name+";";
538 } // endfor
540 //get a random bound property
541 PTT.bound=getRandomString(bound);
542 log.println("Bound: "+PTT.bound);
544 //get a random constrained property
545 PTT.constrained=getRandomString(constrained);
546 log.println("Constrained: "+PTT.constrained);
548 //get a random normal property
549 PTT.normal=getRandomString(normal);
551 return PTT;
556 * Retrieves one random property name from list (property names separated
557 * by ';') of property names.
559 public String getRandomString(String str) {
561 String gRS = "none";
562 Random rnd = new Random();
564 if (str.equals("")) str = "none";
565 StringTokenizer ST=new StringTokenizer(str,";");
566 int nr = rnd.nextInt(ST.countTokens());
567 if (nr < 1) nr+=1;
568 for (int i=1; i<nr+1; i++) gRS = ST.nextToken();
570 return gRS;
574 public boolean isChangeable(String name) {
575 boolean hasChanged = false;
576 try {
577 Object getProp = oObj.getPropertyValue(name);
578 log.println("Getting: "+getProp);
579 if (name.equals("xinterfaceA")) {
580 System.out.println("drin");
583 Object setValue = null;
584 if (getProp != null) {
585 if (!utils.isVoid(getProp))
586 setValue = ValueChanger.changePValue(getProp);
587 else log.println("Property '"+name+
588 "' is void but MAYBEVOID isn't set");
589 } else log.println("Property '"+name+"'is null and can't be changed");
590 if (name.equals("LineStyle")) setValue = null;
591 if (setValue != null) {
592 log.println("Setting to :"+setValue);
593 oObj.setPropertyValue(name, setValue);
594 hasChanged = (! getProp.equals(oObj.getPropertyValue(name)));
595 } else log.println("Couldn't change Property '"+name+"'");
596 } catch (com.sun.star.beans.PropertyVetoException e) {
597 log.println("'" + name + "' throws exception '" + e + "'");
598 e.printStackTrace(log);
599 } catch (com.sun.star.lang.IllegalArgumentException e) {
600 log.println("'" + name + "' throws exception '" + e + "'");
601 e.printStackTrace(log);
602 } catch (com.sun.star.beans.UnknownPropertyException e) {
603 log.println("'" + name + "' throws exception '" + e + "'");
604 e.printStackTrace(log);
605 } catch (com.sun.star.lang.WrappedTargetException e) {
606 log.println("'" + name + "' throws exception '" + e + "'");
607 e.printStackTrace(log);
608 } catch (com.sun.star.uno.RuntimeException e) {
609 log.println("'" + name + "' throws exception '" + e + "'");
610 e.printStackTrace(log);
611 } catch (java.lang.ArrayIndexOutOfBoundsException e) {
612 log.println("'" + name + "' throws exception '" + e + "'");
613 e.printStackTrace(log);
616 return hasChanged;
620 * Forces environment recreation.
622 protected void after() {
623 disposeEnvironment();
627 } // finish class _XPropertySet