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 .
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
;
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
60 public void before() {
61 PS
= UnoRuntime
.queryInterface(XPropertySet
.class,
62 tEnv
.getTestObject());
65 throw new StatusException(Status
.failed(
66 "Component doesn't provide the needed XPropertySet"));
69 pState
= UnoRuntime
.queryInterface(
70 XPropertyState
.class, tEnv
.getTestObject());
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(
93 boolean res
= (GDPR
.length
== namesOfDirectProperties
.length
);
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
]);
108 for (int i
= 0; i
< GDPR
.length
; i
++) {
109 boolean localres
= GDPR
[i
].Name
.equals(
110 namesOfDirectProperties
[i
]);
113 log
.println("Found: ");
114 log
.println("\t" + GDPR
[i
].Name
);
115 log
.println("Expected: ");
116 log
.println("\t" + namesOfDirectProperties
[i
]);
123 tRes
.tested("getDirectPropertyValuesTolerant()", res
);
126 public void _getPropertyValuesTolerant() {
127 requiredMethod("getDirectPropertyValuesTolerant()");
128 GetPropertyTolerantResult
[] GPR
= oObj
.getPropertyValuesTolerant(
131 boolean res
= (GPR
.length
== namesOfProperties
.length
);
134 log
.println("Length of sequences differs");
135 log
.println("Found: " + GPR
.length
);
136 log
.println("Expected: " + namesOfProperties
.length
);
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
]);
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
]);
159 tRes
.tested("getPropertyValuesTolerant()", res
);
162 public void _setPropertyValuesTolerant() {
163 requiredMethod("getPropertyValuesTolerant()");
165 SetPropertyTolerantFailed
[] SPTF
= null;
168 SPTF
= oObj
.setPropertyValuesTolerant(namesOfProperties
,
170 valuesOfProperties
));
171 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
172 e
.printStackTrace(log
);
175 //read only properties will throw a PropertyVetoExeption if they are set
178 for (int k
= 0; k
< SPTF
.length
; k
++) {
179 if (SPTF
[k
].Result
== com
.sun
.star
.beans
.TolerantPropertySetResultType
.PROPERTY_VETO
) {
184 int roProps
= getCountOfReadOnlyProperties();
186 boolean res
= (failures
== roProps
);
189 log
.println("Failures: " + failures
);
190 log
.println("Count of R/O properties: " + roProps
);
192 for (int i
= 0; i
< SPTF
.length
; i
++) {
193 if (SPTF
[i
].Result
== com
.sun
.star
.beans
.TolerantPropertySetResultType
.PROPERTY_VETO
) {
195 log
.println("Failed for " + SPTF
[i
].Name
);
196 log
.println("\t Result: " + SPTF
[i
].Result
);
200 for (int i
= 0; i
< SPTF
.length
; i
++) {
201 boolean localres
= true;
202 GetPropertyTolerantResult
[] GPR
= oObj
.getPropertyValuesTolerant(
205 if ((!(GPR
[i
].Value
instanceof com
.sun
.star
.uno
.Any
)) &&
206 (SPTF
[i
].Result
== com
.sun
.star
.beans
.TolerantPropertySetResultType
.SUCCESS
)) {
207 localres
= ValueComparer
.equalValue(GPR
[i
].Value
,
208 valuesOfProperties
[i
]);
212 log
.println("Values differ for : " +
213 namesOfProperties
[i
]);
214 log
.println("\t" + GPR
[i
].Value
);
215 log
.println("Expected: ");
216 log
.println("\t" + valuesOfProperties
[i
]);
223 tRes
.tested("setPropertyValuesTolerant()", res
);
227 * This method returns a sorted list of property names
228 * contained in a given sequence of properties that additionally
229 * have the state DIRECT_VALUE
231 protected String
[] getDirectProperties(Property
[] props
) {
232 ArrayList
<String
> direct
= new ArrayList
<String
>();
234 for (int i
= 0; i
< props
.length
; i
++) {
235 String pName
= props
[i
].Name
;
238 PropertyState state
= pState
.getPropertyState(pName
);
240 if (state
.equals(PropertyState
.DIRECT_VALUE
)) {
241 if (isUsable(pName
)) direct
.add(pName
);
243 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
244 log
.println("Property '" + pName
+ "'");
248 Collections
.sort(direct
);
250 Object
[] obj
= direct
.toArray();
251 String
[] ret
= new String
[obj
.length
];
253 for (int i
= 0; i
< obj
.length
; i
++) {
254 ret
[i
] = (String
) obj
[i
];
260 private boolean isUsable(String name
) {
261 boolean isUsable
=true;
262 if (name
.startsWith("TextWriting")) isUsable
= false;
263 if (name
.startsWith("MetaFile")) isUsable
= false;
268 * This method returns a sorted list of property names
269 * contained in a given sequence of properties
271 protected String
[] getProperties() {
272 ArrayList
<String
> names
= new ArrayList
<String
>();
274 for (int i
= 0; i
< properties
.length
; i
++) {
275 String pName
= properties
[i
].Name
;
276 if (isUsable(pName
)) names
.add(pName
);
279 Collections
.sort(names
);
281 Object
[] obj
= names
.toArray();
282 String
[] ret
= new String
[obj
.length
];
284 for (int i
= 0; i
< obj
.length
; i
++) {
285 ret
[i
] = (String
) obj
[i
];
292 * Returns the values of a given array of properties in an Object array
294 protected Object
[] getPropertyValues(String
[] propertyNames
) {
295 Object
[] values
= new Object
[propertyNames
.length
];
297 for (int i
= 0; i
< propertyNames
.length
; i
++) {
299 values
[i
] = PS
.getPropertyValue(propertyNames
[i
]);
300 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
301 e
.printStackTrace(log
);
302 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
303 e
.printStackTrace(log
);
310 protected int getCountOfReadOnlyProperties() {
313 for (int i
= 0; i
< properties
.length
; i
++) {
314 Property property
= properties
[i
];
315 boolean isWritable
= ((property
.Attributes
& PropertyAttribute
.READONLY
) == 0);
325 protected Object
[] getNewValues(Object
[] oldValues
) {
326 Object
[] newValues
= new Object
[oldValues
.length
];
328 for (int i
= 0; i
< oldValues
.length
; i
++) {
329 if (oldValues
[i
] instanceof com
.sun
.star
.uno
.Any
) {
330 newValues
[i
] = oldValues
[i
];
332 newValues
[i
] = ValueChanger
.changePValue(oldValues
[i
]);