merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / java / ifc / beans / _XMultiPropertySet.java
blobd0a82047ed4eea434377ee62581ca707874fb14b
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: _XMultiPropertySet.java,v $
10 * $Revision: 1.6 $
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.io.PrintWriter;
34 import java.util.HashSet;
35 import java.util.Set;
36 import java.util.StringTokenizer;
38 import lib.MultiMethodTest;
39 import lib.Status;
40 import util.ValueChanger;
42 import com.sun.star.beans.Property;
43 import com.sun.star.beans.PropertyAttribute;
44 import com.sun.star.beans.PropertyChangeEvent;
45 import com.sun.star.beans.XMultiPropertySet;
46 import com.sun.star.beans.XPropertiesChangeListener;
47 import com.sun.star.beans.XPropertySetInfo;
48 import com.sun.star.lang.EventObject;
51 /**
52 * Testing <code>com.sun.star.beans.XMultiPropertySet</code>
53 * interface methods :
54 * <ul>
55 * <li><code> getPropertySetInfo()</code></li>
56 * <li><code> setPropertyValues()</code></li>
57 * <li><code> getPropertyValues()</code></li>
58 * <li><code> addPropertiesChangeListener()</code></li>
59 * <li><code> removePropertiesChangeListener()</code></li>
60 * <li><code> firePropertiesChangeEvent()</code></li>
61 * </ul> <p>
63 * Required relations :
64 * <ul>
65 * <li> <code>'XMultiPropertySet.ExcludeProps'</code>
66 * <b>(optional) </b> : java.util.Set.
67 * Has property names which must be skipped from testing in
68 * some reasons (for example property accepts restricted set
69 * of values).
70 * </li>
71 * <ul> <p>
73 * Test is <b> NOT </b> multithread compilant. <p>
74 * After test completion object environment has to be recreated.
75 * @see com.sun.star.beans.XMultiPropertySet
77 public class _XMultiPropertySet extends MultiMethodTest {
79 public XMultiPropertySet oObj = null;
81 private boolean propertiesChanged = false;
82 private XPropertySetInfo propertySetInfo = null;
83 private String [] testPropsNames = null;
84 private int testPropsAmount = 0;
85 private PrintWriter _log = null;
87 private Object[] values = null;
89 private Set exclProps = null;
91 /**
92 * Initializes some fields.
94 public void before() {
95 _log = log;
97 exclProps = (Set) tEnv.getObjRelation("XMultiPropertySet.ExcludeProps");
98 if (exclProps == null) exclProps = new HashSet(0);
102 * Listener implementation which sets a flag when
103 * listener was called.
105 public class MyChangeListener implements XPropertiesChangeListener {
106 public void propertiesChange(PropertyChangeEvent[] e) {
107 //_log.println("Listener was called");
108 propertiesChanged = true;
110 public void disposing (EventObject obj) {}
113 private XPropertiesChangeListener PClistener =
114 new MyChangeListener();
117 * Test calls the method and checks return value.
118 * <code>PropertySetInfo</code> object is stored<p>
119 * Has <b> OK </b> status if the method returns not null value
120 * and no exceptions were thrown. <p>
122 public void _getPropertySetInfo() {
123 boolean bResult = true;
124 propertySetInfo = oObj.getPropertySetInfo();
126 if (propertySetInfo == null) {
127 log.println("getPropertySetInfo() method returned null");
128 bResult = false;
131 tRes.tested("getPropertySetInfo()", bResult) ;
136 * Test collects all property names and retrieves their values,
137 * then checks the value returned. Finally it also collects
138 * bound properties for other methods tests.<p>
139 * Has <b> OK </b> status if the method returns non null value
140 * and no exceptions were thrown. <p>
141 * The following method tests are to be completed successfully before :
142 * <ul>
143 * <li> <code> getPropertySetInfo() </code> : to have a list
144 * of properties.</li>
145 * </ul>
147 public void _getPropertyValues() {
148 requiredMethod("getPropertySetInfo()");
149 boolean bResult = true;
151 Property[] properties = propertySetInfo.getProperties();
152 String[] allnames = new String[properties.length];
153 for (int i = 0; i < properties.length; i++) {
154 allnames[i] = properties[i].Name;
157 values = oObj.getPropertyValues(allnames);
159 bResult &= values!=null;
160 tRes.tested("getPropertyValues()", bResult) ;
162 getPropsToTest(properties);
166 * Test adds listener for all bound properties then each property
167 * is changed and listener call . <p>
168 * Has <b> OK </b> status if on each property change the listener was
169 * called and no exceptions were thrown. <p>
170 * The following method tests are to be completed successfully before :
171 * <ul>
172 * <li> <code> getPropertyValues() </code> : to collect bound
173 * properties.</li>
174 * </ul>
176 public void _addPropertiesChangeListener() {
178 requiredMethod("getPropertyValues()");
180 boolean result = true ;
181 // Creating listener
182 oObj.addPropertiesChangeListener(testPropsNames, PClistener);
184 if ((testPropsAmount==1) && (testPropsNames[0].equals("none"))) {
185 testPropsAmount = 0;
189 // Change one of the property to be sure, that this event was cauched.
190 //Random rnd = new Random();
191 //int idx = rnd.nextInt(testPropsAmount);
192 for (int i=0; i<testPropsAmount;i++) {
193 log.print("Trying to change property " + testPropsNames[i]);
194 try {
195 Object[] gValues = oObj.getPropertyValues(testPropsNames);
196 Object newValue = ValueChanger.changePValue(gValues[i]);
197 gValues[i] = newValue;
198 propertiesChanged = false;
199 oObj.setPropertyValues(testPropsNames, gValues);
200 waitAMoment() ;
201 result &= propertiesChanged ;
202 log.println(" ... done");
203 } catch (com.sun.star.beans.PropertyVetoException e) {
204 log.println("Exception occured while trying to change "+
205 "property '"+testPropsNames[i] + "' :" + e);
206 e.printStackTrace(log);
207 } catch (com.sun.star.lang.IllegalArgumentException e) {
208 log.println("Exception occured while trying to change "+
209 "property '"+testPropsNames[i] + "' :" + e);
210 e.printStackTrace(log);
211 } catch (com.sun.star.lang.WrappedTargetException e) {
212 log.println("Exception occured while trying to change "+
213 "property '"+testPropsNames[i] + "' :" + e);
214 e.printStackTrace(log);
215 } // end of try-catch
217 if (testPropsAmount == 0) {
218 log.println("all properties are read only");
219 tRes.tested("addPropertiesChangeListener()", Status.skipped(true));
220 } else {
221 tRes.tested("addPropertiesChangeListener()", propertiesChanged);
226 * Calls method and check if listener was called. <p>
227 * Has <b> OK </b> status if the listener was
228 * called and no exceptions were thrown. <p>
229 * The following method tests are to be completed successfully before :
230 * <ul>
231 * <li> <code> addPropertiesChangeListener() </code> : listener to
232 * be added.</li>
233 * </ul>
235 public void _firePropertiesChangeEvent() {
236 requiredMethod("addPropertiesChangeListener()");
237 propertiesChanged = false ;
239 oObj.firePropertiesChangeEvent(testPropsNames, PClistener);
240 waitAMoment() ;
242 tRes.tested("firePropertiesChangeEvent()", propertiesChanged);
247 * Removes listener added before. <p>
248 * Has <b> OK </b> status no exceptions were thrown. <p>
249 * The following method tests are to be completed successfully before :
250 * <ul>
251 * <li> <code> addPropertiesChangeListener() </code> : listener to
252 * be added.</li>
253 * </ul>
255 public void _removePropertiesChangeListener() {
256 requiredMethod("firePropertiesChangeEvent()");
257 boolean bResult = true;
259 oObj.removePropertiesChangeListener(PClistener);
261 tRes.tested("removePropertiesChangeListener()", bResult);
266 * Changes all properties, then set them to new values, get them
267 * and checks if their values were changed properly. <p>
268 * Has <b> OK </b> status if all properties properly changed
269 * and no exceptions were thrown. <p>
270 * The following method tests are to be completed successfully before :
271 * <ul>
272 * <li> <code> getPropertyValues() </code> : to collect bound
273 * properties.</li>
274 * </ul>
276 public void _setPropertyValues() {
277 requiredMethod("getPropertyValues()");
278 boolean bResult = true;
280 if ((testPropsNames.length==1)&&(testPropsNames[0].equals("none"))) {
281 log.println("all properties are readOnly");
282 tRes.tested("setPropertyValues()",Status.skipped(true));
283 return;
286 log.println("Changing all properties");
287 Object[] gValues = oObj.getPropertyValues(testPropsNames);
288 for (int i=0; i<testPropsAmount;i++) {
289 Object oldValue = gValues[i];
290 Object newValue = ValueChanger.changePValue(oldValue);
291 gValues[i] = newValue;
294 try {
295 oObj.setPropertyValues(testPropsNames, gValues);
296 Object[] newValues = oObj.getPropertyValues(testPropsNames);
297 for (int i=0; i<testPropsAmount;i++) {
298 if (newValues[i].equals(gValues[i])) {
299 bResult = true;
302 } catch (com.sun.star.beans.PropertyVetoException e) {
303 log.println("Exception occured while setting properties");
304 e.printStackTrace(log);
305 bResult = false;
306 } catch (com.sun.star.lang.IllegalArgumentException e) {
307 log.println("Exception occured while setting properties");
308 e.printStackTrace(log);
309 bResult = false;
310 } catch (com.sun.star.lang.WrappedTargetException e) {
311 log.println("Exception occured while setting properties");
312 e.printStackTrace(log);
313 bResult = false;
314 } // end of try-catch
316 tRes.tested("setPropertyValues()", bResult);
319 //Get the properties being tested
320 private void getPropsToTest(Property[] properties) {
322 String bound = "";
324 for (int i = 0; i < properties.length; i++) {
326 Property property = properties[i];
327 String name = property.Name;
328 boolean isWritable = ((property.Attributes &
329 PropertyAttribute.READONLY) == 0);
330 boolean isNotNull = ((property.Attributes &
331 PropertyAttribute.MAYBEVOID) == 0);
332 boolean isBound = ((property.Attributes &
333 PropertyAttribute.BOUND) != 0);
334 boolean isExcluded = exclProps.contains(name);
336 //exclude UserDefined, because we can't change XNameContainer
337 if (name.indexOf("UserDefined")>0 || name.indexOf("Device")>0) {
338 isWritable=false;
341 values = oObj.getPropertyValues(new String[]{property.Name});
343 boolean isVoid = util.utils.isVoid(values[0]);
345 if ( isWritable && isNotNull && isBound && !isExcluded && !isVoid) {
346 bound+=name+";";
349 } // endfor
351 //get a array of bound properties
352 if (bound.equals("")) bound = "none";
353 StringTokenizer ST=new StringTokenizer(bound,";");
354 int nr = ST.countTokens();
355 testPropsNames = new String[nr];
356 for (int i=0; i<nr; i++) testPropsNames[i] = ST.nextToken();
357 testPropsAmount = nr;
358 return;
363 * Waits some time for listener to be called.
365 private void waitAMoment() {
366 try {
367 Thread.sleep(200) ;
368 } catch (java.lang.InterruptedException e) {
369 log.println("!!! Exception while waiting !!!") ;
374 * Does nothing.
376 protected void after() {
377 disposeEnvironment();