private EvaluationContext getContext() { return TestScenarioCreator.getTestEvaluationContext(); }
/** * @return a new ExpressionState */ private ExpressionState getState() { EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); ExpressionState state = new ExpressionState(context); return state; }
@Test public void testClashingWithSuffixes() throws Exception { // Just wanting to use the prefix or suffix within the template: Expression ex = parser.parseExpression("hello ${3+4} world",DEFAULT_TEMPLATE_PARSER_CONTEXT); String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class); assertEquals("hello 7 world", s); ex = parser.parseExpression("hello ${3+4} wo${'${'}rld",DEFAULT_TEMPLATE_PARSER_CONTEXT); s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class); assertEquals("hello 7 wo${rld", s); ex = parser.parseExpression("hello ${3+4} wo}rld",DEFAULT_TEMPLATE_PARSER_CONTEXT); s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class); assertEquals("hello 7 wo}rld", s); }
@Test public void testComparison() { EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); boolean trueValue = parser.parseExpression("T(java.util.Date) == Birthdate.Class").getValue( context, Boolean.class); assertTrue(trueValue); }
@Test public void testPropertyAccess() throws Exception { EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context); // 1856 assertEquals(1856,year); String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context); assertEquals("SmilJan",city); }
@Test public void testSetParameterizedList() throws Exception { StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); Expression e = parser.parseExpression("listOfInteger.size()"); assertEquals(0,e.getValue(context,Integer.class).intValue()); context.setTypeConverter(new TypeConvertorUsingConversionService()); // Assign a List<String> to the List<Integer> field - the component elements should be converted parser.parseExpression("listOfInteger").setValue(context,listOfString); assertEquals(3,e.getValue(context,Integer.class).intValue()); // size now 3 Class<?> clazz = parser.parseExpression("listOfInteger[1].getClass()").getValue(context, Class.class); // element type correctly Integer assertEquals(Integer.class,clazz); }
private void checkTemplateParsing(String expression, ParserContext context, String expectedValue) { SpelExpressionParser parser = new SpelExpressionParser(); Expression expr = parser.parseExpression(expression, context); assertEquals(expectedValue, expr.getValue(TestScenarioCreator.getTestEvaluationContext())); }
@Test public void testVariableMapAccess() throws Exception { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext(); ctx.setVariable("day", "saturday"); Expression expr = parser.parseExpression("testMap[#day]"); Object value = expr.getValue(ctx, String.class); assertEquals("samstag", value); }
@Test public void testResolvingList() { StandardEvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); try { assertFalse(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class)); fail("should have failed to find List"); } catch (EvaluationException ee) { // success - List not found } ((StandardTypeLocator) context.getTypeLocator()).registerImport("java.util"); assertTrue(parser.parseExpression("T(List)!=null").getValue(context, Boolean.class)); }
@Test public void testCustomMapAccessor() throws Exception { ExpressionParser parser = new SpelExpressionParser(); StandardEvaluationContext ctx = TestScenarioCreator.getTestEvaluationContext(); ctx.addPropertyAccessor(new MapAccessor()); Expression expr = parser.parseExpression("testMap.monday"); Object value = expr.getValue(ctx, String.class); assertEquals("montag", value); }
@Test public void testPropertyNavigation() throws Exception { ExpressionParser parser = new SpelExpressionParser(); // Inventions Array StandardEvaluationContext teslaContext = TestScenarioCreator.getTestEvaluationContext(); // teslaContext.setRootObject(tesla); // evaluates to "Induction motor" String invention = parser.parseExpression("inventions[3]").getValue(teslaContext, String.class); assertEquals("Induction motor",invention); // Members List StandardEvaluationContext societyContext = new StandardEvaluationContext(); IEEE ieee = new IEEE(); ieee.Members[0]= tesla; societyContext.setRootObject(ieee); // evaluates to "Nikola Tesla" String name = parser.parseExpression("Members[0].Name").getValue(societyContext, String.class); assertEquals("Nikola Tesla",name); // List and Array navigation // evaluates to "Wireless communication" invention = parser.parseExpression("Members[0].Inventions[6]").getValue(societyContext, String.class); assertEquals("Wireless communication",invention); }
@Test public void testAssign() throws Exception { StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext(); Expression e = parse("publicName='Andy'"); assertFalse(e.isWritable(eContext)); assertEquals("Andy",e.getValue(eContext)); }
@Test public void testConstruction() { EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); ExpressionState state = new ExpressionState(context); assertEquals(context, state.getEvaluationContext()); }
@Test public void testSetGenericMapElementRequiresCoercion() throws Exception { StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext(); Expression e = parse("mapOfStringToBoolean[42]"); assertNull(e.getValue(eContext)); // Key should be coerced to string representation of 42 e.setValue(eContext, "true"); // All keys should be strings Set<?> ks = parse("mapOfStringToBoolean.keySet()").getValue(eContext, Set.class); for (Object o: ks) { assertEquals(String.class,o.getClass()); } // All values should be booleans Collection<?> vs = parse("mapOfStringToBoolean.values()").getValue(eContext, Collection.class); for (Object o: vs) { assertEquals(Boolean.class, o.getClass()); } // One final test check coercion on the key for a map lookup Object o = e.getValue(eContext); assertEquals(Boolean.TRUE,o); }
/** * 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 | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
@Test public void testSimpleOperations() throws Exception { // no built in support for this: evaluateAndCheckError("'abc'-true",SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES); StandardEvaluationContext eContext = TestScenarioCreator.getTestEvaluationContext(); eContext.setOperatorOverloader(new StringAndBooleanAddition()); SpelExpression expr = (SpelExpression)parser.parseExpression("'abc'+true"); assertEquals("abctrue",expr.getValue(eContext)); expr = (SpelExpression)parser.parseExpression("'abc'-true"); assertEquals("abc",expr.getValue(eContext)); expr = (SpelExpression)parser.parseExpression("'abc'+null"); assertEquals("abcnull",expr.getValue(eContext)); }
protected void setValue(String expression, Object value) { 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); assertEquals("Retrieved value was not equal to set value", value, e.getValue(lContext,value.getClass())); } catch (EvaluationException | ParseException ex) { ex.printStackTrace(); fail("Unexpected Exception: " + ex.getMessage()); } }
/** * Call setValue() but expect it to fail. */ protected void setValueExpectError(String expression, Object value) { 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(); e.setValue(lContext, value); fail("expected an error"); } catch (ParseException pe) { pe.printStackTrace(); fail("Unexpected Exception: " + pe.getMessage()); } catch (EvaluationException ee) { // success! } }
String s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class); assertEquals("hello 4 world",s); assertEquals(3,exprs.length); assertEquals("listOfNumbersUpToTen.$[#root.listOfNumbersUpToTen.$[#this%2==1]==3]",exprs[1].getExpressionString()); s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class); assertEquals("hello world",s); s = ex.getValue(TestScenarioCreator.getTestEvaluationContext(),String.class); assertEquals("hello 4 10 world",s);
@Test public void propertyAccessOnNullTarget_SPR5663() throws AccessException { PropertyAccessor accessor = new ReflectivePropertyAccessor(); EvaluationContext context = TestScenarioCreator.getTestEvaluationContext(); assertFalse(accessor.canRead(context, null, "abc")); assertFalse(accessor.canWrite(context, null, "abc")); try { accessor.read(context, null, "abc"); fail("Should have failed with an IllegalStateException"); } catch (IllegalStateException ex) { // expected } try { accessor.write(context, null, "abc", "foo"); fail("Should have failed with an IllegalStateException"); } catch (IllegalStateException ex) { // expected } }