Polishing
This commit is contained in:
parent
9956cc11bb
commit
551f6c0c66
|
@ -22,6 +22,7 @@ import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.jspecify.annotations.Nullable;
|
import org.jspecify.annotations.Nullable;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
|
@ -29,13 +30,12 @@ import org.springframework.core.convert.TypeDescriptor;
|
||||||
import org.springframework.expression.Expression;
|
import org.springframework.expression.Expression;
|
||||||
import org.springframework.expression.TypeConverter;
|
import org.springframework.expression.TypeConverter;
|
||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
import org.springframework.expression.spel.support.StandardTypeConverter;
|
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expression evaluation where the TypeConverter plugged in uses the
|
* Expression evaluation where the {@link TypeConverter} plugged in uses the
|
||||||
* {@link org.springframework.core.convert.support.GenericConversionService}.
|
* {@link org.springframework.core.convert.support.GenericConversionService}.
|
||||||
*
|
*
|
||||||
* @author Andy Clement
|
* @author Andy Clement
|
||||||
|
@ -49,31 +49,35 @@ class ExpressionWithConversionTests extends AbstractExpressionTests {
|
||||||
|
|
||||||
private static final TypeDescriptor typeDescriptorForListOfString =
|
private static final TypeDescriptor typeDescriptorForListOfString =
|
||||||
new TypeDescriptor(ReflectionUtils.findField(ExpressionWithConversionTests.class, "listOfString"));
|
new TypeDescriptor(ReflectionUtils.findField(ExpressionWithConversionTests.class, "listOfString"));
|
||||||
private static TypeDescriptor typeDescriptorForListOfInteger =
|
private static final TypeDescriptor typeDescriptorForListOfInteger =
|
||||||
new TypeDescriptor(ReflectionUtils.findField(ExpressionWithConversionTests.class, "listOfInteger"));
|
new TypeDescriptor(ReflectionUtils.findField(ExpressionWithConversionTests.class, "listOfInteger"));
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the service can convert what we are about to use in the expression evaluation tests.
|
* Test the service can convert what we are about to use in the expression evaluation tests.
|
||||||
*/
|
*/
|
||||||
@Test
|
@BeforeAll
|
||||||
void conversionsAreSupportedByStandardTypeConverter() {
|
@SuppressWarnings("unchecked")
|
||||||
StandardTypeConverter typeConverter = new StandardTypeConverter();
|
static void verifyConversionsAreSupportedByStandardTypeConverter() {
|
||||||
|
StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
|
||||||
|
TypeConverter typeConverter = evaluationContext.getTypeConverter();
|
||||||
|
|
||||||
// List<Integer> to List<String>
|
// List<Integer> to List<String>
|
||||||
Class<?> clazz = typeDescriptorForListOfString.getElementTypeDescriptor().getType();
|
assertThat(typeDescriptorForListOfString.getElementTypeDescriptor().getType())
|
||||||
assertThat(clazz).isEqualTo(String.class);
|
.isEqualTo(String.class);
|
||||||
List<?> l = (List<?>) typeConverter.convertValue(listOfInteger, TypeDescriptor.forObject(listOfInteger), typeDescriptorForListOfString);
|
List<String> strings = (List<String>) typeConverter.convertValue(listOfInteger,
|
||||||
assertThat(l).isNotNull();
|
typeDescriptorForListOfInteger, typeDescriptorForListOfString);
|
||||||
|
assertThat(strings).containsExactly("4", "5", "6");
|
||||||
|
|
||||||
// List<String> to List<Integer>
|
// List<String> to List<Integer>
|
||||||
clazz = typeDescriptorForListOfInteger.getElementTypeDescriptor().getType();
|
assertThat(typeDescriptorForListOfInteger.getElementTypeDescriptor().getType())
|
||||||
assertThat(clazz).isEqualTo(Integer.class);
|
.isEqualTo(Integer.class);
|
||||||
|
List<Integer> integers = (List<Integer>) typeConverter.convertValue(listOfString,
|
||||||
l = (List<?>) typeConverter.convertValue(listOfString, TypeDescriptor.forObject(listOfString), typeDescriptorForListOfString);
|
typeDescriptorForListOfString, typeDescriptorForListOfInteger);
|
||||||
assertThat(l).isNotNull();
|
assertThat(integers).containsExactly(1, 2, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void setParameterizedList() {
|
void setParameterizedList() {
|
||||||
StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
|
StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
|
||||||
|
@ -82,10 +86,11 @@ class ExpressionWithConversionTests extends AbstractExpressionTests {
|
||||||
assertThat(e.getValue(context, Integer.class)).isZero();
|
assertThat(e.getValue(context, Integer.class)).isZero();
|
||||||
|
|
||||||
// Assign a List<String> to the List<Integer> field - the component elements should be converted
|
// Assign a List<String> to the List<Integer> field - the component elements should be converted
|
||||||
parser.parseExpression("listOfInteger").setValue(context,listOfString);
|
parser.parseExpression("listOfInteger").setValue(context, listOfString);
|
||||||
// size now 3
|
// size now 3
|
||||||
assertThat(e.getValue(context, Integer.class)).isEqualTo(3);
|
assertThat(e.getValue(context, Integer.class)).isEqualTo(3);
|
||||||
Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer
|
// element type correctly Integer
|
||||||
|
Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class);
|
||||||
assertThat(clazz).isEqualTo(Integer.class);
|
assertThat(clazz).isEqualTo(Integer.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,11 +100,7 @@ class ExpressionWithConversionTests extends AbstractExpressionTests {
|
||||||
class TestTarget {
|
class TestTarget {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
public int sum(Collection<Integer> numbers) {
|
public int sum(Collection<Integer> numbers) {
|
||||||
int total = 0;
|
return numbers.stream().reduce(0, (a, b) -> a + b);
|
||||||
for (int i : numbers) {
|
|
||||||
total += i;
|
|
||||||
}
|
|
||||||
return total;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,9 +118,8 @@ class ExpressionWithConversionTests extends AbstractExpressionTests {
|
||||||
evaluationContext.setVariable("target", new TestTarget());
|
evaluationContext.setVariable("target", new TestTarget());
|
||||||
|
|
||||||
// OK up to here, so the evaluation should be fine...
|
// OK up to here, so the evaluation should be fine...
|
||||||
// ... but this fails
|
int sum = parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4", int.class);
|
||||||
int result = parser.parseExpression("#target.sum(#root)").getValue(evaluationContext, "1,2,3,4", int.class);
|
assertThat(sum).isEqualTo(10);
|
||||||
assertThat(result).isEqualTo(10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue