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 .
21 import java
.util
.HashSet
;
23 import java
.util
.StringTokenizer
;
25 import lib
.MultiMethodTest
;
27 import util
.ValueChanger
;
29 import com
.sun
.star
.beans
.Property
;
30 import com
.sun
.star
.beans
.PropertyAttribute
;
31 import com
.sun
.star
.beans
.PropertyChangeEvent
;
32 import com
.sun
.star
.beans
.XMultiPropertySet
;
33 import com
.sun
.star
.beans
.XPropertiesChangeListener
;
34 import com
.sun
.star
.beans
.XPropertySetInfo
;
35 import com
.sun
.star
.lang
.EventObject
;
39 * Testing <code>com.sun.star.beans.XMultiPropertySet</code>
42 * <li><code> getPropertySetInfo()</code></li>
43 * <li><code> setPropertyValues()</code></li>
44 * <li><code> getPropertyValues()</code></li>
45 * <li><code> addPropertiesChangeListener()</code></li>
46 * <li><code> removePropertiesChangeListener()</code></li>
47 * <li><code> firePropertiesChangeEvent()</code></li>
50 * Required relations :
52 * <li> <code>'XMultiPropertySet.ExcludeProps'</code>
53 * <b>(optional) </b> : java.util.Set.
54 * Has property names which must be skipped from testing in
55 * some reasons (for example property accepts restricted set
60 * Test is <b> NOT </b> multithread compilant. <p>
61 * After test completion object environment has to be recreated.
62 * @see com.sun.star.beans.XMultiPropertySet
64 public class _XMultiPropertySet
extends MultiMethodTest
{
66 public XMultiPropertySet oObj
= null;
68 private boolean propertiesChanged
= false;
69 private XPropertySetInfo propertySetInfo
= null;
70 private String
[] testPropsNames
= null;
71 private int testPropsAmount
= 0;
72 private Object
[] values
= null;
74 private Set
<String
> exclProps
= null;
77 * Initializes some fields.
79 public void before() {
80 exclProps
= (Set
<String
>) tEnv
.getObjRelation("XMultiPropertySet.ExcludeProps");
81 if (exclProps
== null) exclProps
= new HashSet
<String
>(0);
85 * Listener implementation which sets a flag when
86 * listener was called.
88 public class MyChangeListener
implements XPropertiesChangeListener
{
89 public void propertiesChange(PropertyChangeEvent
[] e
) {
90 //_log.println("Listener was called");
91 propertiesChanged
= true;
93 public void disposing (EventObject obj
) {}
96 private XPropertiesChangeListener PClistener
=
97 new MyChangeListener();
100 * Test calls the method and checks return value.
101 * <code>PropertySetInfo</code> object is stored<p>
102 * Has <b> OK </b> status if the method returns not null value
103 * and no exceptions were thrown. <p>
105 public void _getPropertySetInfo() {
106 boolean bResult
= true;
107 propertySetInfo
= oObj
.getPropertySetInfo();
109 if (propertySetInfo
== null) {
110 log
.println("getPropertySetInfo() method returned null");
114 tRes
.tested("getPropertySetInfo()", bResult
) ;
119 * Test collects all property names and retrieves their values,
120 * then checks the value returned. Finally it also collects
121 * bound properties for other methods tests.<p>
122 * Has <b> OK </b> status if the method returns non null value
123 * and no exceptions were thrown. <p>
124 * The following method tests are to be completed successfully before :
126 * <li> <code> getPropertySetInfo() </code> : to have a list
127 * of properties.</li>
130 public void _getPropertyValues() {
131 requiredMethod("getPropertySetInfo()");
132 boolean bResult
= true;
134 Property
[] properties
= propertySetInfo
.getProperties();
135 String
[] allnames
= new String
[properties
.length
];
136 for (int i
= 0; i
< properties
.length
; i
++) {
137 allnames
[i
] = properties
[i
].Name
;
140 values
= oObj
.getPropertyValues(allnames
);
142 bResult
&= values
!=null;
143 tRes
.tested("getPropertyValues()", bResult
) ;
145 getPropsToTest(properties
);
149 * Test adds listener for all bound properties then each property
150 * is changed and listener call . <p>
151 * Has <b> OK </b> status if on each property change the listener was
152 * called and no exceptions were thrown. <p>
153 * The following method tests are to be completed successfully before :
155 * <li> <code> getPropertyValues() </code> : to collect bound
159 public void _addPropertiesChangeListener() {
161 requiredMethod("getPropertyValues()");
164 oObj
.addPropertiesChangeListener(testPropsNames
, PClistener
);
166 if ((testPropsAmount
==1) && (testPropsNames
[0].equals("none"))) {
171 // Change one of the property to be sure, that this event was cauched.
172 //Random rnd = new Random();
173 //int idx = rnd.nextInt(testPropsAmount);
174 for (int i
=0; i
<testPropsAmount
;i
++) {
175 log
.print("Trying to change property " + testPropsNames
[i
]);
177 Object
[] gValues
= oObj
.getPropertyValues(testPropsNames
);
178 Object newValue
= ValueChanger
.changePValue(gValues
[i
]);
179 gValues
[i
] = newValue
;
180 propertiesChanged
= false;
181 oObj
.setPropertyValues(testPropsNames
, gValues
);
183 log
.println(" ... done");
184 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
185 log
.println("Exception occurred while trying to change "+
186 "property '"+testPropsNames
[i
] + "' :" + e
);
187 e
.printStackTrace(log
);
188 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
189 log
.println("Exception occurred while trying to change "+
190 "property '"+testPropsNames
[i
] + "' :" + e
);
191 e
.printStackTrace(log
);
192 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
193 log
.println("Exception occurred while trying to change "+
194 "property '"+testPropsNames
[i
] + "' :" + e
);
195 e
.printStackTrace(log
);
196 } // end of try-catch
198 if (testPropsAmount
== 0) {
199 log
.println("all properties are read only");
200 tRes
.tested("addPropertiesChangeListener()", Status
.skipped(true));
202 tRes
.tested("addPropertiesChangeListener()", propertiesChanged
);
207 * Calls method and check if listener was called. <p>
208 * Has <b> OK </b> status if the listener was
209 * called and no exceptions were thrown. <p>
210 * The following method tests are to be completed successfully before :
212 * <li> <code> addPropertiesChangeListener() </code> : listener to
216 public void _firePropertiesChangeEvent() {
217 requiredMethod("addPropertiesChangeListener()");
218 propertiesChanged
= false ;
220 oObj
.firePropertiesChangeEvent(testPropsNames
, PClistener
);
223 tRes
.tested("firePropertiesChangeEvent()", propertiesChanged
);
228 * Removes listener added before. <p>
229 * Has <b> OK </b> status no exceptions were thrown. <p>
230 * The following method tests are to be completed successfully before :
232 * <li> <code> addPropertiesChangeListener() </code> : listener to
236 public void _removePropertiesChangeListener() {
237 requiredMethod("firePropertiesChangeEvent()");
238 boolean bResult
= true;
240 oObj
.removePropertiesChangeListener(PClistener
);
242 tRes
.tested("removePropertiesChangeListener()", bResult
);
247 * Changes all properties, then set them to new values, get them
248 * and checks if their values were changed properly. <p>
249 * Has <b> OK </b> status if all properties properly changed
250 * and no exceptions were thrown. <p>
251 * The following method tests are to be completed successfully before :
253 * <li> <code> getPropertyValues() </code> : to collect bound
257 public void _setPropertyValues() {
258 requiredMethod("getPropertyValues()");
259 boolean bResult
= true;
261 if ((testPropsNames
.length
==1)&&(testPropsNames
[0].equals("none"))) {
262 log
.println("all properties are readOnly");
263 tRes
.tested("setPropertyValues()",Status
.skipped(true));
267 log
.println("Changing all properties");
268 Object
[] gValues
= oObj
.getPropertyValues(testPropsNames
);
269 for (int i
=0; i
<testPropsAmount
;i
++) {
270 Object oldValue
= gValues
[i
];
271 Object newValue
= ValueChanger
.changePValue(oldValue
);
272 gValues
[i
] = newValue
;
276 oObj
.setPropertyValues(testPropsNames
, gValues
);
277 Object
[] newValues
= oObj
.getPropertyValues(testPropsNames
);
278 for (int i
=0; i
<testPropsAmount
;i
++) {
279 if (newValues
[i
].equals(gValues
[i
])) {
283 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
284 log
.println("Exception occurred while setting properties");
285 e
.printStackTrace(log
);
287 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
288 log
.println("Exception occurred while setting properties");
289 e
.printStackTrace(log
);
291 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
292 log
.println("Exception occurred while setting properties");
293 e
.printStackTrace(log
);
295 } // end of try-catch
297 tRes
.tested("setPropertyValues()", bResult
);
300 //Get the properties being tested
301 private void getPropsToTest(Property
[] properties
) {
305 for (int i
= 0; i
< properties
.length
; i
++) {
307 Property property
= properties
[i
];
308 String name
= property
.Name
;
309 boolean isWritable
= ((property
.Attributes
&
310 PropertyAttribute
.READONLY
) == 0);
311 boolean isNotNull
= ((property
.Attributes
&
312 PropertyAttribute
.MAYBEVOID
) == 0);
313 boolean isBound
= ((property
.Attributes
&
314 PropertyAttribute
.BOUND
) != 0);
315 boolean isExcluded
= exclProps
.contains(name
);
317 //exclude UserDefined, because we can't change XNameContainer
318 if (name
.indexOf("UserDefined")>0 || name
.indexOf("Device")>0) {
322 values
= oObj
.getPropertyValues(new String
[]{property
.Name
});
324 boolean isVoid
= util
.utils
.isVoid(values
[0]);
326 if ( isWritable
&& isNotNull
&& isBound
&& !isExcluded
&& !isVoid
) {
332 //get a array of bound properties
333 if (bound
.equals("")) bound
= "none";
334 StringTokenizer ST
=new StringTokenizer(bound
,";");
335 int nr
= ST
.countTokens();
336 testPropsNames
= new String
[nr
];
337 for (int i
=0; i
<nr
; i
++) testPropsNames
[i
] = ST
.nextToken();
338 testPropsAmount
= nr
;
344 * Waits some time for listener to be called.
346 private void waitAMoment() {
349 } catch (java
.lang
.InterruptedException e
) {
350 log
.println("!!! Exception while waiting !!!") ;
357 protected void after() {
358 disposeEnvironment();