use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class OpPlusTests method test_binaryPlusWithTimeConverted.
@Test
public void test_binaryPlusWithTimeConverted() {
final SimpleDateFormat format = new SimpleDateFormat("hh :--: mm :--: ss", Locale.ENGLISH);
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(new Converter<Time, String>() {
@Override
public String convert(Time source) {
return format.format(source);
}
});
StandardEvaluationContext evaluationContextConverter = new StandardEvaluationContext();
evaluationContextConverter.setTypeConverter(new StandardTypeConverter(conversionService));
ExpressionState expressionState = new ExpressionState(evaluationContextConverter);
Time time = new Time(new Date().getTime());
VariableReference var = new VariableReference("timeVar", -1);
var.setValue(expressionState, time);
StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\"");
OpPlus o = new OpPlus(-1, var, n2);
TypedValue value = o.getValueInternal(expressionState);
assertEquals(String.class, value.getTypeDescriptor().getObjectType());
assertEquals(String.class, value.getTypeDescriptor().getType());
assertEquals(format.format(time) + " is now", value.getValue());
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class OpPlusTests method test_binaryPlusWithStringOperands.
@Test
public void test_binaryPlusWithStringOperands() {
ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());
StringLiteral n1 = new StringLiteral("\"foo\"", -1, "\"foo\"");
StringLiteral n2 = new StringLiteral("\"bar\"", -1, "\"bar\"");
OpPlus o = new OpPlus(-1, n1, n2);
TypedValue value = o.getValueInternal(expressionState);
assertEquals(String.class, value.getTypeDescriptor().getObjectType());
assertEquals(String.class, value.getTypeDescriptor().getType());
assertEquals("foobar", value.getValue());
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class SpelReproTests method SPR9495.
@Test
public void SPR9495() throws Exception {
SpelParserConfiguration configuration = new SpelParserConfiguration(false, false);
ExpressionParser parser = new SpelExpressionParser(configuration);
StandardEvaluationContext context = new StandardEvaluationContext();
Expression spel = parser.parseExpression("#enumType.values()");
context.setVariable("enumType", ABC.class);
Object result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(ABC.A, Array.get(result, 0));
assertEquals(ABC.B, Array.get(result, 1));
assertEquals(ABC.C, Array.get(result, 2));
context.addMethodResolver(new MethodResolver() {
@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);
}
}
};
}
});
result = spel.getValue(context);
assertNotNull(result);
assertTrue(result.getClass().isArray());
assertEquals(XYZ.X, Array.get(result, 0));
assertEquals(XYZ.Y, Array.get(result, 1));
assertEquals(XYZ.Z, Array.get(result, 2));
}
use of org.springframework.expression.TypedValue in project spring-framework by spring-projects.
the class SpelReproTests method SPR10162_onlyBridgeMethod.
@Test
public void SPR10162_onlyBridgeMethod() throws Exception {
ReflectivePropertyAccessor accessor = new ReflectivePropertyAccessor();
StandardEvaluationContext context = new StandardEvaluationContext();
Object target = new OnlyBridgeMethod();
TypedValue value = accessor.read(context, target, "property");
assertEquals(Integer.class, value.getTypeDescriptor().getType());
}
use of org.springframework.expression.TypedValue in project spring-security by spring-projects.
the class DelegatingEvaluationContextTests method getRootObject.
@Test
public void getRootObject() {
TypedValue expected = mock(TypedValue.class);
when(this.delegate.getRootObject()).thenReturn(expected);
assertThat(this.context.getRootObject()).isEqualTo(expected);
}
Aggregations