use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class IndexingTests method setGenericPropertyContainingListAutogrow.
@Test
public void setGenericPropertyContainingListAutogrow() {
List<Integer> property = new ArrayList<>();
this.property = property;
SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
Expression expression = parser.parseExpression("property");
assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString());
assertEquals(property, expression.getValue(this));
expression = parser.parseExpression("property[0]");
try {
expression.setValue(this, "4");
} catch (EvaluationException ex) {
assertTrue(ex.getMessage().startsWith("EL1053E"));
}
}
use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class IndexingTests method indexIntoGenericPropertyContainingGrowingList2.
@Test
public void indexIntoGenericPropertyContainingGrowingList2() {
List<String> property2 = new ArrayList<>();
this.property2 = property2;
SpelParserConfiguration configuration = new SpelParserConfiguration(true, true);
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("property2");
assertEquals("java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString());
assertEquals(property2, expression.getValue(this));
expression = parser.parseExpression("property2[0]");
try {
assertEquals("bar", expression.getValue(this));
} catch (EvaluationException ex) {
assertTrue(ex.getMessage().startsWith("EL1053E"));
}
}
use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class StandardTypeLocatorTests method testImports.
@Test
public void testImports() throws EvaluationException {
StandardTypeLocator locator = new StandardTypeLocator();
assertEquals(Integer.class, locator.findType("java.lang.Integer"));
assertEquals(String.class, locator.findType("java.lang.String"));
List<String> prefixes = locator.getImportPrefixes();
assertEquals(1, prefixes.size());
assertTrue(prefixes.contains("java.lang"));
assertFalse(prefixes.contains("java.util"));
assertEquals(Boolean.class, locator.findType("Boolean"));
try {
locator.findType("URL");
fail("Should have failed");
} catch (EvaluationException ee) {
SpelEvaluationException sEx = (SpelEvaluationException) ee;
assertEquals(SpelMessage.TYPE_NOT_FOUND, sEx.getMessageCode());
}
locator.registerImport("java.net");
assertEquals(java.net.URL.class, locator.findType("URL"));
}
use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class SpelReproTests method SPR5899.
@Test
public void SPR5899() throws Exception {
StandardEvaluationContext eContext = new StandardEvaluationContext(new Spr5899Class());
Expression expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(12)");
assertEquals(12, expr.getValue(eContext));
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull(null)");
assertEquals(null, expr.getValue(eContext));
try {
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull2(null)");
expr.getValue();
fail("Should have failed to find a method to which it could pass null");
} catch (EvaluationException see) {
// success
}
eContext.setTypeLocator(new MyTypeLocator());
// varargs
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(null,'a','b')");
assertEquals("ab", expr.getValue(eContext));
// varargs 2 - null is packed into the varargs
expr = new SpelExpressionParser().parseRaw("tryToInvokeWithNull3(12,'a',null,'c')");
assertEquals("anullc", expr.getValue(eContext));
// check we can find the ctor ok
expr = new SpelExpressionParser().parseRaw("new Spr5899Class().toString()");
assertEquals("instance", expr.getValue(eContext));
expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null).toString()");
assertEquals("instance", expr.getValue(eContext));
// ctor varargs
expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a','b').toString()");
assertEquals("instance", expr.getValue(eContext));
// ctor varargs 2
expr = new SpelExpressionParser().parseRaw("new Spr5899Class(null,'a', null, 'b').toString()");
assertEquals("instance", expr.getValue(eContext));
}
use of org.springframework.expression.EvaluationException in project spring-framework by spring-projects.
the class SetValueTests method setValue.
/**
* For use when coercion is happening during a setValue(). The expectedValue should be
* the coerced form of the value.
*/
protected void setValue(String expression, Object value, Object expectedValue) {
try {
Expression e = parser.parseExpression(expression);
if (e == null) {
fail("Parser returned null for expression");
}
if (DEBUG) {
SpelUtilities.printAbstractSyntaxTree(System.out, e);
}
StandardEvaluationContext lContext = TestScenarioCreator.getTestEvaluationContext();
assertTrue("Expression is not writeable but should be", e.isWritable(lContext));
e.setValue(lContext, value);
Object a = expectedValue;
Object b = e.getValue(lContext);
if (!a.equals(b)) {
fail("Not the same: [" + a + "] type=" + a.getClass() + " [" + b + "] type=" + b.getClass());
// assertEquals("Retrieved value was not equal to set value", expectedValue, e.getValue(lContext));
}
} catch (EvaluationException ee) {
ee.printStackTrace();
fail("Unexpected Exception: " + ee.getMessage());
} catch (ParseException pe) {
pe.printStackTrace();
fail("Unexpected Exception: " + pe.getMessage());
}
}
Aggregations