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