Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / qadevOOo / tests / java / ifc / beans / _XTolerantMultiPropertySet.java
blob0d783ad18e97a6eb90dfe24d14bdc0b2550b245b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package ifc.beans;
29 import com.sun.star.beans.GetDirectPropertyTolerantResult;
30 import com.sun.star.beans.GetPropertyTolerantResult;
31 import com.sun.star.beans.Property;
32 import com.sun.star.beans.PropertyAttribute;
33 import com.sun.star.beans.PropertyState;
34 import com.sun.star.beans.SetPropertyTolerantFailed;
35 import com.sun.star.beans.XPropertySet;
36 import com.sun.star.beans.XPropertyState;
37 import com.sun.star.beans.XTolerantMultiPropertySet;
38 import com.sun.star.uno.UnoRuntime;
40 import java.util.ArrayList;
41 import java.util.Collections;
43 import lib.MultiMethodTest;
44 import lib.Status;
45 import lib.StatusException;
47 import util.ValueChanger;
48 import util.ValueComparer;
51 public class _XTolerantMultiPropertySet extends MultiMethodTest {
52 public XTolerantMultiPropertySet oObj;
53 protected String[] namesOfDirectProperties = null;
54 protected String[] namesOfProperties = null;
55 protected Object[] valuesOfProperties = null;
56 protected Property[] properties = null;
57 protected XPropertyState pState = null;
58 protected XPropertySet PS = null;
62 * Queries XPropertySet from the given Component and gets XPropertySetInfo
63 * from it to get the PropertyNames available and their Values<br>
64 * Then queries XPropertyState from the given Component
65 * to get the direct properties<br>
66 * Throws a lib StatusException if the Component doesn't support XPropertySet or XPropertyState
68 public void before() {
69 PS = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class,
70 tEnv.getTestObject());
72 if (PS == null) {
73 throw new StatusException(Status.failed(
74 "Component doesn't provide the needed XPropertySet"));
77 pState = (XPropertyState) UnoRuntime.queryInterface(
78 XPropertyState.class, tEnv.getTestObject());
80 if (pState == null) {
81 throw new StatusException(Status.failed(
82 "Component doesn't provide the needed XPropertyState"));
85 properties = PS.getPropertySetInfo().getProperties();
86 namesOfProperties = getProperties();
87 valuesOfProperties = getPropertyValues(namesOfProperties);
91 * Calls the method getDirectPropertyValuesTolerant() and compares the resulting
92 * sequence with the one gained as direct values in the before() method.<br>
93 * Has OK state if both sequences equal.
95 public void _getDirectPropertyValuesTolerant() {
96 namesOfDirectProperties = getDirectProperties(properties);
98 GetDirectPropertyTolerantResult[] GDPR = oObj.getDirectPropertyValuesTolerant(
99 namesOfProperties);
101 boolean res = (GDPR.length == namesOfDirectProperties.length);
103 if (!res) {
104 log.println("Found: ");
106 for (int i = 0; i < GDPR.length; i++) {
107 log.println("\t" + GDPR[i].Name);
110 log.println("Expected: ");
112 for (int i = 0; i < namesOfDirectProperties.length; i++) {
113 log.println("\t" + namesOfDirectProperties[i]);
115 } else {
116 for (int i = 0; i < GDPR.length; i++) {
117 boolean localres = GDPR[i].Name.equals(
118 namesOfDirectProperties[i]);
120 if (!localres) {
121 log.println("Found: ");
122 log.println("\t" + GDPR[i].Name);
123 log.println("Expected: ");
124 log.println("\t" + namesOfDirectProperties[i]);
127 res &= localres;
131 tRes.tested("getDirectPropertyValuesTolerant()", res);
134 public void _getPropertyValuesTolerant() {
135 requiredMethod("getDirectPropertyValuesTolerant()");
136 GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
137 namesOfProperties);
139 boolean res = (GPR.length == namesOfProperties.length);
141 if (!res) {
142 log.println("Length of sequences differs");
143 log.println("Found: " + GPR.length);
144 log.println("Expected: " + namesOfProperties.length);
145 } else {
146 for (int i = 0; i < GPR.length; i++) {
147 boolean localres = true;
149 if (!(GPR[i].Value instanceof com.sun.star.uno.Any)) {
150 localres = ValueComparer.equalValue(GPR[i].Value,
151 valuesOfProperties[i]);
155 if (!localres) {
156 log.println("Values differ for : " +
157 namesOfProperties[i]);
158 log.println("\t" + GPR[i].Value);
159 log.println("Expected: ");
160 log.println("\t" + valuesOfProperties[i]);
163 res &= localres;
167 tRes.tested("getPropertyValuesTolerant()", res);
170 public void _setPropertyValuesTolerant() {
171 requiredMethod("getPropertyValuesTolerant()");
173 SetPropertyTolerantFailed[] SPTF = null;
175 try {
176 SPTF = oObj.setPropertyValuesTolerant(namesOfProperties,
177 getNewValues(
178 valuesOfProperties));
179 } catch (com.sun.star.lang.IllegalArgumentException e) {
180 e.printStackTrace(log);
183 //read only properties will throw a PropertyVetoExeption if they are set
184 int failures = 0;
186 for (int k = 0; k < SPTF.length; k++) {
187 if (SPTF[k].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
188 failures++;
192 int roProps = getCountOfReadOnlyProperties();
194 boolean res = (failures == roProps);
196 if (!res) {
197 log.println("Failures: " + failures);
198 log.println("Count of R/O properties: " + roProps);
200 for (int i = 0; i < SPTF.length; i++) {
201 if (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.PROPERTY_VETO) {
202 failures++;
203 log.println("Failed for " + SPTF[i].Name);
204 log.println("\t Result: " + SPTF[i].Result);
207 } else {
208 for (int i = 0; i < SPTF.length; i++) {
209 boolean localres = true;
210 GetPropertyTolerantResult[] GPR = oObj.getPropertyValuesTolerant(
211 namesOfProperties);
213 if ((!(GPR[i].Value instanceof com.sun.star.uno.Any)) &&
214 (SPTF[i].Result == com.sun.star.beans.TolerantPropertySetResultType.SUCCESS)) {
215 localres = ValueComparer.equalValue(GPR[i].Value,
216 valuesOfProperties[i]);
219 if (!localres) {
220 log.println("Values differ for : " +
221 namesOfProperties[i]);
222 log.println("\t" + GPR[i].Value);
223 log.println("Expected: ");
224 log.println("\t" + valuesOfProperties[i]);
227 res &= localres;
231 tRes.tested("setPropertyValuesTolerant()", res);
235 * This method returns a sorted list of property names
236 * contained in a given sequence of properties that additionally
237 * have the state DIRECT_VALUE
239 protected String[] getDirectProperties(Property[] props) {
240 ArrayList direct = new ArrayList();
242 for (int i = 0; i < props.length; i++) {
243 String pName = props[i].Name;
245 try {
246 PropertyState state = pState.getPropertyState(pName);
248 if (state.equals(PropertyState.DIRECT_VALUE)) {
249 if (isUsable(pName)) direct.add(pName);
251 } catch (com.sun.star.beans.UnknownPropertyException e) {
252 log.println("Property '" + pName + "'");
256 Collections.sort(direct);
258 Object[] obj = direct.toArray();
259 String[] ret = new String[obj.length];
261 for (int i = 0; i < obj.length; i++) {
262 ret[i] = (String) obj[i];
265 return ret;
268 private boolean isUsable(String name) {
269 boolean isUsable=true;
270 if (name.startsWith("TextWriting")) isUsable = false;
271 if (name.startsWith("MetaFile")) isUsable = false;
272 return isUsable;
276 * This method returns a sorted list of property names
277 * contained in a given sequence of properties
279 protected String[] getProperties() {
280 ArrayList names = new ArrayList();
282 for (int i = 0; i < properties.length; i++) {
283 String pName = properties[i].Name;
284 if (isUsable(pName)) names.add(pName);
287 Collections.sort(names);
289 Object[] obj = names.toArray();
290 String[] ret = new String[obj.length];
292 for (int i = 0; i < obj.length; i++) {
293 ret[i] = (String) obj[i];
296 return ret;
300 * Returns the values of a given array of properties in an Object array
302 protected Object[] getPropertyValues(String[] propertyNames) {
303 Object[] values = new Object[propertyNames.length];
305 for (int i = 0; i < propertyNames.length; i++) {
306 try {
307 values[i] = PS.getPropertyValue(propertyNames[i]);
308 } catch (com.sun.star.beans.UnknownPropertyException e) {
309 e.printStackTrace(log);
310 } catch (com.sun.star.lang.WrappedTargetException e) {
311 e.printStackTrace(log);
315 return values;
318 protected int getCountOfReadOnlyProperties() {
319 int ro = 0;
321 for (int i = 0; i < properties.length; i++) {
322 Property property = properties[i];
323 boolean isWritable = ((property.Attributes & PropertyAttribute.READONLY) == 0);
325 if (!isWritable) {
326 ro++;
330 return ro;
333 protected Object[] getNewValues(Object[] oldValues) {
334 Object[] newValues = new Object[oldValues.length];
336 for (int i = 0; i < oldValues.length; i++) {
337 if (oldValues[i] instanceof com.sun.star.uno.Any) {
338 newValues[i] = oldValues[i];
339 } else {
340 newValues[i] = ValueChanger.changePValue(oldValues[i]);
344 return newValues;