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