xref: /trunk/main/sc/qa/complex/dataPilot/_XPropertySet.java (revision ffd38472365e95f6a578737bc9a5eb0fac624a86)
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 package complex.dataPilot;
23 
24 import com.sun.star.beans.Property;
25 import com.sun.star.beans.PropertyAttribute;
26 import com.sun.star.beans.PropertyChangeEvent;
27 import com.sun.star.beans.XPropertyChangeListener;
28 import com.sun.star.beans.XPropertySet;
29 import com.sun.star.beans.XPropertySetInfo;
30 import com.sun.star.beans.XVetoableChangeListener;
31 import com.sun.star.lang.EventObject;
32 import java.util.Random;
33 import java.util.StringTokenizer;
34 import lib.TestParameters;
35 // import share.LogWriter;
36 //import lib.MultiMethodTest;
37 import util.ValueChanger;
38 import util.utils;
39 
40 /**
41 * Testing <code>com.sun.star.beans.XPropertySet</code>
42 * interface methods :
43 * <ul>
44 *  <li><code>getPropertySetInfo()</code></li>
45 *  <li><code>setPropertyValue()</code></li>
46 *  <li><code>getPropertyValue()</code></li>
47 *  <li><code>addPropertyChangeListener()</code></li>
48 *  <li><code>removePropertyChangeListener()</code></li>
49 *  <li><code>addVetoableChangeListener()</code></li>
50 *  <li><code>removeVetoableChangeListener()</code></li>
51 * </ul>
52 * @see com.sun.star.beans.XPropertySet
53 */
54 public class _XPropertySet {
55 
56     /**
57      * The object that is tested.
58      */
59     private XPropertySet oObj = null;
60 
61     /**
62      * The test parameters
63      */
64     private TestParameters param = null;
65 
66     /**
67      * The log writer
68      */
69     // private LogWriter log = null;
70 
71     /**
72     * Flag that indicates change listener was called.
73     */
74     boolean propertyChanged = false;
75 
76 
77     /**
78      * The own property change listener
79      */
80     XPropertyChangeListener PClistener = new MyChangeListener();
81 
82     /**
83     * Listener that must be called on bound property changing.
84     */
85     public class MyChangeListener implements XPropertyChangeListener {
86          /**
87          * Just set <code>propertyChanged</code> flag to true.
88          */
89          public void propertyChange(PropertyChangeEvent e) {
90             propertyChanged = true;
91          }
92          public void disposing (EventObject obj) {}
93     };
94 
95 
96     /**
97     * Flag that indicates veto listener was called.
98     */
99     boolean vetoableChanged = false;
100 
101     /**
102      * The own vetoable change listener
103      */
104     XVetoableChangeListener VClistener = new MyVetoListener();
105 
106     /**
107     * Listener that must be called on constrained property changing.
108     */
109     public class MyVetoListener implements XVetoableChangeListener {
110          /**
111          * Just set <code>vetoableChanged</code> flag to true.
112          */
113          public void vetoableChange(PropertyChangeEvent e) {
114             vetoableChanged = true;
115          }
116          public void disposing (EventObject obj) {}
117     };
118 
119 
120     /**
121      * Properties to test
122      */
123     PropsToTest PTT = new PropsToTest();
124 
125     /**
126     * Structure that collects three properties of each type to test :
127     * Constrained, Bound and Normal.
128     */
129     public class PropsToTest {
130            String constrained = null;
131            String bound = null;
132            String normal = null;
133     }
134 
135     /**
136      * Constructor: gets the object to test, a logger and the test parameters
137      * @param xObj The test object
138      * @param log A log writer
139      * @param param The test parameters
140      */
141     public _XPropertySet(XPropertySet xObj/*, LogWriter log*/, TestParameters param) {
142         oObj = xObj;
143         // this.log = log;
144         this.param = param;
145     }
146 
147     /**
148     * Tests method <code>getPropertySetInfo</code>. After test completed
149     * call {@link #getPropsToTest} method to retrieve different kinds
150     * of properties to test then. <p>
151     * Has OK status if not null <code>XPropertySetInfo</code>
152     * object returned.<p>
153     * Since <code>getPropertySetInfo</code> is optional, it may return null,
154     * if it is not implemented. This method uses then an object relation
155     * <code>PTT</code> (Properties To Test) to determine available properties.
156     * All tests for services without <code>getPropertySetInfo</code> must
157     * provide this object relation.
158     */
159     public boolean _getPropertySetInfo() {
160         XPropertySetInfo propertySetInfo = oObj.getPropertySetInfo();
161 
162         if (propertySetInfo == null) {
163             System.out.println("getPropertySetInfo() method returned null");
164             String[] ptt = (String[]) param.get("PTT");
165             PTT.normal=ptt[0];
166             PTT.bound=ptt[1];
167             PTT.constrained=ptt[2];
168         } else {
169             getPropsToTest(propertySetInfo);
170         }
171 
172         return true;
173 
174     } // end of getPropertySetInfo()
175 
176     /**
177     * Tests change listener which added for bound properties.
178     * Adds listener to bound property (if it exists), then changes
179     * its value and check if listener was called. <p>
180     * Method tests to be successfully completed before :
181     * <ul>
182     *  <li> <code>getPropertySetInfo</code> : in this method test
183     *    one of bound properties is retrieved. </li>
184     * </ul> <p>
185     * Has OK status if NO bound properties exist or if listener
186     * was successfully called.
187     */
188     public boolean _addPropertyChangeListener() {
189 
190         propertyChanged = false;
191         boolean result = true;
192 
193         if ( PTT.bound.equals("none") ) {
194             System.out.println("*** No bound properties found ***");
195         } else {
196             try {
197                 oObj.addPropertyChangeListener(PTT.bound,PClistener);
198                 Object gValue = oObj.getPropertyValue(PTT.bound);
199                 oObj.setPropertyValue(PTT.bound,
200                     ValueChanger.changePValue(gValue));
201             } catch (com.sun.star.beans.PropertyVetoException e) {
202                 System.out.println("Exception occurred while trying to change "+
203                     "property '"+ PTT.bound+"'");
204                 e.printStackTrace();
205             } catch (com.sun.star.lang.IllegalArgumentException e) {
206                 System.out.println("Exception occurred while trying to change "+
207                     "property '"+ PTT.bound+"'");
208                 e.printStackTrace();
209             } catch (com.sun.star.beans.UnknownPropertyException e) {
210                 System.out.println("Exception occurred while trying to change "+
211                     "property '"+ PTT.bound+"'");
212                 e.printStackTrace();
213             } catch (com.sun.star.lang.WrappedTargetException e) {
214                 System.out.println("Exception occurred while trying to change "+
215                     "property '"+ PTT.bound+"'");
216                 e.printStackTrace();
217             } // end of try-catch
218             result = propertyChanged;
219             if (!propertyChanged) {
220                 System.out.println("propertyChangeListener wasn't called for '"+
221                     PTT.bound+"'");
222             }
223         } //endif
224 
225         return result;
226 
227     } // end of addPropertyChangeListener()
228 
229     /**
230     * Tests vetoable listener which added for constrained properties.
231     * Adds listener to constrained property (if it exists), then changes
232     * its value and check if listener was called. <p>
233     * Method tests to be successfully completed before :
234     * <ul>
235     *  <li> <code>getPropertySetInfo</code> : in this method test
236     *    one of constrained properties is retrieved. </li>
237     * </ul> <p>
238     * Has OK status if NO constrained properties exist or if listener
239     * was successfully called.
240     */
241     public boolean _addVetoableChangeListener() {
242 
243 //        requiredMethod("getPropertySetInfo()");
244 
245         vetoableChanged = false;
246         boolean result = true;
247 
248         if ( PTT.constrained.equals("none") ) {
249             System.out.println("*** No constrained properties found ***");
250         } else {
251             try {
252                 oObj.addVetoableChangeListener(PTT.constrained,VClistener);
253                 Object gValue = oObj.getPropertyValue(PTT.constrained);
254                 oObj.setPropertyValue(PTT.constrained,
255                     ValueChanger.changePValue(gValue));
256             } catch (com.sun.star.beans.PropertyVetoException e) {
257                 System.out.println("Exception occurred while trying to change "+
258                     "property '"+ PTT.constrained+"'");
259                 e.printStackTrace();
260             } catch (com.sun.star.lang.IllegalArgumentException e) {
261                 System.out.println("Exception occurred while trying to change "+
262                     "property '"+ PTT.constrained+"'");
263                 e.printStackTrace();
264             } catch (com.sun.star.beans.UnknownPropertyException e) {
265                 System.out.println("Exception occurred while trying to change "+
266                     "property '"+ PTT.constrained+"'");
267                 e.printStackTrace();
268             } catch (com.sun.star.lang.WrappedTargetException e) {
269                 System.out.println("Exception occurred while trying to change "+
270                     "property '"+ PTT.constrained+"'");
271                 e.printStackTrace();
272             } // end of try-catch
273             result = vetoableChanged;
274             if (!vetoableChanged) {
275                 System.out.println("vetoableChangeListener wasn't called for '"+
276                     PTT.constrained+"'");
277             }
278         } //endif
279 
280         return result;
281 
282     } // end of addVetoableChangeListener()
283 
284 
285     /**
286     * Tests <code>setPropertyValue</code> method.
287     * Stores value before call, and compares it with value after
288     * call. <p>
289     * Method tests to be successfully completed before :
290     * <ul>
291     *  <li> <code>getPropertySetInfo</code> : in this method test
292     *    one of normal properties is retrieved. </li>
293     * </ul> <p>
294     * Has OK status if NO normal properties exist or if value before
295     * method call is not equal to value after.
296     */
297     public boolean _setPropertyValue() {
298 
299 //        requiredMethod("getPropertySetInfo()");
300 
301         Object gValue = null;
302         Object sValue = null;
303 
304         boolean result = true;
305 
306         if ( PTT.normal.equals("none") ) {
307             System.out.println("*** No changeable properties found ***");
308         } else {
309             try {
310                 gValue = oObj.getPropertyValue(PTT.normal);
311                 sValue = ValueChanger.changePValue(gValue);
312                 oObj.setPropertyValue(PTT.normal, sValue);
313                 sValue = oObj.getPropertyValue(PTT.normal);
314             } catch (com.sun.star.beans.PropertyVetoException e) {
315                 System.out.println("Exception occurred while trying to change "+
316                     "property '"+ PTT.normal+"'");
317                 e.printStackTrace();
318             } catch (com.sun.star.lang.IllegalArgumentException e) {
319                 System.out.println("Exception occurred while trying to change "+
320                     "property '"+ PTT.normal+"'");
321                 e.printStackTrace();
322             } catch (com.sun.star.beans.UnknownPropertyException e) {
323                 System.out.println("Exception occurred while trying to change "+
324                     "property '"+ PTT.normal+"'");
325                 e.printStackTrace();
326             } catch (com.sun.star.lang.WrappedTargetException e) {
327                 System.out.println("Exception occurred while trying to change "+
328                     "property '"+ PTT.normal+"'");
329                 e.printStackTrace();
330             } // end of try-catch
331             result = !gValue.equals(sValue);
332         } //endif
333 
334         return result;
335 
336     } // end of setPropertyValue()
337 
338     /**
339     * Tests <code>getPropertyValue</code> method.
340     * Just call this method and checks for no exceptions <p>
341     * Method tests to be successfully completed before :
342     * <ul>
343     *  <li> <code>getPropertySetInfo</code> : in this method test
344     *    one of normal properties is retrieved. </li>
345     * </ul> <p>
346     * Has OK status if NO normal properties exist or if no
347     * exceptions were thrown.
348     */
349     public boolean _getPropertyValue() {
350 
351 //        requiredMethod("getPropertySetInfo()");
352 
353         boolean result = true;
354         String toCheck = PTT.normal;
355 
356         if ( PTT.normal.equals("none") ) {
357             toCheck = oObj.getPropertySetInfo().getProperties()[0].Name;
358             System.out.println("All properties are Read Only");
359             System.out.println("Using: "+toCheck);
360         }
361 
362         try {
363             Object gValue = oObj.getPropertyValue(toCheck);
364         } catch (com.sun.star.beans.UnknownPropertyException e) {
365             System.out.println("Exception occurred while trying to get property '"+
366                  PTT.normal+"'");
367             e.printStackTrace();
368             result = false;
369         } catch (com.sun.star.lang.WrappedTargetException e) {
370             System.out.println("Exception occurred while trying to get property '"+
371                 PTT.normal+"'");
372             e.printStackTrace();
373             result = false;
374         } // end of try-catch
375 
376         return result;
377     }
378 
379     /**
380     * Tests <code>removePropertyChangeListener</code> method.
381     * Removes change listener, then changes bound property value
382     * and checks if the listener was NOT called.
383     * Method tests to be successfully completed before :
384     * <ul>
385     *  <li> <code>addPropertyChangeListener</code> : here listener
386     *   was added. </li>
387     * </ul> <p>
388     * Has OK status if NO bound properties exist or if listener
389     * was not called and no exceptions arose.
390     */
391     public boolean _removePropertyChangeListener() {
392 
393 //        requiredMethod("addPropertyChangeListener()");
394 
395         propertyChanged = false;
396         boolean result = true;
397 
398         if ( PTT.bound.equals("none") ) {
399             System.out.println("*** No bound properties found ***");
400         } else {
401             try {
402                 propertyChanged = false;
403                 oObj.removePropertyChangeListener(PTT.bound,PClistener);
404                 Object gValue = oObj.getPropertyValue(PTT.bound);
405                 oObj.setPropertyValue(PTT.bound,
406                     ValueChanger.changePValue(gValue));
407             } catch (com.sun.star.beans.PropertyVetoException e) {
408                 System.out.println("Exception occurred while trying to change "+
409                     "property '"+ PTT.bound+"'");
410                 e.printStackTrace();
411             } catch (com.sun.star.lang.IllegalArgumentException e) {
412                 System.out.println("Exception occurred while trying to change "+
413                     "property '"+ PTT.bound+"'");
414                 e.printStackTrace();
415             } catch (com.sun.star.beans.UnknownPropertyException e) {
416                 System.out.println("Exception occurred while trying to change "+
417                     "property '"+ PTT.bound+"'");
418                 e.printStackTrace();
419             } catch (com.sun.star.lang.WrappedTargetException e) {
420                 System.out.println("Exception occurred while trying to change "+
421                     "property '"+ PTT.bound+"'");
422                 e.printStackTrace();
423             } // end of try-catch
424 
425              result = !propertyChanged;
426             if (propertyChanged) {
427                 System.out.println("propertyChangeListener was called after removing"+
428                     " for '"+PTT.bound+"'");
429             }
430         } //endif
431 
432         return result;
433 
434     } // end of removePropertyChangeListener()
435 
436 
437     /**
438     * Tests <code>removeVetoableChangeListener</code> method.
439     * Removes vetoable listener, then changes constrained property value
440     * and checks if the listener was NOT called.
441     * Method tests to be successfully completed before :
442     * <ul>
443     *  <li> <code>addPropertyChangeListener</code> : here vetoable listener
444     *   was added. </li>
445     * </ul> <p>
446     * Has OK status if NO constrained properties exist or if listener
447     * was NOT called and no exceptions arose.
448     */
449     public boolean _removeVetoableChangeListener() {
450 
451 //        requiredMethod("addVetoableChangeListener()");
452 
453         vetoableChanged = false;
454         boolean result = true;
455 
456         if ( PTT.constrained.equals("none") ) {
457             System.out.println("*** No constrained properties found ***");
458         } else {
459             try {
460                 oObj.removeVetoableChangeListener(PTT.constrained,VClistener);
461                 Object gValue = oObj.getPropertyValue(PTT.constrained);
462                 oObj.setPropertyValue(PTT.constrained,
463                     ValueChanger.changePValue(gValue));
464             } catch (com.sun.star.beans.PropertyVetoException e) {
465                 System.out.println("Exception occurred while trying to change "+
466                     "property '"+ PTT.constrained+"'");
467                 e.printStackTrace();
468             } catch (com.sun.star.lang.IllegalArgumentException e) {
469                 System.out.println("Exception occurred while trying to change "+
470                     "property '"+ PTT.constrained+"'");
471                 e.printStackTrace();
472             } catch (com.sun.star.beans.UnknownPropertyException e) {
473                 System.out.println("Exception occurred while trying to change "+
474                     "property '"+ PTT.constrained+"'");
475                 e.printStackTrace();
476             } catch (com.sun.star.lang.WrappedTargetException e) {
477                 System.out.println("Exception occurred while trying to change "+
478                     "property '"+ PTT.constrained+"'");
479                 e.printStackTrace();
480             } // end of try-catch
481 
482             result = !vetoableChanged;
483             if (vetoableChanged) {
484                 System.out.println("vetoableChangeListener was called after "+
485                     "removing for '"+PTT.constrained+"'");
486             }
487         } //endif
488 
489         return result;
490 
491     } // end of removeVetoableChangeListener()
492 
493 
494     /**
495     * Gets the properties being tested. Searches and stores by one
496     * property of each kind (Bound, Vetoable, Normal).
497     */
498     public PropsToTest getPropsToTest(XPropertySetInfo xPSI) {
499 
500         Property[] properties = xPSI.getProperties();
501         String bound = "";
502         String constrained = "";
503         String normal = "";
504 
505         for (int i = 0; i < properties.length; i++) {
506 
507             Property property = properties[i];
508             String name = property.Name;
509             System.out.println("Checking '"+name+"'");
510             boolean isWritable = ((property.Attributes &
511                 PropertyAttribute.READONLY) == 0);
512             boolean isNotNull = ((property.Attributes &
513                 PropertyAttribute.MAYBEVOID) == 0);
514             boolean isBound = ((property.Attributes &
515                 PropertyAttribute.BOUND) != 0);
516             boolean isConstr = ((property.Attributes &
517                 PropertyAttribute.CONSTRAINED) != 0);
518             boolean canChange = false;
519 
520             if ( !isWritable ) System.out.println("Property '"+name+"' is READONLY");
521 
522             if (name.endsWith("URL")) isWritable = false;
523             if (name.startsWith("Fill")) isWritable = false;
524             if (name.startsWith("Font")) isWritable = false;
525             if (name.startsWith("IsNumbering")) isWritable = false;
526             if (name.startsWith("LayerName")) isWritable = false;
527             if (name.startsWith("Line")) isWritable = false;
528 
529             //if (name.equals("xinterfaceA") || name.equals("xtypeproviderA")
530             //|| name.equals("arAnyA")) isWritable=false;
531 
532             if ( isWritable && isNotNull ) canChange = isChangeable(name);
533 
534             if ( isWritable && isNotNull && isBound && canChange) {
535                 bound+=name+";";
536             }
537 
538             if ( isWritable && isNotNull && isConstr && canChange) {
539                 constrained+=name+";";
540             }
541 
542             if ( isWritable && isNotNull && canChange) normal+=name+";";
543 
544 
545         } // endfor
546 
547         //get a random bound property
548         PTT.bound=getRandomString(bound);
549         System.out.println("Bound: "+PTT.bound);
550 
551         //get a random constrained property
552         PTT.constrained=getRandomString(constrained);
553         System.out.println("Constrained: "+PTT.constrained);
554 
555         //get a random normal property
556         PTT.normal=getRandomString(normal);
557 
558         return PTT;
559 
560     }
561 
562     /**
563     * Retrieves one random property name from list (property names separated
564     * by ';') of property names.
565     */
566     public String getRandomString(String str) {
567 
568         String gRS = "none";
569         Random rnd = new Random();
570 
571         if (str.equals("")) str = "none";
572         StringTokenizer ST=new StringTokenizer(str,";");
573         int nr = rnd.nextInt(ST.countTokens());
574         if (nr < 1) nr+=1;
575         for (int i=1; i<nr+1; i++) gRS = ST.nextToken();
576 
577         return gRS;
578 
579     }
580 
581     public boolean isChangeable(String name) {
582         boolean hasChanged = false;
583         try {
584             Object getProp = oObj.getPropertyValue(name);
585             System.out.println("Getting: "+getProp);
586 
587             Object setValue = null;
588             if (getProp != null) {
589                 if (!utils.isVoid(getProp))
590                     setValue = ValueChanger.changePValue(getProp);
591                 else System.out.println("Property '"+name+
592                     "' is void but MAYBEVOID isn't set");
593             } else System.out.println("Property '"+name+"'is null and can't be changed");
594             if (name.equals("LineStyle")) setValue = null;
595             if (setValue != null) {
596                 oObj.setPropertyValue(name, setValue);
597                 System.out.println("Setting to :"+setValue);
598                 hasChanged = (! getProp.equals(oObj.getPropertyValue(name)));
599             } else System.out.println("Couldn't change Property '"+name+"'");
600         } catch (com.sun.star.beans.PropertyVetoException e) {
601             System.out.println("'" + name + "' throws exception '" + e + "'");
602             e.printStackTrace();
603         } catch (com.sun.star.lang.IllegalArgumentException e) {
604             System.out.println("'" + name + "' throws exception '" + e + "'");
605             e.printStackTrace();
606         } catch (com.sun.star.beans.UnknownPropertyException e) {
607             System.out.println("'" + name + "' throws exception '" + e + "'");
608             e.printStackTrace();
609         } catch (com.sun.star.lang.WrappedTargetException e) {
610             System.out.println("'" + name + "' throws exception '" + e + "'");
611             e.printStackTrace();
612         } catch (com.sun.star.uno.RuntimeException e) {
613             System.out.println("'" + name + "' throws exception '" + e + "'");
614             e.printStackTrace();
615         } catch (java.lang.ArrayIndexOutOfBoundsException e) {
616             System.out.println("'" + name + "' throws exception '" + e + "'");
617             e.printStackTrace();
618         }
619 
620         return hasChanged;
621     }
622 
623 
624 }  // finish class _XPropertySet
625