Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / beans / _XMultiPropertySet.java
blob052e588436797adc88e1d88048da5f2905706dc5
1 /*
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 ifc.beans;
21 import java.util.HashSet;
22 import java.util.ArrayList;
23 import java.util.Set;
24 import java.util.StringTokenizer;
25 import java.util.Arrays;
27 import lib.MultiMethodTest;
28 import lib.Status;
29 import util.ValueChanger;
31 import com.sun.star.beans.Property;
32 import com.sun.star.beans.PropertyAttribute;
33 import com.sun.star.beans.PropertyChangeEvent;
34 import com.sun.star.beans.XMultiPropertySet;
35 import com.sun.star.beans.XPropertiesChangeListener;
36 import com.sun.star.beans.XPropertySetInfo;
37 import com.sun.star.lang.EventObject;
40 /**
41 * Testing <code>com.sun.star.beans.XMultiPropertySet</code>
42 * interface methods :
43 * <ul>
44 * <li><code> getPropertySetInfo()</code></li>
45 * <li><code> setPropertyValues()</code></li>
46 * <li><code> getPropertyValues()</code></li>
47 * <li><code> addPropertiesChangeListener()</code></li>
48 * <li><code> removePropertiesChangeListener()</code></li>
49 * <li><code> firePropertiesChangeEvent()</code></li>
50 * </ul> <p>
52 * Required relations :
53 * <ul>
54 * <li> <code>'XMultiPropertySet.ExcludeProps'</code>
55 * <b>(optional) </b> : java.util.Set.
56 * Has property names which must be skipped from testing in
57 * some reasons (for example property accepts restricted set
58 * of values).
59 * </li>
60 * <ul> <p>
62 * Test is <b> NOT </b> multithread compliant. <p>
63 * After test completion object environment has to be recreated.
64 * @see com.sun.star.beans.XMultiPropertySet
66 public class _XMultiPropertySet extends MultiMethodTest {
68 public XMultiPropertySet oObj = null;
70 private boolean propertiesChanged = false;
71 private XPropertySetInfo propertySetInfo = null;
72 private String [] testPropsNames = null;
73 private int testPropsAmount = 0;
74 private Object[] values = null;
76 private Set<String> exclProps = null;
78 /**
79 * Initializes some fields.
81 @SuppressWarnings("unchecked")
82 @Override
83 public void before() {
84 exclProps = (Set<String>) tEnv.getObjRelation("XMultiPropertySet.ExcludeProps");
85 if (exclProps == null) exclProps = new HashSet<String>(0);
88 /**
89 * Listener implementation which sets a flag when
90 * listener was called.
92 public class MyChangeListener implements XPropertiesChangeListener {
93 public void propertiesChange(PropertyChangeEvent[] e) {
94 propertiesChanged = true;
96 public void disposing (EventObject obj) {}
99 private final XPropertiesChangeListener PClistener =
100 new MyChangeListener();
103 * Test calls the method and checks return value.
104 * <code>PropertySetInfo</code> object is stored<p>
105 * Has <b> OK </b> status if the method returns not null value
106 * and no exceptions were thrown. <p>
108 public void _getPropertySetInfo() {
109 boolean bResult = true;
110 propertySetInfo = oObj.getPropertySetInfo();
112 if (propertySetInfo == null) {
113 log.println("getPropertySetInfo() method returned null");
114 bResult = false;
117 tRes.tested("getPropertySetInfo()", bResult) ;
122 * Test collects all property names and retrieves their values,
123 * then checks the value returned. Finally it also collects
124 * bound properties for other methods tests.<p>
125 * Has <b> OK </b> status if the method returns non null value
126 * and no exceptions were thrown. <p>
127 * The following method tests are to be completed successfully before :
128 * <ul>
129 * <li> <code> getPropertySetInfo() </code> : to have a list
130 * of properties.</li>
131 * </ul>
133 public void _getPropertyValues() {
134 requiredMethod("getPropertySetInfo()");
135 boolean bResult = true;
137 Property[] properties = propertySetInfo.getProperties();
139 ArrayList<String> allFilteredNames = new ArrayList<String>();
140 ArrayList<Property> allFilteredProperties = new ArrayList<Property>();
142 String[] skipNames = (String[]) tEnv.getObjRelation("SkipProperties");
144 for (int i = 0; i < properties.length; i++) {
145 if (skipNames == null || !Arrays.asList(skipNames).contains(properties[i].Name))
147 allFilteredNames.add(properties[i].Name);
148 allFilteredProperties.add(properties[i]);
152 String[] arrayAllFilteredNames = allFilteredNames.toArray(new String[allFilteredNames.size()]);
154 values = oObj.getPropertyValues(arrayAllFilteredNames);
156 bResult &= values!=null;
157 tRes.tested("getPropertyValues()", bResult) ;
159 Property[] arrayFilteredProperties = allFilteredProperties.toArray(new Property[allFilteredProperties.size()]);
161 getPropsToTest(arrayFilteredProperties);
165 * Test adds listener for all bound properties then each property
166 * is changed and listener call . <p>
167 * Has <b> OK </b> status if on each property change the listener was
168 * called and no exceptions were thrown. <p>
169 * The following method tests are to be completed successfully before :
170 * <ul>
171 * <li> <code> getPropertyValues() </code> : to collect bound
172 * properties.</li>
173 * </ul>
175 public void _addPropertiesChangeListener() {
177 requiredMethod("getPropertyValues()");
179 // Creating listener
180 oObj.addPropertiesChangeListener(testPropsNames, PClistener);
182 if ((testPropsAmount==1) && (testPropsNames[0].equals("none"))) {
183 testPropsAmount = 0;
186 if (testPropsAmount == 0) {
187 log.println("all properties are read only");
188 tRes.tested("addPropertiesChangeListener()", Status.skipped(true));
189 return;
192 // Change one of the property to be sure that this event was caught.
193 for (int i=0; i<testPropsAmount;i++) {
194 log.print("Trying to change property " + testPropsNames[i]);
195 try {
196 Object[] gValues = oObj.getPropertyValues(testPropsNames);
197 Object newValue = ValueChanger.changePValue(gValues[i]);
198 gValues[i] = newValue;
199 propertiesChanged = false;
200 oObj.setPropertyValues(testPropsNames, gValues);
201 log.println(" ... done");
202 } catch (com.sun.star.beans.PropertyVetoException e) {
203 log.println("Exception occurred while trying to change "+
204 "property '"+testPropsNames[i] + "' :" + e);
205 e.printStackTrace(log);
206 } catch (com.sun.star.lang.IllegalArgumentException e) {
207 log.println("Exception occurred while trying to change "+
208 "property '"+testPropsNames[i] + "' :" + e);
209 e.printStackTrace(log);
210 } catch (com.sun.star.lang.WrappedTargetException e) {
211 log.println("Exception occurred while trying to change "+
212 "property '"+testPropsNames[i] + "' :" + e);
213 e.printStackTrace(log);
214 } // end of try-catch
216 waitForEventIdle();
217 tRes.tested("addPropertiesChangeListener()", propertiesChanged);
221 * Calls method and check if listener was called. <p>
222 * Has <b> OK </b> status if the listener was
223 * called and no exceptions were thrown. <p>
224 * The following method tests are to be completed successfully before :
225 * <ul>
226 * <li> <code> addPropertiesChangeListener() </code> : listener to
227 * be added.</li>
228 * </ul>
230 public void _firePropertiesChangeEvent() {
231 requiredMethod("addPropertiesChangeListener()");
232 propertiesChanged = false ;
234 oObj.firePropertiesChangeEvent(testPropsNames, PClistener);
235 waitForEventIdle();
237 tRes.tested("firePropertiesChangeEvent()", propertiesChanged);
242 * Removes listener added before. <p>
243 * Has <b> OK </b> status no exceptions were thrown. <p>
244 * The following method tests are to be completed successfully before :
245 * <ul>
246 * <li> <code> addPropertiesChangeListener() </code> : listener to
247 * be added.</li>
248 * </ul>
250 public void _removePropertiesChangeListener() {
251 requiredMethod("firePropertiesChangeEvent()");
252 boolean bResult = true;
254 oObj.removePropertiesChangeListener(PClistener);
256 tRes.tested("removePropertiesChangeListener()", bResult);
261 * Changes all properties, then set them to new values, get them
262 * and checks if their values were changed properly. <p>
263 * Has <b> OK </b> status if all properties properly changed
264 * and no exceptions were thrown. <p>
265 * The following method tests are to be completed successfully before :
266 * <ul>
267 * <li> <code> getPropertyValues() </code> : to collect bound
268 * properties.</li>
269 * </ul>
271 public void _setPropertyValues() {
272 requiredMethod("getPropertyValues()");
273 boolean bResult = true;
275 if ((testPropsNames.length==1)&&(testPropsNames[0].equals("none"))) {
276 log.println("all properties are readOnly");
277 tRes.tested("setPropertyValues()",Status.skipped(true));
278 return;
281 log.println("Changing all properties");
282 Object[] gValues = oObj.getPropertyValues(testPropsNames);
283 for (int i=0; i<testPropsAmount;i++) {
284 Object oldValue = gValues[i];
285 Object newValue = ValueChanger.changePValue(oldValue);
286 gValues[i] = newValue;
289 try {
290 oObj.setPropertyValues(testPropsNames, gValues);
291 Object[] newValues = oObj.getPropertyValues(testPropsNames);
292 for (int i=0; i<testPropsAmount;i++) {
293 if (newValues[i].equals(gValues[i])) {
294 bResult = true;
297 } catch (com.sun.star.beans.PropertyVetoException e) {
298 log.println("Exception occurred while setting properties");
299 e.printStackTrace(log);
300 bResult = false;
301 } catch (com.sun.star.lang.IllegalArgumentException e) {
302 log.println("Exception occurred while setting properties");
303 e.printStackTrace(log);
304 bResult = false;
305 } catch (com.sun.star.lang.WrappedTargetException e) {
306 log.println("Exception occurred while setting properties");
307 e.printStackTrace(log);
308 bResult = false;
309 } // end of try-catch
311 tRes.tested("setPropertyValues()", bResult);
314 //Get the properties being tested
315 private void getPropsToTest(Property[] properties) {
317 StringBuilder sb = new StringBuilder();
319 for (int i = 0; i < properties.length; i++) {
321 Property property = properties[i];
322 String name = property.Name;
323 boolean isWritable = ((property.Attributes &
324 PropertyAttribute.READONLY) == 0);
325 boolean isNotNull = ((property.Attributes &
326 PropertyAttribute.MAYBEVOID) == 0);
327 boolean isBound = ((property.Attributes &
328 PropertyAttribute.BOUND) != 0);
329 boolean isExcluded = exclProps.contains(name);
331 //exclude UserDefined, because we can't change XNameContainer
332 if (name.indexOf("UserDefined")>0 || name.indexOf("Device")>0) {
333 isWritable=false;
336 values = oObj.getPropertyValues(new String[]{property.Name});
338 boolean isVoid = util.utils.isVoid(values[0]);
340 if ( isWritable && isNotNull && isBound && !isExcluded && !isVoid) {
341 sb.append(name + ";");
344 } // endfor
346 String bound = sb.toString();
348 //get an 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;
358 * Does nothing.
360 @Override
361 protected void after() {
362 disposeEnvironment();