2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package complex
.dataPilot
;
21 import com
.sun
.star
.beans
.Property
;
22 import com
.sun
.star
.beans
.PropertyAttribute
;
23 import com
.sun
.star
.beans
.PropertyChangeEvent
;
24 import com
.sun
.star
.beans
.XPropertyChangeListener
;
25 import com
.sun
.star
.beans
.XPropertySet
;
26 import com
.sun
.star
.beans
.XPropertySetInfo
;
27 import com
.sun
.star
.beans
.XVetoableChangeListener
;
28 import com
.sun
.star
.lang
.EventObject
;
29 import java
.util
.Random
;
30 import java
.util
.StringTokenizer
;
31 import lib
.TestParameters
;
32 import util
.ValueChanger
;
36 * Testing <code>com.sun.star.beans.XPropertySet</code>
39 * <li><code>getPropertySetInfo()</code></li>
40 * <li><code>setPropertyValue()</code></li>
41 * <li><code>getPropertyValue()</code></li>
42 * <li><code>addPropertyChangeListener()</code></li>
43 * <li><code>removePropertyChangeListener()</code></li>
44 * <li><code>addVetoableChangeListener()</code></li>
45 * <li><code>removeVetoableChangeListener()</code></li>
47 * @see com.sun.star.beans.XPropertySet
49 public class _XPropertySet
{
52 * The object that is testsed.
54 private final XPropertySet oObj
;
59 private final TestParameters param
;
62 * Flag that indicates change listener was called.
64 private boolean propertyChanged
= false;
68 * The own property change listener
70 private final XPropertyChangeListener PClistener
= new MyChangeListener();
73 * Listener that must be called on bound property changing.
75 private class MyChangeListener
implements XPropertyChangeListener
{
77 * Just set <code>propertyChanged</code> flag to true.
79 public void propertyChange(PropertyChangeEvent e
) {
80 propertyChanged
= true;
82 public void disposing (EventObject obj
) {}
87 * Flag that indicates veto listener was called.
89 private boolean vetoableChanged
= false;
92 * The own vetoable change listener
94 private final XVetoableChangeListener VClistener
= new MyVetoListener();
97 * Listener that must be called on constrained property changing.
99 private class MyVetoListener
implements XVetoableChangeListener
{
101 * Just set <code>vetoableChanged</code> flag to true.
103 public void vetoableChange(PropertyChangeEvent e
) {
104 vetoableChanged
= true;
106 public void disposing (EventObject obj
) {}
113 private final PropsToTest PTT
= new PropsToTest();
116 * Structure that collects three properties of each type to test :
117 * Constrained, Bound and Normal.
119 private class PropsToTest
{
120 private String constrained
= null;
121 private String bound
= null;
122 String normal
= null;
126 * Constructor: gets the object to test, a logger and the test parameters
127 * @param xObj The test object
128 * @param param The test parameters
130 public _XPropertySet(XPropertySet xObj
/*, LogWriter log*/, TestParameters param
) {
137 * Tests method <code>getPropertySetInfo</code>. After test completed
138 * call {@link #getPropsToTest} method to retrieve different kinds
139 * of properties to test then. <p>
140 * Has OK status if not null <code>XPropertySetInfo</code>
141 * object returned.<p>
142 * Since <code>getPropertySetInfo</code> is optional, it may return null,
143 * if it is not implemented. This method uses then an object relation
144 * <code>PTT</code> (Properties To Test) to determine available properties.
145 * All tests for services without <code>getPropertySetInfo</code> must
146 * provide this object relation.
148 public boolean _getPropertySetInfo() {
149 XPropertySetInfo propertySetInfo
= oObj
.getPropertySetInfo();
151 if (propertySetInfo
== null) {
152 System
.out
.println("getPropertySetInfo() method returned null");
153 String
[] ptt
= (String
[]) param
.get("PTT");
156 PTT
.constrained
=ptt
[2];
158 getPropsToTest(propertySetInfo
);
163 } // end of getPropertySetInfo()
166 * Tests change listener which added for bound properties.
167 * Adds listener to bound property (if it exists), then changes
168 * its value and check if listener was called. <p>
169 * Method tests to be successfully completed before :
171 * <li> <code>getPropertySetInfo</code> : in this method test
172 * one of bound properties is retrieved. </li>
174 * Has OK status if NO bound properties exist or if listener
175 * was successfully called.
177 public boolean _addPropertyChangeListener() {
179 propertyChanged
= false;
180 boolean result
= true;
182 if ( PTT
.bound
.equals("none") ) {
183 System
.out
.println("*** No bound properties found ***");
186 oObj
.addPropertyChangeListener(PTT
.bound
,PClistener
);
187 Object gValue
= oObj
.getPropertyValue(PTT
.bound
);
188 oObj
.setPropertyValue(PTT
.bound
,
189 ValueChanger
.changePValue(gValue
));
190 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
191 System
.out
.println("Exception occurred while trying to change "+
192 "property '"+ PTT
.bound
+"'");
194 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
195 System
.out
.println("Exception occurred while trying to change "+
196 "property '"+ PTT
.bound
+"'");
198 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
199 System
.out
.println("Exception occurred while trying to change "+
200 "property '"+ PTT
.bound
+"'");
202 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
203 System
.out
.println("Exception occurred while trying to change "+
204 "property '"+ PTT
.bound
+"'");
206 } // end of try-catch
207 result
= propertyChanged
;
208 if (!propertyChanged
) {
209 System
.out
.println("propertyChangeListener wasn't called for '"+
216 } // end of addPropertyChangeListener()
219 * Tests vetoable listener which added for constrained properties.
220 * Adds listener to constrained property (if it exists), then changes
221 * its value and check if listener was called. <p>
222 * Method tests to be successfully completed before :
224 * <li> <code>getPropertySetInfo</code> : in this method test
225 * one of constrained properties is retrieved. </li>
227 * Has OK status if NO constrained properties exist or if listener
228 * was successfully called.
230 public boolean _addVetoableChangeListener() {
232 vetoableChanged
= false;
233 boolean result
= true;
235 if ( PTT
.constrained
.equals("none") ) {
236 System
.out
.println("*** No constrained properties found ***");
239 oObj
.addVetoableChangeListener(PTT
.constrained
,VClistener
);
240 Object gValue
= oObj
.getPropertyValue(PTT
.constrained
);
241 oObj
.setPropertyValue(PTT
.constrained
,
242 ValueChanger
.changePValue(gValue
));
243 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
244 System
.out
.println("Exception occurred while trying to change "+
245 "property '"+ PTT
.constrained
+"'");
247 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
248 System
.out
.println("Exception occurred while trying to change "+
249 "property '"+ PTT
.constrained
+"'");
251 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
252 System
.out
.println("Exception occurred while trying to change "+
253 "property '"+ PTT
.constrained
+"'");
255 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
256 System
.out
.println("Exception occurred while trying to change "+
257 "property '"+ PTT
.constrained
+"'");
259 } // end of try-catch
260 result
= vetoableChanged
;
261 if (!vetoableChanged
) {
262 System
.out
.println("vetoableChangeListener wasn't called for '"+
263 PTT
.constrained
+"'");
269 } // end of addVetoableChangeListener()
273 * Tests <code>setPropertyValue</code> method.
274 * Stores value before call, and compares it with value after
276 * Method tests to be successfully completed before :
278 * <li> <code>getPropertySetInfo</code> : in this method test
279 * one of normal properties is retrieved. </li>
281 * Has OK status if NO normal properties exist or if value before
282 * method call is not equal to value after.
284 public boolean _setPropertyValue() {
286 Object gValue
= null;
287 Object sValue
= null;
289 boolean result
= true;
291 if ( PTT
.normal
.equals("none") ) {
292 System
.out
.println("*** No changeable properties found ***");
295 gValue
= oObj
.getPropertyValue(PTT
.normal
);
296 sValue
= ValueChanger
.changePValue(gValue
);
297 oObj
.setPropertyValue(PTT
.normal
, sValue
);
298 sValue
= oObj
.getPropertyValue(PTT
.normal
);
299 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
300 System
.out
.println("Exception occurred while trying to change "+
301 "property '"+ PTT
.normal
+"'");
303 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
304 System
.out
.println("Exception occurred while trying to change "+
305 "property '"+ PTT
.normal
+"'");
307 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
308 System
.out
.println("Exception occurred while trying to change "+
309 "property '"+ PTT
.normal
+"'");
311 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
312 System
.out
.println("Exception occurred while trying to change "+
313 "property '"+ PTT
.normal
+"'");
315 } // end of try-catch
316 result
= !gValue
.equals(sValue
);
321 } // end of setPropertyValue()
324 * Tests <code>getPropertyValue</code> method.
325 * Just call this method and checks for no exceptions <p>
326 * Method tests to be successfully completed before :
328 * <li> <code>getPropertySetInfo</code> : in this method test
329 * one of normal properties is retrieved. </li>
331 * Has OK status if NO normal properties exist or if no
332 * exceptions were thrown.
334 public boolean _getPropertyValue() {
336 boolean result
= true;
337 String toCheck
= PTT
.normal
;
339 if ( PTT
.normal
.equals("none") ) {
340 toCheck
= oObj
.getPropertySetInfo().getProperties()[0].Name
;
341 System
.out
.println("All properties are Read Only");
342 System
.out
.println("Using: "+toCheck
);
346 oObj
.getPropertyValue(toCheck
);
347 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
348 System
.out
.println("Exception occurred while trying to get property '"+
352 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
353 System
.out
.println("Exception occurred while trying to get property '"+
357 } // end of try-catch
363 * Tests <code>removePropertyChangeListener</code> method.
364 * Removes change listener, then changes bound property value
365 * and checks if the listener was NOT called.
366 * Method tests to be successfully completed before :
368 * <li> <code>addPropertyChangeListener</code> : here listener
371 * Has OK status if NO bound properties exist or if listener
372 * was not called and no exceptions arose.
374 public boolean _removePropertyChangeListener() {
376 propertyChanged
= false;
377 boolean result
= true;
379 if ( PTT
.bound
.equals("none") ) {
380 System
.out
.println("*** No bound properties found ***");
383 propertyChanged
= false;
384 oObj
.removePropertyChangeListener(PTT
.bound
,PClistener
);
385 Object gValue
= oObj
.getPropertyValue(PTT
.bound
);
386 oObj
.setPropertyValue(PTT
.bound
,
387 ValueChanger
.changePValue(gValue
));
388 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
389 System
.out
.println("Exception occurred while trying to change "+
390 "property '"+ PTT
.bound
+"'");
392 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
393 System
.out
.println("Exception occurred while trying to change "+
394 "property '"+ PTT
.bound
+"'");
396 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
397 System
.out
.println("Exception occurred while trying to change "+
398 "property '"+ PTT
.bound
+"'");
400 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
401 System
.out
.println("Exception occurred while trying to change "+
402 "property '"+ PTT
.bound
+"'");
404 } // end of try-catch
406 result
= !propertyChanged
;
407 if (propertyChanged
) {
408 System
.out
.println("propertyChangeListener was called after removing"+
409 " for '"+PTT
.bound
+"'");
415 } // end of removePropertyChangeListener()
419 * Tests <code>removeVetoableChangeListener</code> method.
420 * Removes vetoable listener, then changes constrained property value
421 * and checks if the listener was NOT called.
422 * Method tests to be successfully completed before :
424 * <li> <code>addPropertyChangeListener</code> : here vetoable listener
427 * Has OK status if NO constrained properties exist or if listener
428 * was NOT called and no exceptions arose.
430 public boolean _removeVetoableChangeListener() {
432 vetoableChanged
= false;
433 boolean result
= true;
435 if ( PTT
.constrained
.equals("none") ) {
436 System
.out
.println("*** No constrained properties found ***");
439 oObj
.removeVetoableChangeListener(PTT
.constrained
,VClistener
);
440 Object gValue
= oObj
.getPropertyValue(PTT
.constrained
);
441 oObj
.setPropertyValue(PTT
.constrained
,
442 ValueChanger
.changePValue(gValue
));
443 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
444 System
.out
.println("Exception occurred while trying to change "+
445 "property '"+ PTT
.constrained
+"'");
447 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
448 System
.out
.println("Exception occurred while trying to change "+
449 "property '"+ PTT
.constrained
+"'");
451 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
452 System
.out
.println("Exception occurred while trying to change "+
453 "property '"+ PTT
.constrained
+"'");
455 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
456 System
.out
.println("Exception occurred while trying to change "+
457 "property '"+ PTT
.constrained
+"'");
459 } // end of try-catch
461 result
= !vetoableChanged
;
462 if (vetoableChanged
) {
463 System
.out
.println("vetoableChangeListener was called after "+
464 "removing for '"+PTT
.constrained
+"'");
470 } // end of removeVetoableChangeListener()
474 * Gets the properties being tested. Searches and stores by one
475 * property of each kind (Bound, Vetoable, Normal).
477 private PropsToTest
getPropsToTest(XPropertySetInfo xPSI
) {
479 Property
[] properties
= xPSI
.getProperties();
481 String constrained
= "";
484 for (int i
= 0; i
< properties
.length
; i
++) {
486 Property property
= properties
[i
];
487 String name
= property
.Name
;
488 System
.out
.println("Checking '"+name
+"'");
489 boolean isWritable
= ((property
.Attributes
&
490 PropertyAttribute
.READONLY
) == 0);
491 boolean isNotNull
= ((property
.Attributes
&
492 PropertyAttribute
.MAYBEVOID
) == 0);
493 boolean isBound
= ((property
.Attributes
&
494 PropertyAttribute
.BOUND
) != 0);
495 boolean isConstr
= ((property
.Attributes
&
496 PropertyAttribute
.CONSTRAINED
) != 0);
497 boolean canChange
= false;
499 if ( !isWritable
) System
.out
.println("Property '"+name
+"' is READONLY");
501 if (name
.endsWith("URL")) isWritable
= false;
502 if (name
.startsWith("Fill")) isWritable
= false;
503 if (name
.startsWith("Font")) isWritable
= false;
504 if (name
.startsWith("IsNumbering")) isWritable
= false;
505 if (name
.startsWith("LayerName")) isWritable
= false;
506 if (name
.startsWith("Line")) isWritable
= false;
508 if ( isWritable
&& isNotNull
) canChange
= isChangeable(name
);
510 if ( isWritable
&& isNotNull
&& isBound
&& canChange
) {
514 if ( isWritable
&& isNotNull
&& isConstr
&& canChange
) {
515 constrained
+=name
+";";
518 if ( isWritable
&& isNotNull
&& canChange
) normal
+=name
+";";
523 //get a random bound property
524 PTT
.bound
=getRandomString(bound
);
525 System
.out
.println("Bound: "+PTT
.bound
);
527 //get a random constrained property
528 PTT
.constrained
=getRandomString(constrained
);
529 System
.out
.println("Constrained: "+PTT
.constrained
);
531 //get a random normal property
532 PTT
.normal
=getRandomString(normal
);
539 * Retrieves one random property name from list (property names separated
540 * by ';') of property names.
542 private String
getRandomString(String str
) {
545 Random rnd
= new Random();
547 if (str
.equals("")) str
= "none";
548 StringTokenizer ST
=new StringTokenizer(str
,";");
549 int nr
= rnd
.nextInt(ST
.countTokens());
551 for (int i
=1; i
<nr
+1; i
++) gRS
= ST
.nextToken();
557 private boolean isChangeable(String name
) {
558 boolean hasChanged
= false;
560 Object getProp
= oObj
.getPropertyValue(name
);
561 System
.out
.println("Getting: "+getProp
);
563 Object setValue
= null;
564 if (getProp
!= null) {
565 if (!utils
.isVoid(getProp
))
566 setValue
= ValueChanger
.changePValue(getProp
);
567 else System
.out
.println("Property '"+name
+
568 "' is void but MAYBEVOID isn't set");
569 } else System
.out
.println("Property '"+name
+"'is null and can't be changed");
570 if (name
.equals("LineStyle")) setValue
= null;
571 if (setValue
!= null) {
572 oObj
.setPropertyValue(name
, setValue
);
573 System
.out
.println("Setting to :"+setValue
);
574 hasChanged
= (! getProp
.equals(oObj
.getPropertyValue(name
)));
575 } else System
.out
.println("Couldn't change Property '"+name
+"'");
576 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
577 System
.out
.println("'" + name
+ "' throws exception '" + e
+ "'");
579 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
580 System
.out
.println("'" + name
+ "' throws exception '" + e
+ "'");
582 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
583 System
.out
.println("'" + name
+ "' throws exception '" + e
+ "'");
585 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
586 System
.out
.println("'" + name
+ "' throws exception '" + e
+ "'");
588 } catch (com
.sun
.star
.uno
.RuntimeException e
) {
589 System
.out
.println("'" + name
+ "' throws exception '" + e
+ "'");
591 } catch (java
.lang
.ArrayIndexOutOfBoundsException e
) {
592 System
.out
.println("'" + name
+ "' throws exception '" + e
+ "'");
600 } // finish class _XPropertySet