bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / beans / _XTolerantMultiPropertySet.java
blob5d38019a41d6a9dc584b3768e0b6f477c46cd4a4
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 public void before() {
60 PS = UnoRuntime.queryInterface(XPropertySet.class,
61 tEnv.getTestObject());
63 if (PS == null) {
64 throw new StatusException(Status.failed(
65 "Component doesn't provide the needed XPropertySet"));
68 pState = UnoRuntime.queryInterface(
69 XPropertyState.class, tEnv.getTestObject());
71 if (pState == null) {
72 throw new StatusException(Status.failed(
73 "Component doesn't provide the needed XPropertyState"));
76 properties = PS.getPropertySetInfo().getProperties();
77 namesOfProperties = getProperties();
78 valuesOfProperties = getPropertyValues(namesOfProperties);
82 * Calls the method getDirectPropertyValuesTolerant() and compares the resulting
83 * sequence with the one gained as direct values in the before() method.<br>
84 * Has OK state if both sequences equal.
86 public void _getDirectPropertyValuesTolerant() {
87 namesOfDirectProperties = getDirectProperties(properties);
89 GetDirectPropertyTolerantResult[] GDPR = oObj.getDirectPropertyValuesTolerant(
90 namesOfProperties);
92 boolean res = (GDPR.length == namesOfDirectProperties.length);
94 if (!res) {
95 log.println("Found: ");
97 for (int i = 0; i < GDPR.length; i++) {
98 log.println("\t" + GDPR[i].Name);
101 log.println("Expected: ");
103 for (int i = 0; i < namesOfDirectProperties.length; i++) {
104 log.println("\t" + namesOfDirectProperties[i]);
106 } else {
107 for (int i = 0; i < GDPR.length; i++) {
108 boolean localres = GDPR[i].Name.equals(
109 namesOfDirectProperties[i]);
111 if (!localres) {
112 log.println("Found: ");
113 log.println("\t" + GDPR[i].Name);
114 log.println("Expected: ");
115 log.println("\t" + namesOfDirectProperties[i]);
118 res &= localres;
122 tRes.tested("getDirectPropertyValuesTolerant()", res);
125 public void _getPropertyValuesTolerant() {
126 requiredMethod("getDirectPropertyValuesTolerant()");
127 GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
128 namesOfProperties);
130 boolean res = (GPR.length == namesOfProperties.length);
132 if (!res) {
133 log.println("Length of sequences differs");
134 log.println("Found: " + GPR.length);
135 log.println("Expected: " + namesOfProperties.length);
136 } else {
137 for (int i = 0; i < GPR.length; i++) {
138 boolean localres = true;
140 if (!(GPR[i].Value instanceof com.sun.star.uno.Any)) {
141 localres = ValueComparer.equalValue(GPR[i].Value,
142 valuesOfProperties[i]);
146 if (!localres) {
147 log.println("Values differ for : " +
148 namesOfProperties[i]);
149 log.println("\t" + GPR[i].Value);
150 log.println("Expected: ");
151 log.println("\t" + valuesOfProperties[i]);
154 res &= localres;
158 tRes.tested("getPropertyValuesTolerant()", res);
161 public void _setPropertyValuesTolerant() {
162 requiredMethod("getPropertyValuesTolerant()");
164 SetPropertyTolerantFailed[] SPTF = null;
166 try {
167 SPTF = oObj.setPropertyValuesTolerant(namesOfProperties,
168 getNewValues(
169 valuesOfProperties));
170 } catch (com.sun.star.lang.IllegalArgumentException e) {
171 e.printStackTrace(log);
174 //read only properties will throw a PropertyVetoExeption if they are set
175 int failures = 0;
177 for (int k = 0; k < SPTF.length; k++) {
178 if (SPTF[k].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
179 failures++;
183 int roProps = getCountOfReadOnlyProperties();
185 boolean res = (failures == roProps);
187 if (!res) {
188 log.println("Failures: " + failures);
189 log.println("Count of R/O properties: " + roProps);
191 for (int i = 0; i < SPTF.length; i++) {
192 if (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
193 failures++;
194 log.println("Failed for " + SPTF[i].Name);
195 log.println("\t Result: " + SPTF[i].Result);
198 } else {
199 for (int i = 0; i < SPTF.length; i++) {
200 boolean localres = true;
201 GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
202 namesOfProperties);
204 if ((!(GPR[i].Value instanceof com.sun.star.uno.Any)) &&
205 (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.SUCCESS)) {
206 localres = ValueComparer.equalValue(GPR[i].Value,
207 valuesOfProperties[i]);
210 if (!localres) {
211 log.println("Values differ for : " +
212 namesOfProperties[i]);
213 log.println("\t" + GPR[i].Value);
214 log.println("Expected: ");
215 log.println("\t" + valuesOfProperties[i]);
218 res &= localres;
222 tRes.tested("setPropertyValuesTolerant()", res);
226 * This method returns a sorted list of property names
227 * contained in a given sequence of properties that additionally
228 * have the state DIRECT_VALUE
230 protected String[] getDirectProperties(Property[] props) {
231 ArrayList<String> direct = new ArrayList<String>();
233 for (int i = 0; i < props.length; i++) {
234 String pName = props[i].Name;
236 try {
237 PropertyState state = pState.getPropertyState(pName);
239 if (state.equals(PropertyState.DIRECT_VALUE)) {
240 if (isUsable(pName)) direct.add(pName);
242 } catch (com.sun.star.beans.UnknownPropertyException e) {
243 log.println("Property '" + pName + "'");
247 Collections.sort(direct);
249 Object[] obj = direct.toArray();
250 String[] ret = new String[obj.length];
252 for (int i = 0; i < obj.length; i++) {
253 ret[i] = (String) obj[i];
256 return ret;
259 private boolean isUsable(String name) {
260 boolean isUsable=true;
261 if (name.startsWith("TextWriting")) isUsable = false;
262 if (name.startsWith("MetaFile")) isUsable = false;
263 return isUsable;
267 * This method returns a sorted list of property names
268 * contained in a given sequence of properties
270 protected String[] getProperties() {
271 ArrayList<String> names = new ArrayList<String>();
273 for (int i = 0; i < properties.length; i++) {
274 String pName = properties[i].Name;
275 if (isUsable(pName)) names.add(pName);
278 Collections.sort(names);
280 Object[] obj = names.toArray();
281 String[] ret = new String[obj.length];
283 for (int i = 0; i < obj.length; i++) {
284 ret[i] = (String) obj[i];
287 return ret;
291 * Returns the values of a given array of properties in an Object array
293 protected Object[] getPropertyValues(String[] propertyNames) {
294 Object[] values = new Object[propertyNames.length];
296 for (int i = 0; i < propertyNames.length; i++) {
297 try {
298 values[i] = PS.getPropertyValue(propertyNames[i]);
299 } catch (com.sun.star.beans.UnknownPropertyException e) {
300 e.printStackTrace(log);
301 } catch (com.sun.star.lang.WrappedTargetException e) {
302 e.printStackTrace(log);
306 return values;
309 protected int getCountOfReadOnlyProperties() {
310 int ro = 0;
312 for (int i = 0; i < properties.length; i++) {
313 Property property = properties[i];
314 boolean isWritable = ((property.Attributes & PropertyAttribute.READONLY) == 0);
316 if (!isWritable) {
317 ro++;
321 return ro;
324 protected Object[] getNewValues(Object[] oldValues) {
325 Object[] newValues = new Object[oldValues.length];
327 for (int i = 0; i < oldValues.length; i++) {
328 if (oldValues[i] instanceof com.sun.star.uno.Any) {
329 newValues[i] = oldValues[i];
330 } else {
331 newValues[i] = ValueChanger.changePValue(oldValues[i]);
335 return newValues;