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 .
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
23 import java
.util
.Random
;
26 import lib
.MultiMethodTest
;
27 import lib
.StatusException
;
28 import util
.ValueChanger
;
30 import com
.sun
.star
.beans
.Property
;
31 import com
.sun
.star
.beans
.PropertyAttribute
;
32 import com
.sun
.star
.beans
.XFastPropertySet
;
33 import com
.sun
.star
.beans
.XPropertySet
;
34 import com
.sun
.star
.beans
.XPropertySetInfo
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
38 * Testing <code>com.sun.star.beans.XFastPropertySet</code>
41 * <li><code> setFastPropertyValue()</code></li>
42 * <li><code> getFastPropertyValue()</code></li>
44 * Required relations :
46 * <li> <code>'XFastPropertySet.ExcludeProps'</code>
47 * <b>(optional) </b> : java.util.Set.
48 * Has property names which must be skipped from testing in
49 * some reasons (for example property accepts restricted set
53 * @see com.sun.star.beans.XFastPropertySet
55 public class _XFastPropertySet
extends MultiMethodTest
{
56 private static final class Prop
{
57 public final int handle
;
58 public final String name
;
60 public Prop(int handle
, String name
) {
66 public XFastPropertySet oObj
= null;
67 private final List
<Prop
> props
= new ArrayList
<Prop
>();
69 private Set
<String
> exclude
= null ;
75 protected void before() {
76 exclude
= (Set
<String
>) tEnv
.getObjRelation("XFastPropertySet.ExcludeProps") ;
77 if (exclude
== null) {
78 exclude
= new java
.util
.HashSet
<String
>() ;
83 * Test selects random property which can not be VOID and
84 * is writable, then change property value using <code>
85 * get/set</code> methods, and checks if value properly changed.
86 * Has <b> OK </b> status if value after change is not equal to value
87 * before and no exceptions were thrown. <p>
89 public void _setFastPropertyValue() {
90 XPropertySet PS
= UnoRuntime
.queryInterface
91 (XPropertySet
.class, oObj
);
92 XPropertySetInfo propertySetInfo
= PS
.getPropertySetInfo();
94 if (propertySetInfo
== null) {
95 log
.println("getPropertySetInfo() method returned null");
96 tRes
.tested("setFastPropertyValue()", false) ;
98 getPropsToTest(propertySetInfo
);
100 Object gValue
= null;
101 Object sValue
= null;
103 if ( prop
.handle
== -1) {
104 log
.println("*** No changeable properties found ***");
105 tRes
.tested("setFastPropertyValue()", false) ;
108 gValue
= oObj
.getFastPropertyValue(prop
.handle
);
109 sValue
= ValueChanger
.changePValue(gValue
, prop
.name
);
110 oObj
.setFastPropertyValue(prop
.handle
, sValue
);
111 sValue
= oObj
.getFastPropertyValue(prop
.handle
);
112 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
113 log
.println("Exception occurred while trying to change property with handle = " + prop
.handle
);
114 e
.printStackTrace(log
);
115 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
116 log
.println("Exception occurred while trying to change property with handle = " + prop
.handle
);
117 e
.printStackTrace(log
);
118 } catch (com
.sun
.star
.beans
.PropertyVetoException e
) {
119 log
.println("Exception occurred while trying to change property with handle = " + prop
.handle
);
120 e
.printStackTrace(log
);
121 } catch (com
.sun
.star
.lang
.IllegalArgumentException e
) {
122 log
.println("Exception occurred while trying to change property with handle = " + prop
.handle
);
123 e
.printStackTrace(log
);
126 tRes
.tested("setFastPropertyValue()",(!gValue
.equals(sValue
)));
131 * Test selects random property which can not be VOID and
132 * is writable, then calls the method and checks that
133 * no exceptions were thrown. <p>
134 * Has <b> OK </b> status if exceptions were thrown. <p>
136 public void _getFastPropertyValue() {
137 XPropertySet PS
= UnoRuntime
.queryInterface
138 (XPropertySet
.class, oObj
);
139 XPropertySetInfo propertySetInfo
= PS
.getPropertySetInfo();
141 if (propertySetInfo
== null) {
142 log
.println("getPropertySetInfo() method returned null");
143 tRes
.tested("getFastPropertyValue()", false) ;
146 getPropsToTest(propertySetInfo
);
149 oObj
.getFastPropertyValue(prop
.handle
);
150 tRes
.tested("getFastPropertyValue()",true);
151 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
152 log
.println("Exception occurred while trying to get property '"
154 e
.printStackTrace(log
);
155 tRes
.tested("getFastPropertyValue()",false);
156 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
157 log
.println("Exception occurred while trying to get property '"
159 e
.printStackTrace(log
);
160 tRes
.tested("getFastPropertyValue()",false);
165 //Get the properties being tested
166 private void getPropsToTest(XPropertySetInfo xPSI
) {
168 Property
[] properties
= xPSI
.getProperties();
170 for (int i
= 0; i
< properties
.length
; i
++) {
171 if (exclude
.contains(properties
[i
].Name
)) continue ;
172 Property property
= properties
[i
];
173 String name
= property
.Name
;
174 int handle
= property
.Handle
;
175 log
.println("Checking '" + name
+ "' with handle = " + handle
);
177 ((property
.Attributes
& PropertyAttribute
.READONLY
) == 0);
179 ((property
.Attributes
& PropertyAttribute
.MAYBEVOID
) == 0);
180 boolean canChange
= false;
181 if ( isWritable
&& isNotNull
)
182 canChange
= isChangeable(handle
, name
);
183 if ( isWritable
&& isNotNull
&& canChange
)
184 props
.add(new Prop(handle
, name
));
187 Random rnd
= new Random();
188 int nr
= rnd
.nextInt(props
.size());
189 prop
= props
.get(nr
);
192 private boolean isChangeable(int handle
, String name
) {
193 boolean hasChanged
= false;
195 Object getProp
= oObj
.getFastPropertyValue(handle
);
196 Object setValue
= null;
199 setValue
= ValueChanger
.changePValue(getProp
, name
);
201 log
.println("Property with handle = " + handle
202 + " is null but 'MAYBEVOID' isn't set");
203 if (setValue
!= null) {
204 oObj
.setFastPropertyValue(handle
, setValue
);
206 (!getProp
.equals(oObj
.getFastPropertyValue(handle
)));
209 log
.println("Couldn't change Property with handle " + handle
);
210 } catch(com
.sun
.star
.lang
.WrappedTargetException e
) {
211 log
.println("Property with handle " + handle
+ " throws exception");
212 e
.printStackTrace(log
);
213 throw new StatusException("Property with handle " + handle
214 + " throws exception", e
);
215 } catch(com
.sun
.star
.lang
.IllegalArgumentException e
) {
216 log
.println("Property with handle " + handle
+ " throws exception");
217 e
.printStackTrace(log
);
218 throw new StatusException("Property with handle " + handle
219 + " throws exception", e
);
220 } catch(com
.sun
.star
.beans
.PropertyVetoException e
) {
221 log
.println("Property with handle " + handle
+ " throws exception");
222 e
.printStackTrace(log
);
223 throw new StatusException("Property with handle " + handle
224 + " throws exception", e
);
225 } catch(com
.sun
.star
.beans
.UnknownPropertyException e
) {
226 log
.println("Property with handle " + handle
+ " throws exception");
227 e
.printStackTrace(log
);
228 throw new StatusException("Property with handle " + handle
229 + " throws exception", e
);