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.io.PrintWriter;
27 import java.util.HashSet;
28 import java.util.Set;
29 import java.util.StringTokenizer;
30 
31 import lib.MultiMethodTest;
32 import lib.Status;
33 import util.ValueChanger;
34 
35 import com.sun.star.beans.Property;
36 import com.sun.star.beans.PropertyAttribute;
37 import com.sun.star.beans.PropertyChangeEvent;
38 import com.sun.star.beans.XMultiPropertySet;
39 import com.sun.star.beans.XPropertiesChangeListener;
40 import com.sun.star.beans.XPropertySetInfo;
41 import com.sun.star.lang.EventObject;
42 
43 
44 /**
45 * Testing <code>com.sun.star.beans.XMultiPropertySet</code>
46 * interface methods :
47 * <ul>
48 *  <li><code> getPropertySetInfo()</code></li>
49 *  <li><code> setPropertyValues()</code></li>
50 *  <li><code> getPropertyValues()</code></li>
51 *  <li><code> addPropertiesChangeListener()</code></li>
52 *  <li><code> removePropertiesChangeListener()</code></li>
53 *  <li><code> firePropertiesChangeEvent()</code></li>
54 * </ul> <p>
55 *
56 * Required relations :
57 * <ul>
58 *  <li> <code>'XMultiPropertySet.ExcludeProps'</code>
59 *    <b>(optional) </b> : java.util.Set.
60 *    Has property names which must be skipped from testing in
61 *    some reasons (for example property accepts restricted set
62 *    of values).
63 *  </li>
64 * <ul> <p>
65 *
66 * Test is <b> NOT </b> multithread compilant. <p>
67 * After test completion object environment has to be recreated.
68 * @see com.sun.star.beans.XMultiPropertySet
69 */
70 public class _XMultiPropertySet extends MultiMethodTest {
71 
72     public XMultiPropertySet oObj = null;
73 
74     private boolean propertiesChanged = false;
75     private XPropertySetInfo propertySetInfo = null;
76     private String [] testPropsNames = null;
77     private int testPropsAmount = 0;
78     private PrintWriter _log = null;
79 
80     private Object[] values = null;
81 
82     private Set exclProps = null;
83 
84     /**
85     * Initializes some fields.
86     */
before()87     public void before() {
88         _log = log;
89 
90         exclProps = (Set) tEnv.getObjRelation("XMultiPropertySet.ExcludeProps");
91         if (exclProps == null) exclProps = new HashSet(0);
92     }
93 
94     /**
95     * Listener implementation which sets a flag when
96     * listener was called.
97     */
98     public class MyChangeListener implements XPropertiesChangeListener {
propertiesChange(PropertyChangeEvent[] e)99          public void propertiesChange(PropertyChangeEvent[] e) {
100              //_log.println("Listener was called");
101              propertiesChanged = true;
102          }
disposing(EventObject obj)103          public void disposing (EventObject obj) {}
104     }
105 
106     private XPropertiesChangeListener PClistener =
107         new MyChangeListener();
108 
109     /**
110     * Test calls the method and checks return value.
111     * <code>PropertySetInfo</code> object is stored<p>
112     * Has <b> OK </b> status if the method returns not null value
113     * and no exceptions were thrown. <p>
114     */
_getPropertySetInfo()115     public void _getPropertySetInfo() {
116         boolean bResult = true;
117         propertySetInfo = oObj.getPropertySetInfo();
118 
119         if (propertySetInfo == null) {
120             log.println("getPropertySetInfo() method returned null");
121             bResult = false;
122         }
123 
124         tRes.tested("getPropertySetInfo()", bResult) ;
125     }
126 
127 
128     /**
129     * Test collects all property names and retrieves their values,
130     * then checks the value returned. Finally it also collects
131     * bound properties for other methods tests.<p>
132     * Has <b> OK </b> status if the method  returns non null value
133     * and no exceptions were thrown. <p>
134     * The following method tests are to be completed successfully before :
135     * <ul>
136     *  <li> <code> getPropertySetInfo() </code> : to have a list
137     *   of properties.</li>
138     * </ul>
139     */
_getPropertyValues()140     public void _getPropertyValues() {
141         requiredMethod("getPropertySetInfo()");
142         boolean bResult = true;
143 
144         Property[] properties = propertySetInfo.getProperties();
145         String[] allnames = new String[properties.length];
146         for (int i = 0; i < properties.length; i++) {
147             allnames[i] = properties[i].Name;
148         }
149 
150         values = oObj.getPropertyValues(allnames);
151 
152         bResult &= values!=null;
153         tRes.tested("getPropertyValues()", bResult) ;
154 
155         getPropsToTest(properties);
156     }
157 
158     /**
159     * Test adds listener for all bound properties then each property
160     * is changed and listener call . <p>
161     * Has <b> OK </b> status if on each property change the listener was
162     * called and no exceptions were thrown. <p>
163     * The following method tests are to be completed successfully before :
164     * <ul>
165     *  <li> <code> getPropertyValues() </code> : to collect bound
166     *   properties.</li>
167     * </ul>
168     */
_addPropertiesChangeListener()169     public void _addPropertiesChangeListener() {
170 
171         requiredMethod("getPropertyValues()");
172 
173         boolean result  = true ;
174         // Creating listener
175         oObj.addPropertiesChangeListener(testPropsNames, PClistener);
176 
177         if ((testPropsAmount==1) && (testPropsNames[0].equals("none"))) {
178             testPropsAmount = 0;
179         }
180 
181 
182         // Change one of the property to be sure, that this event was cauched.
183         //Random rnd = new Random();
184         //int idx = rnd.nextInt(testPropsAmount);
185         for (int i=0; i<testPropsAmount;i++) {
186             log.print("Trying to change property " + testPropsNames[i]);
187             try {
188                 Object[] gValues = oObj.getPropertyValues(testPropsNames);
189                 Object newValue = ValueChanger.changePValue(gValues[i]);
190                 gValues[i] = newValue;
191                 propertiesChanged = false;
192                 oObj.setPropertyValues(testPropsNames, gValues);
193                 waitAMoment() ;
194                 result &= propertiesChanged ;
195                 log.println(" ... done");
196             } catch (com.sun.star.beans.PropertyVetoException e) {
197                 log.println("Exception occurred while trying to change "+
198                     "property '"+testPropsNames[i] + "' :" + e);
199                 e.printStackTrace(log);
200             } catch (com.sun.star.lang.IllegalArgumentException e) {
201                 log.println("Exception occurred while trying to change "+
202                     "property '"+testPropsNames[i] + "' :" + e);
203                 e.printStackTrace(log);
204             } catch (com.sun.star.lang.WrappedTargetException e) {
205                 log.println("Exception occurred while trying to change "+
206                     "property '"+testPropsNames[i] + "' :" + e);
207                 e.printStackTrace(log);
208             } // end of try-catch
209         }
210         if (testPropsAmount == 0) {
211             log.println("all properties are read only");
212             tRes.tested("addPropertiesChangeListener()", Status.skipped(true));
213         } else {
214             tRes.tested("addPropertiesChangeListener()", propertiesChanged);
215         }
216     }
217 
218     /**
219     * Calls method and check if listener was called. <p>
220     * Has <b> OK </b> status if the listener was
221     * called and no exceptions were thrown. <p>
222     * The following method tests are to be completed successfully before :
223     * <ul>
224     *  <li> <code> addPropertiesChangeListener() </code> : listener to
225     *    be added.</li>
226     * </ul>
227     */
_firePropertiesChangeEvent()228     public void _firePropertiesChangeEvent() {
229         requiredMethod("addPropertiesChangeListener()");
230         propertiesChanged = false ;
231 
232         oObj.firePropertiesChangeEvent(testPropsNames, PClistener);
233         waitAMoment() ;
234 
235         tRes.tested("firePropertiesChangeEvent()", propertiesChanged);
236     }
237 
238 
239     /**
240     * Removes listener added before. <p>
241     * Has <b> OK </b> status no exceptions were thrown. <p>
242     * The following method tests are to be completed successfully before :
243     * <ul>
244     *  <li> <code> addPropertiesChangeListener() </code> : listener to
245     *    be added.</li>
246     * </ul>
247     */
_removePropertiesChangeListener()248     public void _removePropertiesChangeListener() {
249         requiredMethod("firePropertiesChangeEvent()");
250         boolean bResult = true;
251 
252         oObj.removePropertiesChangeListener(PClistener);
253 
254         tRes.tested("removePropertiesChangeListener()", bResult);
255     }
256 
257 
258     /**
259     * Changes all properties, then set them to new values, get them
260     * and checks if their values were changed properly. <p>
261     * Has <b> OK </b> status if all properties properly changed
262     * and no exceptions were thrown. <p>
263     * The following method tests are to be completed successfully before :
264     * <ul>
265     *  <li> <code> getPropertyValues() </code> : to collect bound
266     *   properties.</li>
267     * </ul>
268     */
_setPropertyValues()269     public void _setPropertyValues() {
270         requiredMethod("getPropertyValues()");
271         boolean bResult = true;
272 
273         if ((testPropsNames.length==1)&&(testPropsNames[0].equals("none"))) {
274             log.println("all properties are readOnly");
275             tRes.tested("setPropertyValues()",Status.skipped(true));
276             return;
277         }
278 
279         log.println("Changing all properties");
280         Object[] gValues = oObj.getPropertyValues(testPropsNames);
281         for (int i=0; i<testPropsAmount;i++) {
282             Object oldValue = gValues[i];
283             Object newValue = ValueChanger.changePValue(oldValue);
284             gValues[i] = newValue;
285         }
286 
287         try {
288             oObj.setPropertyValues(testPropsNames, gValues);
289             Object[] newValues = oObj.getPropertyValues(testPropsNames);
290             for (int i=0; i<testPropsAmount;i++) {
291                 if (newValues[i].equals(gValues[i])) {
292                     bResult = true;
293                 }
294             }
295         } catch (com.sun.star.beans.PropertyVetoException e) {
296             log.println("Exception occurred while setting properties");
297             e.printStackTrace(log);
298             bResult = false;
299         } catch (com.sun.star.lang.IllegalArgumentException e) {
300             log.println("Exception occurred while setting properties");
301             e.printStackTrace(log);
302             bResult = false;
303         } catch (com.sun.star.lang.WrappedTargetException e) {
304             log.println("Exception occurred while setting properties");
305             e.printStackTrace(log);
306             bResult = false;
307         } // end of try-catch
308 
309         tRes.tested("setPropertyValues()", bResult);
310     }
311 
312     //Get the properties being tested
getPropsToTest(Property[] properties)313     private void getPropsToTest(Property[] properties) {
314 
315         String bound = "";
316 
317         for (int i = 0; i < properties.length; i++) {
318 
319             Property property = properties[i];
320             String name = property.Name;
321             boolean isWritable = ((property.Attributes &
322                 PropertyAttribute.READONLY) == 0);
323             boolean isNotNull = ((property.Attributes &
324                 PropertyAttribute.MAYBEVOID) == 0);
325             boolean isBound = ((property.Attributes &
326                 PropertyAttribute.BOUND) != 0);
327             boolean isExcluded = exclProps.contains(name);
328 
329             //exclude UserDefined, because we can't change XNameContainer
330             if (name.indexOf("UserDefined")>0 || name.indexOf("Device")>0) {
331                 isWritable=false;
332             }
333 
334             values = oObj.getPropertyValues(new String[]{property.Name});
335 
336             boolean isVoid = util.utils.isVoid(values[0]);
337 
338             if ( isWritable && isNotNull && isBound && !isExcluded && !isVoid) {
339                 bound+=name+";";
340             }
341 
342         } // endfor
343 
344         //get a array of bound properties
345         if (bound.equals("")) bound = "none";
346         StringTokenizer ST=new StringTokenizer(bound,";");
347         int nr = ST.countTokens();
348         testPropsNames = new String[nr];
349         for (int i=0; i<nr; i++) testPropsNames[i] = ST.nextToken();
350         testPropsAmount = nr;
351         return;
352 
353     }
354 
355     /**
356     * Waits some time for listener to be called.
357     */
waitAMoment()358     private void waitAMoment() {
359         try {
360             Thread.sleep(200) ;
361         } catch (java.lang.InterruptedException e) {
362             log.println("!!! Exception while waiting !!!") ;
363         }
364     }
365 
366     /*
367     * Does nothing.
368     */
after()369     protected void after() {
370         disposeEnvironment();
371     }
372 }
373 
374 
375