private void createStandardBag(boolean allowLazyAdding) { try { m_bag = null; final Object initArgs[] = { new NamedValue("AutomaticAddition", Boolean.valueOf(allowLazyAdding)) }; final String serviceName = "com.sun.star.beans.PropertyBag"; m_bag = UnoRuntime.queryInterface( XPropertyContainer.class, m_orb.createInstanceWithArguments(serviceName, initArgs)); if (m_bag == null) { fail("could not create a " + serviceName + " instance"); } m_set = UnoRuntime.queryInterface(XPropertySet.class, m_bag); m_access = UnoRuntime.queryInterface(XPropertyAccess.class, m_bag); final Object properties[][] = { {"BoolValue", Boolean.TRUE}, {"StringValue", ""}, {"IntegerValue", Integer.valueOf(3)}, {"InterfaceValue", m_bag} }; for (int i = 0; i < properties.length; ++i) { m_bag.addProperty((String) properties[i][0], PropertyAttribute.MAYBEVOID, properties[i][1]); } } catch (com.sun.star.uno.Exception e) { } }
@Test public void checkBasics() { createEmptyBag(); System.out.println("testing the basics"); // check whether empty property names are rejected boolean caughtExpected = false; try { m_bag.addProperty("", PropertyAttribute.BOUND, Integer.valueOf(3)); } catch (com.sun.star.lang.IllegalArgumentException e) { caughtExpected = true; } catch (com.sun.star.uno.Exception e) { } if (!caughtExpected) { fail("empty property names are not rejected by XPropertyContainer::addProperty"); } // check whether duplicate insertions are rejected caughtExpected = false; try { m_bag.addProperty(VALUE, PropertyAttribute.BOUND, ""); m_bag.addProperty(VALUE, PropertyAttribute.BOUND, ""); } catch (com.sun.star.beans.PropertyExistException e) { caughtExpected = true; } catch (com.sun.star.uno.Exception e) { } if (!caughtExpected) { fail("insertion of duplicate property names is not rejected"); } // try removing the property we just added - this should fail, as it does not have // the REMOVABLE attribute caughtExpected = false; try { m_bag.removeProperty(VALUE); } catch (com.sun.star.beans.NotRemoveableException e) { caughtExpected = true; } catch (com.sun.star.uno.Exception e) { } if (!caughtExpected) { fail("removing non-removable properties is expected to fail - but it didn't"); } // try removing a non-existent property caughtExpected = false; try { m_bag.removeProperty("NonExistent"); } catch (com.sun.star.beans.UnknownPropertyException e) { caughtExpected = true; } catch (com.sun.star.uno.Exception e) { } if (!caughtExpected) { fail("removing non-existent properties is expected to fail - but it didn't"); } // try writing and reading a value for the one property we have so far try { final String testValue = "someArbitraryValue"; m_set.setPropertyValue(VALUE, testValue); final String currentValue = (String) m_set.getPropertyValue(VALUE); if (!currentValue.equals(testValue)) { fail("set property is not remembered"); } } catch (com.sun.star.uno.Exception e) { fail("setting or getting a property value failed"); } // try setting an illegal value for the property caughtExpected = false; try { m_set.setPropertyValue(VALUE, Integer.valueOf(3)); } catch (com.sun.star.lang.IllegalArgumentException e) { caughtExpected = true; } catch (com.sun.star.uno.Exception e) { } if (!caughtExpected) { fail("the bag does not respect the property type we declared for the property"); } }