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