/** * Returns the {@link SingleProperty} with the given logical name in the configuration or * <code>null</code> if such does not exist. * * @param <T> * Type of property value. * * @param propertyLogicalName * Property logical name to search for. * @return {@link SingleProperty} or <code>null</code> */ @SuppressWarnings("unchecked") public <T> SingleProperty<T> forLogicalName(String propertyLogicalName) { for (AbstractProperty property : getAllProperties()) { SingleProperty<?> returnProperty = property.forLogicalname(propertyLogicalName); if (null != returnProperty) { return (SingleProperty<T>) returnProperty; } } return null; }
/** * Validates all properties in this configuration returning the map of containing the properties * that have validation errors. * * @return Map of properties with {@link PropertyValidation} containing errors. */ public Map<AbstractProperty, PropertyValidation> validate() { Map<AbstractProperty, PropertyValidation> validationMap = new HashMap<>(); for (AbstractProperty property : getAllProperties()) { PropertyValidation propertyValidation = property.validate(); if (propertyValidation.hasErrors()) { validationMap.put(property, propertyValidation); } } return validationMap; }
/** * 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)); }
for (AbstractProperty property : configuration.getAllProperties()) { if (!validationMap.containsKey(property)) { property.register(properties);