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