1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 package complex
.path_settings
;
29 import com
.sun
.star
.beans
.Property
;
30 import com
.sun
.star
.beans
.XFastPropertySet
;
31 import com
.sun
.star
.beans
.XMultiPropertySet
;
32 import com
.sun
.star
.beans
.XPropertySet
;
33 import com
.sun
.star
.beans
.XPropertiesChangeListener
;
34 import com
.sun
.star
.beans
.XPropertyChangeListener
;
35 import com
.sun
.star
.beans
.XVetoableChangeListener
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.uno
.AnyConverter
;
39 import complexlib
.ComplexTestCase
;
41 public class PathSettingsTest
extends ComplexTestCase
{
43 private static XMultiServiceFactory xMSF
;
45 // the test object: an instance of the tested service
46 private static Object oObj
= null;
47 // the properties of the service
48 private static Property
[] props
= null;
49 private static String
[] propNames
= null;
50 private static String
[] availablePropNames
= new String
[]{
74 private static String
[] propVals
= null;
77 * A function to tell the framework, which test functions are available.
78 * Right now, it's only 'checkComplexTemplateState'.
79 * @return All test methods.
81 public String
[] getTestMethodNames() {
82 return new String
[]{"checkXFastPropertySet",
83 "checkXMultiPropertySet",
89 * Initialize before the tests start: this has to be done only once.
90 * This methods sets the 'oObj' and 'props' variables.
92 public void before() {
94 xMSF
= (XMultiServiceFactory
)param
.getMSF();
95 // oObj = xMSF.createInstance("com.sun.star.util.PathSettings");
96 oObj
= xMSF
.createInstance("com.sun.star.comp.framework.PathSettings");
97 System
.out
.println("Implementation: " + util
.utils
.getImplName(oObj
));
98 System
.out
.println("Service: ");
99 util
.dbg
.getSuppServices(oObj
);
100 if (oObj
== null) throw new com
.sun
.star
.uno
.Exception();
101 XPropertySet xProp
= (XPropertySet
)
102 UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
104 props
= xProp
.getPropertySetInfo().getProperties();
105 propNames
= new String
[props
.length
];
106 propVals
= new String
[props
.length
];
108 // get intitial values and create new ones
109 log
.println("\n---- All properties ----");
110 for (int i
= 1; i
< props
.length
; i
++) {
111 propNames
[i
] = props
[i
].Name
;
112 Object o
= xProp
.getPropertyValue(propNames
[i
]);
113 System
.out
.println("#### Object: " + o
.getClass().getName() + " - " + o
.toString());
114 propVals
[i
] = AnyConverter
.toString(o
);
115 System
.out
.println("#### String " + propVals
[i
]);
116 log
.println("Property Name: " + propNames
[i
]);
117 log
.println("Property Value: " + propVals
[i
]);
119 log
.println("---- Finish showing properties ----\n");
121 catch(com
.sun
.star
.uno
.Exception e
) {
123 log
.println(e
.getClass().getName());
124 log
.println("Message: " + e
.getMessage());
125 failed("Could not create an instance of the test object.");
129 failed("What exception?");
134 * This tests the XFastPropertySet interface implementation.
136 public void checkXFastPropertySet()
138 log
.println("---- Testing the XFastPropertySet interface ----");
140 // creating instances
141 XFastPropertySet xFPS
= (XFastPropertySet
)
142 UnoRuntime
.queryInterface(XFastPropertySet
.class, oObj
);
145 // do for all properties
146 for (int i
= 0; i
< props
.length
; i
++) {
148 Property property
= props
[i
];
149 name
= property
.Name
;
150 int handle
= property
.Handle
;
152 // get property name and initial value
153 log
.println("Test property with name: " + name
);
154 String val
= (String
)xFPS
.getFastPropertyValue(handle
);
155 log
.println("Property has initial value: '" + val
+ "'");
157 // set to a new correct value
158 String newVal
= changeToCorrectValue(val
);
159 log
.println("Try to change to correct value '" + newVal
+ "'");
160 xFPS
.setFastPropertyValue(handle
, newVal
);
163 String checkVal
= (String
)xFPS
.getFastPropertyValue(handle
);
164 assure("Did not change value on property " + name
+ ".", checkVal
.equals(newVal
));
166 newVal
= changeToIncorrectValue(val
);
167 log
.println("Try to change to incorrect value '" + newVal
+ "'");
169 xFPS
.setFastPropertyValue(handle
, newVal
);
171 catch(com
.sun
.star
.lang
.IllegalArgumentException e
) {
172 log
.println("Correctly thrown Exception caught.");
176 checkVal
= (String
)xFPS
.getFastPropertyValue(handle
);
177 assure("Value did change on property " + name
+ " though it should not have.",
178 !checkVal
.equals(newVal
));
180 // set back to initial setting
181 xFPS
.setFastPropertyValue(handle
, val
);
184 checkVal
= (String
)xFPS
.getFastPropertyValue(handle
);
185 assure("Did not change value back to original on property "
186 + name
, checkVal
.equals(val
));
187 log
.println("Test of property " + name
+ " finished\n");
189 catch(com
.sun
.star
.uno
.Exception e
) {
190 // e.printStackTrace();
191 log
.println(e
.getClass().getName());
192 log
.println("Message: " + e
.getMessage());
193 failed("Unexpected exception on property " + name
+ ".");
197 log
.println("---- Test of XFastPropertySet finished ----\n");
201 // ____________________
203 * This tests the XMultiPropertySet interface implementation.
205 public void checkXMultiPropertySet()
207 log
.println("---- Testing the XMultiPropertySet interface ----");
208 XMultiPropertySet xMPS
= (XMultiPropertySet
)
209 UnoRuntime
.queryInterface(XMultiPropertySet
.class, oObj
);
211 String
[] correctVals
= new String
[props
.length
];
212 String
[] incorrectVals
= new String
[props
.length
];
214 // get intitial values and create new ones
215 for (int i
= 0; i
< props
.length
; i
++) {
216 correctVals
[i
] = changeToCorrectValue(propVals
[i
]);
217 incorrectVals
[i
] = changeToIncorrectValue(propVals
[i
]);
221 // add a change listener
222 MyChangeListener mListener
= new MyChangeListener();
223 xMPS
.addPropertiesChangeListener(propNames
, mListener
);
225 // first change props to correct values
226 log
.println("Change to correct values.");
227 xMPS
.setPropertyValues(propNames
, correctVals
);
228 assure("Could not change to correct values with XMultiPropoertySet.",
229 verifyPropertySet(xMPS
,propNames
,correctVals
)>0);
231 // second, change to incorrect values: expect an exception
232 log
.println("Try to change to incorrect values.");
234 xMPS
.setPropertyValues(propNames
, incorrectVals
);
236 catch(com
.sun
.star
.lang
.IllegalArgumentException r
) {
237 log
.println("Correctly thrown Exception caught.");
239 assure("Did change to incorrect values with XMultiPropertySet," +
240 " but should not have.",
241 verifyPropertySet(xMPS
,propNames
,correctVals
)>0);
243 // third, change back to initial values
244 log
.println("Change back to initial values.");
245 xMPS
.setPropertyValues(propNames
, propVals
);
246 assure("Could not change back to initial values with" +
247 " XMultiPropertySet.",
248 verifyPropertySet(xMPS
,propNames
,propVals
)>0);
250 // fire the event for the listener
251 log
.println("Fire event.");
252 xMPS
.firePropertiesChangeEvent(propNames
, mListener
);
253 assure("Event was not fired on XMultiPropertySet.",
254 mListener
.changePropertiesEventFired());
256 catch(com
.sun
.star
.uno
.Exception e
) {
257 // e.printStackTrace();
258 log
.println(e
.getClass().getName());
259 log
.println("Message: " + e
.getMessage());
260 failed("Unexpected exception on XMultiPropertySet.");
264 log
.println("---- Test of XMultiPropertySet finished ----\n");
268 * Verify if the values of xProp are the same as vals.
269 * @param xProp A XMultiPropertySet.
270 * @param propNames An array with property names.
271 * @param vals An array with values of the properties
272 * @return -1 if none are equal, 1 if all are equal, 0 if some were equal
274 * @throws com.sun.star.lang.IllegalArgumentException
276 private int verifyPropertySet(XMultiPropertySet xProp
,
277 String
[] propNames
, String
[] vals
)
280 if (vals
.length
!= propNames
.length
) {
281 log
.println("Length of array parameters must be equal.");
284 for (int i
= 0; i
< vals
.length
; i
++) {
285 Object
[] objs
= xProp
.getPropertyValues(new String
[]{propNames
[i
]});
286 String retVal
= (String
)objs
[0];
287 boolean nCheck
= retVal
.equals(vals
[i
]);
289 log
.println("Property '" + propNames
[i
] +
290 "' was supposed to have value:");
291 log
.println(vals
[i
]);
292 log
.println("but has value:");
300 // return 0 if equal state changes compared to initial value
301 if ((nCheck
&& ret
<0) || (!nCheck
&& ret
>0)) {
309 // ____________________
311 * This tests the XPropertySet interface implementation.
313 public void checkXPropertySet()
315 log
.println("---- Testing the XPropertySet interface ----");
317 XPropertySet xPS
= (XPropertySet
)
318 UnoRuntime
.queryInterface(XPropertySet
.class, oObj
);
320 MyChangeListener mListener1
= new MyChangeListener();
321 MyChangeListener mListener2
= new MyChangeListener();
323 for (int i
=0; i
<props
.length
; i
++) {
325 String name
= propNames
[i
];
326 log
.println("Testing property '" + name
+ "'");
328 log
.println("Add 2 Listeners.");
329 xPS
.addPropertyChangeListener(name
, mListener1
);
330 xPS
.addVetoableChangeListener(name
, mListener1
);
331 xPS
.addPropertyChangeListener(name
, mListener2
);
332 xPS
.addVetoableChangeListener(name
, mListener2
);
334 // change the property
335 log
.println("Change value.");
336 String changeVal
= changeToCorrectValue(propVals
[i
]);
337 xPS
.setPropertyValue(name
, changeVal
);
338 String newVal
= (String
)xPS
.getPropertyValue(name
);
340 assure("Value did not change on property " + name
+ ".",
341 newVal
.equals(changeVal
));
343 assure("Listener 1 was not called.", checkListener(mListener1
), true);
344 assure("Listener 2 was not called.", checkListener(mListener2
), true);
346 mListener1
.resetListener();
347 mListener2
.resetListener();
349 log
.println("Remove Listener 1.");
351 xPS
.removePropertyChangeListener(name
, mListener1
);
352 xPS
.removeVetoableChangeListener(name
, mListener1
);
354 // change the property
355 log
.println("Change value back.");
356 xPS
.setPropertyValue(name
, propVals
[i
]);
357 newVal
= (String
)xPS
.getPropertyValue(name
);
358 assure("Value did not change on property " + name
,
359 newVal
.equals(propVals
[i
]));
361 assure("Listener was called, although it was removed on" +
362 " property " + name
+ ".", !checkListener(mListener1
), true);
363 assure("Listener 2 was not called on property " + name
+ ".",
364 checkListener(mListener2
), true);
366 catch(com
.sun
.star
.uno
.Exception e
) {
368 log
.println(e
.getClass().getName());
369 log
.println("Message: " + e
.getMessage());
370 failed("Unexpcted exception on property " + name
);
373 log
.println("Finish testing property '" + propNames
[i
] + "'\n");
375 log
.println("---- Test of XPropertySet finished ----\n");
379 private boolean checkListener(MyChangeListener ml
) {
380 return ml
.changePropertyEventFired() ||
381 ml
.changePropertiesEventFired() ||
382 ml
.vetoableChangeEventFired();
385 // ____________________
387 * Change the given String to a correct path URL.
388 * @return The changed path URL.
390 private String
changeToCorrectValue(String path
) {
391 // the simplest possiblity
392 if ( path
== null || path
.equals("") ) {
393 return "file:///tmp";
395 return path
+ "/tmp";
400 * Change the given String to an incorrect path URL.
401 * @return The changed path URL.
403 private String
changeToIncorrectValue(String path
) {
404 // the simplest possiblity
405 return "fileblablabla";
410 * Listener implementation which sets a flag when
411 * listener was called.
413 public class MyChangeListener
implements XPropertiesChangeListener
,
414 XPropertyChangeListener
,
415 XVetoableChangeListener
{
417 private boolean propChanged
= false;
418 private boolean propertiesChanged
= false;
419 private boolean disposeCalled
= false;
420 private boolean vetoableChanged
= false;
422 public void propertiesChange(
423 com
.sun
.star
.beans
.PropertyChangeEvent
[] e
) {
424 propertiesChanged
= true;
427 public void vetoableChange(com
.sun
.star
.beans
.PropertyChangeEvent pE
)
428 throws com
.sun
.star
.beans
.PropertyVetoException
{
429 vetoableChanged
= true;
432 public void propertyChange(com
.sun
.star
.beans
.PropertyChangeEvent pE
) {
436 public void disposing(com
.sun
.star
.lang
.EventObject eventObject
) {
437 disposeCalled
= true;
440 public void resetListener() {
442 propertiesChanged
= false;
443 disposeCalled
= false;
444 vetoableChanged
= false;
447 public boolean changePropertyEventFired() {
450 public boolean changePropertiesEventFired() {
451 return propertiesChanged
;
453 public boolean vetoableChangeEventFired() {
454 return vetoableChanged
;