1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XTolerantMultiPropertySet.java,v $
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 ************************************************************************/
32 import com
.sun
.star
.beans
.GetDirectPropertyTolerantResult
;
33 import com
.sun
.star
.beans
.GetPropertyTolerantResult
;
34 import com
.sun
.star
.beans
.Property
;
35 import com
.sun
.star
.beans
.PropertyAttribute
;
36 import com
.sun
.star
.beans
.PropertyState
;
37 import com
.sun
.star
.beans
.SetPropertyTolerantFailed
;
38 import com
.sun
.star
.beans
.XPropertySet
;
39 import com
.sun
.star
.beans
.XPropertyState
;
40 import com
.sun
.star
.beans
.XTolerantMultiPropertySet
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
43 import java
.util
.ArrayList
;
44 import java
.util
.Collections
;
46 import lib
.MultiMethodTest
;
48 import lib
.StatusException
;
50 import util
.ValueChanger
;
51 import util
.ValueComparer
;
54 public class _XTolerantMultiPropertySet
extends MultiMethodTest
{
55 public XTolerantMultiPropertySet oObj
;
56 protected String
[] namesOfDirectProperties
= null;
57 protected String
[] namesOfProperties
= null;
58 protected Object
[] valuesOfProperties
= null;
59 protected Property
[] properties
= null;
60 protected XPropertyState pState
= null;
61 protected XPropertySet PS
= null;
65 * Queries XPropertySet from the given Component and gets XPropertySetInfo
66 * from it to get the PropertyNames available and their Values<br>
67 * Then queries XPropertyState from the given Component
68 * to get the direct properties<br>
69 * Throws a lib StatusException if the Component doesn't support XPropertySet or XPropertyState
71 public void before() {
72 PS
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class,
73 tEnv
.getTestObject());
76 throw new StatusException(Status
.failed(
77 "Component doesn't provide the needed XPropertySet"));
80 pState
= (XPropertyState
) UnoRuntime
.queryInterface(
81 XPropertyState
.class, tEnv
.getTestObject());
84 throw new StatusException(Status
.failed(
85 "Component doesn't provide the needed XPropertyState"));
88 properties
= PS
.getPropertySetInfo().getProperties();
89 namesOfProperties
= getProperties();
90 valuesOfProperties
= getPropertyValues(namesOfProperties
);
94 * Calls the method getDirectPropertyValuesTolerant() and compares the resulting
95 * sequence with the one gained as direct values in the before() method.<br>
96 * Has OK state if both sequences equal.
98 public void _getDirectPropertyValuesTolerant() {
99 namesOfDirectProperties
= getDirectProperties(properties
);
101 GetDirectPropertyTolerantResult
[] GDPR
= oObj
.getDirectPropertyValuesTolerant(
104 boolean res
= (GDPR
.length
== namesOfDirectProperties
.length
);
107 log
.println("Found: ");
109 for (int i
= 0; i
< GDPR
.length
; i
++) {
110 log
.println("\t" + GDPR
[i
].Name
);
113 log
.println("Expected: ");
115 for (int i
= 0; i
< namesOfDirectProperties
.length
; i
++) {
116 log
.println("\t" + namesOfDirectProperties
[i
]);
119 for (int i
= 0; i
< GDPR
.length
; i
++) {
120 boolean localres
= GDPR
[i
].Name
.equals(
121 namesOfDirectProperties
[i
]);
124 log
.println("Found: ");
125 log
.println("\t" + GDPR
[i
].Name
);
126 log
.println("Expected: ");
127 log
.println("\t" + namesOfDirectProperties
[i
]);
134 tRes
.tested("getDirectPropertyValuesTolerant()", res
);
137 public void _getPropertyValuesTolerant() {
138 requiredMethod("getDirectPropertyValuesTolerant()");
139 GetPropertyTolerantResult
[] GPR
= oObj
.getPropertyValuesTolerant(
142 boolean res
= (GPR
.length
== namesOfProperties
.length
);
145 log
.println("Length of sequences differs");
146 log
.println("Found: " + GPR
.length
);
147 log
.println("Expected: " + namesOfProperties
.length
);
149 for (int i
= 0; i
< GPR
.length
; i
++) {
150 boolean localres
= true;
152 if (!(GPR
[i
].Value
instanceof com
.sun
.star
.uno
.Any
)) {
153 localres
= ValueComparer
.equalValue(GPR
[i
].Value
,
154 valuesOfProperties
[i
]);
159 log
.println("Values differ for : " +
160 namesOfProperties
[i
]);
161 log
.println("\t" + GPR
[i
].Value
);
162 log
.println("Expected: ");
163 log
.println("\t" + valuesOfProperties
[i
]);
170 tRes
.tested("getPropertyValuesTolerant()", res
);
173 public void _setPropertyValuesTolerant() {
174 requiredMethod("getPropertyValuesTolerant()");
176 SetPropertyTolerantFailed
[] SPTF
= null;
179 SPTF
= oObj
.setPropertyValuesTolerant(namesOfProperties
,
181 valuesOfProperties
));
182 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
183 e
.printStackTrace(log
);
186 //read only properties will throw a PropertyVetoExeption if they are set
189 for (int k
= 0; k
< SPTF
.length
; k
++) {
190 if (SPTF
[k
].Result
== com
.sun
.star
.beans
.TolerantPropertySetResultType
.PROPERTY_VETO
) {
195 int roProps
= getCountOfReadOnlyProperties();
197 boolean res
= (failures
== roProps
);
200 log
.println("Failures: " + failures
);
201 log
.println("Count of R/O properties: " + roProps
);
203 for (int i
= 0; i
< SPTF
.length
; i
++) {
204 if (SPTF
[i
].Result
== com
.sun
.star
.beans
.TolerantPropertySetResultType
.PROPERTY_VETO
) {
206 log
.println("Failed for " + SPTF
[i
].Name
);
207 log
.println("\t Result: " + SPTF
[i
].Result
);
211 for (int i
= 0; i
< SPTF
.length
; i
++) {
212 boolean localres
= true;
213 GetPropertyTolerantResult
[] GPR
= oObj
.getPropertyValuesTolerant(
216 if ((!(GPR
[i
].Value
instanceof com
.sun
.star
.uno
.Any
)) &&
217 (SPTF
[i
].Result
== com
.sun
.star
.beans
.TolerantPropertySetResultType
.SUCCESS
)) {
218 localres
= ValueComparer
.equalValue(GPR
[i
].Value
,
219 valuesOfProperties
[i
]);
223 log
.println("Values differ for : " +
224 namesOfProperties
[i
]);
225 log
.println("\t" + GPR
[i
].Value
);
226 log
.println("Expected: ");
227 log
.println("\t" + valuesOfProperties
[i
]);
234 tRes
.tested("setPropertyValuesTolerant()", res
);
238 * This method returns a sorted list of property names
239 * contained in a given sequence of properties that additionally
240 * have the state DIRECT_VALUE
242 protected String
[] getDirectProperties(Property
[] props
) {
243 ArrayList direct
= new ArrayList();
245 for (int i
= 0; i
< props
.length
; i
++) {
246 String pName
= props
[i
].Name
;
249 PropertyState state
= pState
.getPropertyState(pName
);
251 if (state
.equals(PropertyState
.DIRECT_VALUE
)) {
252 if (isUsable(pName
)) direct
.add(pName
);
254 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
255 log
.println("Property '" + pName
+ "'");
259 Collections
.sort(direct
);
261 Object
[] obj
= direct
.toArray();
262 String
[] ret
= new String
[obj
.length
];
264 for (int i
= 0; i
< obj
.length
; i
++) {
265 ret
[i
] = (String
) obj
[i
];
271 private boolean isUsable(String name
) {
272 boolean isUsable
=true;
273 if (name
.startsWith("TextWriting")) isUsable
= false;
274 if (name
.startsWith("MetaFile")) isUsable
= false;
279 * This method returns a sorted list of property names
280 * contained in a given sequence of properties
282 protected String
[] getProperties() {
283 ArrayList names
= new ArrayList();
285 for (int i
= 0; i
< properties
.length
; i
++) {
286 String pName
= properties
[i
].Name
;
287 if (isUsable(pName
)) names
.add(pName
);
290 Collections
.sort(names
);
292 Object
[] obj
= names
.toArray();
293 String
[] ret
= new String
[obj
.length
];
295 for (int i
= 0; i
< obj
.length
; i
++) {
296 ret
[i
] = (String
) obj
[i
];
303 * Returns the values of a given array of properties in an Object array
305 protected Object
[] getPropertyValues(String
[] propertyNames
) {
306 Object
[] values
= new Object
[propertyNames
.length
];
308 for (int i
= 0; i
< propertyNames
.length
; i
++) {
310 values
[i
] = PS
.getPropertyValue(propertyNames
[i
]);
311 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
312 e
.printStackTrace(log
);
313 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
314 e
.printStackTrace(log
);
321 protected int getCountOfReadOnlyProperties() {
324 for (int i
= 0; i
< properties
.length
; i
++) {
325 Property property
= properties
[i
];
326 boolean isWritable
= ((property
.Attributes
& PropertyAttribute
.READONLY
) == 0);
336 protected Object
[] getNewValues(Object
[] oldValues
) {
337 Object
[] newValues
= new Object
[oldValues
.length
];
339 for (int i
= 0; i
< oldValues
.length
; i
++) {
340 if (oldValues
[i
] instanceof com
.sun
.star
.uno
.Any
) {
341 newValues
[i
] = oldValues
[i
];
343 newValues
[i
] = ValueChanger
.changePValue(oldValues
[i
]);