Avoid potential negative array index access to cached text.
[LibreOffice.git] / qadevOOo / tests / java / ifc / beans / _XTolerantMultiPropertySet.java
blob6860c86902345f06d6246244dde0e2b24d437278
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 .
18 package ifc.beans;
20 import com.sun.star.beans.GetDirectPropertyTolerantResult;
21 import com.sun.star.beans.GetPropertyTolerantResult;
22 import com.sun.star.beans.Property;
23 import com.sun.star.beans.PropertyAttribute;
24 import com.sun.star.beans.PropertyState;
25 import com.sun.star.beans.SetPropertyTolerantFailed;
26 import com.sun.star.beans.XPropertySet;
27 import com.sun.star.beans.XPropertyState;
28 import com.sun.star.beans.XTolerantMultiPropertySet;
29 import com.sun.star.uno.UnoRuntime;
31 import java.util.ArrayList;
32 import java.util.Collections;
34 import lib.MultiMethodTest;
35 import lib.Status;
36 import lib.StatusException;
38 import util.ValueChanger;
39 import util.ValueComparer;
42 public class _XTolerantMultiPropertySet extends MultiMethodTest {
43 public XTolerantMultiPropertySet oObj;
44 protected String[] namesOfDirectProperties = null;
45 protected String[] namesOfProperties = null;
46 protected Object[] valuesOfProperties = null;
47 protected Property[] properties = null;
48 protected XPropertyState pState = null;
49 protected XPropertySet PS = null;
53 * Queries XPropertySet from the given Component and gets XPropertySetInfo
54 * from it to get the PropertyNames available and their Values<br>
55 * Then queries XPropertyState from the given Component
56 * to get the direct properties<br>
57 * Throws a lib StatusException if the Component doesn't support XPropertySet or XPropertyState
59 @Override
60 public void before() {
61 PS = UnoRuntime.queryInterface(XPropertySet.class,
62 tEnv.getTestObject());
64 if (PS == null) {
65 throw new StatusException(Status.failed(
66 "Component doesn't provide the needed XPropertySet"));
69 pState = UnoRuntime.queryInterface(
70 XPropertyState.class, tEnv.getTestObject());
72 if (pState == null) {
73 throw new StatusException(Status.failed(
74 "Component doesn't provide the needed XPropertyState"));
77 properties = PS.getPropertySetInfo().getProperties();
78 namesOfProperties = getProperties();
79 valuesOfProperties = getPropertyValues(namesOfProperties);
83 * Calls the method getDirectPropertyValuesTolerant() and compares the resulting
84 * sequence with the one gained as direct values in the before() method.<br>
85 * Has OK state if both sequences equal.
87 public void _getDirectPropertyValuesTolerant() {
88 namesOfDirectProperties = getDirectProperties(properties);
90 GetDirectPropertyTolerantResult[] GDPR = oObj.getDirectPropertyValuesTolerant(
91 namesOfProperties);
93 boolean res = (GDPR.length == namesOfDirectProperties.length);
95 if (!res) {
96 log.println("Found: ");
98 for (int i = 0; i < GDPR.length; i++) {
99 log.println("\t" + GDPR[i].Name);
102 log.println("Expected: ");
104 for (int i = 0; i < namesOfDirectProperties.length; i++) {
105 log.println("\t" + namesOfDirectProperties[i]);
107 } else {
108 for (int i = 0; i < GDPR.length; i++) {
109 boolean localres = GDPR[i].Name.equals(
110 namesOfDirectProperties[i]);
112 if (!localres) {
113 log.println("Found: ");
114 log.println("\t" + GDPR[i].Name);
115 log.println("Expected: ");
116 log.println("\t" + namesOfDirectProperties[i]);
119 res &= localres;
123 tRes.tested("getDirectPropertyValuesTolerant()", res);
126 public void _getPropertyValuesTolerant() {
127 requiredMethod("getDirectPropertyValuesTolerant()");
128 GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
129 namesOfProperties);
131 boolean res = (GPR.length == namesOfProperties.length);
133 if (!res) {
134 log.println("Length of sequences differs");
135 log.println("Found: " + GPR.length);
136 log.println("Expected: " + namesOfProperties.length);
137 } else {
138 for (int i = 0; i < GPR.length; i++) {
139 boolean localres = true;
141 if (!(GPR[i].Value instanceof com.sun.star.uno.Any)) {
142 localres = ValueComparer.equalValue(GPR[i].Value,
143 valuesOfProperties[i]);
147 if (!localres) {
148 log.println("Values differ for : " +
149 namesOfProperties[i]);
150 log.println("\t" + GPR[i].Value);
151 log.println("Expected: ");
152 log.println("\t" + valuesOfProperties[i]);
155 res &= localres;
159 tRes.tested("getPropertyValuesTolerant()", res);
162 public void _setPropertyValuesTolerant() {
163 requiredMethod("getPropertyValuesTolerant()");
165 SetPropertyTolerantFailed[] SPTF = oObj.setPropertyValuesTolerant(namesOfProperties,
166 getNewValues(valuesOfProperties));
168 //read only properties will throw a PropertyVetoException if they are set
169 int failures = 0;
171 for (int k = 0; k < SPTF.length; k++) {
172 if (SPTF[k].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
173 failures++;
177 int roProps = getCountOfReadOnlyProperties();
179 boolean res = (failures == roProps);
181 if (!res) {
182 log.println("Failures: " + failures);
183 log.println("Count of R/O properties: " + roProps);
185 for (int i = 0; i < SPTF.length; i++) {
186 if (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
187 failures++;
188 log.println("Failed for " + SPTF[i].Name);
189 log.println("\t Result: " + SPTF[i].Result);
192 } else {
193 for (int i = 0; i < SPTF.length; i++) {
194 boolean localres = true;
195 GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
196 namesOfProperties);
198 if ((!(GPR[i].Value instanceof com.sun.star.uno.Any)) &&
199 (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.SUCCESS)) {
200 localres = ValueComparer.equalValue(GPR[i].Value,
201 valuesOfProperties[i]);
204 if (!localres) {
205 log.println("Values differ for : " +
206 namesOfProperties[i]);
207 log.println("\t" + GPR[i].Value);
208 log.println("Expected: ");
209 log.println("\t" + valuesOfProperties[i]);
212 res &= localres;
216 tRes.tested("setPropertyValuesTolerant()", res);
220 * This method returns a sorted list of property names
221 * contained in a given sequence of properties that additionally
222 * have the state DIRECT_VALUE
224 protected String[] getDirectProperties(Property[] props) {
225 ArrayList<String> direct = new ArrayList<String>();
227 for (int i = 0; i < props.length; i++) {
228 String pName = props[i].Name;
230 try {
231 PropertyState state = pState.getPropertyState(pName);
233 if (state.equals(PropertyState.DIRECT_VALUE) && isUsable(pName))
234 direct.add(pName);
235 } catch (com.sun.star.beans.UnknownPropertyException e) {
236 log.println("Property '" + pName + "'");
240 Collections.sort(direct);
242 Object[] obj = direct.toArray();
243 String[] ret = new String[obj.length];
245 for (int i = 0; i < obj.length; i++) {
246 ret[i] = (String) obj[i];
249 return ret;
252 private boolean isUsable(String name) {
253 boolean isUsable=true;
254 if (name.startsWith("TextWriting")) isUsable = false;
255 if (name.startsWith("MetaFile")) isUsable = false;
256 return isUsable;
260 * This method returns a sorted list of property names
261 * contained in a given sequence of properties
263 protected String[] getProperties() {
264 ArrayList<String> names = new ArrayList<String>();
266 for (int i = 0; i < properties.length; i++) {
267 String pName = properties[i].Name;
268 if (isUsable(pName)) names.add(pName);
271 Collections.sort(names);
273 Object[] obj = names.toArray();
274 String[] ret = new String[obj.length];
276 for (int i = 0; i < obj.length; i++) {
277 ret[i] = (String) obj[i];
280 return ret;
284 * Returns the values of a given array of properties in an Object array
286 protected Object[] getPropertyValues(String[] propertyNames) {
287 Object[] values = new Object[propertyNames.length];
289 for (int i = 0; i < propertyNames.length; i++) {
290 try {
291 values[i] = PS.getPropertyValue(propertyNames[i]);
292 } catch (com.sun.star.beans.UnknownPropertyException e) {
293 e.printStackTrace(log);
294 } catch (com.sun.star.lang.WrappedTargetException e) {
295 e.printStackTrace(log);
299 return values;
302 protected int getCountOfReadOnlyProperties() {
303 int ro = 0;
305 for (int i = 0; i < properties.length; i++) {
306 Property property = properties[i];
307 boolean isWritable = ((property.Attributes & PropertyAttribute.READONLY) == 0);
309 if (!isWritable) {
310 ro++;
314 return ro;
317 protected Object[] getNewValues(Object[] oldValues) {
318 Object[] newValues = new Object[oldValues.length];
320 for (int i = 0; i < oldValues.length; i++) {
321 if (oldValues[i] instanceof com.sun.star.uno.Any) {
322 newValues[i] = oldValues[i];
323 } else {
324 newValues[i] = ValueChanger.changePValue(oldValues[i]);
328 return newValues;