1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.beans;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.beans.Property;
31 import com.sun.star.beans.PropertyAttribute;
32 import com.sun.star.beans.PropertyState;
33 import com.sun.star.beans.XPropertySet;
34 import com.sun.star.beans.XPropertySetInfo;
35 import com.sun.star.beans.XPropertyState;
36 import com.sun.star.uno.Any;
37 import com.sun.star.uno.UnoRuntime;
38 
39 
40 /**
41 * Testing <code>com.sun.star.beans.XPropertyState</code>
42 * interface methods :
43 * <ul>
44 *  <li><code> getPropertyState()</code></li>
45 *  <li><code> getPropertyStates()</code></li>
46 *  <li><code> setPropertyToDefault()</code></li>
47 *  <li><code> getPropertyDefault()</code></li>
48 * </ul>
49 * Test is <b> NOT </b> multithread compilant. <p>
50 * After test completion object environment has to be recreated. <p>
51 * <b>Note:</b> object tested must also implement
52 * <code>com.sun.star.beans.XPropertySet</code> interface.
53 * @see com.sun.star.beans.XPropertyState
54 */
55 public class _XPropertyState extends MultiMethodTest {
56 
57         public XPropertyState oObj = null;
58 
59         private XPropertySet oPS = null ;
60         private XPropertySetInfo propertySetInfo = null;
61         private Property[] properties = null ;
62         private String pName = null ;
63         private Object propDef = null ;
64 
65         /**
66         * Queries object for <code>XPropertySet</code> interface and
67         * initializes some fields used by all methods. <p>
68         *
69         * Searches property which is not READONLY and MAYBEDEFAULT, if
70         * such property is not found, then uses property with only
71         * READONLY attribute. This property name is stored and is used
72         * by all tests.
73         *
74         * @throws StatusException If <code>XPropertySet</code> is not
75         * implemented by object.
76         */
before()77         public void before() throws StatusException {
78             oPS = (XPropertySet)
79                 UnoRuntime.queryInterface( XPropertySet.class, oObj );
80             if (oPS == null)
81                 throw new StatusException
82                     ("XPropertySet interface isn't implemented.",
83                         new NullPointerException
84                             ("XPropertySet interface isn't implemented.")) ;
85 
86             propertySetInfo = oPS.getPropertySetInfo();
87             properties = propertySetInfo.getProperties();
88             Property prop = null;
89             for (int i=0;i<properties.length;i++) {
90                 try {
91                     prop = propertySetInfo.getPropertyByName
92                         (properties[i].Name);
93                 } catch (com.sun.star.beans.UnknownPropertyException e) {
94                     log.println("Unknown Property "+prop.Name);
95                 }
96                 boolean readOnly = (prop.Attributes &
97                     PropertyAttribute.READONLY) != 0;
98                 boolean maybeDefault = (prop.Attributes &
99                     PropertyAttribute.MAYBEDEFAULT) != 0;
100                 if (!readOnly && maybeDefault) {
101                     pName = properties[i].Name;
102                     log.println("Property '" + pName + "' has attributes "+
103                          prop.Attributes);
104                     break ;
105                 } else
106                 if (!readOnly) {
107                     pName = properties[i].Name;
108                     log.println("Property '" + pName +
109                          "' is not readonly, may be used ...");
110                 } else {
111                     log.println("Skipping property '" + properties[i].Name +
112                         "' Readonly: " + readOnly + ", MaybeDefault: " +
113                              maybeDefault);
114                 }
115             }
116 
117         }
118 
119         /**
120         * Test calls the method and checks that no exceptions were thrown. <p>
121         * Has <b> OK </b> status if no exceptions were thrown. <p>
122         */
_getPropertyDefault()123         public void _getPropertyDefault(){
124             boolean result = true ;
125             String localName = pName;
126             if (localName == null) {
127                 localName = (propertySetInfo.getProperties()[0]).Name;
128             }
129             try {
130                 propDef = oObj.getPropertyDefault(localName);
131                 log.println("Default property value is : '" + propDef + "'");
132             } catch (com.sun.star.beans.UnknownPropertyException e) {
133                 log.println("Exception " + e +
134                     "occured while getting Property default");
135                 result=false;
136             } catch (com.sun.star.lang.WrappedTargetException e) {
137                 log.println("Exception " + e +
138                     "occured while getting Property default");
139                 result=false;
140             }
141             tRes.tested("getPropertyDefault()", result);
142         }
143 
144         /**
145         * Test calls the method and checks return value and that
146         * no exceptions were thrown. <p>
147         * Has <b> OK </b> status if the method returns not null value
148         * and no exceptions were thrown. <p>
149         */
_getPropertyState()150         public void _getPropertyState(){
151             boolean result = true ;
152 
153             String localName = pName;
154             if (localName == null) {
155                 localName = (propertySetInfo.getProperties()[0]).Name;
156             }
157 
158             try {
159                 PropertyState ps = oObj.getPropertyState(localName);
160                 if (ps == null) {
161                     log.println("!!! Returned value == null") ;
162                     result = false ;
163                 }
164             } catch (com.sun.star.beans.UnknownPropertyException e) {
165                 log.println("Exception " + e +
166                     "occured while getting Property state");
167                 result = false;
168             }
169             tRes.tested("getPropertyState()", result);
170         }
171 
172         /**
173         * Test calls the method with array of one property name
174         * and checks return value and that no exceptions were thrown. <p>
175         * Has <b> OK </b> status if the method returns array with one
176         * PropertyState and no exceptions were thrown. <p>
177         */
_getPropertyStates()178         public void _getPropertyStates(){
179             boolean result = true ;
180 
181             String localName = pName;
182             if (localName == null) {
183                 localName = (propertySetInfo.getProperties()[0]).Name;
184             }
185 
186             try {
187                 PropertyState[] ps = oObj.getPropertyStates
188                     (new String[] {localName});
189                 if (ps == null) {
190                     log.println("!!! Returned value == null") ;
191                     result = false ;
192                 } else {
193                     if (ps.length != 1) {
194                         log.println("!!! Array lebgth returned is invalid - " +
195                              ps.length) ;
196                         result = false ;
197                     }
198                 }
199             } catch (com.sun.star.beans.UnknownPropertyException e) {
200                 log.println("Exception " + e +
201                     "occured while getting Property state");
202                 result = false;
203             }
204 
205             tRes.tested("getPropertyStates()", result);
206         }
207 
208 
209         /**
210         * Sets the property to default, then compares the current property
211         * value to value received by method <code>getPropertyDefault</code>.
212         * Has <b> OK </b> status if the current proeprty value equals to
213         * default property. <p>
214         * The following method tests are to be completed successfully before :
215         * <ul>
216         *  <li> <code>getPropertyDefault</code>: we have to know what is
217         * default value</li></ul>
218         */
_setPropertyToDefault()219         public void _setPropertyToDefault(){
220             requiredMethod("getPropertyDefault()") ;
221 
222             if (pName == null) {
223                 log.println("all found properties are read only");
224                 tRes.tested("setPropertyToDefault()",Status.skipped(true));
225                 return;
226             }
227 
228             boolean result = true ;
229             try {
230                 try {
231                     oObj.setPropertyToDefault(pName);
232                 }
233                 catch(RuntimeException e) {
234                     System.out.println("Ignoring RuntimeException: " + e.getMessage());
235                 }
236                 if ((properties[0].Attributes &
237                         PropertyAttribute.MAYBEDEFAULT) != 0) {
238                     Object actualDef = propDef ;
239                     if (propDef instanceof Any) {
240                         actualDef = ((Any)propDef).getObject() ;
241                     }
242                     Object actualVal = oPS.getPropertyValue(pName) ;
243                     if (actualVal instanceof Any) {
244                         actualVal = ((Any)actualVal).getObject() ;
245                     }
246                     result = util.ValueComparer.equalValue
247                         (actualDef,actualVal) ;
248                     log.println("Default value = '" + actualDef +
249                          "', returned value = '"
250                         + actualVal + "' for property " + pName) ;
251                 }
252             } catch (com.sun.star.beans.UnknownPropertyException e) {
253                 log.println("Exception " + e +
254                     "occured while setting Property to default");
255                 result=false;
256             } catch (com.sun.star.lang.WrappedTargetException e) {
257                 log.println("Exception " + e +
258                     "occured while testing property value");
259                 result=false;
260             }
261 
262             tRes.tested("setPropertyToDefault()", result);
263         }
264 
after()265         public void after() {
266             disposeEnvironment() ;
267         }
268 
269  }// EOF _XPropertyState
270 
271