/************************************************************************* * * $RCSfile: PathSettingsTest.java,v $ * * $Revision: 1.2 $ * * last change: $Date: 2007-07-24 13:27:52 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses * * - GNU Lesser General Public License Version 2.1 * - Sun Industry Standards Source License Version 1.1 * * Sun Microsystems Inc., October, 2000 * * GNU Lesser General Public License Version 2.1 * ============================================= * Copyright 2000 by Sun Microsystems, Inc. * 901 San Antonio Road, Palo Alto, CA 94303, USA * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License version 2.1, as published by the Free Software Foundation. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA * * * Sun Industry Standards Source License Version 1.1 * ================================================= * The contents of this file are subject to the Sun Industry Standards * Source License Version 1.1 (the "License"); You may not use this file * except in compliance with the License. You may obtain a copy of the * License at http://www.openoffice.org/license.html. * * Software provided under this License is provided on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS, * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING. * See the License for the specific provisions governing your rights and * obligations concerning the Software. * * The Initial Developer of the Original Code is: Sun Microsystems, Inc. * * Copyright: 2002 by Sun Microsystems, Inc. * * All Rights Reserved. * * Contributor(s): _______________________________________ * * ************************************************************************/ package complex.path_settings; import com.sun.star.beans.Property; import com.sun.star.beans.XFastPropertySet; import com.sun.star.beans.XMultiPropertySet; import com.sun.star.beans.XPropertySet; import com.sun.star.beans.XPropertiesChangeListener; import com.sun.star.beans.XPropertyChangeListener; import com.sun.star.beans.XVetoableChangeListener; import com.sun.star.lang.XMultiServiceFactory; import com.sun.star.uno.UnoRuntime; import com.sun.star.uno.AnyConverter; import complexlib.ComplexTestCase; public class PathSettingsTest extends ComplexTestCase { private static XMultiServiceFactory xMSF; // the test object: an instance of the tested service private static Object oObj = null; // the properties of the service private static Property[] props = null; private static String[] propNames = null; private static String[] availablePropNames = new String[]{ "Addin", "AutoCorrect", "Autotext", "Backup", "Basic", "Bitmap", "Config", "Dictionary", "Favorite", "Filter", "Gallery", "Help", "Linguistic", "Module", "Palette", "Plugin", "Temp", "Template", "UIConfig", "UserConfig", "UserDictionary", "Work", }; private static String[] propVals = null; /** * A function to tell the framework, which test functions are available. * Right now, it's only 'checkComplexTemplateState'. * @return All test methods. */ public String[] getTestMethodNames() { return new String[]{"checkXFastPropertySet", "checkXMultiPropertySet", "checkXPropertySet" }; } /** * Initialize before the tests start: this has to be done only once. * This methods sets the 'oObj' and 'props' variables. */ public void before() { try { xMSF = (XMultiServiceFactory)param.getMSF(); // oObj = xMSF.createInstance("com.sun.star.util.PathSettings"); oObj = xMSF.createInstance("com.sun.star.comp.framework.PathSettings"); System.out.println("Implementation: " + util.utils.getImplName(oObj)); System.out.println("Service: "); util.dbg.getSuppServices(oObj); if (oObj == null) throw new com.sun.star.uno.Exception(); XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oObj); props = xProp.getPropertySetInfo().getProperties(); propNames = new String[props.length]; propVals = new String[props.length]; // get intitial values and create new ones log.println("\n---- All properties ----"); for (int i = 1; i < props.length; i++) { propNames[i] = props[i].Name; Object o = xProp.getPropertyValue(propNames[i]); System.out.println("#### Object: " + o.getClass().getName() + " - " + o.toString()); propVals[i] = AnyConverter.toString(o); System.out.println("#### String " + propVals[i]); log.println("Property Name: " + propNames[i]); log.println("Property Value: " + propVals[i]); } log.println("---- Finish showing properties ----\n"); } catch(com.sun.star.uno.Exception e) { e.printStackTrace(); log.println(e.getClass().getName()); log.println("Message: " + e.getMessage()); failed("Could not create an instance of the test object."); } catch(Exception e) { e.printStackTrace(); failed("What exception?"); } } /** * This tests the XFastPropertySet interface implementation. */ public void checkXFastPropertySet() { log.println("---- Testing the XFastPropertySet interface ----"); // creating instances XFastPropertySet xFPS = (XFastPropertySet) UnoRuntime.queryInterface(XFastPropertySet.class, oObj); String name = null; // do for all properties for (int i = 0; i < props.length; i++) { try { Property property = props[i]; name = property.Name; int handle = property.Handle; // get property name and initial value log.println("Test property with name: " + name); String val = (String)xFPS.getFastPropertyValue(handle); log.println("Property has initial value: '" + val + "'"); // set to a new correct value String newVal = changeToCorrectValue(val); log.println("Try to change to correct value '" + newVal + "'"); xFPS.setFastPropertyValue(handle, newVal); // check the change String checkVal = (String)xFPS.getFastPropertyValue(handle); assure("Did not change value on property " + name + ".", checkVal.equals(newVal)); newVal = changeToIncorrectValue(val); log.println("Try to change to incorrect value '" + newVal + "'"); try { xFPS.setFastPropertyValue(handle, newVal); } catch(com.sun.star.lang.IllegalArgumentException e) { log.println("Correctly thrown Exception caught."); } // check if changed checkVal = (String)xFPS.getFastPropertyValue(handle); assure("Value did change on property " + name + " though it should not have.", !checkVal.equals(newVal)); // set back to initial setting xFPS.setFastPropertyValue(handle, val); // check if changed checkVal = (String)xFPS.getFastPropertyValue(handle); assure("Did not change value back to original on property " + name, checkVal.equals(val)); log.println("Test of property " + name + " finished\n"); } catch(com.sun.star.uno.Exception e) { // e.printStackTrace(); log.println(e.getClass().getName()); log.println("Message: " + e.getMessage()); failed("Unexpected exception on property " + name + "."); continue; } } log.println("---- Test of XFastPropertySet finished ----\n"); } // ____________________ /** * This tests the XMultiPropertySet interface implementation. */ public void checkXMultiPropertySet() { log.println("---- Testing the XMultiPropertySet interface ----"); XMultiPropertySet xMPS = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oObj); String[] correctVals = new String[props.length]; String[] incorrectVals = new String[props.length]; // get intitial values and create new ones for (int i = 0; i < props.length; i++) { correctVals[i] = changeToCorrectValue(propVals[i]); incorrectVals[i] = changeToIncorrectValue(propVals[i]); } try { // add a change listener MyChangeListener mListener = new MyChangeListener(); xMPS.addPropertiesChangeListener(propNames, mListener); // first change props to correct values log.println("Change to correct values."); xMPS.setPropertyValues(propNames, correctVals); assure("Could not change to correct values with XMultiPropoertySet.", verifyPropertySet(xMPS,propNames,correctVals)>0); // second, change to incorrect values: expect an exception log.println("Try to change to incorrect values."); try { xMPS.setPropertyValues(propNames, incorrectVals); } catch(com.sun.star.lang.IllegalArgumentException r) { log.println("Correctly thrown Exception caught."); } assure("Did change to incorrect values with XMultiPropertySet," + " but should not have.", verifyPropertySet(xMPS,propNames,correctVals)>0); // third, change back to initial values log.println("Change back to initial values."); xMPS.setPropertyValues(propNames, propVals); assure("Could not change back to initial values with" + " XMultiPropertySet.", verifyPropertySet(xMPS,propNames,propVals)>0); // fire the event for the listener log.println("Fire event."); xMPS.firePropertiesChangeEvent(propNames, mListener); assure("Event was not fired on XMultiPropertySet.", mListener.changePropertiesEventFired()); } catch(com.sun.star.uno.Exception e) { // e.printStackTrace(); log.println(e.getClass().getName()); log.println("Message: " + e.getMessage()); failed("Unexpected exception on XMultiPropertySet."); } // test finished log.println("---- Test of XMultiPropertySet finished ----\n"); } /** * Verify if the values of xProp are the same as vals. * @param xProp A XMultiPropertySet. * @param propNames An array with property names. * @param vals An array with values of the properties * @return -1 if none are equal, 1 if all are equal, 0 if some were equal * and some not. * @throws com.sun.star.lang.IllegalArgumentException */ private int verifyPropertySet(XMultiPropertySet xProp, String[] propNames, String[] vals) { int ret=0; if (vals.length != propNames.length) { log.println("Length of array parameters must be equal."); return ret; } for (int i = 0; i < vals.length; i++) { Object[] objs = xProp.getPropertyValues(new String[]{propNames[i]}); String retVal = (String)objs[0]; boolean nCheck = retVal.equals(vals[i]); if (!nCheck) { log.println("Property '" + propNames[i] + "' was supposed to have value:"); log.println(vals[i]); log.println("but has value:"); log.println(retVal); } // initialize if (i==0) { ret = nCheck?1:-1; continue; } // return 0 if equal state changes compared to initial value if ((nCheck && ret<0) || (!nCheck && ret>0)) { ret = 0; } } return ret; } // ____________________ /** * This tests the XPropertySet interface implementation. */ public void checkXPropertySet() { log.println("---- Testing the XPropertySet interface ----"); XPropertySet xPS = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oObj); MyChangeListener mListener1 = new MyChangeListener(); MyChangeListener mListener2 = new MyChangeListener(); for (int i=0; i