1*ef39d40dSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*ef39d40dSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*ef39d40dSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*ef39d40dSAndrew Rist * distributed with this work for additional information 6*ef39d40dSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*ef39d40dSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*ef39d40dSAndrew Rist * "License"); you may not use this file except in compliance 9*ef39d40dSAndrew Rist * with the License. You may obtain a copy of the License at 10*ef39d40dSAndrew Rist * 11*ef39d40dSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*ef39d40dSAndrew Rist * 13*ef39d40dSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*ef39d40dSAndrew Rist * software distributed under the License is distributed on an 15*ef39d40dSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*ef39d40dSAndrew Rist * KIND, either express or implied. See the License for the 17*ef39d40dSAndrew Rist * specific language governing permissions and limitations 18*ef39d40dSAndrew Rist * under the License. 19*ef39d40dSAndrew Rist * 20*ef39d40dSAndrew Rist *************************************************************/ 21*ef39d40dSAndrew Rist 22*ef39d40dSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package ifc.beans; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import java.util.Random; 27cdf0e10cSrcweir import java.util.Set; 28cdf0e10cSrcweir import java.util.Vector; 29cdf0e10cSrcweir 30cdf0e10cSrcweir import lib.MultiMethodTest; 31cdf0e10cSrcweir import lib.StatusException; 32cdf0e10cSrcweir import util.ValueChanger; 33cdf0e10cSrcweir 34cdf0e10cSrcweir import com.sun.star.beans.Property; 35cdf0e10cSrcweir import com.sun.star.beans.PropertyAttribute; 36cdf0e10cSrcweir import com.sun.star.beans.XFastPropertySet; 37cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 38cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo; 39cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 40cdf0e10cSrcweir 41cdf0e10cSrcweir /** 42cdf0e10cSrcweir * Testing <code>com.sun.star.beans.XFastPropertySet</code> 43cdf0e10cSrcweir * interface methods : 44cdf0e10cSrcweir * <ul> 45cdf0e10cSrcweir * <li><code> setFastPropertyValue()</code></li> 46cdf0e10cSrcweir * <li><code> getFastPropertyValue()</code></li> 47cdf0e10cSrcweir * </ul> <p> 48cdf0e10cSrcweir * Required relations : 49cdf0e10cSrcweir * <ul> 50cdf0e10cSrcweir * <li> <code>'XFastPropertySet.ExcludeProps'</code> 51cdf0e10cSrcweir * <b>(optional) </b> : java.util.Set. 52cdf0e10cSrcweir * Has property names which must be skipped from testing in 53cdf0e10cSrcweir * some reasons (for example property accepts restricted set 54cdf0e10cSrcweir * of values). 55cdf0e10cSrcweir * </li> 56cdf0e10cSrcweir * <ul> <p> 57cdf0e10cSrcweir * @see com.sun.star.beans.XFastPropertySet 58cdf0e10cSrcweir */ 59cdf0e10cSrcweir public class _XFastPropertySet extends MultiMethodTest { 60cdf0e10cSrcweir 61cdf0e10cSrcweir public XFastPropertySet oObj = null; 62cdf0e10cSrcweir private Vector handles = new Vector(); 63cdf0e10cSrcweir private int handle = -1; 64cdf0e10cSrcweir private Set exclude = null ; 65cdf0e10cSrcweir 66cdf0e10cSrcweir /** 67cdf0e10cSrcweir * Retrieves relation. 68cdf0e10cSrcweir */ before()69cdf0e10cSrcweir protected void before() { 70cdf0e10cSrcweir exclude = (Set) tEnv.getObjRelation("XFastPropertySet.ExcludeProps") ; 71cdf0e10cSrcweir if (exclude == null) { 72cdf0e10cSrcweir exclude = new java.util.HashSet() ; 73cdf0e10cSrcweir } 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir /** 77cdf0e10cSrcweir * Test selects random property which can not be VOID and 78cdf0e10cSrcweir * is writable, then change property value using <code> 79cdf0e10cSrcweir * get/set</code> methods, and checks if value properly changed. 80cdf0e10cSrcweir * Has <b> OK </b> status if value after change is not equal to value 81cdf0e10cSrcweir * before and no exceptions were thrown. <p> 82cdf0e10cSrcweir */ _setFastPropertyValue()83cdf0e10cSrcweir public void _setFastPropertyValue() { 84cdf0e10cSrcweir XPropertySet PS = (XPropertySet)UnoRuntime.queryInterface 85cdf0e10cSrcweir (XPropertySet.class, oObj); 86cdf0e10cSrcweir XPropertySetInfo propertySetInfo = PS.getPropertySetInfo(); 87cdf0e10cSrcweir 88cdf0e10cSrcweir if (propertySetInfo == null) { 89cdf0e10cSrcweir log.println("getPropertySetInfo() method returned null"); 90cdf0e10cSrcweir tRes.tested("setFastPropertyValue()", false) ; 91cdf0e10cSrcweir } 92cdf0e10cSrcweir getPropsToTest(propertySetInfo); 93cdf0e10cSrcweir 94cdf0e10cSrcweir Object gValue = null; 95cdf0e10cSrcweir Object sValue = null; 96cdf0e10cSrcweir 97cdf0e10cSrcweir if ( handle == -1) { 98cdf0e10cSrcweir log.println("*** No changeable properties found ***"); 99cdf0e10cSrcweir tRes.tested("setFastPropertyValue()", false) ; 100cdf0e10cSrcweir } else { 101cdf0e10cSrcweir try { 102cdf0e10cSrcweir gValue = oObj.getFastPropertyValue(handle); 103cdf0e10cSrcweir sValue = ValueChanger.changePValue(gValue); 104cdf0e10cSrcweir oObj.setFastPropertyValue(handle, sValue); 105cdf0e10cSrcweir sValue = oObj.getFastPropertyValue(handle); 106cdf0e10cSrcweir } catch (com.sun.star.beans.UnknownPropertyException e) { 107cdf0e10cSrcweir log.println("Exception occured while trying to change property with handle = " + handle); 108cdf0e10cSrcweir e.printStackTrace(log); 109cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 110cdf0e10cSrcweir log.println("Exception occured while trying to change property with handle = " + handle); 111cdf0e10cSrcweir e.printStackTrace(log); 112cdf0e10cSrcweir } catch (com.sun.star.beans.PropertyVetoException e) { 113cdf0e10cSrcweir log.println("Exception occured while trying to change property with handle = " + handle); 114cdf0e10cSrcweir e.printStackTrace(log); 115cdf0e10cSrcweir } catch (com.sun.star.lang.IllegalArgumentException e) { 116cdf0e10cSrcweir log.println("Exception occured while trying to change property with handle = " + handle); 117cdf0e10cSrcweir e.printStackTrace(log); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir 120cdf0e10cSrcweir tRes.tested("setFastPropertyValue()",(!gValue.equals(sValue))); 121cdf0e10cSrcweir } 122cdf0e10cSrcweir } 123cdf0e10cSrcweir 124cdf0e10cSrcweir /** 125cdf0e10cSrcweir * Test selects random property which can not be VOID and 126cdf0e10cSrcweir * is writable, then calls the method and checks that 127cdf0e10cSrcweir * no exceptions were thrown. <p> 128cdf0e10cSrcweir * Has <b> OK </b> status if exceptions were thrown. <p> 129cdf0e10cSrcweir */ _getFastPropertyValue()130cdf0e10cSrcweir public void _getFastPropertyValue() { 131cdf0e10cSrcweir XPropertySet PS = (XPropertySet)UnoRuntime.queryInterface 132cdf0e10cSrcweir (XPropertySet.class, oObj); 133cdf0e10cSrcweir XPropertySetInfo propertySetInfo = PS.getPropertySetInfo(); 134cdf0e10cSrcweir 135cdf0e10cSrcweir if (propertySetInfo == null) { 136cdf0e10cSrcweir log.println("getPropertySetInfo() method returned null"); 137cdf0e10cSrcweir tRes.tested("getFastPropertyValue()", false) ; 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir getPropsToTest(propertySetInfo); 141cdf0e10cSrcweir 142cdf0e10cSrcweir try { 143cdf0e10cSrcweir oObj.getFastPropertyValue(handle); 144cdf0e10cSrcweir tRes.tested("getFastPropertyValue()",true); 145cdf0e10cSrcweir } catch (com.sun.star.beans.UnknownPropertyException e) { 146cdf0e10cSrcweir log.println("Exception occured while trying to get property '" 147cdf0e10cSrcweir + handle +"'"); 148cdf0e10cSrcweir e.printStackTrace(log); 149cdf0e10cSrcweir tRes.tested("getFastPropertyValue()",false); 150cdf0e10cSrcweir } catch (com.sun.star.lang.WrappedTargetException e) { 151cdf0e10cSrcweir log.println("Exception occured while trying to get property '" 152cdf0e10cSrcweir + handle +"'"); 153cdf0e10cSrcweir e.printStackTrace(log); 154cdf0e10cSrcweir tRes.tested("getFastPropertyValue()",false); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir return; 157cdf0e10cSrcweir } 158cdf0e10cSrcweir 159cdf0e10cSrcweir 160cdf0e10cSrcweir //Get the properties being tested getPropsToTest(XPropertySetInfo xPSI)161cdf0e10cSrcweir private void getPropsToTest(XPropertySetInfo xPSI) { 162cdf0e10cSrcweir 163cdf0e10cSrcweir Property[] properties = xPSI.getProperties(); 164cdf0e10cSrcweir 165cdf0e10cSrcweir for (int i = 0; i < properties.length; i++) { 166cdf0e10cSrcweir if (exclude.contains(properties[i].Name)) continue ; 167cdf0e10cSrcweir Property property = properties[i]; 168cdf0e10cSrcweir String name = property.Name; 169cdf0e10cSrcweir int handle = property.Handle; 170cdf0e10cSrcweir log.println("Checking '" + name + "' with handle = " + handle); 171cdf0e10cSrcweir boolean isWritable = 172cdf0e10cSrcweir ((property.Attributes & PropertyAttribute.READONLY) == 0); 173cdf0e10cSrcweir boolean isNotNull = 174cdf0e10cSrcweir ((property.Attributes & PropertyAttribute.MAYBEVOID) == 0); 175cdf0e10cSrcweir boolean canChange = false; 176cdf0e10cSrcweir if ( isWritable && isNotNull ) 177cdf0e10cSrcweir canChange = isChangeable(handle); 178cdf0e10cSrcweir if ( isWritable && isNotNull && canChange) 179cdf0e10cSrcweir handles.add(new Integer(handle)); 180cdf0e10cSrcweir } // endfor 181cdf0e10cSrcweir 182cdf0e10cSrcweir Random rnd = new Random(); 183cdf0e10cSrcweir int nr = rnd.nextInt(handles.size()); 184cdf0e10cSrcweir handle = ((Integer)handles.elementAt(nr)).intValue(); 185cdf0e10cSrcweir } 186cdf0e10cSrcweir isChangeable(int handle)187cdf0e10cSrcweir private boolean isChangeable(int handle) { 188cdf0e10cSrcweir boolean hasChanged = false; 189cdf0e10cSrcweir try { 190cdf0e10cSrcweir Object getProp = oObj.getFastPropertyValue(handle); 191cdf0e10cSrcweir Object setValue = null; 192cdf0e10cSrcweir 193cdf0e10cSrcweir if (getProp != null) 194cdf0e10cSrcweir setValue = ValueChanger.changePValue(getProp); 195cdf0e10cSrcweir else 196cdf0e10cSrcweir log.println("Property with handle = " + handle 197cdf0e10cSrcweir + " is null but 'MAYBEVOID' isn't set"); 198cdf0e10cSrcweir if (setValue != null) { 199cdf0e10cSrcweir oObj.setFastPropertyValue(handle, setValue); 200cdf0e10cSrcweir hasChanged = 201cdf0e10cSrcweir (!getProp.equals(oObj.getFastPropertyValue(handle))); 202cdf0e10cSrcweir } 203cdf0e10cSrcweir else 204cdf0e10cSrcweir log.println("Couldn't change Property with handle " + handle); 205cdf0e10cSrcweir } catch(com.sun.star.lang.WrappedTargetException e) { 206cdf0e10cSrcweir log.println("Property with handle " + handle + " throws exception"); 207cdf0e10cSrcweir e.printStackTrace(log); 208cdf0e10cSrcweir throw new StatusException("Property with handle " + handle 209cdf0e10cSrcweir + " throws exception", e); 210cdf0e10cSrcweir } catch(com.sun.star.lang.IllegalArgumentException e) { 211cdf0e10cSrcweir log.println("Property with handle " + handle + " throws exception"); 212cdf0e10cSrcweir e.printStackTrace(log); 213cdf0e10cSrcweir throw new StatusException("Property with handle " + handle 214cdf0e10cSrcweir + " throws exception", e); 215cdf0e10cSrcweir } catch(com.sun.star.beans.PropertyVetoException e) { 216cdf0e10cSrcweir log.println("Property with handle " + handle + " throws exception"); 217cdf0e10cSrcweir e.printStackTrace(log); 218cdf0e10cSrcweir throw new StatusException("Property with handle " + handle 219cdf0e10cSrcweir + " throws exception", e); 220cdf0e10cSrcweir } catch(com.sun.star.beans.UnknownPropertyException e) { 221cdf0e10cSrcweir log.println("Property with handle " + handle + " throws exception"); 222cdf0e10cSrcweir e.printStackTrace(log); 223cdf0e10cSrcweir throw new StatusException("Property with handle " + handle 224cdf0e10cSrcweir + " throws exception", e); 225cdf0e10cSrcweir } 226cdf0e10cSrcweir 227cdf0e10cSrcweir return hasChanged; 228cdf0e10cSrcweir } 229cdf0e10cSrcweir } 230cdf0e10cSrcweir 231cdf0e10cSrcweir 232