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
= oObj
.setPropertyValuesTolerant(namesOfProperties
,
166 getNewValues(valuesOfProperties
));
168 //read only properties will throw a PropertyVetoException if they are set
171 for (int k
= 0; k
< SPTF
.length
; k
++) {
172 if (SPTF
[k
].Result
== com
.sun
.star
.beans
.TolerantPropertySetResultType
.PROPERTY_VETO
) {
177 int roProps
= getCountOfReadOnlyProperties();
179 boolean res
= (failures
== roProps
);
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
) {
188 log
.println("Failed for " + SPTF
[i
].Name
);
189 log
.println("\t Result: " + SPTF
[i
].Result
);
193 for (int i
= 0; i
< SPTF
.length
; i
++) {
194 boolean localres
= true;
195 GetPropertyTolerantResult
[] GPR
= oObj
.getPropertyValuesTolerant(
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
]);
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
]);
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
;
231 PropertyState state
= pState
.getPropertyState(pName
);
233 if (state
.equals(PropertyState
.DIRECT_VALUE
) && isUsable(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
];
252 private boolean isUsable(String name
) {
253 boolean isUsable
=true;
254 if (name
.startsWith("TextWriting")) isUsable
= false;
255 if (name
.startsWith("MetaFile")) isUsable
= false;
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
];
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
++) {
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
);
302 protected int getCountOfReadOnlyProperties() {
305 for (int i
= 0; i
< properties
.length
; i
++) {
306 Property property
= properties
[i
];
307 boolean isWritable
= ((property
.Attributes
& PropertyAttribute
.READONLY
) == 0);
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
];
324 newValues
[i
] = ValueChanger
.changePValue(oldValues
[i
]);