1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.beans;
29 
30 import java.util.Random;
31 import java.util.Set;
32 import java.util.Vector;
33 
34 import lib.MultiMethodTest;
35 import lib.StatusException;
36 import util.ValueChanger;
37 
38 import com.sun.star.beans.Property;
39 import com.sun.star.beans.PropertyAttribute;
40 import com.sun.star.beans.XFastPropertySet;
41 import com.sun.star.beans.XPropertySet;
42 import com.sun.star.beans.XPropertySetInfo;
43 import com.sun.star.uno.UnoRuntime;
44 
45 /**
46 * Testing <code>com.sun.star.beans.XFastPropertySet</code>
47 * interface methods :
48 * <ul>
49 *  <li><code> setFastPropertyValue()</code></li>
50 *  <li><code> getFastPropertyValue()</code></li>
51 * </ul> <p>
52 * Required relations :
53 * <ul>
54 *  <li> <code>'XFastPropertySet.ExcludeProps'</code>
55 *    <b>(optional) </b> : java.util.Set.
56 *    Has property names which must be skipped from testing in
57 *    some reasons (for example property accepts restricted set
58 *    of values).
59 *  </li>
60 * <ul> <p>
61 * @see com.sun.star.beans.XFastPropertySet
62 */
63 public class _XFastPropertySet extends MultiMethodTest {
64 
65     public XFastPropertySet oObj = null;
66     private Vector handles = new Vector();
67     private int handle = -1;
68     private Set exclude = null ;
69 
70     /**
71      * Retrieves relation.
72      */
73      protected void before() {
74         exclude = (Set) tEnv.getObjRelation("XFastPropertySet.ExcludeProps") ;
75         if (exclude == null) {
76             exclude = new java.util.HashSet() ;
77         }
78      }
79 
80     /**
81     * Test selects random property which can not be VOID and
82     * is writable, then change property value using <code>
83     * get/set</code> methods, and checks if value properly changed.
84     * Has <b> OK </b> status if value after change is not equal to value
85     * before and no exceptions were thrown. <p>
86     */
87     public void _setFastPropertyValue() {
88         XPropertySet PS = (XPropertySet)UnoRuntime.queryInterface
89             (XPropertySet.class, oObj);
90         XPropertySetInfo propertySetInfo = PS.getPropertySetInfo();
91 
92         if (propertySetInfo == null) {
93             log.println("getPropertySetInfo() method returned null");
94             tRes.tested("setFastPropertyValue()", false) ;
95         }
96         getPropsToTest(propertySetInfo);
97 
98         Object gValue = null;
99         Object sValue = null;
100 
101         if ( handle == -1) {
102             log.println("*** No changeable properties found ***");
103             tRes.tested("setFastPropertyValue()", false) ;
104         } else {
105             try {
106                 gValue = oObj.getFastPropertyValue(handle);
107                 sValue = ValueChanger.changePValue(gValue);
108                 oObj.setFastPropertyValue(handle, sValue);
109                 sValue = oObj.getFastPropertyValue(handle);
110             } catch (com.sun.star.beans.UnknownPropertyException e) {
111                 log.println("Exception occured while trying to change property with handle = " + handle);
112                 e.printStackTrace(log);
113             } catch (com.sun.star.lang.WrappedTargetException e) {
114                 log.println("Exception occured while trying to change property with handle = " + handle);
115                 e.printStackTrace(log);
116             } catch (com.sun.star.beans.PropertyVetoException e) {
117                 log.println("Exception occured while trying to change property with handle = " + handle);
118                 e.printStackTrace(log);
119             }  catch (com.sun.star.lang.IllegalArgumentException e) {
120                 log.println("Exception occured while trying to change property with handle = " + handle);
121                 e.printStackTrace(log);
122             }
123 
124             tRes.tested("setFastPropertyValue()",(!gValue.equals(sValue)));
125         }
126     }
127 
128     /**
129     * Test selects random property which can not be VOID and
130     * is writable, then calls the method and  checks that
131     * no exceptions were thrown. <p>
132     * Has <b> OK </b> status if exceptions were thrown. <p>
133     */
134     public void _getFastPropertyValue() {
135         XPropertySet PS = (XPropertySet)UnoRuntime.queryInterface
136             (XPropertySet.class, oObj);
137         XPropertySetInfo propertySetInfo = PS.getPropertySetInfo();
138 
139         if (propertySetInfo == null) {
140             log.println("getPropertySetInfo() method returned null");
141             tRes.tested("getFastPropertyValue()", false) ;
142         }
143 
144         getPropsToTest(propertySetInfo);
145 
146         try {
147             oObj.getFastPropertyValue(handle);
148             tRes.tested("getFastPropertyValue()",true);
149         } catch (com.sun.star.beans.UnknownPropertyException e) {
150             log.println("Exception occured while trying to get property '"
151                 + handle +"'");
152             e.printStackTrace(log);
153             tRes.tested("getFastPropertyValue()",false);
154         } catch (com.sun.star.lang.WrappedTargetException e) {
155             log.println("Exception occured while trying to get property '"
156                 + handle +"'");
157             e.printStackTrace(log);
158             tRes.tested("getFastPropertyValue()",false);
159         }
160         return;
161     }
162 
163 
164     //Get the properties being tested
165     private void getPropsToTest(XPropertySetInfo xPSI) {
166 
167         Property[] properties = xPSI.getProperties();
168 
169         for (int i = 0; i < properties.length; i++) {
170             if (exclude.contains(properties[i].Name)) continue ;
171             Property property = properties[i];
172             String name = property.Name;
173             int handle = property.Handle;
174             log.println("Checking '" + name + "' with handle = " + handle);
175             boolean isWritable =
176                 ((property.Attributes & PropertyAttribute.READONLY) == 0);
177             boolean isNotNull =
178                 ((property.Attributes & PropertyAttribute.MAYBEVOID) == 0);
179             boolean canChange = false;
180             if ( isWritable && isNotNull )
181                 canChange = isChangeable(handle);
182             if ( isWritable && isNotNull && canChange)
183                 handles.add(new Integer(handle));
184         } // endfor
185 
186         Random rnd = new Random();
187         int nr = rnd.nextInt(handles.size());
188         handle = ((Integer)handles.elementAt(nr)).intValue();
189     }
190 
191     private boolean isChangeable(int handle) {
192         boolean hasChanged = false;
193         try {
194             Object getProp = oObj.getFastPropertyValue(handle);
195             Object setValue = null;
196 
197             if (getProp != null)
198                 setValue = ValueChanger.changePValue(getProp);
199             else
200                 log.println("Property with handle = " + handle
201                     + " is null but 'MAYBEVOID' isn't set");
202             if (setValue != null) {
203                 oObj.setFastPropertyValue(handle, setValue);
204                 hasChanged =
205                     (!getProp.equals(oObj.getFastPropertyValue(handle)));
206             }
207             else
208                 log.println("Couldn't change Property with handle " + handle);
209         } catch(com.sun.star.lang.WrappedTargetException e) {
210             log.println("Property with handle " + handle + " throws exception");
211             e.printStackTrace(log);
212             throw new StatusException("Property with handle " + handle
213                 + " throws exception", e);
214         } catch(com.sun.star.lang.IllegalArgumentException e) {
215             log.println("Property with handle " + handle + " throws exception");
216             e.printStackTrace(log);
217             throw new StatusException("Property with handle " + handle
218                 + " throws exception", e);
219         } catch(com.sun.star.beans.PropertyVetoException e) {
220             log.println("Property with handle " + handle + " throws exception");
221             e.printStackTrace(log);
222             throw new StatusException("Property with handle " + handle
223                 + " throws exception", e);
224         } catch(com.sun.star.beans.UnknownPropertyException e) {
225             log.println("Property with handle " + handle + " throws exception");
226             e.printStackTrace(log);
227             throw new StatusException("Property with handle " + handle
228                 + " throws exception", e);
229         }
230 
231         return hasChanged;
232     }
233 }
234 
235 
236