1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir package lib;
28*cdf0e10cSrcweir 
29*cdf0e10cSrcweir import com.sun.star.beans.Property;
30*cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute;
31*cdf0e10cSrcweir import com.sun.star.beans.PropertyVetoException;
32*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
33*cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo;
34*cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException;
35*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
36*cdf0e10cSrcweir import com.sun.star.lang.IllegalArgumentException;
37*cdf0e10cSrcweir import com.sun.star.lang.WrappedTargetException;
38*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir import java.lang.reflect.Method;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir import util.ValueChanger;
43*cdf0e10cSrcweir import util.ValueComparer;
44*cdf0e10cSrcweir import util.utils;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir import com.sun.star.uno.Any;
47*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
48*cdf0e10cSrcweir import com.sun.star.uno.Type;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir /**
51*cdf0e10cSrcweir  * MultiPropertyTest extends the functionality of MultiMethodTest to support
52*cdf0e10cSrcweir  * services testing. Since, in most cases, service tests has one method testing
53*cdf0e10cSrcweir  * most of its properties, the MultiPropertyTest provides unified version of
54*cdf0e10cSrcweir  * the method: testProperty().
55*cdf0e10cSrcweir  *
56*cdf0e10cSrcweir  * <p>The testProperty() is called, when the MultiMethodTest's testing method
57*cdf0e10cSrcweir  * is not found in the subclass. So, by defining such methods for properties
58*cdf0e10cSrcweir  * the standard testing behavioutr can be changed.
59*cdf0e10cSrcweir  *
60*cdf0e10cSrcweir  * <p>The testing behaviour also can be changed by overriding compare(),
61*cdf0e10cSrcweir  * getNewVAlue() or toString(Object) methods, or by extending PropertyTester
62*cdf0e10cSrcweir  * class.
63*cdf0e10cSrcweir  *
64*cdf0e10cSrcweir  * @see MultiMethodTest
65*cdf0e10cSrcweir  * @see #testProperty(String)
66*cdf0e10cSrcweir  * @see #testProperty(String, Propertytester)
67*cdf0e10cSrcweir  * @see #getNewValue
68*cdf0e10cSrcweir  * @see #compare
69*cdf0e10cSrcweir  * @see #toString(Object)
70*cdf0e10cSrcweir  */
71*cdf0e10cSrcweir public class MultiPropertyTest extends MultiMethodTest
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     /**
75*cdf0e10cSrcweir      * Contains a XPropertySet interface of the tested object. Is initialized
76*cdf0e10cSrcweir      * in MultiMethodTest code.
77*cdf0e10cSrcweir      */
78*cdf0e10cSrcweir     public XPropertySet oObj;
79*cdf0e10cSrcweir     protected boolean optionalService = false;
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     /**
82*cdf0e10cSrcweir      * Overrides super.before() to check the service is supported by the object.
83*cdf0e10cSrcweir      */
84*cdf0e10cSrcweir     protected void before()
85*cdf0e10cSrcweir     {
86*cdf0e10cSrcweir         XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
87*cdf0e10cSrcweir                 XServiceInfo.class, oObj);
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         optionalService = entry.isOptional;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         String theService = getTestedClassName();
92*cdf0e10cSrcweir         if (xInfo != null && !xInfo.supportsService(theService))
93*cdf0e10cSrcweir         {
94*cdf0e10cSrcweir             log.println("Service " + theService + " not available");
95*cdf0e10cSrcweir             if (optionalService)
96*cdf0e10cSrcweir             {
97*cdf0e10cSrcweir                 log.println("This is OK since it is optional");
98*cdf0e10cSrcweir             }
99*cdf0e10cSrcweir             else
100*cdf0e10cSrcweir             {
101*cdf0e10cSrcweir                 Status.failed(theService + " is not supported");
102*cdf0e10cSrcweir             }
103*cdf0e10cSrcweir         }
104*cdf0e10cSrcweir     }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir     /**
107*cdf0e10cSrcweir      * Overrides MultiMethodTest.invokeTestMethod(). If the test for the
108*cdf0e10cSrcweir      * <code>meth</code> is not available (<code>meth</code> == <tt>null</tt>)
109*cdf0e10cSrcweir      * calls testProperty method for the method. Otherwise calls
110*cdf0e10cSrcweir      * super.invokeTestMethod().
111*cdf0e10cSrcweir      *
112*cdf0e10cSrcweir      * @see #MultiMethodTest.invokeTestMethod()
113*cdf0e10cSrcweir      */
114*cdf0e10cSrcweir     protected void invokeTestMethod(Method meth, String methName)
115*cdf0e10cSrcweir     {
116*cdf0e10cSrcweir         if (meth != null)
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             super.invokeTestMethod(meth, methName);
119*cdf0e10cSrcweir         }
120*cdf0e10cSrcweir         else
121*cdf0e10cSrcweir         {
122*cdf0e10cSrcweir             testProperty(methName);
123*cdf0e10cSrcweir         }
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     /**
127*cdf0e10cSrcweir      * PropertyTester class defines how to test a property and defined
128*cdf0e10cSrcweir      * to allow subclasses of MultiPropertyTest to change the testing
129*cdf0e10cSrcweir      * behaviour more flexible, since the behaviour can be customized for
130*cdf0e10cSrcweir      * each property separately, by providing subclass of PropertyTester
131*cdf0e10cSrcweir      * and passing it to testProperty(String, PropertyTester method).
132*cdf0e10cSrcweir      */
133*cdf0e10cSrcweir     public class PropertyTester
134*cdf0e10cSrcweir     {
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir         /**
137*cdf0e10cSrcweir          * The method defines the whole process of testing propName
138*cdf0e10cSrcweir          * property.
139*cdf0e10cSrcweir          *
140*cdf0e10cSrcweir          * <p>First, it checks if the property exists(it maybe optional).
141*cdf0e10cSrcweir          * Then, a value to set the property with is calculated with
142*cdf0e10cSrcweir          * getNewValue method. Normally, the new value is calculated
143*cdf0e10cSrcweir          * based on old value, but subclasses can override the behaviour
144*cdf0e10cSrcweir          * (for example, if old value is null) and specify their own value.
145*cdf0e10cSrcweir          * Then the property is set with that new value and the result(
146*cdf0e10cSrcweir          * it maybe an exception too, for example a PropertyVetoException)
147*cdf0e10cSrcweir          * is checked with checkResult method.
148*cdf0e10cSrcweir          *
149*cdf0e10cSrcweir          * @param propName - the property to test.
150*cdf0e10cSrcweir          * @result - adds the result of testing propName property to
151*cdf0e10cSrcweir          *           MultiMethodTest.tRes.
152*cdf0e10cSrcweir          */
153*cdf0e10cSrcweir         protected void testProperty(String propName)
154*cdf0e10cSrcweir         {
155*cdf0e10cSrcweir             XPropertySetInfo info = oObj.getPropertySetInfo();
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir             if (info != null)
158*cdf0e10cSrcweir             {
159*cdf0e10cSrcweir                 final boolean bHasProperty = info.hasPropertyByName(propName);
160*cdf0e10cSrcweir                 if (!bHasProperty)
161*cdf0e10cSrcweir                 {
162*cdf0e10cSrcweir                     if (isOptional(propName) || optionalService)
163*cdf0e10cSrcweir                     {
164*cdf0e10cSrcweir                         // skipping optional property test
165*cdf0e10cSrcweir                         log.println("Property '" + propName + "' is optional and not supported");
166*cdf0e10cSrcweir                         tRes.tested(propName, true);
167*cdf0e10cSrcweir                         return;
168*cdf0e10cSrcweir                     }
169*cdf0e10cSrcweir                     else
170*cdf0e10cSrcweir                     {
171*cdf0e10cSrcweir                         // cannot test the property
172*cdf0e10cSrcweir                         log.println("Tested XPropertySet does not contain'" + propName + "' property");
173*cdf0e10cSrcweir                         tRes.tested(propName, false);
174*cdf0e10cSrcweir                         return;
175*cdf0e10cSrcweir                     }
176*cdf0e10cSrcweir                 }
177*cdf0e10cSrcweir             }
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir             try
180*cdf0e10cSrcweir             {
181*cdf0e10cSrcweir                 Object oldValue = oObj.getPropertyValue(propName);
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir                 if( (oldValue==null) || utils.isVoid(oldValue) )
184*cdf0e10cSrcweir                 {
185*cdf0e10cSrcweir                     // #i111560# method getNewValue() does not work with an empty oldValue
186*cdf0e10cSrcweir                     Property prop = info.getPropertyByName(propName);
187*cdf0e10cSrcweir                     if( (prop.Attributes & PropertyAttribute.MAYBEVOID) != 0 )
188*cdf0e10cSrcweir                     {
189*cdf0e10cSrcweir                         // todo: implement a new test independent from method getNewValue()
190*cdf0e10cSrcweir                         log.println("changing initially empty MAYBEVOID properties is not supported by the test framework so far - skip test of property: " + propName);
191*cdf0e10cSrcweir                         tRes.tested(propName, true);
192*cdf0e10cSrcweir                         return;
193*cdf0e10cSrcweir                     }
194*cdf0e10cSrcweir                     else
195*cdf0e10cSrcweir                     {
196*cdf0e10cSrcweir                         log.println( "property '"+propName+"' is not set but is not MAYBEVOID");
197*cdf0e10cSrcweir                         tRes.tested(propName, false);
198*cdf0e10cSrcweir                         return;
199*cdf0e10cSrcweir                     }
200*cdf0e10cSrcweir                 }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir                 Object newValue;
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir                 // trying to create new value
205*cdf0e10cSrcweir                 try
206*cdf0e10cSrcweir                 {
207*cdf0e10cSrcweir                     newValue = getNewValue(propName, oldValue);
208*cdf0e10cSrcweir                 }
209*cdf0e10cSrcweir                 catch (java.lang.IllegalArgumentException e)
210*cdf0e10cSrcweir                 {
211*cdf0e10cSrcweir                     // skipping test since new value is not available
212*cdf0e10cSrcweir                     Status.failed("Cannot create new value for '" + propName + " : " + e.getMessage());
213*cdf0e10cSrcweir                     return;
214*cdf0e10cSrcweir                 }
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir                 // for an exception thrown during setting new value
217*cdf0e10cSrcweir                 // to pass it to checkResult method
218*cdf0e10cSrcweir                 Exception exception = null;
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir                 try
221*cdf0e10cSrcweir                 {
222*cdf0e10cSrcweir                     log.println("try to set:");
223*cdf0e10cSrcweir                     log.println("old = " + toString(oldValue));
224*cdf0e10cSrcweir                     log.println("new = " + toString(newValue));
225*cdf0e10cSrcweir                     oObj.setPropertyValue(propName, newValue);
226*cdf0e10cSrcweir                 }
227*cdf0e10cSrcweir                 catch (IllegalArgumentException e)
228*cdf0e10cSrcweir                 {
229*cdf0e10cSrcweir                     exception = e;
230*cdf0e10cSrcweir                 }
231*cdf0e10cSrcweir                 catch (PropertyVetoException e)
232*cdf0e10cSrcweir                 {
233*cdf0e10cSrcweir                     exception = e;
234*cdf0e10cSrcweir                 }
235*cdf0e10cSrcweir                 catch (WrappedTargetException e)
236*cdf0e10cSrcweir                 {
237*cdf0e10cSrcweir                     exception = e;
238*cdf0e10cSrcweir                 }
239*cdf0e10cSrcweir                 catch (UnknownPropertyException e)
240*cdf0e10cSrcweir                 {
241*cdf0e10cSrcweir                     exception = e;
242*cdf0e10cSrcweir                 }
243*cdf0e10cSrcweir                 catch (RuntimeException e)
244*cdf0e10cSrcweir                 {
245*cdf0e10cSrcweir                     exception = e;
246*cdf0e10cSrcweir                 }
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir                 // getting result value
249*cdf0e10cSrcweir                 Object resValue = oObj.getPropertyValue(propName);
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir                 // checking results
252*cdf0e10cSrcweir                 checkResult(propName, oldValue, newValue, resValue, exception);
253*cdf0e10cSrcweir             }
254*cdf0e10cSrcweir             catch (Exception e)
255*cdf0e10cSrcweir             {
256*cdf0e10cSrcweir                 log.println("Exception occured while testing property '" + propName + "'");
257*cdf0e10cSrcweir                 e.printStackTrace(log);
258*cdf0e10cSrcweir                 tRes.tested(propName, false);
259*cdf0e10cSrcweir             }
260*cdf0e10cSrcweir         }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir         /**
263*cdf0e10cSrcweir          * The method checks result of setting a new value to the
264*cdf0e10cSrcweir          * property based o the following arguments:
265*cdf0e10cSrcweir          *   @propName - the property to test
266*cdf0e10cSrcweir          *   @oldValue - the old value of the property, before changing it.
267*cdf0e10cSrcweir          *   @newValue - the new value the property has been set with
268*cdf0e10cSrcweir          *   @resValue - the value of the property after having changed it
269*cdf0e10cSrcweir          *   @exception - if not null - the exception thrown by
270*cdf0e10cSrcweir          *                 XPropertySet.setPropertyValue, else indicates
271*cdf0e10cSrcweir          *                 normal method completion.
272*cdf0e10cSrcweir          *
273*cdf0e10cSrcweir          * <p>If the property is READ_ONLY, than either PropertyVetoException
274*cdf0e10cSrcweir          * should be thrown or the value of property should not have changed
275*cdf0e10cSrcweir          * (resValue is compared with oldValue with compare method).
276*cdf0e10cSrcweir          *
277*cdf0e10cSrcweir          * <p>If the property is not READ_ONLY, checks that the new value has
278*cdf0e10cSrcweir          * been successfully set(resValue is compared with newValue with
279*cdf0e10cSrcweir          * compare method).
280*cdf0e10cSrcweir          *
281*cdf0e10cSrcweir          * <p>If the exception is not null then(except the case of read-only
282*cdf0e10cSrcweir          * property and PropertyVetoException above) it is rethrown to allow
283*cdf0e10cSrcweir          * further catching it if needed.
284*cdf0e10cSrcweir          *
285*cdf0e10cSrcweir          * <p>Subclasses can override to change this behaviour.
286*cdf0e10cSrcweir          */
287*cdf0e10cSrcweir         protected void checkResult(String propName, Object oldValue,
288*cdf0e10cSrcweir                 Object newValue, Object resValue, Exception exception)
289*cdf0e10cSrcweir                 throws Exception
290*cdf0e10cSrcweir         {
291*cdf0e10cSrcweir             XPropertySetInfo info = oObj.getPropertySetInfo();
292*cdf0e10cSrcweir             if (info == null)
293*cdf0e10cSrcweir             {
294*cdf0e10cSrcweir                 log.println("Can't get XPropertySetInfo for property " + propName);
295*cdf0e10cSrcweir                 tRes.tested(propName, false);
296*cdf0e10cSrcweir                 return;
297*cdf0e10cSrcweir             }
298*cdf0e10cSrcweir             Property prop = info.getPropertyByName(propName);
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir             short attr = prop.Attributes;
301*cdf0e10cSrcweir             boolean readOnly = (prop.Attributes & PropertyAttribute.READONLY) != 0;
302*cdf0e10cSrcweir             boolean maybeVoid = (prop.Attributes & PropertyAttribute.MAYBEVOID) != 0;
303*cdf0e10cSrcweir             //check get-set methods
304*cdf0e10cSrcweir             if (maybeVoid)
305*cdf0e10cSrcweir             {
306*cdf0e10cSrcweir                 log.println("Property " + propName + " is void");
307*cdf0e10cSrcweir             }
308*cdf0e10cSrcweir             if (readOnly)
309*cdf0e10cSrcweir             {
310*cdf0e10cSrcweir                 log.println("Property " + propName + " is readOnly");
311*cdf0e10cSrcweir             }
312*cdf0e10cSrcweir             if (util.utils.isVoid(oldValue) && !maybeVoid)
313*cdf0e10cSrcweir             {
314*cdf0e10cSrcweir                 log.println(propName + " is void, but it's not MAYBEVOID");
315*cdf0e10cSrcweir                 tRes.tested(propName, false);
316*cdf0e10cSrcweir             }
317*cdf0e10cSrcweir             else if (oldValue == null)
318*cdf0e10cSrcweir             {
319*cdf0e10cSrcweir                 log.println(propName + " has null value, and therefore can't be changed");
320*cdf0e10cSrcweir                 tRes.tested(propName, true);
321*cdf0e10cSrcweir             }
322*cdf0e10cSrcweir             else if (readOnly)
323*cdf0e10cSrcweir             {
324*cdf0e10cSrcweir                 // check if exception was thrown
325*cdf0e10cSrcweir                 if (exception != null)
326*cdf0e10cSrcweir                 {
327*cdf0e10cSrcweir                     if (exception instanceof PropertyVetoException)
328*cdf0e10cSrcweir                     {
329*cdf0e10cSrcweir                         // the change of read only prohibited - OK
330*cdf0e10cSrcweir                         log.println("Property is ReadOnly and wasn't changed");
331*cdf0e10cSrcweir                         log.println("Property '" + propName + "' OK");
332*cdf0e10cSrcweir                         tRes.tested(propName, true);
333*cdf0e10cSrcweir                     }
334*cdf0e10cSrcweir                     else if (exception instanceof IllegalArgumentException)
335*cdf0e10cSrcweir                     {
336*cdf0e10cSrcweir                         // the change of read only prohibited - OK
337*cdf0e10cSrcweir                         log.println("Property is ReadOnly and wasn't changed");
338*cdf0e10cSrcweir                         log.println("Property '" + propName + "' OK");
339*cdf0e10cSrcweir                         tRes.tested(propName, true);
340*cdf0e10cSrcweir                     }
341*cdf0e10cSrcweir                     else if (exception instanceof UnknownPropertyException)
342*cdf0e10cSrcweir                     {
343*cdf0e10cSrcweir                         // the change of read only prohibited - OK
344*cdf0e10cSrcweir                         log.println("Property is ReadOnly and wasn't changed");
345*cdf0e10cSrcweir                         log.println("Property '" + propName + "' OK");
346*cdf0e10cSrcweir                         tRes.tested(propName, true);
347*cdf0e10cSrcweir                     }
348*cdf0e10cSrcweir                     else if (exception instanceof RuntimeException)
349*cdf0e10cSrcweir                     {
350*cdf0e10cSrcweir                         // the change of read only prohibited - OK
351*cdf0e10cSrcweir                         log.println("Property is ReadOnly and wasn't changed");
352*cdf0e10cSrcweir                         log.println("Property '" + propName + "' OK");
353*cdf0e10cSrcweir                         tRes.tested(propName, true);
354*cdf0e10cSrcweir                     }
355*cdf0e10cSrcweir                     else
356*cdf0e10cSrcweir                     {
357*cdf0e10cSrcweir                         throw exception;
358*cdf0e10cSrcweir                     }
359*cdf0e10cSrcweir                 }
360*cdf0e10cSrcweir                 else
361*cdf0e10cSrcweir                 {
362*cdf0e10cSrcweir                     // if no exception - check that value
363*cdf0e10cSrcweir                     // has not changed
364*cdf0e10cSrcweir                     if (!compare(resValue, oldValue))
365*cdf0e10cSrcweir                     {
366*cdf0e10cSrcweir                         log.println("Read only property '" + propName + "' has changed");
367*cdf0e10cSrcweir                         try
368*cdf0e10cSrcweir                         {
369*cdf0e10cSrcweir                             if (!util.utils.isVoid(oldValue) && oldValue instanceof Any)
370*cdf0e10cSrcweir                             {
371*cdf0e10cSrcweir                                 oldValue = AnyConverter.toObject(new Type(((Any) oldValue).getClass()), oldValue);
372*cdf0e10cSrcweir                             }
373*cdf0e10cSrcweir //                            log.println("old = " + toString(oldValue));
374*cdf0e10cSrcweir //                            log.println("new = " + toString(newValue));
375*cdf0e10cSrcweir                             log.println("result = " + toString(resValue));
376*cdf0e10cSrcweir                         }
377*cdf0e10cSrcweir                         catch (com.sun.star.lang.IllegalArgumentException iae)
378*cdf0e10cSrcweir                         {
379*cdf0e10cSrcweir                             log.println("NOTIFY: this property needs further investigations.");
380*cdf0e10cSrcweir                             log.println("\t The type seems to be an Any with value of NULL.");
381*cdf0e10cSrcweir                             log.println("\t Maybe the property should get it's own test method.");
382*cdf0e10cSrcweir                         }
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir                         tRes.tested(propName, false);
385*cdf0e10cSrcweir                     }
386*cdf0e10cSrcweir                     else
387*cdf0e10cSrcweir                     {
388*cdf0e10cSrcweir                         log.println("Read only property '" + propName + "' hasn't changed");
389*cdf0e10cSrcweir                         log.println("Property '" + propName + "' OK");
390*cdf0e10cSrcweir                         tRes.tested(propName, true);
391*cdf0e10cSrcweir                     }
392*cdf0e10cSrcweir                 }
393*cdf0e10cSrcweir             }
394*cdf0e10cSrcweir             else
395*cdf0e10cSrcweir             {
396*cdf0e10cSrcweir                 if (exception == null)
397*cdf0e10cSrcweir                 {
398*cdf0e10cSrcweir                     // if no exception thrown
399*cdf0e10cSrcweir                     // check that the new value is set
400*cdf0e10cSrcweir                     if ((!compare(resValue, newValue)) || (compare(resValue, oldValue)))
401*cdf0e10cSrcweir                     {
402*cdf0e10cSrcweir                         log.println("Value for '" + propName + "' hasn't changed as expected");
403*cdf0e10cSrcweir                         try
404*cdf0e10cSrcweir                         {
405*cdf0e10cSrcweir                             if (!util.utils.isVoid(oldValue) && oldValue instanceof Any)
406*cdf0e10cSrcweir                             {
407*cdf0e10cSrcweir                                 oldValue = AnyConverter.toObject(new Type(((Any) oldValue).getClass()), oldValue);
408*cdf0e10cSrcweir                             }
409*cdf0e10cSrcweir //                            log.println("old = " + toString(oldValue));
410*cdf0e10cSrcweir //                            log.println("new = " + toString(newValue));
411*cdf0e10cSrcweir                             log.println("result = " + toString(resValue));
412*cdf0e10cSrcweir                         }
413*cdf0e10cSrcweir                         catch (com.sun.star.lang.IllegalArgumentException iae)
414*cdf0e10cSrcweir                         {
415*cdf0e10cSrcweir                             log.println("NOTIFY: this property needs further investigations.");
416*cdf0e10cSrcweir                             log.println("\t The type seems to be an Any with value of NULL.");
417*cdf0e10cSrcweir                             log.println("\t Maybe the property should get it's own test method.");
418*cdf0e10cSrcweir                         }
419*cdf0e10cSrcweir                         if (resValue != null)
420*cdf0e10cSrcweir                         {
421*cdf0e10cSrcweir                             if ((!compare(resValue, oldValue)) || (!resValue.equals(oldValue)))
422*cdf0e10cSrcweir                             {
423*cdf0e10cSrcweir                                 log.println("But it has changed.");
424*cdf0e10cSrcweir                                 tRes.tested(propName, true);
425*cdf0e10cSrcweir                             }
426*cdf0e10cSrcweir                             else
427*cdf0e10cSrcweir                             {
428*cdf0e10cSrcweir                                 tRes.tested(propName, false);
429*cdf0e10cSrcweir                             }
430*cdf0e10cSrcweir                         }
431*cdf0e10cSrcweir                         else
432*cdf0e10cSrcweir                         {
433*cdf0e10cSrcweir                             tRes.tested(propName, false);
434*cdf0e10cSrcweir                         }
435*cdf0e10cSrcweir                         //tRes.tested(propName, false);
436*cdf0e10cSrcweir                     }
437*cdf0e10cSrcweir                     else
438*cdf0e10cSrcweir                     {
439*cdf0e10cSrcweir                         log.println("Property '" + propName + "' OK");
440*cdf0e10cSrcweir                         try
441*cdf0e10cSrcweir                         {
442*cdf0e10cSrcweir                             if (!util.utils.isVoid(oldValue) && oldValue instanceof Any)
443*cdf0e10cSrcweir                             {
444*cdf0e10cSrcweir                                 oldValue = AnyConverter.toObject(new Type(((Any) oldValue).getClass()), oldValue);
445*cdf0e10cSrcweir                             }
446*cdf0e10cSrcweir //                            log.println("old = " + toString(oldValue));
447*cdf0e10cSrcweir //                            log.println("new = " + toString(newValue));
448*cdf0e10cSrcweir                             log.println("result = " + toString(resValue));
449*cdf0e10cSrcweir                         }
450*cdf0e10cSrcweir                         catch (com.sun.star.lang.IllegalArgumentException iae)
451*cdf0e10cSrcweir                         {
452*cdf0e10cSrcweir                         }
453*cdf0e10cSrcweir                         tRes.tested(propName, true);
454*cdf0e10cSrcweir                     }
455*cdf0e10cSrcweir                 }
456*cdf0e10cSrcweir                 else
457*cdf0e10cSrcweir                 {
458*cdf0e10cSrcweir                     throw exception;
459*cdf0e10cSrcweir                 }
460*cdf0e10cSrcweir             }
461*cdf0e10cSrcweir         }
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir         /**
464*cdf0e10cSrcweir          * The method produces new value of the property from the oldValue.
465*cdf0e10cSrcweir          * It returns the result of ValueChanger.changePValue method.
466*cdf0e10cSrcweir          * Subclasses can override the method to return their own value,
467*cdf0e10cSrcweir          * when the changePValue beahviour is not enough, for example,
468*cdf0e10cSrcweir          * when oldValue is null.
469*cdf0e10cSrcweir          */
470*cdf0e10cSrcweir         protected Object getNewValue(String propName, Object oldValue)
471*cdf0e10cSrcweir                 throws java.lang.IllegalArgumentException
472*cdf0e10cSrcweir         {
473*cdf0e10cSrcweir             return ValueChanger.changePValue(oldValue);
474*cdf0e10cSrcweir         }
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir         /**
477*cdf0e10cSrcweir          * The method compares obj1 and obj2. It calls
478*cdf0e10cSrcweir          * MultiPropertyTest.compare, but subclasses can override to change
479*cdf0e10cSrcweir          * the behaviour, since normally compare calls Object.equals method
480*cdf0e10cSrcweir          * which is not apropriate in some cases(e.g., structs with equals
481*cdf0e10cSrcweir          * not overridden).
482*cdf0e10cSrcweir          */
483*cdf0e10cSrcweir         protected boolean compare(Object obj1, Object obj2)
484*cdf0e10cSrcweir         {
485*cdf0e10cSrcweir             return callCompare(obj1, obj2);
486*cdf0e10cSrcweir         }
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir         /**
489*cdf0e10cSrcweir          * The method returns a String representation of the obj. It calls
490*cdf0e10cSrcweir          * MultipropertyTest.toString(Object), but subclasses can override
491*cdf0e10cSrcweir          * to change the behaviour.
492*cdf0e10cSrcweir          */
493*cdf0e10cSrcweir         protected String toString(Object obj)
494*cdf0e10cSrcweir         {
495*cdf0e10cSrcweir             return callToString(obj);
496*cdf0e10cSrcweir         }
497*cdf0e10cSrcweir     }
498*cdf0e10cSrcweir 
499*cdf0e10cSrcweir     /**
500*cdf0e10cSrcweir      * Extension for <code>PropertyTester</code> which switches two
501*cdf0e10cSrcweir      * different values. <code>getNewValue()</code> method of this
502*cdf0e10cSrcweir      * class returns one of these two values depending on the
503*cdf0e10cSrcweir      * old value, so new value is not equal to old value.
504*cdf0e10cSrcweir      */
505*cdf0e10cSrcweir     public class PropertyValueSwitcher extends PropertyTester
506*cdf0e10cSrcweir     {
507*cdf0e10cSrcweir 
508*cdf0e10cSrcweir         Object val1 = null;
509*cdf0e10cSrcweir         Object val2 = null;
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir         /**
512*cdf0e10cSrcweir          * Constructs a property tester with two different values
513*cdf0e10cSrcweir          * specified as parameters.
514*cdf0e10cSrcweir          *
515*cdf0e10cSrcweir          * @param val1 Not <code>null</code> value for the property
516*cdf0e10cSrcweir          * tested.
517*cdf0e10cSrcweir          * @param val1 Not <code>null</code> value for the property
518*cdf0e10cSrcweir          * tested which differs from the first value.
519*cdf0e10cSrcweir          */
520*cdf0e10cSrcweir         public PropertyValueSwitcher(Object val1, Object val2)
521*cdf0e10cSrcweir         {
522*cdf0e10cSrcweir             this.val1 = val1;
523*cdf0e10cSrcweir             this.val2 = val2;
524*cdf0e10cSrcweir         }
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir         /**
527*cdf0e10cSrcweir          * Overriden method of <code>PropertyTester</code> which
528*cdf0e10cSrcweir          * retruns new value from two values specified.
529*cdf0e10cSrcweir          *
530*cdf0e10cSrcweir          * @return The second value if old value is equal to the first
531*cdf0e10cSrcweir          * one, the first value otherwise.
532*cdf0e10cSrcweir          */
533*cdf0e10cSrcweir         protected Object getNewValue(String propName, Object old)
534*cdf0e10cSrcweir         {
535*cdf0e10cSrcweir             if (ValueComparer.equalValue(val1, old))
536*cdf0e10cSrcweir             {
537*cdf0e10cSrcweir                 return val2;
538*cdf0e10cSrcweir             }
539*cdf0e10cSrcweir             else
540*cdf0e10cSrcweir             {
541*cdf0e10cSrcweir                 return val1;
542*cdf0e10cSrcweir             }
543*cdf0e10cSrcweir         }
544*cdf0e10cSrcweir     }
545*cdf0e10cSrcweir 
546*cdf0e10cSrcweir     /**
547*cdf0e10cSrcweir      * The method performs testing of propName property using propTester.
548*cdf0e10cSrcweir      */
549*cdf0e10cSrcweir     protected void testProperty(String propName, PropertyTester propTester)
550*cdf0e10cSrcweir     {
551*cdf0e10cSrcweir         propTester.testProperty(propName);
552*cdf0e10cSrcweir     }
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir     /**
555*cdf0e10cSrcweir      * The method performs testing of propName property. It uses PropertyTester
556*cdf0e10cSrcweir      * instance for testing.
557*cdf0e10cSrcweir      */
558*cdf0e10cSrcweir     protected void testProperty(String propName)
559*cdf0e10cSrcweir     {
560*cdf0e10cSrcweir         testProperty(propName, new PropertyTester());
561*cdf0e10cSrcweir     }
562*cdf0e10cSrcweir 
563*cdf0e10cSrcweir     /**
564*cdf0e10cSrcweir      * Tests the property using <code>PropertyValueSwitcher</code>
565*cdf0e10cSrcweir      * tester and two values for this property.
566*cdf0e10cSrcweir      *
567*cdf0e10cSrcweir      * @see #PropertyValueSwitcher
568*cdf0e10cSrcweir      */
569*cdf0e10cSrcweir     protected void testProperty(String propName, Object val1, Object val2)
570*cdf0e10cSrcweir     {
571*cdf0e10cSrcweir         testProperty(propName, new PropertyValueSwitcher(val1, val2));
572*cdf0e10cSrcweir     }
573*cdf0e10cSrcweir 
574*cdf0e10cSrcweir     /**
575*cdf0e10cSrcweir      * The method just calls compare. This is a workaround to CodeWarrior's
576*cdf0e10cSrcweir      * compiler bug.
577*cdf0e10cSrcweir      */
578*cdf0e10cSrcweir     private boolean callCompare(Object obj1, Object obj2)
579*cdf0e10cSrcweir     {
580*cdf0e10cSrcweir         return compare(obj1, obj2);
581*cdf0e10cSrcweir     }
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir     /**
584*cdf0e10cSrcweir      * Compares two object. In the implementation calls obj1.equals(obj2).
585*cdf0e10cSrcweir      */
586*cdf0e10cSrcweir     protected boolean compare(Object obj1, Object obj2)
587*cdf0e10cSrcweir     {
588*cdf0e10cSrcweir         return ValueComparer.equalValue(obj1, obj2);
589*cdf0e10cSrcweir     }
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir     /**
592*cdf0e10cSrcweir      * The method just calls toString. This is a workaround to
593*cdf0e10cSrcweir      * CodeWarrior's compiler bug.
594*cdf0e10cSrcweir      */
595*cdf0e10cSrcweir     private String callToString(Object obj)
596*cdf0e10cSrcweir     {
597*cdf0e10cSrcweir         return toString(obj);
598*cdf0e10cSrcweir     }
599*cdf0e10cSrcweir 
600*cdf0e10cSrcweir     /**
601*cdf0e10cSrcweir      * Gets string representation of the obj. In the implementation
602*cdf0e10cSrcweir      * returns obj.toString().
603*cdf0e10cSrcweir      */
604*cdf0e10cSrcweir     protected String toString(Object obj)
605*cdf0e10cSrcweir     {
606*cdf0e10cSrcweir         return obj == null ? "null" : obj.toString();
607*cdf0e10cSrcweir     }
608*cdf0e10cSrcweir }
609