DATACMNS-260 - Extracted SpelExpressionParameterValueProvider.

Extracted SpelExpressionParameterValueProvider from PersistentEntityParameterValueProvider to allow extension to recursively map the value resolved from the SpEL expression evaluation. This extension has to override the potentiallyConvertSpelValue(…) method to continue nested conversion.
This commit is contained in:
Oliver Gierke
2012-12-16 15:04:04 +01:00
parent 0c4ed8a86a
commit c289428fe5
4 changed files with 232 additions and 16 deletions

View File

@@ -36,8 +36,6 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty
private final PropertyValueProvider<P> provider;
private final Object parent;
private SpELExpressionEvaluator spELEvaluator;
/**
* Creates a new {@link PersistentEntityParameterValueProvider} for the given {@link PersistentEntity} and
* {@link PropertyValueProvider}.
@@ -57,16 +55,6 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty
this.parent = parent;
}
/**
* Configures a {@link DefaultSpELExpressionEvaluator} to evaluate the SpEL Expression the {@link Parameter}
* potentially carries.
*
* @param spELEvaluator
*/
public void setSpELEvaluator(SpELExpressionEvaluator spELEvaluator) {
this.spELEvaluator = spELEvaluator;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
@@ -74,10 +62,6 @@ public class PersistentEntityParameterValueProvider<P extends PersistentProperty
@SuppressWarnings("unchecked")
public <T> T getParameterValue(Parameter<T, P> parameter) {
if (spELEvaluator != null && parameter.hasSpelExpression()) {
return spELEvaluator.evaluate(parameter.getSpelExpression());
}
PreferredConstructor<?, P> constructor = entity.getPersistenceConstructor();
if (constructor.isEnclosingClassParameter(parameter)) {

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mapping.model;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.util.Assert;
/**
* {@link ParameterValueProvider} that can be used to front a {@link ParameterValueProvider} delegate to prefer a Spel
* expression evaluation over directly resolving the parameter value with the delegate.
*
* @author Oliver Gierke
*/
public class SpELExpressionParameterValueProvider<P extends PersistentProperty<P>> implements ParameterValueProvider<P> {
private final SpELExpressionEvaluator evaluator;
private final ParameterValueProvider<P> delegate;
private final ConversionService conversionService;
/**
* Creates a new {@link SpELExpressionParameterValueProvider} using the given {@link SpELExpressionEvaluator},
* {@link ConversionService} and {@link ParameterValueProvider} delegate to forward calls to, that resolve parameters
* that do not have a Spel expression configured with them.
*
* @param evaluator must not be {@literal null}.
* @param conversionService must not be {@literal null}.
* @param delegate must not be {@literal null}.
*/
public SpELExpressionParameterValueProvider(SpELExpressionEvaluator evaluator, ConversionService conversionService,
ParameterValueProvider<P> delegate) {
Assert.notNull(evaluator, "SpELExpressionEvaluator must not be null!");
Assert.notNull(conversionService, "ConversionService must not be null!");
Assert.notNull(delegate, "ParameterValueProvider delegate must not be null!");
this.evaluator = evaluator;
this.conversionService = conversionService;
this.delegate = delegate;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mapping.model.ParameterValueProvider#getParameterValue(org.springframework.data.mapping.PreferredConstructor.Parameter)
*/
public <T> T getParameterValue(Parameter<T, P> parameter) {
if (!parameter.hasSpelExpression()) {
return delegate == null ? null : delegate.getParameterValue(parameter);
}
Object object = evaluator.evaluate(parameter.getSpelExpression());
return object == null ? null : potentiallyConvertSpelValue(object, parameter);
}
/**
* Hook to allow to massage the value resulting from the Spel expression evaluation. Default implementation will
* leverage the configured {@link ConversionService} to massage the value into the parameter type.
*
* @param object the value to massage, will never be {@literal null}.
* @param parameter the {@link Parameter} we create the value for
* @return
*/
protected <T> T potentiallyConvertSpelValue(Object object, Parameter<T, P> parameter) {
return conversionService.convert(object, parameter.getRawType());
}
}

View File

@@ -20,7 +20,9 @@ import static org.junit.Assert.*;
import java.util.Iterator;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@@ -39,6 +41,9 @@ import org.springframework.data.util.ClassTypeInformation;
@RunWith(MockitoJUnitRunner.class)
public class PersistentEntityParameterValueProviderUnitTests<P extends PersistentProperty<P>> {
@Rule
public ExpectedException exception = ExpectedException.none();
@Mock
PropertyValueProvider<P> propertyValueProvider;
@Mock
@@ -53,6 +58,7 @@ public class PersistentEntityParameterValueProviderUnitTests<P extends Persisten
Object outer = new Outer();
PersistentEntity<Inner, P> entity = new BasicPersistentEntity<Inner, P>(ClassTypeInformation.from(Inner.class)) {
@Override
public P getPersistentProperty(String name) {
return property;
}
@@ -67,6 +73,22 @@ public class PersistentEntityParameterValueProviderUnitTests<P extends Persisten
assertThat(iterator.hasNext(), is(false));
}
@Test
public void rejectsPropertyIfNameDoesNotMatch() {
PersistentEntity<Entity, P> entity = new BasicPersistentEntity<Entity, P>(ClassTypeInformation.from(Entity.class));
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<P>(entity, propertyValueProvider,
property);
PreferredConstructor<Entity, P> constructor = entity.getPersistenceConstructor();
exception.expect(MappingException.class);
exception.expectMessage("bar");
exception.expectMessage(Entity.class.getName());
provider.getParameterValue(constructor.getParameters().iterator().next());
}
static class Outer {
class Inner {
@@ -78,4 +100,13 @@ public class PersistentEntityParameterValueProviderUnitTests<P extends Persisten
}
}
}
static class Entity {
String foo;
public Entity(String bar) {
}
}
}

View File

@@ -0,0 +1,120 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mapping.model;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.AbstractPersistentPropertyUnitTests.SamplePersistentProperty;
/**
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
public class SpelExpressionParameterProviderUnitTests {
@Mock
SpELExpressionEvaluator evaluator;
@Mock
ParameterValueProvider<SamplePersistentProperty> delegate;
@Mock
ConversionService conversionService;
SpELExpressionParameterValueProvider<SamplePersistentProperty> provider;
Parameter<Object, SamplePersistentProperty> parameter;
@Before
@SuppressWarnings("unchecked")
public void setUp() {
provider = new SpELExpressionParameterValueProvider<SamplePersistentProperty>(evaluator, conversionService,
delegate);
parameter = mock(Parameter.class);
when(parameter.hasSpelExpression()).thenReturn(true);
when(parameter.getRawType()).thenReturn(Object.class);
}
@Test
@SuppressWarnings("unchecked")
public void delegatesIfParameterDoesNotHaveASpELExpression() {
Parameter<Object, SamplePersistentProperty> parameter = mock(Parameter.class);
when(parameter.hasSpelExpression()).thenReturn(false);
provider.getParameterValue(parameter);
verify(delegate, times(1)).getParameterValue(parameter);
verify(evaluator, times(0)).evaluate("expression");
}
@Test
public void evaluatesSpELExpression() {
when(parameter.getSpelExpression()).thenReturn("expression");
provider.getParameterValue(parameter);
verify(delegate, times(0)).getParameterValue(parameter);
verify(evaluator, times(1)).evaluate("expression");
}
@Test
public void handsSpELValueToConversionService() {
when(evaluator.evaluate(Mockito.any(String.class))).thenReturn("value");
provider.getParameterValue(parameter);
verify(delegate, times(0)).getParameterValue(parameter);
verify(conversionService, times(1)).convert("value", Object.class);
}
@Test
public void doesNotConvertNullValue() {
when(evaluator.evaluate(Mockito.any(String.class))).thenReturn(null);
provider.getParameterValue(parameter);
verify(delegate, times(0)).getParameterValue(parameter);
verify(conversionService, times(0)).convert("value", Object.class);
}
@Test
public void returnsMassagedObjectOnOverride() {
provider = new SpELExpressionParameterValueProvider<SamplePersistentProperty>(evaluator, conversionService,
delegate) {
@Override
@SuppressWarnings("unchecked")
protected <T> T potentiallyConvertSpelValue(Object object, Parameter<T, SamplePersistentProperty> parameter) {
return (T) "FOO";
}
};
when(evaluator.evaluate(Mockito.anyString())).thenReturn("value");
Object result = provider.getParameterValue(parameter);
assertThat(result, is((Object) "FOO"));
verify(delegate, times(0)).getParameterValue(parameter);
}
}