/** * Returns property value as human-readable string . * * @return Returns property value as human-readable string . */ public String getFormattedValue() { return getValue().toString(); }
/** * {@inheritDoc} */ @Override public void register(Properties properties) { properties.setProperty(logicalName, getValue().toString()); }
/** * @return Returns the last correctly set value for the property. */ protected V getLastCorrectValue() { if (null != propertyUpdate) { return propertyUpdate.getUpdateValue(); } else { return property.getValue(); } }
/** * {@inheritDoc} */ @Override public void validate(SingleProperty<? extends T> property, PropertyValidation propertyValidation) { T against = getAgainstValue(property, propertyValidation); if (null != against) { compare(property, property.getValue(), against, propertyValidation); } }
/** * {@inheritDoc} */ public void validate(SingleProperty<? extends T> property, PropertyValidation propertyValidation) { if (!prove(property.getValue())) { addValidationError(property, propertyValidation, getErrorMessage(property)); } }
@Test public void validatorIsNotEmpty() { when(singleProperty.getLogicalName()).thenReturn("my.property"); NotEmptyValidator<Object> validator = new NotEmptyValidator<>(); when(singleProperty.getValue()).thenReturn("Some string"); verifyZeroInteractions(propertyValidation); when(singleProperty.getValue()).thenReturn(""); validator.validate(singleProperty, propertyValidation); when(singleProperty.getValue()).thenReturn(Collections.emptyList()); validator.validate(singleProperty, propertyValidation); when(singleProperty.getValue()).thenReturn(Collections.emptyMap()); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(3)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsPercentage() { when(singleProperty.getValue()).thenReturn(0.75f); when(singleProperty.getLogicalName()).thenReturn("my.property"); PercentageValidator<Number> validator = new PercentageValidator<>(); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); }
@Test public void validatorIsPositive() { when(singleProperty.getLogicalName()).thenReturn("my.property"); PositiveValidator<Number> validator = new PositiveValidator<>(); when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); when(singleProperty.getValue()).thenReturn(Integer.valueOf(-10)); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsNegative() { when(singleProperty.getLogicalName()).thenReturn("my.property"); NegativeValidator<Number> validator = new NegativeValidator<>(); when(singleProperty.getValue()).thenReturn(Integer.valueOf(-10)); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test @SuppressWarnings("unchecked") public void fieldOnePropertyNotMatchingType() { SingleProperty<Integer> property = Mockito.mock(SingleProperty.class); Mockito.when(property.getLogicalName()).thenReturn(PROPERTY1); Mockito.when(property.getValue()).thenReturn(Integer.valueOf(10)); Mockito.when(typeConverter.convertIfNecessary(Integer.valueOf(10), int.class)).thenReturn(10); FieldTestClass fieldTestClass = new FieldTestClass(); Object returnedObject = propertyUpdateExecutor.postProcessAfterInitialization(fieldTestClass, "testClass"); propertyUpdateExecutor.executePropertyUpdates(Collections.<SingleProperty<?>> singletonList(property)); assertThat(returnedObject, is((Object) fieldTestClass)); assertThat(fieldTestClass.update1, is(10)); assertThat(fieldTestClass.update2, is(nullValue())); assertThat(fieldTestClass.noUpdate, is(nullValue())); }
@Test public void validatorIsFullyQualifiedClassName() { SingleProperty<String> singleProperty = mock(SingleProperty.class); when(singleProperty.getLogicalName()).thenReturn("my.property"); FullyQualifiedClassNameValidator validator = new FullyQualifiedClassNameValidator(); when(singleProperty.getValue()).thenReturn(getClass().getName()); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); // test not FQN when(singleProperty.getValue()).thenReturn("package,Class"); validator.validate(singleProperty, propertyValidation); when(singleProperty.getValue()).thenReturn("1canot.start.with.Number"); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(2)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test @SuppressWarnings("unchecked") public void fieldOnePropertyMatchingType() { SingleProperty<Long> property = Mockito.mock(SingleProperty.class); Mockito.when(property.getLogicalName()).thenReturn(PROPERTY2); Mockito.when(property.getValue()).thenReturn(Long.valueOf(10)); FieldTestClass fieldTestClass = new FieldTestClass(); Object returnedObject = propertyUpdateExecutor.postProcessAfterInitialization(fieldTestClass, "testClass"); propertyUpdateExecutor.executePropertyUpdates(Collections.<SingleProperty<?>> singletonList(property)); Mockito.verifyZeroInteractions(typeConverter); assertThat(returnedObject, is((Object) fieldTestClass)); assertThat(fieldTestClass.update1, is(0)); assertThat(fieldTestClass.update2, is(10L)); assertThat(fieldTestClass.noUpdate, is(nullValue())); }
@Test @SuppressWarnings("unchecked") public void proxyFieldTargetNull() throws Exception { SingleProperty<Long> property = Mockito.mock(SingleProperty.class); Mockito.when(property.getLogicalName()).thenReturn(PROPERTY2); Mockito.when(property.getValue()).thenReturn(Long.valueOf(10)); FieldTestClass fieldTestClass = new FieldTestClass(); Mockito.when(targetSource.getTarget()).thenReturn(null); InvocationHandler handler = new TestInvocationHanler(); Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { SpringProxy.class, Advised.class }, handler); Object returnedObject = propertyUpdateExecutor.postProcessAfterInitialization(proxy, "testClass"); propertyUpdateExecutor.executePropertyUpdates(Collections.<SingleProperty<?>> singletonList(property)); Mockito.verifyZeroInteractions(typeConverter); assertThat(returnedObject, is(proxy)); assertThat(fieldTestClass.update1, is(0)); assertThat(fieldTestClass.update2, is(nullValue())); assertThat(fieldTestClass.noUpdate, is(nullValue())); }
@Test @SuppressWarnings("unchecked") public void proxyFieldException() throws Exception { SingleProperty<Long> property = Mockito.mock(SingleProperty.class); Mockito.when(property.getLogicalName()).thenReturn(PROPERTY2); Mockito.when(property.getValue()).thenReturn(Long.valueOf(10)); FieldTestClass fieldTestClass = new FieldTestClass(); Mockito.when(targetSource.getTarget()).thenThrow(RuntimeException.class); InvocationHandler handler = new TestInvocationHanler(); Object proxy = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { SpringProxy.class, Advised.class }, handler); Object returnedObject = propertyUpdateExecutor.postProcessAfterInitialization(proxy, "testClass"); propertyUpdateExecutor.executePropertyUpdates(Collections.<SingleProperty<?>> singletonList(property)); Mockito.verifyZeroInteractions(typeConverter); assertThat(returnedObject, is(proxy)); assertThat(fieldTestClass.update1, is(0)); assertThat(fieldTestClass.update2, is(nullValue())); assertThat(fieldTestClass.noUpdate, is(nullValue())); }
@Test public void validatorIsGreaterThanOrEqual() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); GreaterOrEqualValidator<Number> validator = new GreaterOrEqualValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(9, 10, 11); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsLessThan() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); LessValidator<Number> validator = new LessValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(11, 10, 9); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(2)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsLessThanOrEqual() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); LessOrEqualValidator<Number> validator = new LessOrEqualValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(11, 10, 9); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(1)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void validatorIsGreaterThan() { when(singleProperty.getValue()).thenReturn(Integer.valueOf(10)); when(singleProperty.getLogicalName()).thenReturn("my.property"); GreaterValidator<Number> validator = new GreaterValidator<>(); when(singleProperty.parseLiteral(null)).thenReturn(9, 10, 11); validator.validate(singleProperty, propertyValidation); verifyZeroInteractions(propertyValidation); validator.validate(singleProperty, propertyValidation); validator.validate(singleProperty, propertyValidation); verify(propertyValidation, times(2)).addValidationError(Matchers.<ValidationError> anyObject()); }
@Test public void increaseBufferSizeWithOccupancy() throws Exception { Configuration configuration = propertyManager.getConfiguration(); SingleProperty<Float> maxOldSpaceOcc = configuration.forLogicalName("buffer.maxOldSpaceOccupancy"); SingleProperty<Float> minOldSpaceOcc = configuration.forLogicalName("buffer.minOldSpaceOccupancy"); long oldBufferSize = bufferProperties.getInitialBufferSize(); ConfigurationUpdate configurationUpdate = new ConfigurationUpdate(); configurationUpdate.addPropertyUpdate(maxOldSpaceOcc.createAndValidatePropertyUpdate(Float.valueOf(maxOldSpaceOcc.getValue().floatValue() + 0.05f))); configurationUpdate.addPropertyUpdate(minOldSpaceOcc.createAndValidatePropertyUpdate(Float.valueOf(minOldSpaceOcc.getValue().floatValue() + 0.05f))); propertyManager.updateConfiguration(configurationUpdate, false); long newBufferSize = bufferProperties.getInitialBufferSize(); assertThat(newBufferSize, is(greaterThan(oldBufferSize))); assertThat(newBufferSize, is(buffer.getMaxSize())); }
@Test public void increaseExpansionRate() throws Exception { long bufferSize = bufferProperties.getInitialBufferSize(); Configuration configuration = propertyManager.getConfiguration(); SingleProperty<Float> maxExpansionRate = configuration.forLogicalName("buffer.maxObjectExpansionRate"); float oldExpansionrate = bufferProperties.getObjectSecurityExpansionRate(bufferSize); ConfigurationUpdate configurationUpdate = new ConfigurationUpdate(); configurationUpdate.addPropertyUpdate(maxExpansionRate.createAndValidatePropertyUpdate(Float.valueOf(maxExpansionRate.getValue().floatValue() + 0.1f))); propertyManager.updateConfiguration(configurationUpdate, false); float newExpansionrate = bufferProperties.getObjectSecurityExpansionRate(bufferSize); assertThat(newExpansionrate, is(greaterThan(oldExpansionrate))); }