1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PathSettingsTest.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package complex
.path_settings
;
32 import com
.sun
.star
.beans
.Property
;
33 import com
.sun
.star
.beans
.XFastPropertySet
;
34 import com
.sun
.star
.beans
.XMultiPropertySet
;
35 import com
.sun
.star
.beans
.XPropertySet
;
36 import com
.sun
.star
.beans
.XPropertiesChangeListener
;
37 import com
.sun
.star
.beans
.XPropertyChangeListener
;
38 import com
.sun
.star
.beans
.XVetoableChangeListener
;
39 import com
.sun
.star
.lang
.XMultiServiceFactory
;
40 import com
.sun
.star
.uno
.UnoRuntime
;
41 import com
.sun
.star
.uno
.AnyConverter
;
42 import complexlib
.ComplexTestCase
;
44 public class PathSettingsTest
extends ComplexTestCase
{
46 private static XMultiServiceFactory xMSF
;
48 // the test object: an instance of the tested service
49 private static Object oObj
= null;
50 // the properties of the service
51 private static Property
[] props
= null;
52 private static String
[] propNames
= null;
53 private static String
[] availablePropNames
= new String
[]{
77 private static String
[] propVals
= null;
80 * A function to tell the framework, which test functions are available.
81 * Right now, it's only 'checkComplexTemplateState'.
82 * @return All test methods.
84 public String
[] getTestMethodNames() {
85 return new String
[]{"checkXFastPropertySet",
86 "checkXMultiPropertySet",
92 * Initialize before the tests start: this has to be done only once.
93 * This methods sets the 'oObj' and 'props' variables.
95 public void before() {
97 xMSF
= (XMultiServiceFactory
)param
.getMSF();
98 // oObj = xMSF.createInstance("com.sun.star.util.PathSettings");
99 oObj
= xMSF
.createInstance("com.sun.star.comp.framework.PathSettings");
100 System
.out
.println("Implementation: " + util
.utils
.getImplName(oObj
));
101 System
.out
.println("Service: ");
102 util
.dbg
.getSuppServices(oObj
);
103 if (oObj
== null) throw new com
.sun
.star
.uno
.Exception();
104 XPropertySet xProp
= (XPropertySet
)
105 UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
107 props
= xProp
.getPropertySetInfo().getProperties();
108 propNames
= new String
[props
.length
];
109 propVals
= new String
[props
.length
];
111 // get intitial values and create new ones
112 log
.println("\n---- All properties ----");
113 for (int i
= 1; i
< props
.length
; i
++) {
114 propNames
[i
] = props
[i
].Name
;
115 Object o
= xProp
.getPropertyValue(propNames
[i
]);
116 System
.out
.println("#### Object: " + o
.getClass().getName() + " - " + o
.toString());
117 propVals
[i
] = AnyConverter
.toString(o
);
118 System
.out
.println("#### String " + propVals
[i
]);
119 log
.println("Property Name: " + propNames
[i
]);
120 log
.println("Property Value: " + propVals
[i
]);
122 log
.println("---- Finish showing properties ----\n");
124 catch(com
.sun
.star
.uno
.Exception e
) {
126 log
.println(e
.getClass().getName());
127 log
.println("Message: " + e
.getMessage());
128 failed("Could not create an instance of the test object.");
132 failed("What exception?");
137 * This tests the XFastPropertySet interface implementation.
139 public void checkXFastPropertySet()
141 log
.println("---- Testing the XFastPropertySet interface ----");
143 // creating instances
144 XFastPropertySet xFPS
= (XFastPropertySet
)
145 UnoRuntime
.queryInterface(XFastPropertySet
.class, oObj
);
148 // do for all properties
149 for (int i
= 0; i
< props
.length
; i
++) {
151 Property property
= props
[i
];
152 name
= property
.Name
;
153 int handle
= property
.Handle
;
155 // get property name and initial value
156 log
.println("Test property with name: " + name
);
157 String val
= (String
)xFPS
.getFastPropertyValue(handle
);
158 log
.println("Property has initial value: '" + val
+ "'");
160 // set to a new correct value
161 String newVal
= changeToCorrectValue(val
);
162 log
.println("Try to change to correct value '" + newVal
+ "'");
163 xFPS
.setFastPropertyValue(handle
, newVal
);
166 String checkVal
= (String
)xFPS
.getFastPropertyValue(handle
);
167 assure("Did not change value on property " + name
+ ".", checkVal
.equals(newVal
));
169 newVal
= changeToIncorrectValue(val
);
170 log
.println("Try to change to incorrect value '" + newVal
+ "'");
172 xFPS
.setFastPropertyValue(handle
, newVal
);
174 catch(com
.sun
.star
.lang
.IllegalArgumentException e
) {
175 log
.println("Correctly thrown Exception caught.");
179 checkVal
= (String
)xFPS
.getFastPropertyValue(handle
);
180 assure("Value did change on property " + name
+ " though it should not have.",
181 !checkVal
.equals(newVal
));
183 // set back to initial setting
184 xFPS
.setFastPropertyValue(handle
, val
);
187 checkVal
= (String
)xFPS
.getFastPropertyValue(handle
);
188 assure("Did not change value back to original on property "
189 + name
, checkVal
.equals(val
));
190 log
.println("Test of property " + name
+ " finished\n");
192 catch(com
.sun
.star
.uno
.Exception e
) {
193 // e.printStackTrace();
194 log
.println(e
.getClass().getName());
195 log
.println("Message: " + e
.getMessage());
196 failed("Unexpected exception on property " + name
+ ".");
200 log
.println("---- Test of XFastPropertySet finished ----\n");
204 // ____________________
206 * This tests the XMultiPropertySet interface implementation.
208 public void checkXMultiPropertySet()
210 log
.println("---- Testing the XMultiPropertySet interface ----");
211 XMultiPropertySet xMPS
= (XMultiPropertySet
)
212 UnoRuntime
.queryInterface(XMultiPropertySet
.class, oObj
);
214 String
[] correctVals
= new String
[props
.length
];
215 String
[] incorrectVals
= new String
[props
.length
];
217 // get intitial values and create new ones
218 for (int i
= 0; i
< props
.length
; i
++) {
219 correctVals
[i
] = changeToCorrectValue(propVals
[i
]);
220 incorrectVals
[i
] = changeToIncorrectValue(propVals
[i
]);
224 // add a change listener
225 MyChangeListener mListener
= new MyChangeListener();
226 xMPS
.addPropertiesChangeListener(propNames
, mListener
);
228 // first change props to correct values
229 log
.println("Change to correct values.");
230 xMPS
.setPropertyValues(propNames
, correctVals
);
231 assure("Could not change to correct values with XMultiPropoertySet.",
232 verifyPropertySet(xMPS
,propNames
,correctVals
)>0);
234 // second, change to incorrect values: expect an exception
235 log
.println("Try to change to incorrect values.");
237 xMPS
.setPropertyValues(propNames
, incorrectVals
);
239 catch(com
.sun
.star
.lang
.IllegalArgumentException r
) {
240 log
.println("Correctly thrown Exception caught.");
242 assure("Did change to incorrect values with XMultiPropertySet," +
243 " but should not have.",
244 verifyPropertySet(xMPS
,propNames
,correctVals
)>0);
246 // third, change back to initial values
247 log
.println("Change back to initial values.");
248 xMPS
.setPropertyValues(propNames
, propVals
);
249 assure("Could not change back to initial values with" +
250 " XMultiPropertySet.",
251 verifyPropertySet(xMPS
,propNames
,propVals
)>0);
253 // fire the event for the listener
254 log
.println("Fire event.");
255 xMPS
.firePropertiesChangeEvent(propNames
, mListener
);
256 assure("Event was not fired on XMultiPropertySet.",
257 mListener
.changePropertiesEventFired());
259 catch(com
.sun
.star
.uno
.Exception e
) {
260 // e.printStackTrace();
261 log
.println(e
.getClass().getName());
262 log
.println("Message: " + e
.getMessage());
263 failed("Unexpected exception on XMultiPropertySet.");
267 log
.println("---- Test of XMultiPropertySet finished ----\n");
271 * Verify if the values of xProp are the same as vals.
272 * @param xProp A XMultiPropertySet.
273 * @param propNames An array with property names.
274 * @param vals An array with values of the properties
275 * @return -1 if none are equal, 1 if all are equal, 0 if some were equal
277 * @throws com.sun.star.lang.IllegalArgumentException
279 private int verifyPropertySet(XMultiPropertySet xProp
,
280 String
[] propNames
, String
[] vals
)
283 if (vals
.length
!= propNames
.length
) {
284 log
.println("Length of array parameters must be equal.");
287 for (int i
= 0; i
< vals
.length
; i
++) {
288 Object
[] objs
= xProp
.getPropertyValues(new String
[]{propNames
[i
]});
289 String retVal
= (String
)objs
[0];
290 boolean nCheck
= retVal
.equals(vals
[i
]);
292 log
.println("Property '" + propNames
[i
] +
293 "' was supposed to have value:");
294 log
.println(vals
[i
]);
295 log
.println("but has value:");
303 // return 0 if equal state changes compared to initial value
304 if ((nCheck
&& ret
<0) || (!nCheck
&& ret
>0)) {
312 // ____________________
314 * This tests the XPropertySet interface implementation.
316 public void checkXPropertySet()
318 log
.println("---- Testing the XPropertySet interface ----");
320 XPropertySet xPS
= (XPropertySet
)
321 UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
323 MyChangeListener mListener1
= new MyChangeListener();
324 MyChangeListener mListener2
= new MyChangeListener();
326 for (int i
=0; i
<props
.length
; i
++) {
328 String name
= propNames
[i
];
329 log
.println("Testing property '" + name
+ "'");
331 log
.println("Add 2 Listeners.");
332 xPS
.addPropertyChangeListener(name
, mListener1
);
333 xPS
.addVetoableChangeListener(name
, mListener1
);
334 xPS
.addPropertyChangeListener(name
, mListener2
);
335 xPS
.addVetoableChangeListener(name
, mListener2
);
337 // change the property
338 log
.println("Change value.");
339 String changeVal
= changeToCorrectValue(propVals
[i
]);
340 xPS
.setPropertyValue(name
, changeVal
);
341 String newVal
= (String
)xPS
.getPropertyValue(name
);
343 assure("Value did not change on property " + name
+ ".",
344 newVal
.equals(changeVal
));
346 assure("Listener 1 was not called.", checkListener(mListener1
), true);
347 assure("Listener 2 was not called.", checkListener(mListener2
), true);
349 mListener1
.resetListener();
350 mListener2
.resetListener();
352 log
.println("Remove Listener 1.");
354 xPS
.removePropertyChangeListener(name
, mListener1
);
355 xPS
.removeVetoableChangeListener(name
, mListener1
);
357 // change the property
358 log
.println("Change value back.");
359 xPS
.setPropertyValue(name
, propVals
[i
]);
360 newVal
= (String
)xPS
.getPropertyValue(name
);
361 assure("Value did not change on property " + name
,
362 newVal
.equals(propVals
[i
]));
364 assure("Listener was called, although it was removed on" +
365 " property " + name
+ ".", !checkListener(mListener1
), true);
366 assure("Listener 2 was not called on property " + name
+ ".",
367 checkListener(mListener2
), true);
369 catch(com
.sun
.star
.uno
.Exception e
) {
371 log
.println(e
.getClass().getName());
372 log
.println("Message: " + e
.getMessage());
373 failed("Unexpcted exception on property " + name
);
376 log
.println("Finish testing property '" + propNames
[i
] + "'\n");
378 log
.println("---- Test of XPropertySet finished ----\n");
382 private boolean checkListener(MyChangeListener ml
) {
383 return ml
.changePropertyEventFired() ||
384 ml
.changePropertiesEventFired() ||
385 ml
.vetoableChangeEventFired();
388 // ____________________
390 * Change the given String to a correct path URL.
391 * @return The changed path URL.
393 private String
changeToCorrectValue(String path
) {
394 // the simplest possiblity
395 if ( path
== null || path
.equals("") ) {
396 return "file:///tmp";
398 return path
+ "/tmp";
403 * Change the given String to an incorrect path URL.
404 * @return The changed path URL.
406 private String
changeToIncorrectValue(String path
) {
407 // the simplest possiblity
408 return "fileblablabla";
413 * Listener implementation which sets a flag when
414 * listener was called.
416 public class MyChangeListener
implements XPropertiesChangeListener
,
417 XPropertyChangeListener
,
418 XVetoableChangeListener
{
420 private boolean propChanged
= false;
421 private boolean propertiesChanged
= false;
422 private boolean disposeCalled
= false;
423 private boolean vetoableChanged
= false;
425 public void propertiesChange(
426 com
.sun
.star
.beans
.PropertyChangeEvent
[] e
) {
427 propertiesChanged
= true;
430 public void vetoableChange(com
.sun
.star
.beans
.PropertyChangeEvent pE
)
431 throws com
.sun
.star
.beans
.PropertyVetoException
{
432 vetoableChanged
= true;
435 public void propertyChange(com
.sun
.star
.beans
.PropertyChangeEvent pE
) {
439 public void disposing(com
.sun
.star
.lang
.EventObject eventObject
) {
440 disposeCalled
= true;
443 public void resetListener() {
445 propertiesChanged
= false;
446 disposeCalled
= false;
447 vetoableChanged
= false;
450 public boolean changePropertyEventFired() {
453 public boolean changePropertiesEventFired() {
454 return propertiesChanged
;
456 public boolean vetoableChangeEventFired() {
457 return vetoableChanged
;