/** * {@inheritDoc} */ @Override public void register(Properties properties) { if (CollectionUtils.isNotEmpty(singleProperties)) { for (SingleProperty<?> property : singleProperties) { property.register(properties); } } }
@Test public void register() { GroupedProperty groupedProperty = new GroupedProperty(); groupedProperty.addSingleProperty(singleProperty1); groupedProperty.addSingleProperty(singleProperty2); Properties properties = new Properties(); groupedProperty.register(properties); verify(singleProperty1, times(1)).register(properties); verify(singleProperty2, times(1)).register(properties); verifyNoMoreInteractions(singleProperty1, singleProperty2); } }
@Test public void register() { SingleProperty<Object> singleProperty = new SingleProperty<Object>("", "", "property1", "defaultValue", false, false) { @Override public Object getDefaultValue() { return null; } @Override protected void setDefaultValue(Object defaultValue) { } @Override protected Object getUsedValue() { return "myValue"; } @Override protected void setUsedValue(Object usedValue) { } @Override public AbstractPropertyUpdate<Object> createPropertyUpdate(Object updateValue) { return null; } @Override public Object parseLiteral(String literal) { return null; } }; Properties properties = new Properties(); singleProperty.register(properties); assertThat(properties.getProperty("property1"), is(equalTo("myValue"))); }
/** * Test that if validation fails for the property it won't be included in the * {@link Properties} returned by {@link PropertyManager}. */ @SuppressWarnings("unchecked") @Test public void propertyNotValidInDefaultConfiguration() throws JAXBException, IOException, SAXException { doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); SingleProperty<?> property = mock(SingleProperty.class); PropertyValidation propertyValidation = mock(PropertyValidation.class, Mockito.RETURNS_SMART_NULLS); when(configuration.getAllProperties()).thenReturn(Collections.<AbstractProperty> singleton(property)); when(configuration.validate()).thenReturn(MapUtils.putAll(new HashMap<AbstractProperty, PropertyValidation>(), new Object[][] { { property, propertyValidation } })); Properties properties = propertyManager.getProperties(); verify(property, times(0)).register(Matchers.<Properties> anyObject()); assertThat(properties.size(), is(0)); }
/** * Test that the {@link Properties} returned by the {@link PropertyManager} are correctly * take from {@link Configuration}. */ @Test public void propertyInDefaultConfiguration() throws JAXBException, IOException, SAXException { doReturn(configuration).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(Configuration.class)); doReturn(null).when(transformator).unmarshall(Matchers.<Path> anyObject(), Matchers.<Path> anyObject(), eq(ConfigurationUpdate.class)); when(configuration.validate()).thenReturn(Collections.<AbstractProperty, PropertyValidation> emptyMap()); SingleProperty<?> property = mock(SingleProperty.class); Answer<Object> answer = new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Properties properties = (Properties) invocation.getArguments()[0]; properties.put("property1", "value1"); return null; } }; doAnswer(answer).when(property).register(Matchers.<Properties> anyObject()); when(configuration.getAllProperties()).thenReturn(Collections.<AbstractProperty> singleton(property)); Properties properties = propertyManager.getProperties(); assertThat(properties.getProperty("property1"), is(equalTo("value1"))); assertThat(properties.size(), is(1)); }