use of org.springframework.expression.spel.testresources.PlaceOfBirth in project spring-framework by spring-projects.
the class TestScenarioCreator method setupRootContextObject.
/**
* Create the root context object, an Inventor instance. Non-qualified property
* and method references will be resolved against this context object.
* @param testContext the evaluation context in which to set the root object
*/
private static void setupRootContextObject(StandardEvaluationContext testContext) {
GregorianCalendar c = new GregorianCalendar();
c.set(1856, 7, 9);
Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
tesla.setPlaceOfBirth(new PlaceOfBirth("SmilJan"));
tesla.setInventions(new String[] { "Telephone repeater", "Rotating magnetic field principle", "Polyphase alternating-current system", "Induction motor", "Alternating-current power transmission", "Tesla coil transformer", "Wireless communication", "Radio", "Fluorescent lights" });
testContext.setRootObject(tesla);
}
use of org.springframework.expression.spel.testresources.PlaceOfBirth in project spring-framework by spring-projects.
the class MethodInvocationTests method testMethodThrowingException_SPR6760.
@Test
public void testMethodThrowingException_SPR6760() {
// Test method on inventor: throwException()
// On 1 it will throw an IllegalArgumentException
// On 2 it will throw a RuntimeException
// On 3 it will exit normally
// In each case it increments the Inventor field 'counter' when invoked
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("throwException(#bar)");
// Normal exit
StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
eContext.setVariable("bar", 3);
Object o = expr.getValue(eContext);
assertEquals(o, 3);
assertEquals(1, parser.parseExpression("counter").getValue(eContext));
// Now the expression has cached that throwException(int) is the right thing to call
// Let's change 'bar' to be a PlaceOfBirth which indicates the cached reference is
// out of date.
eContext.setVariable("bar", new PlaceOfBirth("London"));
o = expr.getValue(eContext);
assertEquals("London", o);
// That confirms the logic to mark the cached reference stale and retry is working
// Now let's cause the method to exit via exception and ensure it doesn't cause a retry.
// First, switch back to throwException(int)
eContext.setVariable("bar", 3);
o = expr.getValue(eContext);
assertEquals(3, o);
assertEquals(2, parser.parseExpression("counter").getValue(eContext));
// Now cause it to throw an exception:
eContext.setVariable("bar", 1);
try {
o = expr.getValue(eContext);
fail();
} catch (Exception ex) {
if (ex instanceof SpelEvaluationException) {
fail("Should not be a SpelEvaluationException: " + ex);
}
// normal
}
// If counter is 4 then the method got called twice!
assertEquals(3, parser.parseExpression("counter").getValue(eContext));
eContext.setVariable("bar", 4);
try {
o = expr.getValue(eContext);
fail();
} catch (Exception ex) {
// 4 means it will throw a checked exception - this will be wrapped
if (!(ex instanceof ExpressionInvocationTargetException)) {
fail("Should have been wrapped: " + ex);
}
// normal
}
// If counter is 5 then the method got called twice!
assertEquals(4, parser.parseExpression("counter").getValue(eContext));
}
use of org.springframework.expression.spel.testresources.PlaceOfBirth in project spring-framework by spring-projects.
the class ConstructorInvocationTests method testConstructorThrowingException_SPR6760.
@Test
public void testConstructorThrowingException_SPR6760() {
// Test ctor on inventor:
// On 1 it will throw an IllegalArgumentException
// On 2 it will throw a RuntimeException
// On 3 it will exit normally
// In each case it increments the Tester field 'counter' when invoked
SpelExpressionParser parser = new SpelExpressionParser();
Expression expr = parser.parseExpression("new org.springframework.expression.spel.ConstructorInvocationTests$Tester(#bar).i");
// Normal exit
StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext();
eContext.setRootObject(new Tester());
eContext.setVariable("bar", 3);
Object o = expr.getValue(eContext);
assertEquals(o, 3);
assertEquals(1, parser.parseExpression("counter").getValue(eContext));
// Now the expression has cached that throwException(int) is the right thing to
// call. Let's change 'bar' to be a PlaceOfBirth which indicates the cached
// reference is out of date.
eContext.setVariable("bar", new PlaceOfBirth("London"));
o = expr.getValue(eContext);
assertEquals(0, o);
// That confirms the logic to mark the cached reference stale and retry is working
// Now let's cause the method to exit via exception and ensure it doesn't cause
// a retry.
// First, switch back to throwException(int)
eContext.setVariable("bar", 3);
o = expr.getValue(eContext);
assertEquals(3, o);
assertEquals(2, parser.parseExpression("counter").getValue(eContext));
// 4 will make it throw a checked exception - this will be wrapped by spel on the
// way out
eContext.setVariable("bar", 4);
try {
o = expr.getValue(eContext);
fail("Should have failed");
} catch (Exception e) {
// A problem occurred whilst attempting to construct an object of type
// 'org.springframework.expression.spel.ConstructorInvocationTests$Tester'
// using arguments '(java.lang.Integer)'
int idx = e.getMessage().indexOf("Tester");
if (idx == -1) {
fail("Expected reference to Tester in :" + e.getMessage());
}
// normal
}
// If counter is 4 then the method got called twice!
assertEquals(3, parser.parseExpression("counter").getValue(eContext));
// 1 will make it throw a RuntimeException - SpEL will let this through
eContext.setVariable("bar", 1);
try {
o = expr.getValue(eContext);
fail("Should have failed");
} catch (Exception e) {
// using arguments '(java.lang.Integer)'
if (e instanceof SpelEvaluationException) {
e.printStackTrace();
fail("Should not have been wrapped");
}
}
// If counter is 5 then the method got called twice!
assertEquals(4, parser.parseExpression("counter").getValue(eContext));
}
Aggregations