fluent parser context

This commit is contained in:
Keith Donald
2008-03-25 23:49:15 +00:00
parent 565e34a1eb
commit 0965f93c97
14 changed files with 137 additions and 129 deletions

View File

@@ -15,7 +15,7 @@ import org.jboss.el.ExpressionFactoryImpl;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionVariable;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.support.ParserContextImpl;
import org.springframework.binding.expression.support.FluentParserContext;
public class ELExpressionParserTests extends TestCase {
@@ -53,7 +53,8 @@ public class ELExpressionParserTests extends TestCase {
public void testParseSimpleEvalExpressionNoEvalContextWithTypeCoersion() {
String expressionString = "3 + 4";
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().expect(Integer.class));
Expression exp = parser
.parseExpression(expressionString, new FluentParserContext().expectResult(Integer.class));
assertEquals(new Integer(7), exp.getValue(null));
}
@@ -65,13 +66,13 @@ public class ELExpressionParserTests extends TestCase {
public void testParseEvalExpressionWithContextTypeCoersion() {
String expressionString = "maximum";
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().expect(Long.class));
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().expectResult(Long.class));
assertEquals(new Long(2), exp.getValue(new TestBean()));
}
public void testParseEvalExpressionWithContextCustomTestBeanResolver() {
String expressionString = "specialProperty";
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().eval(TestBean.class));
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().evaluate(TestBean.class));
assertEquals("Custom resolver resolved this special property!", exp.getValue(new TestBean()));
}
@@ -81,25 +82,32 @@ public class ELExpressionParserTests extends TestCase {
assertEquals("value", exp.getValue(null));
}
public void testParseTemplateExpression() {
String expressionString = "text text text #{value} text text text#{value}";
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().template());
TestBean target = new TestBean();
assertEquals("text text text foo text text textfoo", exp.getValue(target));
}
public void testParseTemplateExpressionWithVariables() {
String expressionString = "#{value}#{max}";
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().template().variable(
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().template().variable(
new ExpressionVariable("max", "maximum")));
TestBean target = new TestBean();
assertEquals("foo2", exp.getValue(target));
}
public void testVariablesWithCoersion() {
Expression exp = parser.parseExpression("max", new ParserContextImpl().variable(new ExpressionVariable("max",
"maximum", new ParserContextImpl().expect(Long.class))));
Expression exp = parser.parseExpression("max", new FluentParserContext().variable(new ExpressionVariable("max",
"maximum", new FluentParserContext().expectResult(Long.class))));
TestBean target = new TestBean();
assertEquals(new Long(2), exp.getValue(target));
}
public void testTemplateNestedVariables() {
String expressionString = "#{value}#{max}";
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().template().variable(
new ExpressionVariable("max", "#{maximum}#{var}", new ParserContextImpl().template().variable(
Expression exp = parser.parseExpression(expressionString, new FluentParserContext().template().variable(
new ExpressionVariable("max", "#{maximum}#{var}", new FluentParserContext().template().variable(
new ExpressionVariable("var", "'bar'")))));
TestBean target = new TestBean();
assertEquals("foo2bar", exp.getValue(target));

View File

@@ -20,7 +20,7 @@ import junit.framework.TestCase;
import org.springframework.binding.expression.Expression;
import org.springframework.binding.expression.ExpressionVariable;
import org.springframework.binding.expression.ParserException;
import org.springframework.binding.expression.support.ParserContextImpl;
import org.springframework.binding.expression.support.FluentParserContext;
public class OgnlExpressionParserTests extends TestCase {
@@ -56,20 +56,20 @@ public class OgnlExpressionParserTests extends TestCase {
public void testParseTemplateSimpleLiteral() {
String exp = "flag";
Expression e = parser.parseExpression(exp, new ParserContextImpl().template());
Expression e = parser.parseExpression(exp, new FluentParserContext().template());
assertNotNull(e);
assertEquals("flag", e.getValue(bean));
}
public void testParseTemplateEmpty() {
Expression e = parser.parseExpression("", new ParserContextImpl().template());
Expression e = parser.parseExpression("", new FluentParserContext().template());
assertNotNull(e);
assertEquals("", e.getValue(bean));
}
public void testParseTemplateComposite() {
String exp = "hello ${flag} ${flag} ${flag}";
Expression e = parser.parseExpression(exp, new ParserContextImpl().template());
Expression e = parser.parseExpression(exp, new FluentParserContext().template());
assertNotNull(e);
String str = (String) e.getValue(bean);
assertEquals("hello false false false", str);
@@ -78,7 +78,7 @@ public class OgnlExpressionParserTests extends TestCase {
public void testTemplateEnclosedCompositeNotSupported() {
String exp = "${hello ${flag} ${flag} ${flag}}";
try {
parser.parseExpression(exp, new ParserContextImpl().template());
parser.parseExpression(exp, new FluentParserContext().template());
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
@@ -86,7 +86,7 @@ public class OgnlExpressionParserTests extends TestCase {
public void testSyntaxError1() {
try {
parser.parseExpression("${", new ParserContextImpl().template());
parser.parseExpression("${", new FluentParserContext().template());
fail();
} catch (ParserException e) {
}
@@ -100,7 +100,7 @@ public class OgnlExpressionParserTests extends TestCase {
public void testSyntaxError2() {
try {
parser.parseExpression("${}", new ParserContextImpl().template());
parser.parseExpression("${}", new FluentParserContext().template());
fail("Should've failed - not intended use");
} catch (ParserException e) {
}
@@ -115,40 +115,40 @@ public class OgnlExpressionParserTests extends TestCase {
public void testCollectionConstructionSyntax() {
// lists
parser.parseExpression("name in {null, \"Untitled\"}", null);
parser.parseExpression("${name in {null, \"Untitled\"}}", new ParserContextImpl().template());
parser.parseExpression("${name in {null, \"Untitled\"}}", new FluentParserContext().template());
// native arrays
parser.parseExpression("new int[] {1, 2, 3}", null);
parser.parseExpression("${new int[] {1, 2, 3}}", new ParserContextImpl().template());
parser.parseExpression("${new int[] {1, 2, 3}}", new FluentParserContext().template());
// maps
parser.parseExpression("#{ 'foo' : 'foo value', 'bar' : 'bar value' }", null);
parser.parseExpression("${#{ 'foo' : 'foo value', 'bar' : 'bar value' }}", new ParserContextImpl().template());
parser.parseExpression("${#{ 'foo' : 'foo value', 'bar' : 'bar value' }}", new FluentParserContext().template());
parser.parseExpression("#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }", null);
parser.parseExpression("${#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }}",
new ParserContextImpl().template());
new FluentParserContext().template());
// complex examples
parser.parseExpression("b,#{1:2}", null);
parser.parseExpression("${b,#{1:2}}", new ParserContextImpl().template());
parser.parseExpression("a${b,#{1:2},e}f${g,#{3:4},j}k", new ParserContextImpl().template());
parser.parseExpression("${b,#{1:2}}", new FluentParserContext().template());
parser.parseExpression("a${b,#{1:2},e}f${g,#{3:4},j}k", new FluentParserContext().template());
}
public void testVariables() {
Expression exp = parser.parseExpression("#var", new ParserContextImpl().variable(new ExpressionVariable("var",
Expression exp = parser.parseExpression("#var", new FluentParserContext().variable(new ExpressionVariable("var",
"flag")));
assertEquals(false, ((Boolean) exp.getValue(bean)).booleanValue());
}
public void testVariablesWithCoersion() {
Expression exp = parser.parseExpression("#var", new ParserContextImpl().variable(new ExpressionVariable("var",
"number", new ParserContextImpl().expect(Long.class))));
Expression exp = parser.parseExpression("#var", new FluentParserContext().variable(new ExpressionVariable("var",
"number", new FluentParserContext().expectResult(Long.class))));
assertEquals(new Long(0), exp.getValue(bean));
}
public void testNestedVariablesWithTemplates() {
Expression exp = parser.parseExpression("#var", new ParserContextImpl()
.variable(new ExpressionVariable("var", "${flag}${#var}", new ParserContextImpl().template().variable(
Expression exp = parser.parseExpression("#var", new FluentParserContext()
.variable(new ExpressionVariable("var", "${flag}${#var}", new FluentParserContext().template().variable(
new ExpressionVariable("var", "number")))));
assertEquals("false0", exp.getValue(bean));
}