use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class EvaluationTests method expectFail.
private void expectFail(ExpressionParser parser, EvaluationContext eContext, String expressionString, SpelMessage messageCode) {
try {
Expression e = parser.parseExpression(expressionString);
SpelUtilities.printAbstractSyntaxTree(System.out, e);
e.getValue(eContext);
fail();
} catch (SpelEvaluationException see) {
see.printStackTrace();
assertEquals(messageCode, see.getMessageCode());
}
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class EvaluationTests method limitCollectionGrowing.
@Test
public void limitCollectionGrowing() throws Exception {
TestClass instance = new TestClass();
StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true, 3));
Expression expression = parser.parseExpression("foo[2]");
expression.setValue(ctx, "2");
assertThat(instance.getFoo().size(), equalTo(3));
expression = parser.parseExpression("foo[3]");
try {
expression.setValue(ctx, "3");
} catch (SpelEvaluationException see) {
assertEquals(SpelMessage.UNABLE_TO_GROW_COLLECTION, see.getMessageCode());
assertThat(instance.getFoo().size(), equalTo(3));
}
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class EvaluationTests method increment01root.
// For now I am making #this not assignable
@Test
public void increment01root() {
Integer i = 42;
StandardEvaluationContext ctx = new StandardEvaluationContext(i);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("#this++");
assertEquals(42, i.intValue());
try {
e.getValue(ctx, Integer.class);
fail();
} catch (SpelEvaluationException see) {
assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode());
}
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class EvaluationTests method collectionGrowingViaIndexer.
/**
* This test is checking that with the changes for 9751 that the refactoring in Indexer is
* coping correctly for references beyond collection boundaries.
*/
@Test
public void collectionGrowingViaIndexer() {
Spr9751 instance = new Spr9751();
// Add a new element to the list
StandardEvaluationContext ctx = new StandardEvaluationContext(instance);
ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression e = parser.parseExpression("listOfStrings[++index3]='def'");
e.getValue(ctx);
assertEquals(2, instance.listOfStrings.size());
assertEquals("def", instance.listOfStrings.get(1));
// Check reference beyond end of collection
ctx = new StandardEvaluationContext(instance);
parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
e = parser.parseExpression("listOfStrings[0]");
String value = e.getValue(ctx, String.class);
assertEquals("abc", value);
e = parser.parseExpression("listOfStrings[1]");
value = e.getValue(ctx, String.class);
assertEquals("def", value);
e = parser.parseExpression("listOfStrings[2]");
value = e.getValue(ctx, String.class);
assertEquals("", value);
// Now turn off growing and reference off the end
ctx = new StandardEvaluationContext(instance);
parser = new SpelExpressionParser(new SpelParserConfiguration(false, false));
e = parser.parseExpression("listOfStrings[3]");
try {
e.getValue(ctx, String.class);
fail();
} catch (SpelEvaluationException see) {
assertEquals(SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS, see.getMessageCode());
}
}
use of org.springframework.expression.Expression in project spring-framework by spring-projects.
the class ExpressionLanguageScenarioTests method testScenario_UsingStandardInfrastructure.
/**
* Scenario: using the standard infrastructure and running simple expression evaluation.
*/
@Test
public void testScenario_UsingStandardInfrastructure() {
try {
// Create a parser
SpelExpressionParser parser = new SpelExpressionParser();
// Parse an expression
Expression expr = parser.parseRaw("new String('hello world')");
// Evaluate it using a 'standard' context
Object value = expr.getValue();
// They are reusable
value = expr.getValue();
assertEquals("hello world", value);
assertEquals(String.class, value.getClass());
} catch (EvaluationException ee) {
ee.printStackTrace();
fail("Unexpected Exception: " + ee.getMessage());
} catch (ParseException pe) {
pe.printStackTrace();
fail("Unexpected Exception: " + pe.getMessage());
}
}
Aggregations