@Test public void SPR16123() { ExpressionParser parser = new SpelExpressionParser(); parser.parseExpression("simpleProperty").setValue(new BooleanHolder(), null); try { parser.parseExpression("primitiveProperty").setValue(new BooleanHolder(), null); fail("Should have thrown EvaluationException"); } catch (EvaluationException ex) { // expected } }
@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); }
@Test public void testAssignment() throws Exception { Inventor inventor = new Inventor(); StandardEvaluationContext inventorContext = new StandardEvaluationContext(); inventorContext.setRootObject(inventor); parser.parseExpression("foo").setValue(inventorContext, "Alexander Seovic2"); assertEquals("Alexander Seovic2",parser.parseExpression("foo").getValue(inventorContext,String.class)); // alternatively String aleks = parser.parseExpression("foo = 'Alexandar Seovic'").getValue(inventorContext, String.class); assertEquals("Alexandar Seovic",parser.parseExpression("foo").getValue(inventorContext,String.class)); assertEquals("Alexandar Seovic",aleks); }
@Test public void testTernary() throws Exception { String falseString = parser.parseExpression("false ? 'trueExp' : 'falseExp'").getValue(String.class); assertEquals("falseExp",falseString); StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); parser.parseExpression("Name").setValue(societyContext, "IEEE"); societyContext.setVariable("queryName", "Nikola Tesla"); String expression = "isMember(#queryName)? #queryName + ' is a member of the ' " + "+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'"; String queryResultString = parser.parseExpression(expression).getValue(societyContext, String.class); assertEquals("Nikola Tesla is a member of the IEEE Society",queryResultString); // queryResultString = "Nikola Tesla is a member of the IEEE Society" }
@Test public void testDictionaryAccess() throws Exception { StandardEvaluationContext societyContext = new StandardEvaluationContext(); societyContext.setRootObject(new IEEE()); // Officer's Dictionary Inventor pupin = parser.parseExpression("officers['president']").getValue(societyContext, Inventor.class); assertNotNull(pupin); // evaluates to "Idvor" String city = parser.parseExpression("officers['president'].PlaceOfBirth.city").getValue(societyContext, String.class); assertNotNull(city); // setting values Inventor i = parser.parseExpression("officers['advisors'][0]").getValue(societyContext,Inventor.class); assertEquals("Nikola Tesla",i.getName()); parser.parseExpression("officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext, "Croatia"); Inventor i2 = parser.parseExpression("reverse[0]['advisors'][0]").getValue(societyContext,Inventor.class); assertEquals("Nikola Tesla",i2.getName()); }
@Test public void setGenericPropertyContainingMap() { Map<String, String> property = new HashMap<>(); property.put("foo", "bar"); this.property = property; SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("property"); assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("property['foo']"); assertEquals("bar", expression.getValue(this)); expression.setValue(this, "baz"); assertEquals("baz", expression.getValue(this)); }
@Test public void setGenericPropertyContainingList() { List<Integer> property = new ArrayList<>(); property.add(3); this.property = property; SpelExpressionParser parser = new SpelExpressionParser(); 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]"); assertEquals(3, expression.getValue(this)); expression.setValue(this, "4"); assertEquals("4", expression.getValue(this)); }
@Test public void setPropertyContainingMap() { Map<Integer, Integer> property = new HashMap<>(); property.put(9, 3); this.parameterizedMap = property; SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("parameterizedMap"); assertEquals("java.util.HashMap<java.lang.Integer, java.lang.Integer>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("parameterizedMap['9']"); assertEquals(3, expression.getValue(this)); expression.setValue(this, "37"); assertEquals(37, expression.getValue(this)); }
@Test public void setPropertyContainingList() { List<Integer> property = new ArrayList<>(); property.add(3); this.parameterizedList = property; SpelExpressionParser parser = new SpelExpressionParser(); Expression expression = parser.parseExpression("parameterizedList"); assertEquals("java.util.ArrayList<java.lang.Integer>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("parameterizedList[0]"); assertEquals(3, expression.getValue(this)); expression.setValue(this, "4"); assertEquals(4, expression.getValue(this)); }
@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); }
@Test public void propertyReadWrite() { EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build(); Expression expr = parser.parseExpression("name"); Person target = new Person("p1"); assertEquals("p1", expr.getValue(context, target)); target.setName("p2"); assertEquals("p2", expr.getValue(context, target)); parser.parseExpression("name='p3'").getValue(context, target); assertEquals("p3", target.getName()); assertEquals("p3", expr.getValue(context, target)); expr.setValue(context, target, "p4"); assertEquals("p4", target.getName()); assertEquals("p4", expr.getValue(context, target)); }
/** * SPR-6984: attempting to index a collection on write using an index that * doesn't currently exist in the collection (address.crossStreets[0] below) */ @Test public void initializingCollectionElementsOnWrite() { TestPerson person = new TestPerson(); EvaluationContext context = new StandardEvaluationContext(person); SpelParserConfiguration config = new SpelParserConfiguration(true, true); ExpressionParser parser = new SpelExpressionParser(config); Expression e = parser.parseExpression("name"); e.setValue(context, "Oleg"); assertEquals("Oleg", person.getName()); e = parser.parseExpression("address.street"); e.setValue(context, "123 High St"); assertEquals("123 High St", person.getAddress().getStreet()); e = parser.parseExpression("address.crossStreets[0]"); e.setValue(context, "Blah"); assertEquals("Blah", person.getAddress().getCrossStreets().get(0)); e = parser.parseExpression("address.crossStreets[3]"); e.setValue(context, "Wibble"); assertEquals("Blah", person.getAddress().getCrossStreets().get(0)); assertEquals("Wibble", person.getAddress().getCrossStreets().get(3)); }
@Test public void limitCollectionGrowing() { TestClass instance = new TestClass(); StandardEvaluationContext ctx = new StandardEvaluationContext(instance); SpelExpressionParser parser = new SpelExpressionParser( new SpelParserConfiguration(true, true, 3)); Expression e = parser.parseExpression("foo[2]"); e.setValue(ctx, "2"); assertThat(instance.getFoo().size(), equalTo(3)); e = parser.parseExpression("foo[3]"); try { e.setValue(ctx, "3"); } catch (SpelEvaluationException see) { assertEquals(SpelMessage.UNABLE_TO_GROW_COLLECTION, see.getMessageCode()); assertThat(instance.getFoo().size(), equalTo(3)); } }
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()); } }
@Test public void propertyReadWriteWithRootObject() { Person target = new Person("p1"); EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().withRootObject(target).build(); assertSame(target, context.getRootObject().getValue()); Expression expr = parser.parseExpression("name"); assertEquals("p1", expr.getValue(context, target)); target.setName("p2"); assertEquals("p2", expr.getValue(context, target)); parser.parseExpression("name='p3'").getValue(context, target); assertEquals("p3", target.getName()); assertEquals("p3", expr.getValue(context, target)); expr.setValue(context, target, "p4"); assertEquals("p4", target.getName()); assertEquals("p4", expr.getValue(context, target)); }
@Test public void setPropertyContainingMapAutoGrow() { SpelExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, false)); Expression expression = parser.parseExpression("parameterizedMap"); assertEquals("java.util.Map<java.lang.Integer, java.lang.Integer>", expression.getValueTypeDescriptor(this).toString()); assertEquals(property, expression.getValue(this)); expression = parser.parseExpression("parameterizedMap['9']"); assertEquals(null, expression.getValue(this)); expression.setValue(this, "37"); assertEquals(37, expression.getValue(this)); }
/** * 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! } }
@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")); } }
@Test public void testScenario_AddingYourOwnPropertyResolvers_2() throws Exception { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.addPropertyAccessor(new VegetableColourAccessor()); Expression expr = parser.parseRaw("pea"); Object value = expr.getValue(ctx); assertEquals(Color.green, value); try { expr.setValue(ctx, Color.blue); fail("Should not be allowed to set peas to be blue !"); } catch (SpelEvaluationException ee) { assertEquals(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, ee.getMessageCode()); } }
/** * Scenario: add a property resolver that will get called in the resolver chain, this one only supports reading. */ @Test public void testScenario_AddingYourOwnPropertyResolvers_1() throws Exception { // Create a parser SpelExpressionParser parser = new SpelExpressionParser(); // Use the standard evaluation context StandardEvaluationContext ctx = new StandardEvaluationContext(); ctx.addPropertyAccessor(new FruitColourAccessor()); Expression expr = parser.parseRaw("orange"); Object value = expr.getValue(ctx); assertEquals(Color.orange, value); try { expr.setValue(ctx, Color.blue); fail("Should not be allowed to set oranges to be blue !"); } catch (SpelEvaluationException ee) { assertEquals(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, ee.getMessageCode()); } }