Java Code Examples for org.springframework.expression.spel.standard.SpelExpressionParser#parseExpression()

The following examples show how to use org.springframework.expression.spel.standard.SpelExpressionParser#parseExpression() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpelReproTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void SPR11348() {
	Collection<String> coll = new LinkedHashSet<>();
	coll.add("one");
	coll.add("two");
	coll = Collections.unmodifiableCollection(coll);

	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expr = parser.parseExpression("new java.util.ArrayList(#root)");
	Object value = expr.getValue(coll);
	assertTrue(value instanceof ArrayList);
	@SuppressWarnings("rawtypes")
	ArrayList list = (ArrayList) value;
	assertEquals("one", list.get(0));
	assertEquals("two", list.get(1));
}
 
Example 2
Source File: TemplateExpressionParsingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testParsingSimpleTemplateExpression04() throws Exception {
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expr = parser.parseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
	Object o = expr.getValue();
	assertEquals("hello world", o.toString());

	expr = parser.parseExpression("", DEFAULT_TEMPLATE_PARSER_CONTEXT);
	o = expr.getValue();
	assertEquals("", o.toString());

	expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
	o = expr.getValue();
	assertEquals("abc", o.toString());

	expr = parser.parseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
	o = expr.getValue((Object)null);
	assertEquals("abc", o.toString());
}
 
Example 3
Source File: IndexingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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"));
	}
}
 
Example 4
Source File: IndexingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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"));
	}
}
 
Example 5
Source File: IndexingTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void indexIntoGenericPropertyContainingMap() {
	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));
	assertEquals(property, expression.getValue(this, Map.class));
	expression = parser.parseExpression("property['foo']");
	assertEquals("bar", expression.getValue(this));
}
 
Example 6
Source File: TemplateExpressionParsingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testParsingSimpleTemplateExpression03() throws Exception {
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expr = parser.parseExpression("The quick ${'brown'} fox jumped over the ${'lazy'} dog",
			DEFAULT_TEMPLATE_PARSER_CONTEXT);
	Object o = expr.getValue();
	assertEquals("The quick brown fox jumped over the lazy dog", o.toString());
}
 
Example 7
Source File: MapTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void checkConstantMap(String expressionText, boolean expectedToBeConstant) {
	SpelExpressionParser parser = new SpelExpressionParser();
	SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
	SpelNode node = expression.getAST();
	assertTrue(node instanceof InlineMap);
	InlineMap inlineMap = (InlineMap) node;
	if (expectedToBeConstant) {
		assertTrue(inlineMap.isConstant());
	}
	else {
		assertFalse(inlineMap.isConstant());
	}
}
 
Example 8
Source File: TemplateExpressionParsingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testParsingSimpleTemplateExpression01() throws Exception {
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expr = parser.parseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);
	Object o = expr.getValue();
	assertEquals("hello world", o.toString());
}
 
Example 9
Source File: SpelReproTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void SPR12803() {
	StandardEvaluationContext sec = new StandardEvaluationContext();
	sec.setVariable("iterable", Collections.emptyList());
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expression = parser.parseExpression("T(org.springframework.expression.spel.SpelReproTests.FooLists).newArrayList(#iterable)");
	assertTrue(expression.getValue(sec) instanceof ArrayList);
}
 
Example 10
Source File: IndexingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void emptyList() {
	listOfScalarNotGeneric = new ArrayList();
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expression = parser.parseExpression("listOfScalarNotGeneric");
	assertEquals("java.util.ArrayList<?>", expression.getValueTypeDescriptor(this).toString());
	assertEquals("", expression.getValue(this, String.class));
}
 
Example 11
Source File: IndexingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void indexIntoPropertyContainingList() {
	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));
}
 
Example 12
Source File: MapTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void checkConstantMap(String expressionText, boolean expectedToBeConstant) {
	SpelExpressionParser parser = new SpelExpressionParser();
	SpelExpression expression = (SpelExpression) parser.parseExpression(expressionText);
	SpelNode node = expression.getAST();
	assertTrue(node instanceof InlineMap);
	InlineMap inlineMap = (InlineMap) node;
	if (expectedToBeConstant) {
		assertTrue(inlineMap.isConstant());
	}
	else {
		assertFalse(inlineMap.isConstant());
	}
}
 
Example 13
Source File: IndexingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void indexIntoGenericPropertyContainingList() {
	List<String> property = new ArrayList<>();
	property.add("bar");
	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("bar", expression.getValue(this));
}
 
Example 14
Source File: SpelReproTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void SPR9194() {
	TestClass2 one = new TestClass2("abc");
	TestClass2 two = new TestClass2("abc");
	Map<String, TestClass2> map = new HashMap<>();
	map.put("one", one);
	map.put("two", two);

	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expr = parser.parseExpression("['one'] == ['two']");
	assertTrue(expr.getValue(map, Boolean.class));
}
 
Example 15
Source File: SpelReproTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void varargsAgainstProxy_SPR16122() {
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expr = parser.parseExpression("process('a', 'b')");

	VarargsReceiver receiver = new VarargsReceiver();
	VarargsInterface proxy = (VarargsInterface) Proxy.newProxyInstance(
			getClass().getClassLoader(), new Class<?>[] {VarargsInterface.class},
			(proxy1, method, args) -> method.invoke(receiver, args));

	assertEquals("OK", expr.getValue(new StandardEvaluationContext(receiver)));
	assertEquals("OK", expr.getValue(new StandardEvaluationContext(proxy)));
}
 
Example 16
Source File: IndexingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void resolveMapKeyValueTypes() {
	mapNotGeneric = new HashMap();
	mapNotGeneric.put("baseAmount", 3.11);
	mapNotGeneric.put("bonusAmount", 7.17);
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expression = parser.parseExpression("mapNotGeneric");
	assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.HashMap<?, ?>", expression.getValueTypeDescriptor(this).toString());
}
 
Example 17
Source File: IndexingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testListsOfMap() {
	listOfMapsNotGeneric = new ArrayList();
	Map map = new HashMap();
	map.put("fruit", "apple");
	listOfMapsNotGeneric.add(map);
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expression = parser.parseExpression("listOfMapsNotGeneric[0]['fruit']");
	assertEquals("apple", expression.getValue(this, String.class));
}
 
Example 18
Source File: SpelReproTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
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()));
}
 
Example 19
Source File: IndexingTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void resolveCollectionElementTypeNull() {
	SpelExpressionParser parser = new SpelExpressionParser();
	Expression expression = parser.parseExpression("listNotGeneric");
	assertEquals("@org.springframework.expression.spel.IndexingTests$FieldAnnotation java.util.List<?>", expression.getValueTypeDescriptor(this).toString());
}
 
Example 20
Source File: SpelReproTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
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()));
}