Optimize test code with lambdas where feasible

This commit optimizes test code with lambda expressions
and method references where feasible.

Closes gh-23626
This commit is contained in:
OLPMO 2019-09-12 19:09:31 +08:00 committed by Sam Brannen
parent 00c07e3a50
commit 40fcf876ce
4 changed files with 15 additions and 25 deletions

View File

@ -277,14 +277,11 @@ public class MethodInvocationTests extends AbstractExpressionTests {
final BytesService service = new BytesService();
byte[] bytes = new byte[100];
StandardEvaluationContext context = new StandardEvaluationContext(bytes);
context.setBeanResolver(new BeanResolver() {
@Override
public Object resolve(EvaluationContext context, String beanName) throws AccessException {
if ("service".equals(beanName)) {
return service;
}
return null;
context.setBeanResolver((context1, beanName) -> {
if ("service".equals(beanName)) {
return service;
}
return null;
});
Expression expression = parser.parseExpression("@service.handleBytes(#root)");
byte[] outBytes = expression.getValue(context, byte[].class);

View File

@ -5156,8 +5156,7 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
}
private void assertGetValueFail(Expression expression) {
assertThatExceptionOfType(Exception.class).isThrownBy(() ->
expression.getValue());
assertThatExceptionOfType(Exception.class).isThrownBy(expression::getValue);
}
private void assertIsCompiled(Expression expression) {

View File

@ -1326,18 +1326,14 @@ public class SpelReproTests extends AbstractExpressionTests {
@Override
public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
List<TypeDescriptor> argumentTypes) throws AccessException {
return new MethodExecutor() {
@Override
public TypedValue execute(EvaluationContext context, Object target, Object... arguments)
throws AccessException {
try {
Method method = XYZ.class.getMethod("values");
Object value = method.invoke(target, arguments);
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
}
catch (Exception ex) {
throw new AccessException(ex.getMessage(), ex);
}
return (context1, target, arguments) -> {
try {
Method method = XYZ.class.getMethod("values");
Object value = method.invoke(target, arguments);
return new TypedValue(value, new TypeDescriptor(new MethodParameter(method, -1)).narrow(value));
}
catch (Exception ex) {
throw new AccessException(ex.getMessage(), ex);
}
};
}

View File

@ -345,8 +345,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
assertThat(property.read(ctx, tester, "property").getValue()).isEqualTo("hello");
// cached accessor used
assertThat(property.read(ctx, tester, "property").getValue()).isEqualTo("hello");
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
property.getSpecificTargetClasses());
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(property::getSpecificTargetClasses);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
property.write(ctx, tester, "property", null));
@ -360,8 +359,7 @@ public class ReflectionHelperTests extends AbstractExpressionTests {
assertThat(field.read(ctx, tester, "field").getValue()).isEqualTo(3);
// cached accessor used
assertThat(field.read(ctx, tester, "field").getValue()).isEqualTo(3);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
field.getSpecificTargetClasses());
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(field::getSpecificTargetClasses);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
field.write(ctx, tester, "field", null));
}