separated out the concept of a template
normalized EL and OGNL to respect this concept
This commit is contained in:
@@ -16,16 +16,23 @@
|
||||
package org.springframework.binding.expression;
|
||||
|
||||
/**
|
||||
* Parses expression strings, returning a configured evaluator instance capable of performing parsed expression
|
||||
* evaluation in a thread safe way.
|
||||
* Parses expression strings into compiled expressions that can be evaluated. Supports parsing templates as well as
|
||||
* standard expression strings.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public interface ExpressionParser {
|
||||
|
||||
/**
|
||||
* Parse the provided expression string, returning an expression evaluator capable of evaluating it.
|
||||
* @param expressionString the parseable expression string; cannot be null (required)
|
||||
* Parse the expression string and return a compiled Expression object you can use for evaluation. Some examples:
|
||||
*
|
||||
* <pre>
|
||||
* 3 + 4
|
||||
* name.firstName
|
||||
* </pre>
|
||||
*
|
||||
* @param expressionString the raw expression spring to parse; cannot be null; cannot be encased in any special
|
||||
* delimiters; cannot be a template
|
||||
* @param context a context used to set attributes that influence expression parsing routine (optional)
|
||||
* @return the evaluator for the parsed expression
|
||||
* @throws ParserException an exception occurred during parsing
|
||||
|
||||
@@ -9,7 +9,9 @@ import org.springframework.util.Assert;
|
||||
public class ExpressionVariable {
|
||||
|
||||
private String name;
|
||||
|
||||
private String valueExpression;
|
||||
|
||||
private ParserContext parserContext;
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,4 +25,19 @@ public interface ParserContext {
|
||||
* expression parser will register these variables for reference during evaluation.
|
||||
*/
|
||||
public ExpressionVariable[] getExpressionVariables();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the expression being parsed is a template. A template expression consists of literal text that can
|
||||
* be mixed with evaluatable blocks. Some examples:
|
||||
*
|
||||
* <pre>
|
||||
* Some literal text
|
||||
* Hello #{name.firstName}!
|
||||
* #{3 + 4}
|
||||
* </pre>
|
||||
*
|
||||
* @return true if the expression is a template, false otherwise
|
||||
*/
|
||||
public boolean isTemplate();
|
||||
|
||||
}
|
||||
@@ -55,6 +55,17 @@ public class ELExpressionParser implements ExpressionParser {
|
||||
if (context == null) {
|
||||
context = NullParserContext.INSTANCE;
|
||||
}
|
||||
if (context.isTemplate()) {
|
||||
return parseTemplate(expressionString, context);
|
||||
} else {
|
||||
assertNotDelimited(expressionString);
|
||||
assertHasText(expressionString);
|
||||
return parseTemplate("#{" + expressionString + "}", context);
|
||||
}
|
||||
}
|
||||
|
||||
private Expression parseTemplate(String expressionString, ParserContext context) throws ParserException {
|
||||
Assert.notNull(expressionString, "The expression string to parse is required");
|
||||
try {
|
||||
ValueExpression expression = parseValueExpression(expressionString, context);
|
||||
ELContextFactory contextFactory = getContextFactory(context.getEvaluationContextType(), expressionString);
|
||||
@@ -94,6 +105,24 @@ public class ELExpressionParser implements ExpressionParser {
|
||||
putContextFactory(Object.class, defaultContextFactory);
|
||||
}
|
||||
|
||||
private void assertNotDelimited(String expressionString) {
|
||||
if ((expressionString.startsWith("#{") && expressionString.endsWith("}"))
|
||||
|| (expressionString.startsWith("${") && expressionString.endsWith("}"))) {
|
||||
throw new ParserException(
|
||||
expressionString,
|
||||
"This expression '"
|
||||
+ expressionString
|
||||
+ "' being parsed is expected be an 'eval' EL expression string. Do not attempt to enclose such expression strings in #{} or ${} delimiters--this is redundant. If you need to parse a template that mixes literal text with evaluatable blocks, set the 'template' parser context attribute to true.",
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertHasText(String expressionString) {
|
||||
if (expressionString.length() == 0) {
|
||||
throw new ParserException(expressionString, "The EL eval expression to parse must have text", null);
|
||||
}
|
||||
}
|
||||
|
||||
private class ParserELContext extends ELContext {
|
||||
private VariableMapper variableMapper;
|
||||
|
||||
@@ -116,7 +145,14 @@ public class ELExpressionParser implements ExpressionParser {
|
||||
ExpressionVariable var = variables[i];
|
||||
ParserContext context = var.getParserContext() != null ? var.getParserContext()
|
||||
: NullParserContext.INSTANCE;
|
||||
ValueExpression expr = parseValueExpression(var.getValueExpression(), context);
|
||||
ValueExpression expr;
|
||||
if (context.isTemplate()) {
|
||||
expr = parseValueExpression(var.getValueExpression(), context);
|
||||
} else {
|
||||
assertNotDelimited(var.getValueExpression());
|
||||
assertHasText(var.getValueExpression());
|
||||
expr = parseValueExpression("#{" + var.getValueExpression() + "}", context);
|
||||
}
|
||||
variableMapper.setVariable(var.getName(), expr);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,11 +63,11 @@ public class OgnlExpressionParser implements ExpressionParser {
|
||||
private String expressionSuffix = DEFAULT_EXPRESSION_SUFFIX;
|
||||
|
||||
/**
|
||||
* Should we allow undelimited OGNL eval expressions like "foo.bar"? If not, evalutable OGNL expressions must be
|
||||
* enclosed in delimiters like ${foo.bar} else they are treated as literal expressions. Mainly here for
|
||||
* compatability reasons, as Web Flow 1.0 allows undelimited OGNL eval expressions by default.
|
||||
* Should we allow delimited OGNL eval expressions like "#{foo.bar}"? If not, evalutable OGNL expressions must not
|
||||
* be enclosed in delimiters like ${foo.bar} else an exception is thrown. Only here for compatability reasons, as
|
||||
* Web Flow 1.0 allows delimited OGNL eval expressions while 2.x does not.
|
||||
*/
|
||||
private boolean allowUndelimitedEvalExpressions;
|
||||
private boolean allowDelimitedEvalExpressions;
|
||||
|
||||
/**
|
||||
* Returns the configured expression delimiter prefix. Defaults to "${".
|
||||
@@ -98,26 +98,17 @@ public class OgnlExpressionParser implements ExpressionParser {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns if this parser should we allow undelimited OGNL eval expressions like <code>foo.bar</code>.
|
||||
* Returns if this parser allows delimited OGNL eval expressions like <code>${foo.bar}</code>.
|
||||
*/
|
||||
public boolean getAllowUndelimitedEvalExpressions() {
|
||||
return allowUndelimitedEvalExpressions;
|
||||
public boolean getAllowDelimitedEvalExpressions() {
|
||||
return allowDelimitedEvalExpressions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets if this parser should allow undelimited OGNL eval expressions like "foo.bar"? If not, evalutable OGNL
|
||||
* expressions must be enclosed in delimiters like ${foo.bar}, else they are treated as literal expressions.
|
||||
* Sets if this parser allows OGNL eval expressions like ${foo.bar}.
|
||||
*/
|
||||
public void setAllowUndelimitedEvalExpressions(boolean allowUndelmitedEvalExpressions) {
|
||||
this.allowUndelimitedEvalExpressions = allowUndelmitedEvalExpressions;
|
||||
}
|
||||
|
||||
public static String getDEFAULT_EXPRESSION_PREFIX() {
|
||||
return DEFAULT_EXPRESSION_PREFIX;
|
||||
}
|
||||
|
||||
public static String getDEFAULT_EXPRESSION_SUFFIX() {
|
||||
return DEFAULT_EXPRESSION_SUFFIX;
|
||||
public void setAllowDelimitedEvalExpressions(boolean allowDelmitedEvalExpressions) {
|
||||
this.allowDelimitedEvalExpressions = allowDelmitedEvalExpressions;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -132,6 +123,33 @@ public class OgnlExpressionParser implements ExpressionParser {
|
||||
// expression parser
|
||||
|
||||
public Expression parseExpression(String expressionString, ParserContext context) throws ParserException {
|
||||
Assert.notNull(expressionString, "The expression string to parse is required");
|
||||
if (context == null) {
|
||||
context = NullParserContext.INSTANCE;
|
||||
}
|
||||
if (context.isTemplate()) {
|
||||
return parseTemplate(expressionString, context);
|
||||
} else {
|
||||
if (expressionString.startsWith(getExpressionPrefix()) && expressionString.endsWith(getExpressionSuffix())) {
|
||||
if (!allowDelimitedEvalExpressions) {
|
||||
throw new ParserException(
|
||||
expressionString,
|
||||
"The expression '"
|
||||
+ expressionString
|
||||
+ "' being parsed is expected be a standard OGNL expression. Do not attempt to enclose such expression strings in ${} delimiters--this is redundant. If you need to parse a template that mixes literal text with evaluatable blocks, set the 'template' parser context attribute to true.",
|
||||
null);
|
||||
} else {
|
||||
int lastIndex = expressionString.length() - getExpressionSuffix().length();
|
||||
String ognlExpression = expressionString.substring(getExpressionPrefix().length(), lastIndex);
|
||||
return doParseExpression(ognlExpression, context);
|
||||
}
|
||||
} else {
|
||||
return doParseExpression(expressionString, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Expression parseTemplate(String expressionString, ParserContext context) throws ParserException {
|
||||
Assert.notNull(expressionString, "The expression string to parse is required");
|
||||
if (expressionString.length() == 0) {
|
||||
return parseEmptyExpressionString(context);
|
||||
@@ -144,62 +162,13 @@ public class OgnlExpressionParser implements ExpressionParser {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the provided string a template expression this parser can parse? Always returns <code>true</code> if this
|
||||
* OGNL expression parser is configured to <b>not</b> allow undelimited OGNL expressions. If undelimited OGNL
|
||||
* expressions are allowed like "foo.bar", this method only returns true if an explicitly delimited expression is
|
||||
* present in the string like "hello my name is ${name}" or "${foo.bar}".
|
||||
*
|
||||
* In general, a template expression is either:
|
||||
* <ol>
|
||||
* <li>static literal text like "hello world". In this case, evaluating the expression simply returns the literal
|
||||
* text.
|
||||
* <li>a single eval expression like ${requestParameters.foo}. In this case, evaluating the expression returns the
|
||||
* evaluated value.
|
||||
* <li>a mix of literal text with one or more eval expressions like "hello #{name}". In this case, evaluating the
|
||||
* expression returns a string with the result of #{name} evaluated (often called a composite expression).
|
||||
* </ol>
|
||||
*
|
||||
* This method and the {@link #getAllowUndelimitedEvalExpressions()} flag primarily exist for compatibility reasons.
|
||||
* The OgnlExpressionParser in SWF 1.0 does not treat literal text like "hello world" as a template expression, but
|
||||
* rather a standard, evaluatable OGNL expression. Therefore, callers expecting standard template evaluation
|
||||
* semantics are expected to work with these literal string values themselves, and not pass those strings to
|
||||
* {@link #parseExpression(String, ParserContext)}.
|
||||
*
|
||||
* @param string the string
|
||||
* @return true if the string is a template expression, false otherwise.
|
||||
*/
|
||||
public boolean isTemplateExpression(String string) {
|
||||
if (!allowUndelimitedEvalExpressions) {
|
||||
// every string provided is a "template" style expression - return true
|
||||
return true;
|
||||
}
|
||||
// only returns true when there is ${} somewhere in the string
|
||||
// this is version 1.0 semantics, there for compatability reasons
|
||||
int prefixIndex = string.indexOf(getExpressionPrefix());
|
||||
if (prefixIndex == -1) {
|
||||
return false;
|
||||
}
|
||||
int suffixIndex = string.indexOf(getExpressionSuffix(), prefixIndex);
|
||||
if (suffixIndex == -1) {
|
||||
return false;
|
||||
} else {
|
||||
// make sure there is actually something inside the ${}
|
||||
if (suffixIndex == prefixIndex + getExpressionPrefix().length()) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// helper methods
|
||||
|
||||
/**
|
||||
* Helper that handles a empty expression string.
|
||||
*/
|
||||
private Expression parseEmptyExpressionString(ParserContext context) {
|
||||
if (allowUndelimitedEvalExpressions) {
|
||||
if (allowDelimitedEvalExpressions) {
|
||||
// let the parser handle it
|
||||
return doParseExpression("", context);
|
||||
} else {
|
||||
@@ -253,7 +222,7 @@ public class OgnlExpressionParser implements ExpressionParser {
|
||||
} else {
|
||||
if (startIdx == 0) {
|
||||
// treat the entire string as one expression
|
||||
if (allowUndelimitedEvalExpressions) {
|
||||
if (allowDelimitedEvalExpressions) {
|
||||
expressions.add(doParseExpression(expressionString, context));
|
||||
} else {
|
||||
// treat entire string as a literal
|
||||
|
||||
@@ -44,4 +44,8 @@ public final class NullParserContext implements ParserContext {
|
||||
public ExpressionVariable[] getExpressionVariables() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isTemplate() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import org.springframework.binding.expression.ParserContext;
|
||||
|
||||
/**
|
||||
* Default implementation of the ParserContext interface that has a fluent API for building parser context attributes.
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class ParserContextImpl implements ParserContext {
|
||||
@@ -20,12 +19,15 @@ public class ParserContextImpl implements ParserContext {
|
||||
|
||||
private List expressionVariables;
|
||||
|
||||
private boolean template;
|
||||
|
||||
/**
|
||||
* Create a new parser context, initially with all context attributes as null. Post construction, call one or more
|
||||
* of the fluent builder methods to configure this context.
|
||||
* @see #eval(Class)
|
||||
* @see #expect(Class)
|
||||
* @see #variable(ExpressionVariable)
|
||||
* @see #template()
|
||||
*/
|
||||
public ParserContextImpl() {
|
||||
init();
|
||||
@@ -43,6 +45,10 @@ public class ParserContextImpl implements ParserContext {
|
||||
return (ExpressionVariable[]) expressionVariables.toArray(new ExpressionVariable[expressionVariables.size()]);
|
||||
}
|
||||
|
||||
public boolean isTemplate() {
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure the evaluationContextType attribute with the value provided.
|
||||
* @param contextType the type of context object the parsed expression will evaluate in
|
||||
@@ -50,7 +56,7 @@ public class ParserContextImpl implements ParserContext {
|
||||
*/
|
||||
public ParserContextImpl eval(Class contextType) {
|
||||
evaluationContextType = contextType;
|
||||
return ParserContextImpl.this;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +66,7 @@ public class ParserContextImpl implements ParserContext {
|
||||
*/
|
||||
public ParserContextImpl expect(Class resultType) {
|
||||
evaluationResultType = resultType;
|
||||
return ParserContextImpl.this;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,7 +76,7 @@ public class ParserContextImpl implements ParserContext {
|
||||
*/
|
||||
public ParserContextImpl variable(ExpressionVariable variable) {
|
||||
expressionVariables.add(variable);
|
||||
return ParserContextImpl.this;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +86,16 @@ public class ParserContextImpl implements ParserContext {
|
||||
*/
|
||||
public ParserContextImpl variables(ExpressionVariable[] variables) {
|
||||
expressionVariables.addAll(Arrays.asList(variables));
|
||||
return ParserContextImpl.this;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a flag indicating the expression to parse is a template.
|
||||
* @return this
|
||||
*/
|
||||
public ParserContextImpl template() {
|
||||
template = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
|
||||
@@ -14,6 +14,7 @@ import junit.framework.TestCase;
|
||||
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;
|
||||
|
||||
public class ELExpressionParserTests extends TestCase {
|
||||
@@ -25,7 +26,7 @@ public class ELExpressionParserTests extends TestCase {
|
||||
}
|
||||
|
||||
public void testParseSimpleEvalExpressionNoParserContext() {
|
||||
String expressionString = "#{3 + 4}";
|
||||
String expressionString = "3 + 4";
|
||||
Expression exp = parser.parseExpression(expressionString, null);
|
||||
assertEquals(new Long(7), exp.getValue(null));
|
||||
}
|
||||
@@ -42,71 +43,68 @@ public class ELExpressionParserTests extends TestCase {
|
||||
|
||||
public void testParseEmptyExpressionString() {
|
||||
String expressionString = "";
|
||||
Expression exp = parser.parseExpression(expressionString, null);
|
||||
assertEquals("", exp.getValue(null));
|
||||
try {
|
||||
parser.parseExpression(expressionString, null);
|
||||
fail("Should have failed");
|
||||
} catch (ParserException e) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void testParseSimpleEvalExpressionNoEvalContextWithTypeCoersion() {
|
||||
String expressionString = "#{3 + 4}";
|
||||
String expressionString = "3 + 4";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().expect(Integer.class));
|
||||
assertEquals(new Integer(7), exp.getValue(null));
|
||||
}
|
||||
|
||||
public void testParseBeanEvalExpressionNoParserContext() {
|
||||
String expressionString = "#{value}";
|
||||
String expressionString = "value";
|
||||
Expression exp = parser.parseExpression(expressionString, null);
|
||||
assertEquals("foo", exp.getValue(new TestBean()));
|
||||
}
|
||||
|
||||
public void testParseEvalExpressionWithContextTypeCoersion() {
|
||||
String expressionString = "#{maximum}";
|
||||
String expressionString = "maximum";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().expect(Long.class));
|
||||
assertEquals(new Long(2), exp.getValue(new TestBean()));
|
||||
}
|
||||
|
||||
public void testParseEvalExpressionWithContextCustomTestBeanResolver() {
|
||||
String expressionString = "#{specialProperty}";
|
||||
String expressionString = "specialProperty";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().eval(TestBean.class));
|
||||
assertEquals("Custom resolver resolved this special property!", exp.getValue(new TestBean()));
|
||||
}
|
||||
|
||||
public void testParseLiteralExpression() {
|
||||
String expressionString = "value";
|
||||
String expressionString = "'value'";
|
||||
Expression exp = parser.parseExpression(expressionString, null);
|
||||
assertEquals("value", exp.getValue(null));
|
||||
}
|
||||
|
||||
public void testParseExpressionWithVariables() {
|
||||
public void testParseTemplateExpressionWithVariables() {
|
||||
String expressionString = "#{value}#{max}";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("max", "#{maximum}")));
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().template().variable(
|
||||
new ExpressionVariable("max", "maximum")));
|
||||
TestBean target = new TestBean();
|
||||
assertEquals("foo2", exp.getValue(target));
|
||||
}
|
||||
|
||||
public void testVariablesWithCoersion() {
|
||||
String expressionString = "#{max}";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("max", "#{maximum}", new ParserContextImpl().expect(Long.class))));
|
||||
Expression exp = parser.parseExpression("max", new ParserContextImpl().variable(new ExpressionVariable("max",
|
||||
"maximum", new ParserContextImpl().expect(Long.class))));
|
||||
TestBean target = new TestBean();
|
||||
assertEquals(new Long(2), exp.getValue(target));
|
||||
}
|
||||
|
||||
public void testNestedVariables() {
|
||||
public void testTemplateNestedVariables() {
|
||||
String expressionString = "#{value}#{max}";
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("max", "#{maximum}#{var}", new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("var", "bar")))));
|
||||
Expression exp = parser.parseExpression(expressionString, new ParserContextImpl().template().variable(
|
||||
new ExpressionVariable("max", "#{maximum}#{var}", new ParserContextImpl().template().variable(
|
||||
new ExpressionVariable("var", "'bar'")))));
|
||||
TestBean target = new TestBean();
|
||||
assertEquals("foo2bar", exp.getValue(target));
|
||||
}
|
||||
|
||||
public void testParseImmediateEvalExpression() {
|
||||
String expressionString = "${3 + 4}";
|
||||
Expression exp = parser.parseExpression(expressionString, null);
|
||||
assertEquals(new Long(7), exp.getValue(null));
|
||||
}
|
||||
|
||||
public static class TestBean {
|
||||
private String value = "foo";
|
||||
|
||||
|
||||
@@ -29,6 +29,15 @@ public class OgnlExpressionParserTests extends TestCase {
|
||||
private TestBean bean = new TestBean();
|
||||
|
||||
public void testParseSimple() {
|
||||
String exp = "flag";
|
||||
Expression e = parser.parseExpression(exp, null);
|
||||
assertNotNull(e);
|
||||
Boolean b = (Boolean) e.getValue(bean);
|
||||
assertFalse(b.booleanValue());
|
||||
}
|
||||
|
||||
public void testParseSimpleAllowDelimited() {
|
||||
parser.setAllowDelimitedEvalExpressions(true);
|
||||
String exp = "${flag}";
|
||||
Expression e = parser.parseExpression(exp, null);
|
||||
assertNotNull(e);
|
||||
@@ -36,77 +45,40 @@ public class OgnlExpressionParserTests extends TestCase {
|
||||
assertFalse(b.booleanValue());
|
||||
}
|
||||
|
||||
public void testIsTemplateExpressionAllowUndelimitedOgnlExpressions() {
|
||||
String exp = "some literal text";
|
||||
String exp2 = "${a delimited expression}";
|
||||
String exp3 = "${a malformed delimited expression";
|
||||
String exp4 = "a malformed delimited expression}";
|
||||
String exp5 = "${}";
|
||||
String exp6 = "a ${composite} expression";
|
||||
String exp7 = "a ${composite} ${malformed expression";
|
||||
assertTrue(parser.isTemplateExpression(exp));
|
||||
assertTrue(parser.isTemplateExpression(exp2));
|
||||
assertTrue(parser.isTemplateExpression(exp3));
|
||||
assertTrue(parser.isTemplateExpression(exp4));
|
||||
assertTrue(parser.isTemplateExpression(exp5));
|
||||
assertTrue(parser.isTemplateExpression(exp6));
|
||||
assertTrue(parser.isTemplateExpression(exp7));
|
||||
public void testParseSimpleDelimitedNotAllowed() {
|
||||
String exp = "${flag}";
|
||||
try {
|
||||
parser.parseExpression(exp, null);
|
||||
fail("should have failed");
|
||||
} catch (ParserException e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testIsTemplateExpressionDoNotAllowUndelimitedOgnlExpressions() {
|
||||
parser.setAllowUndelimitedEvalExpressions(true);
|
||||
String exp = "some literal text";
|
||||
String exp2 = "${a delimited expression}";
|
||||
String exp3 = "${a malformed delimited expression";
|
||||
String exp4 = "a malformed delimited expression}";
|
||||
String exp5 = "${}";
|
||||
String exp6 = "a ${composite} expression";
|
||||
String exp7 = "a ${composite} ${malformed expression";
|
||||
|
||||
assertFalse(parser.isTemplateExpression(exp));
|
||||
assertTrue(parser.isTemplateExpression(exp2));
|
||||
assertFalse(parser.isTemplateExpression(exp3));
|
||||
assertFalse(parser.isTemplateExpression(exp4));
|
||||
assertFalse(parser.isTemplateExpression(exp5));
|
||||
assertTrue(parser.isTemplateExpression(exp6));
|
||||
assertTrue(parser.isTemplateExpression(exp7));
|
||||
}
|
||||
|
||||
public void testParseSimpleNotDelimitedAllowUndelimited() {
|
||||
parser.setAllowUndelimitedEvalExpressions(true);
|
||||
public void testParseTemplateSimpleLiteral() {
|
||||
String exp = "flag";
|
||||
Expression e = parser.parseExpression(exp, null);
|
||||
assertNotNull(e);
|
||||
Boolean b = (Boolean) e.getValue(bean);
|
||||
assertFalse(b.booleanValue());
|
||||
}
|
||||
|
||||
public void testParseSimpleLiteral() {
|
||||
String exp = "flag";
|
||||
Expression e = parser.parseExpression(exp, null);
|
||||
Expression e = parser.parseExpression(exp, new ParserContextImpl().template());
|
||||
assertNotNull(e);
|
||||
assertEquals("flag", e.getValue(bean));
|
||||
}
|
||||
|
||||
public void testParseEmpty() {
|
||||
Expression e = parser.parseExpression("", null);
|
||||
public void testParseTemplateEmpty() {
|
||||
Expression e = parser.parseExpression("", new ParserContextImpl().template());
|
||||
assertNotNull(e);
|
||||
assertEquals("", e.getValue(bean));
|
||||
}
|
||||
|
||||
public void testParseComposite() {
|
||||
public void testParseTemplateComposite() {
|
||||
String exp = "hello ${flag} ${flag} ${flag}";
|
||||
assertTrue(parser.isTemplateExpression(exp));
|
||||
Expression e = parser.parseExpression(exp, null);
|
||||
Expression e = parser.parseExpression(exp, new ParserContextImpl().template());
|
||||
assertNotNull(e);
|
||||
String str = (String) e.getValue(bean);
|
||||
assertEquals("hello false false false", str);
|
||||
}
|
||||
|
||||
public void testEnclosedCompositeNotSupported() {
|
||||
public void testTemplateEnclosedCompositeNotSupported() {
|
||||
String exp = "${hello ${flag} ${flag} ${flag}}";
|
||||
try {
|
||||
parser.parseExpression(exp, null);
|
||||
parser.parseExpression(exp, new ParserContextImpl().template());
|
||||
fail("Should've failed - not intended use");
|
||||
} catch (ParserException e) {
|
||||
}
|
||||
@@ -114,7 +86,7 @@ public class OgnlExpressionParserTests extends TestCase {
|
||||
|
||||
public void testSyntaxError1() {
|
||||
try {
|
||||
parser.parseExpression("${", null);
|
||||
parser.parseExpression("${", new ParserContextImpl().template());
|
||||
fail();
|
||||
} catch (ParserException e) {
|
||||
}
|
||||
@@ -128,7 +100,7 @@ public class OgnlExpressionParserTests extends TestCase {
|
||||
|
||||
public void testSyntaxError2() {
|
||||
try {
|
||||
parser.parseExpression("${}", null);
|
||||
parser.parseExpression("${}", new ParserContextImpl().template());
|
||||
fail("Should've failed - not intended use");
|
||||
} catch (ParserException e) {
|
||||
}
|
||||
@@ -143,41 +115,41 @@ public class OgnlExpressionParserTests extends TestCase {
|
||||
public void testCollectionConstructionSyntax() {
|
||||
// lists
|
||||
parser.parseExpression("name in {null, \"Untitled\"}", null);
|
||||
parser.parseExpression("${name in {null, \"Untitled\"}}", null);
|
||||
parser.parseExpression("${name in {null, \"Untitled\"}}", new ParserContextImpl().template());
|
||||
|
||||
// native arrays
|
||||
parser.parseExpression("new int[] {1, 2, 3}", null);
|
||||
parser.parseExpression("${new int[] {1, 2, 3}}", null);
|
||||
parser.parseExpression("${new int[] {1, 2, 3}}", new ParserContextImpl().template());
|
||||
|
||||
// maps
|
||||
parser.parseExpression("#{ 'foo' : 'foo value', 'bar' : 'bar value' }", null);
|
||||
parser.parseExpression("${#{ 'foo' : 'foo value', 'bar' : 'bar value' }}", null);
|
||||
parser.parseExpression("${#{ 'foo' : 'foo value', 'bar' : 'bar value' }}", new ParserContextImpl().template());
|
||||
parser.parseExpression("#@java.util.LinkedHashMap@{ 'foo' : 'foo value', 'bar' : 'bar value' }", null);
|
||||
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());
|
||||
|
||||
// complex examples
|
||||
parser.parseExpression("b,#{1:2}", null);
|
||||
parser.parseExpression("${b,#{1:2}}", null);
|
||||
parser.parseExpression("a${b,#{1:2},e}f${g,#{3:4},j}k", 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());
|
||||
}
|
||||
|
||||
public void testVariables() {
|
||||
Expression exp = parser.parseExpression("${#var}", new ParserContextImpl().variable(new ExpressionVariable(
|
||||
"var", "${flag}")));
|
||||
Expression exp = parser.parseExpression("#var", new ParserContextImpl().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 ParserContextImpl().variable(new ExpressionVariable("var",
|
||||
"number", new ParserContextImpl().expect(Long.class))));
|
||||
assertEquals(new Long(0), exp.getValue(bean));
|
||||
}
|
||||
|
||||
public void testNestedVariables() {
|
||||
Expression exp = parser
|
||||
.parseExpression("${#var}", new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("var", "${flag}${#var}", new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("var", "${number}")))));
|
||||
public void testNestedVariablesWithTemplates() {
|
||||
Expression exp = parser.parseExpression("#var", new ParserContextImpl()
|
||||
.variable(new ExpressionVariable("var", "${flag}${#var}", new ParserContextImpl().template().variable(
|
||||
new ExpressionVariable("var", "number")))));
|
||||
assertEquals("false0", exp.getValue(bean));
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ public class RequiredMappingTests extends TestCase {
|
||||
|
||||
public void testRequired() {
|
||||
MappingBuilder builder = new MappingBuilder(new OgnlExpressionParser());
|
||||
Mapping mapping = builder.source("${foo}").target("${bar}").required().value();
|
||||
Mapping mapping = builder.source("foo").target("bar").required().value();
|
||||
HashMap source = new HashMap();
|
||||
source.put("foo", "baz");
|
||||
HashMap target = new HashMap();
|
||||
@@ -38,7 +38,7 @@ public class RequiredMappingTests extends TestCase {
|
||||
|
||||
public void testRequiredExceptionOnNull() {
|
||||
MappingBuilder builder = new MappingBuilder(new OgnlExpressionParser());
|
||||
Mapping mapping = builder.source("${foo}").target("${bar}").required().value();
|
||||
Mapping mapping = builder.source("foo").target("bar").required().value();
|
||||
HashMap source = new HashMap();
|
||||
source.put("foo", null);
|
||||
HashMap target = new HashMap();
|
||||
@@ -50,7 +50,7 @@ public class RequiredMappingTests extends TestCase {
|
||||
|
||||
public void testRequiredExceptionOnNoKey() {
|
||||
MappingBuilder builder = new MappingBuilder(new OgnlExpressionParser());
|
||||
Mapping mapping = builder.source("${foo}").target("${bar}").required().value();
|
||||
Mapping mapping = builder.source("foo").target("bar").required().value();
|
||||
HashMap source = new HashMap();
|
||||
HashMap target = new HashMap();
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user