1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package complex.dbaccess; 25 26 // import complexlib.ComplexTestCase; 27 28 import com.sun.star.beans.NamedValue; 29 import com.sun.star.beans.PropertyState; 30 import com.sun.star.beans.PropertyValue; 31 import com.sun.star.beans.PropertyAttribute; 32 import com.sun.star.beans.XPropertyAccess; 33 import com.sun.star.beans.XPropertySet; 34 import com.sun.star.beans.XPropertyContainer; 35 import com.sun.star.uno.UnoRuntime; 36 import com.sun.star.uno.XInterface; 37 import com.sun.star.lang.XMultiServiceFactory; 38 39 // ---------- junit imports ----------------- 40 import org.junit.Before; 41 import org.junit.Test; 42 import static org.junit.Assert.*; 43 // ------------------------------------------ 44 45 public class PropertyBag extends TestCase 46 { 47 private static final String VALUE = "Value"; 48 private XPropertyContainer m_bag; 49 private XPropertySet m_set; 50 private XPropertyAccess m_access; 51 private XMultiServiceFactory m_orb = null; 52 getTestObjectName()53 public String getTestObjectName() 54 { 55 return "PropertyBag"; 56 } 57 58 @Before 59 @Override before()60 public void before() 61 { 62 m_orb = getMSF(); 63 } 64 65 @Test checkBasics()66 public void checkBasics() 67 { 68 createEmptyBag(); 69 System.out.println("testing the basics"); 70 71 // check whether empty property names are rejected 72 boolean caughtExpected = false; 73 try 74 { 75 m_bag.addProperty( "", PropertyAttribute.BOUND, Integer.valueOf( 3 ) ); 76 } 77 catch(com.sun.star.lang.IllegalArgumentException e) { caughtExpected = true; } 78 catch(com.sun.star.uno.Exception e) { } 79 if ( !caughtExpected ) 80 { 81 fail("empty property names are not rejected by XPropertyContainer::addProperty"); 82 } 83 84 // check whether duplicate insertions are rejected 85 caughtExpected = false; 86 try 87 { 88 m_bag.addProperty( VALUE, PropertyAttribute.BOUND, "" ); 89 m_bag.addProperty( VALUE, PropertyAttribute.BOUND, "" ); 90 } 91 catch(com.sun.star.beans.PropertyExistException e) { caughtExpected = true; } 92 catch(com.sun.star.uno.Exception e) { } 93 if ( !caughtExpected ) 94 { 95 fail("insertion of duplicate property names is not rejected"); 96 } 97 98 // try removing the property we just added - this should fail, as it does not have 99 // the REMOVEABLE attribute 100 caughtExpected = false; 101 try 102 { 103 m_bag.removeProperty( VALUE); 104 } 105 catch(com.sun.star.beans.NotRemoveableException e) { caughtExpected = true; } 106 catch(com.sun.star.uno.Exception e) { } 107 if ( !caughtExpected ) 108 { 109 fail("removing non-removeable properties is expected to fail - but it didn't"); 110 } 111 112 // try removing a non-existent property 113 caughtExpected = false; 114 try 115 { 116 m_bag.removeProperty( "NonExistent" ); 117 } 118 catch(com.sun.star.beans.UnknownPropertyException e) { caughtExpected = true; } 119 catch(com.sun.star.uno.Exception e) { } 120 if ( !caughtExpected ) 121 { 122 fail("removing non-existent properties is expected to fail - but it didn't"); 123 } 124 125 // try writing and reading a value for the one property we have so far 126 try 127 { 128 final String testValue = "someArbitraryValue"; 129 m_set.setPropertyValue( VALUE , testValue); 130 final String currentValue = (String)m_set.getPropertyValue( VALUE); 131 if ( !currentValue.equals( testValue ) ) 132 { 133 fail("set property is not remembered"); 134 } 135 } 136 catch(com.sun.star.uno.Exception e) 137 { 138 fail( "setting or getting a property value failed" ); 139 } 140 141 // try setting an illegal value for the property 142 caughtExpected = false; 143 try 144 { 145 m_set.setPropertyValue( VALUE, Integer.valueOf( 3 ) ); 146 } 147 catch(com.sun.star.lang.IllegalArgumentException e) { caughtExpected = true; } 148 catch(com.sun.star.uno.Exception e) { } 149 if ( !caughtExpected ) 150 { 151 fail("the bag does not respect the property type we declared for the property"); 152 } 153 } 154 155 @Test checkSequenceAccess()156 public void checkSequenceAccess() throws com.sun.star.uno.Exception 157 { 158 System.out.println( "checking PropertySetAccess via sequences" ); 159 createStandardBag( false ); 160 161 // --------------------------------- 162 // XPropertyAccess.setPropertyValues 163 final PropertyValue expectedValues[] = 164 { 165 new PropertyValue( "BoolValue", -1, Boolean.FALSE, PropertyState.DIRECT_VALUE ), 166 new PropertyValue( "StringValue", -1, "some text", PropertyState.DIRECT_VALUE ), 167 new PropertyValue( "IntegerValue", -1, Integer.valueOf( 3 ), PropertyState.DIRECT_VALUE ), 168 new PropertyValue( "InterfaceValue", -1, m_bag, PropertyState.DIRECT_VALUE ) 169 }; 170 m_access.setPropertyValues( expectedValues ); 171 172 for ( int i=0; i<expectedValues.length; ++i ) 173 { 174 final Object value = m_set.getPropertyValue( expectedValues[i].Name ); 175 if ( !value.equals( expectedValues[i].Value ) ) 176 { 177 System.out.println( "property name : " + expectedValues[i].Name ); 178 System.out.println( "expected value: " + expectedValues[i].Value.toString() ); 179 System.out.println( "current value : " + value.toString() ); 180 fail( "retrieving a previously set property (" + expectedValues[i].Value.getClass().toString() + ") failed" ); 181 } 182 } 183 184 // --------------------------------- 185 // XPropertyAccess.getPropertyValues 186 final PropertyValue currentValues[] = m_access.getPropertyValues(); 187 for ( int i=0; i<currentValues.length; ++i ) 188 { 189 final String name = currentValues[i].Name; 190 final Object value = currentValues[i].Value; 191 for ( int j=0; j<expectedValues.length; ++j ) 192 { 193 if ( expectedValues[j].Name.equals( name ) ) 194 { 195 if ( !expectedValues[j].Value.equals( value ) ) 196 { 197 System.out.println( "property name : " + expectedValues[j].Name ); 198 System.out.println( "expected value: " + expectedValues[j].Value.toString() ); 199 System.out.println( "current value : " + value.toString() ); 200 fail( "getPropertyValues failed for property '" + name + "' failed" ); 201 } 202 break; 203 } 204 } 205 206 if ( !m_set.getPropertyValue( name ).equals( value ) ) 207 { 208 fail("XPropertyAccess::getPropertyValues() and XPropertyset::getPropertyValue results are inconsistent"); 209 } 210 } 211 } 212 213 @Test checkDynamicSet()214 public void checkDynamicSet() throws com.sun.star.uno.Exception 215 { 216 System.out.println( "checking proper dynamic of the set" ); 217 createStandardBag( false ); 218 219 final PropertyValue props[] = 220 { 221 new PropertyValue( "BoolValue", -1, Boolean.FALSE, PropertyState.DIRECT_VALUE), 222 new PropertyValue( "StringValue", -1, "test", PropertyState.DIRECT_VALUE ), 223 new PropertyValue( "SomeOtherStringValue", -1, "string value", PropertyState.DIRECT_VALUE ) 224 }; 225 226 // try setting some property values which are not existent 227 boolean caughtExpected = false; 228 try 229 { 230 m_access.setPropertyValues( props ); 231 } 232 catch( com.sun.star.beans.UnknownPropertyException e ) { caughtExpected = true; } 233 catch( com.sun.star.uno.Exception e ) { } 234 if ( !caughtExpected ) 235 { 236 fail("the set shouldn't accept unknown property values, if not explicitly told to do so"); 237 } 238 239 // re-create the bag, this time allow it to implicitly add properties 240 createStandardBag( true ); 241 boolean success = false; 242 try { m_access.setPropertyValues( props ); success = true; } 243 catch( com.sun.star.uno.Exception e ) { } 244 if ( !success ) 245 { 246 fail("property bag failed to implicitly add unknown properties"); 247 } 248 249 // see whether this property was really added, and not just ignored 250 final PropertyValue newlyAdded = props[ props.length - 1 ]; 251 try 252 { 253 if ( !m_set.getPropertyValue( newlyAdded.Name ).equals( newlyAdded.Value ) ) 254 { 255 fail("the new property was not really added, or not added with the proper value"); 256 } 257 } 258 catch( com.sun.star.uno.Exception e ) { } 259 } 260 createEmptyBag()261 private void createEmptyBag() 262 { 263 try 264 { 265 m_bag = null; 266 final String serviceName = "com.sun.star.beans.PropertyBag"; 267 m_bag = UnoRuntime.queryInterface(XPropertyContainer.class, m_orb.createInstance(serviceName)); 268 if ( m_bag == null ) 269 { 270 fail("could not create a " + serviceName + " instance"); 271 } 272 m_set = UnoRuntime.queryInterface(XPropertySet.class, m_bag); 273 m_access = UnoRuntime.queryInterface(XPropertyAccess.class, m_bag); 274 } 275 catch( com.sun.star.uno.Exception e ) 276 { 277 } 278 } 279 createStandardBag( boolean allowLazyAdding )280 private void createStandardBag( boolean allowLazyAdding ) 281 { 282 try 283 { 284 m_bag = null; 285 286 final Object initArgs[] = { new NamedValue( "AutomaticAddition", Boolean.valueOf( allowLazyAdding ) ) }; 287 288 final String serviceName = "com.sun.star.beans.PropertyBag"; 289 m_bag = UnoRuntime.queryInterface(XPropertyContainer.class, m_orb.createInstanceWithArguments(serviceName, initArgs)); 290 if ( m_bag == null ) 291 { 292 fail("could not create a " + serviceName + " instance"); 293 } 294 m_set = UnoRuntime.queryInterface(XPropertySet.class, m_bag); 295 m_access = UnoRuntime.queryInterface(XPropertyAccess.class, m_bag); 296 297 final Object properties[][] = 298 { 299 { "BoolValue", Boolean.TRUE }, 300 { "StringValue", "" }, 301 { "IntegerValue", Integer.valueOf( 3 ) }, 302 { "InterfaceValue", (XInterface)m_bag } 303 }; 304 for ( int i=0; i<properties.length; ++i ) 305 { 306 m_bag.addProperty( 307 (String)properties[i][0], 308 PropertyAttribute.MAYBEVOID, 309 properties[i][1] 310 ); 311 } 312 } 313 catch( com.sun.star.uno.Exception e ) 314 { 315 } 316 } 317 } 318