@Test public void validate() { when(propertyValidation1.hasErrors()).thenReturn(true); when(property1.validate()).thenReturn(propertyValidation1); when(propertyValidation2.hasErrors()).thenReturn(false); when(property2.validate()).thenReturn(propertyValidation2); Map<AbstractProperty, PropertyValidation> validateMap = configuration.validate(); assertThat(validateMap, hasEntry(property1, propertyValidation1)); assertThat(validateMap, not(hasKey(property2))); } }
/** * 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)); }
Map<AbstractProperty, PropertyValidation> validationMap = configuration.validate();