Java Code Examples for org.springframework.expression.ExpressionParser#parseExpression()

The following examples show how to use org.springframework.expression.ExpressionParser#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: EvaluationTests.java    From java-technology-stack with MIT License 8 votes vote down vote up
/**
 * 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));
}
 
Example 2
Source File: SpelDocumentationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRootObject() throws Exception {
	GregorianCalendar c = new GregorianCalendar();
	c.set(1856, 7, 9);

	//  The constructor arguments are name, birthday, and nationaltiy.
	Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");

	ExpressionParser parser = new SpelExpressionParser();
	Expression exp = parser.parseExpression("name");

	StandardEvaluationContext context = new StandardEvaluationContext();
	context.setRootObject(tesla);

	String name = (String) exp.getValue(context);
	assertEquals("Nikola Tesla",name);
}
 
Example 3
Source File: SpelExceptionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("serial")
public void spelExpressionListIndexAccessWithVariables() {
	ExpressionParser parser = new SpelExpressionParser();
	Expression spelExpression = parser.parseExpression("#aList[0] eq 'one'");
	StandardEvaluationContext ctx = new StandardEvaluationContext();
	ctx.setVariables(new HashMap<String, Object>() {
		{
			put("aList", new ArrayList<String>() {
				{
					add("one");
					add("two");
					add("three");
				}
			});

		}
	});
	boolean result = spelExpression.getValue(ctx, Boolean.class);
	assertTrue(result);
}
 
Example 4
Source File: MapAccessTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 5
Source File: SpelReproTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void SPR9486_floatGreaterThanFloat() {
	Boolean expectedNumber = -10.21f > -10.2f;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("-10.21f > -10.2f");
	Boolean result = expression.getValue(context, null, Boolean.class);
	assertEquals(expectedNumber, result);
}
 
Example 6
Source File: SpelReproTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void SPR9486_floatNotEqDouble() {
	Boolean expectedResult = 10.215f != 10.2109;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("10.215f != 10.2109");
	Boolean result = expression.getValue(context, null, Boolean.class);
	assertEquals(expectedResult, result);
}
 
Example 7
Source File: SpelReproTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void SPR9486_floatEqDouble() {
	Boolean expectedResult = 10.215f == 10.2109;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("10.215f == 10.2109");
	Boolean result = expression.getValue(context, null, Boolean.class);
	assertEquals(expectedResult, result);
}
 
Example 8
Source File: SelectionAndProjectionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void selectionWithMap() {
	EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
	ExpressionParser parser = new SpelExpressionParser();
	Expression exp = parser.parseExpression("colors.?[key.startsWith('b')]");

	Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
	assertEquals(3, colorsMap.size());
	assertTrue(colorsMap.containsKey("beige"));
	assertTrue(colorsMap.containsKey("blue"));
	assertTrue(colorsMap.containsKey("brown"));
}
 
Example 9
Source File: SpelReproTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void SPR9486_floatEqFloat() {
	Boolean expectedResult = 10.215f == 10.2109f;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("10.215f == 10.2109f");
	Boolean result = expression.getValue(context, null, Boolean.class);
	assertEquals(expectedResult, result);
}
 
Example 10
Source File: SpelReproTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void SPR9486_subtractFloatWithFloat() {
	Number expectedNumber = 10.21f - 10.2f;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("10.21f - 10.2f");
	Number result = expression.getValue(context, null, Number.class);
	assertEquals(expectedNumber, result);
}
 
Example 11
Source File: SelectionAndProjectionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void selectLastItemInMap() {
	EvaluationContext context = new StandardEvaluationContext(new MapTestBean());
	ExpressionParser parser = new SpelExpressionParser();

	Expression exp = parser.parseExpression("colors.$[key.startsWith('b')]");
	Map<String, String> colorsMap = (Map<String, String>) exp.getValue(context);
	assertEquals(1, colorsMap.size());
	assertEquals("brown", colorsMap.keySet().iterator().next());
}
 
Example 12
Source File: SpelReproTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void SPR9486_addFloatWithFloat() {
	Number expectedNumber = 10.21f + 10.2f;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("10.21f + 10.2f");
	Number result = expression.getValue(context, null, Number.class);
	assertEquals(expectedNumber, result);
}
 
Example 13
Source File: EvaluationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void decrement01root() {
	Integer i = 42;
	StandardEvaluationContext ctx = new StandardEvaluationContext(i);
	ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
	Expression e =  parser.parseExpression("#this--");
	assertEquals(42, i.intValue());
	try {
		e.getValue(ctx, Integer.class);
		fail();
	}
	catch (SpelEvaluationException see) {
		assertEquals(SpelMessage.NOT_ASSIGNABLE, see.getMessageCode());
	}
}
 
Example 14
Source File: SpelReproTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void SPR9486_floatLessThanDouble() {
	Boolean expectedNumber = -10.21f < -10.2;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("-10.21f < -10.2");
	Boolean result = expression.getValue(context, null, Boolean.class);
	assertEquals(expectedNumber, result);
}
 
Example 15
Source File: SpelDocumentationTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testEqualityCheck() throws Exception {
	ExpressionParser parser = new SpelExpressionParser();

	StandardEvaluationContext context = new StandardEvaluationContext();
	context.setRootObject(tesla);

	Expression exp = parser.parseExpression("name == 'Nikola Tesla'");
	boolean isEqual = exp.getValue(context, Boolean.class);  // evaluates to true
	assertTrue(isEqual);
}
 
Example 16
Source File: SpelReproTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void SPR9486_floatNotEqDouble() {
	Boolean expectedResult = 10.215f != 10.2109;
	ExpressionParser parser = new SpelExpressionParser();
	StandardEvaluationContext context = new StandardEvaluationContext();
	Expression expression = parser.parseExpression("10.215f != 10.2109");
	Boolean result = expression.getValue(context, null, Boolean.class);
	assertEquals(expectedResult, result);
}
 
Example 17
Source File: SpelExceptionTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test(expected = SpelEvaluationException.class)
public void spelExpressionMapNullVariables() {
	ExpressionParser parser = new SpelExpressionParser();
	Expression spelExpression = parser.parseExpression("#aMap.containsKey('one')");
	spelExpression.getValue();
}
 
Example 18
Source File: SpelExceptionTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test(expected = SpelEvaluationException.class)
public void spelExpressionMapIndexAccessNullVariables() {
	ExpressionParser parser = new SpelExpressionParser();
	Expression spelExpression = parser.parseExpression("#aMap['one'] eq 1");
	spelExpression.getValue();
}
 
Example 19
Source File: EvaluationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void increment02postfix() {
	Spr9751 helper = new Spr9751();
	StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
	ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
	Expression e;

	// BigDecimal
	e = parser.parseExpression("bd++");
	assertTrue(new BigDecimal("2").equals(helper.bd));
	BigDecimal return_bd = e.getValue(ctx, BigDecimal.class);
	assertTrue(new BigDecimal("2").equals(return_bd));
	assertTrue(new BigDecimal("3").equals(helper.bd));

	// double
	e = parser.parseExpression("ddd++");
	assertEquals(2.0d, helper.ddd,0d);
	double return_ddd = e.getValue(ctx, Double.TYPE);
	assertEquals(2.0d, return_ddd,0d);
	assertEquals(3.0d, helper.ddd,0d);

	// float
	e = parser.parseExpression("fff++");
	assertEquals(3.0f, helper.fff,0d);
	float return_fff = e.getValue(ctx, Float.TYPE);
	assertEquals(3.0f, return_fff,0d);
	assertEquals(4.0f, helper.fff,0d);

	// long
	e = parser.parseExpression("lll++");
	assertEquals(66666L, helper.lll);
	long return_lll = e.getValue(ctx, Long.TYPE);
	assertEquals(66666L, return_lll);
	assertEquals(66667L, helper.lll);

	// int
	e = parser.parseExpression("iii++");
	assertEquals(42, helper.iii);
	int return_iii = e.getValue(ctx, Integer.TYPE);
	assertEquals(42, return_iii);
	assertEquals(43, helper.iii);
	return_iii = e.getValue(ctx, Integer.TYPE);
	assertEquals(43, return_iii);
	assertEquals(44, helper.iii);

	// short
	e = parser.parseExpression("sss++");
	assertEquals(15, helper.sss);
	short return_sss = e.getValue(ctx, Short.TYPE);
	assertEquals(15, return_sss);
	assertEquals(16, helper.sss);
}
 
Example 20
Source File: EvaluationTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
public void increment02prefix() {
	Spr9751 helper = new Spr9751();
	StandardEvaluationContext ctx = new StandardEvaluationContext(helper);
	ExpressionParser parser = new SpelExpressionParser(new SpelParserConfiguration(true, true));
	Expression e;


	// BigDecimal
	e = parser.parseExpression("++bd");
	assertTrue(new BigDecimal("2").equals(helper.bd));
	BigDecimal return_bd = e.getValue(ctx, BigDecimal.class);
	assertTrue(new BigDecimal("3").equals(return_bd));
	assertTrue(new BigDecimal("3").equals(helper.bd));

	// double
	e = parser.parseExpression("++ddd");
	assertEquals(2.0d, helper.ddd ,0d);
	double return_ddd = e.getValue(ctx, Double.TYPE);
	assertEquals(3.0d, return_ddd, 0d);
	assertEquals(3.0d, helper.ddd, 0d);

	// float
	e = parser.parseExpression("++fff");
	assertEquals(3.0f, helper.fff, 0d);
	float return_fff = e.getValue(ctx, Float.TYPE);
	assertEquals(4.0f, return_fff, 0d);
	assertEquals(4.0f, helper.fff, 0d);

	// long
	e = parser.parseExpression("++lll");
	assertEquals(66666L, helper.lll);
	long return_lll = e.getValue(ctx, Long.TYPE);
	assertEquals(66667L, return_lll);
	assertEquals(66667L, helper.lll);

	// int
	e = parser.parseExpression("++iii");
	assertEquals(42, helper.iii);
	int return_iii = e.getValue(ctx, Integer.TYPE);
	assertEquals(43, return_iii);
	assertEquals(43, helper.iii);
	return_iii = e.getValue(ctx, Integer.TYPE);
	assertEquals(44, return_iii);
	assertEquals(44, helper.iii);

	// short
	e = parser.parseExpression("++sss");
	assertEquals(15, helper.sss);
	int return_sss = (Integer) e.getValue(ctx);
	assertEquals(16, return_sss);
	assertEquals(16, helper.sss);
}