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 lib
.MultiMethodTest
;
23 import lib
.StatusException
;
25 import com
.sun
.star
.beans
.Property
;
26 import com
.sun
.star
.beans
.PropertyAttribute
;
27 import com
.sun
.star
.beans
.PropertyState
;
28 import com
.sun
.star
.beans
.XPropertySet
;
29 import com
.sun
.star
.beans
.XPropertySetInfo
;
30 import com
.sun
.star
.beans
.XPropertyState
;
31 import com
.sun
.star
.uno
.Any
;
32 import com
.sun
.star
.uno
.UnoRuntime
;
36 * Testing <code>com.sun.star.beans.XPropertyState</code>
39 * <li><code> getPropertyState()</code></li>
40 * <li><code> getPropertyStates()</code></li>
41 * <li><code> setPropertyToDefault()</code></li>
42 * <li><code> getPropertyDefault()</code></li>
44 * Test is <b> NOT </b> multithread compliant. <p>
45 * After test completion object environment has to be recreated. <p>
46 * <b>Note:</b> object tested must also implement
47 * <code>com.sun.star.beans.XPropertySet</code> interface.
48 * @see com.sun.star.beans.XPropertyState
50 public class _XPropertyState
extends MultiMethodTest
{
52 public XPropertyState oObj
= null;
54 private XPropertySet oPS
= null ;
55 private XPropertySetInfo propertySetInfo
= null;
56 private Property
[] properties
= null ;
57 private String pName
= null ;
58 private Object propDef
= null ;
61 * Queries object for <code>XPropertySet</code> interface and
62 * initializes some fields used by all methods. <p>
64 * Searches property which is not READONLY and MAYBEDEFAULT, if
65 * such property is not found, then uses property with only
66 * READONLY attribute. This property name is stored and is used
69 * @throws StatusException If <code>XPropertySet</code> is not
70 * implemented by object.
73 public void before() throws StatusException
{
74 oPS
= UnoRuntime
.queryInterface( XPropertySet
.class, oObj
);
76 throw new StatusException
77 ("XPropertySet interface isn't implemented.",
78 new NullPointerException
79 ("XPropertySet interface isn't implemented.")) ;
81 propertySetInfo
= oPS
.getPropertySetInfo();
82 properties
= propertySetInfo
.getProperties();
84 for (int i
=0;i
<properties
.length
;i
++) {
86 prop
= propertySetInfo
.getPropertyByName
88 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
89 log
.println("Unknown Property "+prop
.Name
);
91 boolean readOnly
= (prop
.Attributes
&
92 PropertyAttribute
.READONLY
) != 0;
93 boolean maybeDefault
= (prop
.Attributes
&
94 PropertyAttribute
.MAYBEDEFAULT
) != 0;
95 if (!readOnly
&& maybeDefault
) {
96 pName
= properties
[i
].Name
;
97 log
.println("Property '" + pName
+ "' has attributes "+
102 pName
= properties
[i
].Name
;
103 log
.println("Property '" + pName
+
104 "' is not readonly, may be used ...");
106 log
.println("Skipping property '" + properties
[i
].Name
+
107 "' Readonly: " + readOnly
+ ", MaybeDefault: " +
115 * Test calls the method and checks that no exceptions were thrown. <p>
116 * Has <b> OK </b> status if no exceptions were thrown. <p>
118 public void _getPropertyDefault(){
119 boolean result
= true ;
120 String localName
= pName
;
121 if (localName
== null) {
122 localName
= propertySetInfo
.getProperties()[0].Name
;
125 propDef
= oObj
.getPropertyDefault(localName
);
126 log
.println("Default property value is : '" + propDef
+ "'");
127 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
128 log
.println("Exception " + e
+
129 "occurred while getting Property default");
131 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
132 log
.println("Exception " + e
+
133 "occurred while getting Property default");
136 tRes
.tested("getPropertyDefault()", result
);
140 * Test calls the method and checks return value and that
141 * no exceptions were thrown. <p>
142 * Has <b> OK </b> status if the method returns not null value
143 * and no exceptions were thrown. <p>
145 public void _getPropertyState(){
146 boolean result
= true ;
148 String localName
= pName
;
149 if (localName
== null) {
150 localName
= propertySetInfo
.getProperties()[0].Name
;
154 PropertyState ps
= oObj
.getPropertyState(localName
);
156 log
.println("!!! Returned value == null") ;
159 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
160 log
.println("Exception " + e
+
161 "occurred while getting Property state");
164 tRes
.tested("getPropertyState()", result
);
168 * Test calls the method with array of one property name
169 * and checks return value and that no exceptions were thrown. <p>
170 * Has <b> OK </b> status if the method returns array with one
171 * PropertyState and no exceptions were thrown. <p>
173 public void _getPropertyStates(){
174 boolean result
= true ;
176 String localName
= pName
;
177 if (localName
== null) {
178 localName
= propertySetInfo
.getProperties()[0].Name
;
182 PropertyState
[] ps
= oObj
.getPropertyStates
183 (new String
[] {localName
});
185 log
.println("!!! Returned value == null") ;
188 if (ps
.length
!= 1) {
189 log
.println("!!! Array length returned is invalid - " +
194 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
195 log
.println("Exception " + e
+
196 "occurred while getting Property state");
200 tRes
.tested("getPropertyStates()", result
);
205 * Sets the property to default, then compares the current property
206 * value to value received by method <code>getPropertyDefault</code>.
207 * Has <b> OK </b> status if the current property value equals to
208 * default property. <p>
209 * The following method tests are to be completed successfully before :
211 * <li> <code>getPropertyDefault</code>: we have to know what is
212 * default value</li></ul>
214 public void _setPropertyToDefault(){
215 requiredMethod("getPropertyDefault()") ;
218 log
.println("all found properties are read only");
219 tRes
.tested("setPropertyToDefault()",Status
.skipped(true));
223 boolean result
= true ;
226 oObj
.setPropertyToDefault(pName
);
228 catch(RuntimeException e
) {
229 System
.out
.println("Ignoring RuntimeException: " + e
.getMessage());
231 if ((properties
[0].Attributes
&
232 PropertyAttribute
.MAYBEDEFAULT
) != 0) {
233 Object actualDef
= propDef
;
234 if (propDef
instanceof Any
) {
235 actualDef
= ((Any
)propDef
).getObject() ;
237 Object actualVal
= oPS
.getPropertyValue(pName
) ;
238 if (actualVal
instanceof Any
) {
239 actualVal
= ((Any
)actualVal
).getObject() ;
241 result
= util
.ValueComparer
.equalValue
242 (actualDef
,actualVal
) ;
243 log
.println("Default value = '" + actualDef
+
244 "', returned value = '"
245 + actualVal
+ "' for property " + pName
) ;
247 } catch (com
.sun
.star
.beans
.UnknownPropertyException e
) {
248 log
.println("Exception " + e
+
249 "occurred while setting Property to default");
251 } catch (com
.sun
.star
.lang
.WrappedTargetException e
) {
252 log
.println("Exception " + e
+
253 "occurred while testing property value");
257 tRes
.tested("setPropertyToDefault()", result
);
261 public void after() {
262 disposeEnvironment() ;
265 }// EOF _XPropertyState