1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import java
.io
.PrintWriter
;
31 import java
.util
.HashSet
;
33 import java
.util
.StringTokenizer
;
35 import lib
.MultiMethodTest
;
37 import util
.ValueChanger
;
39 import com
.sun
.star
.beans
.Property
;
40 import com
.sun
.star
.beans
.PropertyAttribute
;
41 import com
.sun
.star
.beans
.PropertyChangeEvent
;
42 import com
.sun
.star
.beans
.XMultiPropertySet
;
43 import com
.sun
.star
.beans
.XPropertiesChangeListener
;
44 import com
.sun
.star
.beans
.XPropertySetInfo
;
45 import com
.sun
.star
.lang
.EventObject
;
49 * Testing <code>com.sun.star.beans.XMultiPropertySet</code>
52 * <li><code> getPropertySetInfo()</code></li>
53 * <li><code> setPropertyValues()</code></li>
54 * <li><code> getPropertyValues()</code></li>
55 * <li><code> addPropertiesChangeListener()</code></li>
56 * <li><code> removePropertiesChangeListener()</code></li>
57 * <li><code> firePropertiesChangeEvent()</code></li>
60 * Required relations :
62 * <li> <code>'XMultiPropertySet.ExcludeProps'</code>
63 * <b>(optional) </b> : java.util.Set.
64 * Has property names which must be skipped from testing in
65 * some reasons (for example property accepts restricted set
70 * Test is <b> NOT </b> multithread compilant. <p>
71 * After test completion object environment has to be recreated.
72 * @see com.sun.star.beans.XMultiPropertySet
74 public class _XMultiPropertySet
extends MultiMethodTest
{
76 public XMultiPropertySet oObj
= null;
78 private boolean propertiesChanged
= false;
79 private XPropertySetInfo propertySetInfo
= null;
80 private String
[] testPropsNames
= null;
81 private int testPropsAmount
= 0;
82 private PrintWriter _log
= null;
84 private Object
[] values
= null;
86 private Set exclProps
= null;
89 * Initializes some fields.
91 public void before() {
94 exclProps
= (Set
) tEnv
.getObjRelation("XMultiPropertySet.ExcludeProps");
95 if (exclProps
== null) exclProps
= new HashSet(0);
99 * Listener implementation which sets a flag when
100 * listener was called.
102 public class MyChangeListener
implements XPropertiesChangeListener
{
103 public void propertiesChange(PropertyChangeEvent
[] e
) {
104 //_log.println("Listener was called");
105 propertiesChanged
= true;
107 public void disposing (EventObject obj
) {}
110 private XPropertiesChangeListener PClistener
=
111 new MyChangeListener();
114 * Test calls the method and checks return value.
115 * <code>PropertySetInfo</code> object is stored<p>
116 * Has <b> OK </b> status if the method returns not null value
117 * and no exceptions were thrown. <p>
119 public void _getPropertySetInfo() {
120 boolean bResult
= true;
121 propertySetInfo
= oObj
.getPropertySetInfo();
123 if (propertySetInfo
== null) {
124 log
.println("getPropertySetInfo() method returned null");
128 tRes
.tested("getPropertySetInfo()", bResult
) ;
133 * Test collects all property names and retrieves their values,
134 * then checks the value returned. Finally it also collects
135 * bound properties for other methods tests.<p>
136 * Has <b> OK </b> status if the method returns non null value
137 * and no exceptions were thrown. <p>
138 * The following method tests are to be completed successfully before :
140 * <li> <code> getPropertySetInfo() </code> : to have a list
141 * of properties.</li>
144 public void _getPropertyValues() {
145 requiredMethod("getPropertySetInfo()");
146 boolean bResult
= true;
148 Property
[] properties
= propertySetInfo
.getProperties();
149 String
[] allnames
= new String
[properties
.length
];
150 for (int i
= 0; i
< properties
.length
; i
++) {
151 allnames
[i
] = properties
[i
].Name
;
154 values
= oObj
.getPropertyValues(allnames
);
156 bResult
&= values
!=null;
157 tRes
.tested("getPropertyValues()", bResult
) ;
159 getPropsToTest(properties
);
163 * Test adds listener for all bound properties then each property
164 * is changed and listener call . <p>
165 * Has <b> OK </b> status if on each property change the listener was
166 * called and no exceptions were thrown. <p>
167 * The following method tests are to be completed successfully before :
169 * <li> <code> getPropertyValues() </code> : to collect bound
173 public void _addPropertiesChangeListener() {
175 requiredMethod("getPropertyValues()");
177 boolean result
= true ;
179 oObj
.addPropertiesChangeListener(testPropsNames
, PClistener
);
181 if ((testPropsAmount
==1) && (testPropsNames
[0].equals("none"))) {
186 // Change one of the property to be sure, that this event was cauched.
187 //Random rnd = new Random();
188 //int idx = rnd.nextInt(testPropsAmount);
189 for (int i
=0; i
<testPropsAmount
;i
++) {
190 log
.print("Trying to change property " + testPropsNames
[i
]);
192 Object
[] gValues
= oObj
.getPropertyValues(testPropsNames
);
193 Object newValue
= ValueChanger
.changePValue(gValues
[i
]);
194 gValues
[i
] = newValue
;
195 propertiesChanged
= false;
196 oObj
.setPropertyValues(testPropsNames
, gValues
);
198 result
&= propertiesChanged
;
199 log
.println(" ... done");
200 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
201 log
.println("Exception occurred while trying to change "+
202 "property '"+testPropsNames
[i
] + "' :" + e
);
203 e
.printStackTrace(log
);
204 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
205 log
.println("Exception occurred while trying to change "+
206 "property '"+testPropsNames
[i
] + "' :" + e
);
207 e
.printStackTrace(log
);
208 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
209 log
.println("Exception occurred while trying to change "+
210 "property '"+testPropsNames
[i
] + "' :" + e
);
211 e
.printStackTrace(log
);
212 } // end of try-catch
214 if (testPropsAmount
== 0) {
215 log
.println("all properties are read only");
216 tRes
.tested("addPropertiesChangeListener()", Status
.skipped(true));
218 tRes
.tested("addPropertiesChangeListener()", propertiesChanged
);
223 * Calls method and check if listener was called. <p>
224 * Has <b> OK </b> status if the listener was
225 * called and no exceptions were thrown. <p>
226 * The following method tests are to be completed successfully before :
228 * <li> <code> addPropertiesChangeListener() </code> : listener to
232 public void _firePropertiesChangeEvent() {
233 requiredMethod("addPropertiesChangeListener()");
234 propertiesChanged
= false ;
236 oObj
.firePropertiesChangeEvent(testPropsNames
, PClistener
);
239 tRes
.tested("firePropertiesChangeEvent()", propertiesChanged
);
244 * Removes listener added before. <p>
245 * Has <b> OK </b> status no exceptions were thrown. <p>
246 * The following method tests are to be completed successfully before :
248 * <li> <code> addPropertiesChangeListener() </code> : listener to
252 public void _removePropertiesChangeListener() {
253 requiredMethod("firePropertiesChangeEvent()");
254 boolean bResult
= true;
256 oObj
.removePropertiesChangeListener(PClistener
);
258 tRes
.tested("removePropertiesChangeListener()", bResult
);
263 * Changes all properties, then set them to new values, get them
264 * and checks if their values were changed properly. <p>
265 * Has <b> OK </b> status if all properties properly changed
266 * and no exceptions were thrown. <p>
267 * The following method tests are to be completed successfully before :
269 * <li> <code> getPropertyValues() </code> : to collect bound
273 public void _setPropertyValues() {
274 requiredMethod("getPropertyValues()");
275 boolean bResult
= true;
277 if ((testPropsNames
.length
==1)&&(testPropsNames
[0].equals("none"))) {
278 log
.println("all properties are readOnly");
279 tRes
.tested("setPropertyValues()",Status
.skipped(true));
283 log
.println("Changing all properties");
284 Object
[] gValues
= oObj
.getPropertyValues(testPropsNames
);
285 for (int i
=0; i
<testPropsAmount
;i
++) {
286 Object oldValue
= gValues
[i
];
287 Object newValue
= ValueChanger
.changePValue(oldValue
);
288 gValues
[i
] = newValue
;
292 oObj
.setPropertyValues(testPropsNames
, gValues
);
293 Object
[] newValues
= oObj
.getPropertyValues(testPropsNames
);
294 for (int i
=0; i
<testPropsAmount
;i
++) {
295 if (newValues
[i
].equals(gValues
[i
])) {
299 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
300 log
.println("Exception occurred while setting properties");
301 e
.printStackTrace(log
);
303 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
304 log
.println("Exception occurred while setting properties");
305 e
.printStackTrace(log
);
307 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
308 log
.println("Exception occurred while setting properties");
309 e
.printStackTrace(log
);
311 } // end of try-catch
313 tRes
.tested("setPropertyValues()", bResult
);
316 //Get the properties being tested
317 private void getPropsToTest(Property
[] properties
) {
321 for (int i
= 0; i
< properties
.length
; i
++) {
323 Property property
= properties
[i
];
324 String name
= property
.Name
;
325 boolean isWritable
= ((property
.Attributes
&
326 PropertyAttribute
.READONLY
) == 0);
327 boolean isNotNull
= ((property
.Attributes
&
328 PropertyAttribute
.MAYBEVOID
) == 0);
329 boolean isBound
= ((property
.Attributes
&
330 PropertyAttribute
.BOUND
) != 0);
331 boolean isExcluded
= exclProps
.contains(name
);
333 //exclude UserDefined, because we can't change XNameContainer
334 if (name
.indexOf("UserDefined")>0 || name
.indexOf("Device")>0) {
338 values
= oObj
.getPropertyValues(new String
[]{property
.Name
});
340 boolean isVoid
= util
.utils
.isVoid(values
[0]);
342 if ( isWritable
&& isNotNull
&& isBound
&& !isExcluded
&& !isVoid
) {
348 //get a array of bound properties
349 if (bound
.equals("")) bound
= "none";
350 StringTokenizer ST
=new StringTokenizer(bound
,";");
351 int nr
= ST
.countTokens();
352 testPropsNames
= new String
[nr
];
353 for (int i
=0; i
<nr
; i
++) testPropsNames
[i
] = ST
.nextToken();
354 testPropsAmount
= nr
;
360 * Waits some time for listener to be called.
362 private void waitAMoment() {
365 } catch (java
.lang
.InterruptedException e
) {
366 log
.println("!!! Exception while waiting !!!") ;
373 protected void after() {
374 disposeEnvironment();