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 java.util.Random;
27 import java.util.Set;
28 import java.util.Vector;
29 
30 import lib.MultiMethodTest;
31 import lib.StatusException;
32 import util.ValueChanger;
33 
34 import com.sun.star.beans.Property;
35 import com.sun.star.beans.PropertyAttribute;
36 import com.sun.star.beans.XFastPropertySet;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.beans.XPropertySetInfo;
39 import com.sun.star.uno.UnoRuntime;
40 
41 /**
42 * Testing <code>com.sun.star.beans.XFastPropertySet</code>
43 * interface methods :
44 * <ul>
45 *  <li><code> setFastPropertyValue()</code></li>
46 *  <li><code> getFastPropertyValue()</code></li>
47 * </ul> <p>
48 * Required relations :
49 * <ul>
50 *  <li> <code>'XFastPropertySet.ExcludeProps'</code>
51 *    <b>(optional) </b> : java.util.Set.
52 *    Has property names which must be skipped from testing in
53 *    some reasons (for example property accepts restricted set
54 *    of values).
55 *  </li>
56 * <ul> <p>
57 * @see com.sun.star.beans.XFastPropertySet
58 */
59 public class _XFastPropertySet extends MultiMethodTest {
60 
61     public XFastPropertySet oObj = null;
62     private Vector handles = new Vector();
63     private int handle = -1;
64     private Set exclude = null ;
65 
66     /**
67      * Retrieves relation.
68      */
before()69      protected void before() {
70         exclude = (Set) tEnv.getObjRelation("XFastPropertySet.ExcludeProps") ;
71         if (exclude == null) {
72             exclude = new java.util.HashSet() ;
73         }
74      }
75 
76     /**
77     * Test selects random property which can not be VOID and
78     * is writable, then change property value using <code>
79     * get/set</code> methods, and checks if value properly changed.
80     * Has <b> OK </b> status if value after change is not equal to value
81     * before and no exceptions were thrown. <p>
82     */
_setFastPropertyValue()83     public void _setFastPropertyValue() {
84         XPropertySet PS = (XPropertySet)UnoRuntime.queryInterface
85             (XPropertySet.class, oObj);
86         XPropertySetInfo propertySetInfo = PS.getPropertySetInfo();
87 
88         if (propertySetInfo == null) {
89             log.println("getPropertySetInfo() method returned null");
90             tRes.tested("setFastPropertyValue()", false) ;
91         }
92         getPropsToTest(propertySetInfo);
93 
94         Object gValue = null;
95         Object sValue = null;
96 
97         if ( handle == -1) {
98             log.println("*** No changeable properties found ***");
99             tRes.tested("setFastPropertyValue()", false) ;
100         } else {
101             try {
102                 gValue = oObj.getFastPropertyValue(handle);
103                 sValue = ValueChanger.changePValue(gValue);
104                 oObj.setFastPropertyValue(handle, sValue);
105                 sValue = oObj.getFastPropertyValue(handle);
106             } catch (com.sun.star.beans.UnknownPropertyException e) {
107                 log.println("Exception occured while trying to change property with handle = " + handle);
108                 e.printStackTrace(log);
109             } catch (com.sun.star.lang.WrappedTargetException e) {
110                 log.println("Exception occured while trying to change property with handle = " + handle);
111                 e.printStackTrace(log);
112             } catch (com.sun.star.beans.PropertyVetoException e) {
113                 log.println("Exception occured while trying to change property with handle = " + handle);
114                 e.printStackTrace(log);
115             }  catch (com.sun.star.lang.IllegalArgumentException e) {
116                 log.println("Exception occured while trying to change property with handle = " + handle);
117                 e.printStackTrace(log);
118             }
119 
120             tRes.tested("setFastPropertyValue()",(!gValue.equals(sValue)));
121         }
122     }
123 
124     /**
125     * Test selects random property which can not be VOID and
126     * is writable, then calls the method and  checks that
127     * no exceptions were thrown. <p>
128     * Has <b> OK </b> status if exceptions were thrown. <p>
129     */
_getFastPropertyValue()130     public void _getFastPropertyValue() {
131         XPropertySet PS = (XPropertySet)UnoRuntime.queryInterface
132             (XPropertySet.class, oObj);
133         XPropertySetInfo propertySetInfo = PS.getPropertySetInfo();
134 
135         if (propertySetInfo == null) {
136             log.println("getPropertySetInfo() method returned null");
137             tRes.tested("getFastPropertyValue()", false) ;
138         }
139 
140         getPropsToTest(propertySetInfo);
141 
142         try {
143             oObj.getFastPropertyValue(handle);
144             tRes.tested("getFastPropertyValue()",true);
145         } catch (com.sun.star.beans.UnknownPropertyException e) {
146             log.println("Exception occured while trying to get property '"
147                 + handle +"'");
148             e.printStackTrace(log);
149             tRes.tested("getFastPropertyValue()",false);
150         } catch (com.sun.star.lang.WrappedTargetException e) {
151             log.println("Exception occured while trying to get property '"
152                 + handle +"'");
153             e.printStackTrace(log);
154             tRes.tested("getFastPropertyValue()",false);
155         }
156         return;
157     }
158 
159 
160     //Get the properties being tested
getPropsToTest(XPropertySetInfo xPSI)161     private void getPropsToTest(XPropertySetInfo xPSI) {
162 
163         Property[] properties = xPSI.getProperties();
164 
165         for (int i = 0; i < properties.length; i++) {
166             if (exclude.contains(properties[i].Name)) continue ;
167             Property property = properties[i];
168             String name = property.Name;
169             int handle = property.Handle;
170             log.println("Checking '" + name + "' with handle = " + handle);
171             boolean isWritable =
172                 ((property.Attributes & PropertyAttribute.READONLY) == 0);
173             boolean isNotNull =
174                 ((property.Attributes & PropertyAttribute.MAYBEVOID) == 0);
175             boolean canChange = false;
176             if ( isWritable && isNotNull )
177                 canChange = isChangeable(handle);
178             if ( isWritable && isNotNull && canChange)
179                 handles.add(new Integer(handle));
180         } // endfor
181 
182         Random rnd = new Random();
183         int nr = rnd.nextInt(handles.size());
184         handle = ((Integer)handles.elementAt(nr)).intValue();
185     }
186 
isChangeable(int handle)187     private boolean isChangeable(int handle) {
188         boolean hasChanged = false;
189         try {
190             Object getProp = oObj.getFastPropertyValue(handle);
191             Object setValue = null;
192 
193             if (getProp != null)
194                 setValue = ValueChanger.changePValue(getProp);
195             else
196                 log.println("Property with handle = " + handle
197                     + " is null but 'MAYBEVOID' isn't set");
198             if (setValue != null) {
199                 oObj.setFastPropertyValue(handle, setValue);
200                 hasChanged =
201                     (!getProp.equals(oObj.getFastPropertyValue(handle)));
202             }
203             else
204                 log.println("Couldn't change Property with handle " + handle);
205         } catch(com.sun.star.lang.WrappedTargetException e) {
206             log.println("Property with handle " + handle + " throws exception");
207             e.printStackTrace(log);
208             throw new StatusException("Property with handle " + handle
209                 + " throws exception", e);
210         } catch(com.sun.star.lang.IllegalArgumentException 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.beans.PropertyVetoException 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.UnknownPropertyException 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         }
226 
227         return hasChanged;
228     }
229 }
230 
231 
232