/** * Returns property defined by {@link #property} from the grouped property. If for some reason * property can not be found, {@link PropertyValidation} will be filled with error(s) and * <code>null</code> returned. * * @param groupProperty * {@link GroupedProperty} to search in. * @param propertyValidation * {@link PropertyValidation} to report errors. * @return {@link SingleProperty} or <code>null</code> if finding fails. */ private SingleProperty<?> getProperty(GroupedProperty groupProperty, PropertyValidation propertyValidation) { if (StringUtils.isEmpty(property)) { ValidationError validationError = new ValidationError(new ArrayList<>(groupProperty.getSingleProperties()), "Validation of grouped property " + groupProperty.getName() + " failed because property logical name is not set."); propertyValidation.addValidationError(validationError); return null; } SingleProperty<?> p = groupProperty.forLogicalname(property); if (null == p) { ValidationError validationError = new ValidationError(new ArrayList<>(groupProperty.getSingleProperties()), "Validation of grouped property " + groupProperty.getName() + " failed because property with logical name '" + property + "' does not exist."); propertyValidation.addValidationError(validationError); } return p; }
@Test public void groupValidation() { GroupedProperty groupedProperty = new GroupedProperty(); groupedProperty.addSingleProperty(singleProperty1); groupedProperty.addSingleProperty(singleProperty2); groupedProperty.addValidator(validator1); groupedProperty.addValidator(validator2); PropertyValidation propertyValidation = groupedProperty.validate(); verify(singleProperty1, times(1)).validate(propertyValidation); verify(singleProperty2, times(1)).validate(propertyValidation); verify(validator1, times(1)).validate(groupedProperty, propertyValidation); verify(validator2, times(1)).validate(groupedProperty, propertyValidation); verifyNoMoreInteractions(singleProperty1, singleProperty2, validator1, validator2); }
/** * Default constructor. * * @param groupedProperty * {@link GroupedProperty} to display. * @see PropertyPreferencePage#PropertyPreferencePage(String, java.util.Collection) */ public GroupedPropertyPreferencePage(GroupedProperty groupedProperty) { super(groupedProperty.getName(), groupedProperty.getSingleProperties()); this.setDescription(groupedProperty.getDescription()); this.groupedProperty = groupedProperty; }
@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); } }
/** * {@inheritDoc} */ @Override @SuppressWarnings("unchecked") public void validate(GroupedProperty groupProperty, PropertyValidation propertyValidation) { SingleProperty<?> compare = getProperty(groupProperty, propertyValidation); SingleProperty<?> against = getAgainstProperty(groupProperty, propertyValidation); if ((null == compare) || (null == against)) { return; } try { compare((SingleProperty<T>) compare, (SingleProperty<T>) against, (T) compare.getValue(), (T) against.getValue(), propertyValidation); } catch (Exception e) { ValidationError validationError = new ValidationError(new ArrayList<>(groupProperty.getSingleProperties()), "Validation of grouped property " + groupProperty.getName() + " failed because exception occurred during validation. Exception message: " + e.getMessage()); propertyValidation.addValidationError(validationError); } }
@Test public void advanced() { GroupedProperty groupedProperty = new GroupedProperty(); groupedProperty.addSingleProperty(singleProperty1); groupedProperty.addSingleProperty(singleProperty2); when(singleProperty1.isAdvanced()).thenReturn(true); when(singleProperty2.isAdvanced()).thenReturn(false); assertThat(groupedProperty.isAdvanced(), is(true)); when(singleProperty1.isAdvanced()).thenReturn(false); when(singleProperty2.isAdvanced()).thenReturn(false); assertThat(groupedProperty.isAdvanced(), is(false)); }
@Test public void serverRestartRequired() { GroupedProperty groupedProperty = new GroupedProperty(); groupedProperty.addSingleProperty(singleProperty1); groupedProperty.addSingleProperty(singleProperty2); when(singleProperty1.isServerRestartRequired()).thenReturn(true); when(singleProperty2.isServerRestartRequired()).thenReturn(false); assertThat(groupedProperty.isServerRestartRequired(), is(true)); when(singleProperty1.isServerRestartRequired()).thenReturn(false); when(singleProperty2.isServerRestartRequired()).thenReturn(false); assertThat(groupedProperty.isServerRestartRequired(), is(false)); }
section.addProperty(property2); GroupedProperty groupedProperty = new GroupedProperty("myGroup", "Lets show how can you group properties"); SingleProperty<Long> property3 = new LongProperty("Max rotation", "Define max rotation", "properties.rotation.max", 90L, false, false); groupedProperty.addSingleProperty(property3); SingleProperty<Long> property4 = new LongProperty("Min rotation", "Define min rotation", "properties.rotation.min", 0L, false, false); groupedProperty.addSingleProperty(property4); LessValidator<Long> lessValidator = new LessValidator<>(); lessValidator.setProperty("properties.rotation.min"); lessValidator.setThan("properties.rotation.max"); groupedProperty.addValidator(lessValidator);
@Test public void validatorGroupValidation() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property1"); when(singleProperty2.getValue()).thenReturn(Integer.valueOf(20)); when(singleProperty2.getLogicalName()).thenReturn("my.property2"); Set<SingleProperty<?>> properties = new HashSet<>(); properties.add(singleProperty); properties.add(singleProperty2); when(groupedProperty.forLogicalname("my.property1")).thenReturn(singleProperty); when(groupedProperty.forLogicalname("my.property2")).thenReturn(singleProperty2); LessValidator<Number> validator = new LessValidator<>(); validator.setProperty("my.property1"); validator.setThan("my.property2"); validator.validate(groupedProperty, propertyValidation); verifyZeroInteractions(propertyValidation); // make it fail when(singleProperty.getValue()).thenReturn(Integer.valueOf(30)); validator.validate(groupedProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
GroupedProperty groupedProperty = (GroupedProperty) property; GroupedPropertyPreferencePage preferencePage = new GroupedPropertyPreferencePage(groupedProperty); PreferenceNode preferenceNode = new PreferenceNode(groupedProperty.getName(), preferencePage); subNodes.add(preferenceNode); } else if (property instanceof SingleProperty<?>) {
compare((SingleProperty<T>) compareProperty, (SingleProperty<T>) againstProperty, value, against, propertyValidation); } catch (Exception e) { ValidationError validationError = new ValidationError(new ArrayList<>(groupProperty.getSingleProperties()), "Validation of grouped property " + groupProperty.getName() + " failed because exception occurred during validation. Exception message: " + e.getMessage()); propertyValidation.addValidationError(validationError);
/** * Returns property defined by {@link #this} from the grouped property. If for some reason * property can not be found, {@link PropertyValidation} will be filled with error(s) and * <code>null</code> returned. * * @param groupProperty * {@link GroupedProperty} to search in. * @param propertyValidation * {@link PropertyValidation} to report errors. * @return {@link SingleProperty} or <code>null</code> if finding fails. */ private SingleProperty<?> getAgainstProperty(GroupedProperty groupProperty, PropertyValidation propertyValidation) { if (StringUtils.isEmpty(than)) { ValidationError validationError = new ValidationError(new ArrayList<>(groupProperty.getSingleProperties()), "Validation of grouped property " + groupProperty.getName() + " failed because logical name of the property to compare against is not set."); propertyValidation.addValidationError(validationError); return null; } SingleProperty<?> p = groupProperty.forLogicalname(than); if (null == p) { ValidationError validationError = new ValidationError(new ArrayList<>(groupProperty.getSingleProperties()), "Validation of grouped property " + groupProperty.getName() + " failed because property with logical name '" + than + "' does not exist."); propertyValidation.addValidationError(validationError); } return p; }
@Test public void validationFailsOnSingleProperty() { GroupedProperty groupedProperty = new GroupedProperty(); groupedProperty.addSingleProperty(singleProperty1); groupedProperty.addSingleProperty(singleProperty2); groupedProperty.addValidator(validator1); groupedProperty.addValidator(validator2); doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); PropertyValidation propertyValidation = (PropertyValidation) args[0]; propertyValidation.addValidationError(new ValidationError()); return null; } }).when(singleProperty1).validate(Matchers.<PropertyValidation> anyObject()); PropertyValidation propertyValidation = groupedProperty.validate(); assertThat(propertyValidation.hasErrors(), is(true)); assertThat(propertyValidation.getErrorCount(), is(1)); verify(singleProperty1, times(1)).validate(propertyValidation); verify(singleProperty2, times(1)).validate(propertyValidation); verify(validator1, times(1)).validate(groupedProperty, propertyValidation); verify(validator2, times(1)).validate(groupedProperty, propertyValidation); verifyNoMoreInteractions(singleProperty1, singleProperty2, validator1, validator2); }
@Test public void validationFailsOnSinglePropertyAndGroupValidation() { GroupedProperty groupedProperty = new GroupedProperty(); groupedProperty.addSingleProperty(singleProperty1); groupedProperty.addSingleProperty(singleProperty2); groupedProperty.addValidator(validator1); groupedProperty.addValidator(validator2); PropertyValidation propertyValidation = groupedProperty.validate(); assertThat(propertyValidation.hasErrors(), is(true)); assertThat(propertyValidation.getErrorCount(), is(2));