Author: henrib Date: Mon Mar 27 12:46:06 2017 New Revision: 1788925 URL: http://svn.apache.org/viewvc?rev=1788925&view=rev Log: JEXL: Fix & re-added a few commented out tests
Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Debugger.java commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/Foo.java commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlTest.java commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/junit/AsserterTest.java Modified: commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Debugger.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Debugger.java?rev=1788925&r1=1788924&r2=1788925&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Debugger.java (original) +++ commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/internal/Debugger.java Mon Mar 27 12:46:06 2017 @@ -578,13 +578,28 @@ public class Debugger extends ParserVisi protected Object visit(ASTGTNode node, Object data) { return infixChildren(node, " > ", false, data); } - /** Checks identifiers that contain space, quote, double-quotes or backspace. */ - protected static final Pattern QUOTED_IDENTIFIER = Pattern.compile("['\"\\s\\\\]"); + + /** Checks identifiers that contain spaces or punctuation + * (but underscore, at-sign, sharp-sign and dollar). + */ + protected static final Pattern QUOTED_IDENTIFIER = + Pattern.compile("[\\s]|[\\p{Punct}&&[^@#\\$_]]"); + + /** + * Checks whether an identifier should be quoted or not. + * @param str the identifier + * @return true if needing quotes, false otherwise + */ + protected boolean needQuotes(String str) { + return QUOTED_IDENTIFIER.matcher(str).find() + || "size".equals(str) + || "empty".equals(str); + } @Override protected Object visit(ASTIdentifier node, Object data) { String image = node.getName(); - if (QUOTED_IDENTIFIER.matcher(image).find()) { + if (needQuotes(image)) { // quote it image = "'" + image.replace("'", "\\'") + "'"; } @@ -595,7 +610,7 @@ public class Debugger extends ParserVisi protected Object visit(ASTIdentifierAccess node, Object data) { builder.append("."); String image = node.getName(); - if (QUOTED_IDENTIFIER.matcher(image).find()) { + if (needQuotes(image)) { // quote it image = "'" + image.replace("'", "\\'") + "'"; } Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/Foo.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/Foo.java?rev=1788925&r1=1788924&r2=1788925&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/Foo.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/Foo.java Mon Mar 27 12:46:06 2017 @@ -51,9 +51,12 @@ public class Foo { return new Foo(); } - public String get(String arg) - { - return "Repeat : " + arg; + public String getQuux() { + return "String : quux"; + } + + public String repeat(String str) { + return "Repeat : " + str; } public String convertBoolean(boolean b) Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlTest.java?rev=1788925&r1=1788924&r2=1788925&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlTest.java Mon Mar 27 12:46:06 2017 @@ -89,7 +89,7 @@ public class JexlTest extends JexlTestCa */ JexlContext jc = new MapContext(); jc.set("foo", new Foo()); - assertExpression(jc, "foo.get(\"woogie\")", "Repeat : woogie"); + assertExpression(jc, "foo.repeat(\"woogie\")", "Repeat : woogie"); } @Test @@ -213,21 +213,20 @@ public class JexlTest extends JexlTestCa // support generic int size() method BitSet bitset = new BitSet(5); jc.set("bitset", bitset); -// -// assertExpression(jc, "size(s)", new Integer(5)); -// assertExpression(jc, "size(array)", new Integer(5)); -// assertExpression(jc, "size(list)", new Integer(5)); -// assertExpression(jc, "size(map)", new Integer(5)); -// assertExpression(jc, "size(set)", new Integer(5)); -// assertExpression(jc, "size(bitset)", new Integer(64)); -// assertExpression(jc, "list.size()", new Integer(5)); -// assertExpression(jc, "map.size()", new Integer(5)); -// assertExpression(jc, "set.size()", new Integer(5)); -// assertExpression(jc, "bitset.size()", new Integer(64)); -// -// assertExpression(jc, "list.get(size(list) - 1)", "5"); -// assertExpression(jc, "list[size(list) - 1]", "5"); - // here + + assertExpression(jc, "size(s)", new Integer(5)); + assertExpression(jc, "size(array)", new Integer(5)); + assertExpression(jc, "size(list)", new Integer(5)); + assertExpression(jc, "size(map)", new Integer(5)); + assertExpression(jc, "size(set)", new Integer(5)); + assertExpression(jc, "size(bitset)", new Integer(64)); + assertExpression(jc, "list.size()", new Integer(5)); + assertExpression(jc, "map.size()", new Integer(5)); + assertExpression(jc, "set.size()", new Integer(5)); + assertExpression(jc, "bitset.size()", new Integer(64)); + + assertExpression(jc, "list.get(size(list) - 1)", "5"); + assertExpression(jc, "list[size(list) - 1]", "5"); assertExpression(jc, "list.get(list.size() - 1)", "5"); } @@ -236,15 +235,17 @@ public class JexlTest extends JexlTestCa JexlContext jc = new MapContext(); Map<String, Object> map = new HashMap<String, Object>(); map.put("size", "cheese"); + map.put("si & ze", "cheese"); jc.set("map", map); jc.set("foo", new Foo()); assertExpression(jc, "map['size']", "cheese"); -// PR - unsure whether or not we should support map.size or force usage of the above 'escaped' version -// assertExpression(jc, "map.size", "cheese"); - assertExpression(jc, "foo.getSize()", new Integer(22)); - // failing assertion for size property - //assertExpression(jc, "foo.size", new Integer(22)); + assertExpression(jc, "map['si & ze']", "cheese"); + assertExpression(jc, "map.'si & ze'", "cheese"); + assertExpression(jc, "map.size()", 2); + assertExpression(jc, "size(map)", 2); + assertExpression(jc, "foo.getSize()", 22); + assertExpression(jc, "foo.'size'", 22); } /** @@ -265,7 +266,7 @@ public class JexlTest extends JexlTestCa Assert.assertEquals(expr.toString(), new Float(100.0), value); expr = JEXL.createExpression("new(foo).quux"); value = expr.evaluate(jc); - Assert.assertEquals(expr.toString(), "Repeat : quux", value); + Assert.assertEquals(expr.toString(), "String : quux", value); } /** Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/junit/AsserterTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/junit/AsserterTest.java?rev=1788925&r1=1788924&r2=1788925&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/junit/AsserterTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/junit/AsserterTest.java Mon Mar 27 12:46:06 2017 @@ -39,7 +39,7 @@ public class AsserterTest extends JexlTe public void testThis() throws Exception { Asserter asserter = new Asserter(JEXL); asserter.setVariable("this", new Foo()); - asserter.assertExpression("this.get('abc')", "Repeat : abc"); + asserter.assertExpression("this.repeat('abc')", "Repeat : abc"); try { asserter.assertExpression("this.count", "Wrong Value"); Assert.fail("This method should have thrown an assertion exception");