Copied: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java (from r1338871, commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/BitwiseOperatorTest.java) URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java?p2=commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java&p1=commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/BitwiseOperatorTest.java&r1=1338871&r2=1357476&rev=1357476&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/BitwiseOperatorTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticOperatorTest.java Thu Jul 5 07:08:06 2012 @@ -16,13 +16,22 @@ */ package org.apache.commons.jexl3; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.SortedSet; +import java.util.TreeSet; import org.apache.commons.jexl3.junit.Asserter; /** - * Tests for the bitwise operators. - * @since 1.1 + * Tests for the startsWith, endsWith, match and range operators. + * @since 3.0 */ -public class BitwiseOperatorTest extends JexlTestCase { +public class ArithmeticOperatorTest extends JexlTestCase { private Asserter asserter; @Override @@ -35,116 +44,197 @@ public class BitwiseOperatorTest extends * Create the named test. * @param name test name */ - public BitwiseOperatorTest(String name) { + public ArithmeticOperatorTest(String name) { super(name); } - - public void testAndWithTwoNulls() throws Exception { - asserter.assertExpression("null & null", new Long(0)); - } - - public void testAndWithLeftNull() throws Exception { - asserter.assertExpression("null & 1", new Long(0)); - } - - public void testAndWithRightNull() throws Exception { - asserter.assertExpression("1 & null", new Long(0)); - } - - public void testAndSimple() throws Exception { - asserter.assertExpression("15 & 3", new Long(15 & 3)); - } - - public void testAndVariableNumberCoercion() throws Exception { - asserter.setVariable("x", new Integer(15)); - asserter.setVariable("y", new Short((short)7)); - asserter.assertExpression("x & y", new Long(15 & 7)); - } - - public void testAndVariableStringCoercion() throws Exception { - asserter.setVariable("x", new Integer(15)); - asserter.setVariable("y", "7"); - asserter.assertExpression("x & y", new Long(15 & 7)); - } - - public void testComplementWithNull() throws Exception { - asserter.assertExpression("~null", new Long(-1)); - } - - public void testComplementSimple() throws Exception { - asserter.assertExpression("~128", new Long(-129)); - } - - public void testComplementVariableNumberCoercion() throws Exception { - asserter.setVariable("x", new Integer(15)); - asserter.assertExpression("~x", new Long(~15)); - } - - public void testComplementVariableStringCoercion() throws Exception { - asserter.setVariable("x", "15"); - asserter.assertExpression("~x", new Long(~15)); - } - - public void testOrWithTwoNulls() throws Exception { - asserter.assertExpression("null | null", new Long(0)); - } - - public void testOrWithLeftNull() throws Exception { - asserter.assertExpression("null | 1", new Long(1)); - } - public void testOrWithRightNull() throws Exception { - asserter.assertExpression("1 | null", new Long(1)); + public void testRegexp() throws Exception { + asserter.setVariable("str", "abc456"); + asserter.assertExpression("str =~ '.*456'", Boolean.TRUE); + asserter.assertExpression("str !~ 'ABC.*'", Boolean.TRUE); + asserter.setVariable("match", "abc.*"); + asserter.setVariable("nomatch", ".*123"); + asserter.assertExpression("str =~ match", Boolean.TRUE); + asserter.assertExpression("str !~ match", Boolean.FALSE); + asserter.assertExpression("str !~ nomatch", Boolean.TRUE); + asserter.assertExpression("str =~ nomatch", Boolean.FALSE); + asserter.setVariable("match", java.util.regex.Pattern.compile("abc.*")); + asserter.setVariable("nomatch", java.util.regex.Pattern.compile(".*123")); + asserter.assertExpression("str =~ match", Boolean.TRUE); + asserter.assertExpression("str !~ match", Boolean.FALSE); + asserter.assertExpression("str !~ nomatch", Boolean.TRUE); + asserter.assertExpression("str =~ nomatch", Boolean.FALSE); + // check the in/not-in variant + asserter.assertExpression("'a' =~ ['a','b','c','d','e','f']", Boolean.TRUE); + asserter.assertExpression("'a' !~ ['a','b','c','d','e','f']", Boolean.FALSE); + asserter.assertExpression("'z' =~ ['a','b','c','d','e','f']", Boolean.FALSE); + asserter.assertExpression("'z' !~ ['a','b','c','d','e','f']", Boolean.TRUE); + } + + public void testStartsEndsWithString() throws Exception { + asserter.setVariable("x", "foobar"); + asserter.assertExpression("x =^ 'foo'", Boolean.TRUE); + asserter.assertExpression("x =$ 'foo'", Boolean.FALSE); + asserter.setVariable("x", "barfoo"); + asserter.assertExpression("x =^ 'foo'", Boolean.FALSE); + asserter.assertExpression("x =$ 'foo'", Boolean.TRUE); + } + + public static class MatchingContainer { + private final Set<Integer> values; + + public MatchingContainer(int[] is) { + values = new HashSet<Integer>(); + for (int value : is) { + values.add(value); + } + } + + public boolean contains(int value) { + return values.contains(value); + } + } + + public static class IterableContainer implements Iterable<Integer> { + private final SortedSet<Integer> values; + + public IterableContainer(int[] is) { + values = new TreeSet<Integer>(); + for (int value : is) { + values.add(value); + } + } + + @Override + public Iterator<Integer> iterator() { + return values.iterator(); + } + + public boolean startsWith(int i) { + return values.first().equals(i); + } + + public boolean endsWith(int i) { + return values.last().equals(i); + } + + public boolean startsWith(int[] i) { + SortedSet<Integer> sw = values.headSet(i.length); + int n = 0; + for(Integer value : sw) { + if(!value.equals(i[n++])) { + return false; + } + } + return true; + } + public boolean endsWith(int[] i) { + SortedSet<Integer> sw = values.tailSet(values.size() - i.length); + int n = 0; + for(Integer value : sw) { + if(!value.equals(i[n++])) { + return false; + } + } + return true; + } + } + + public void testMatch() throws Exception { + // check in/not-in on array, list, map, set and duck-type collection + int[] ai = {2, 4, 42, 54}; + List<Integer> al = new ArrayList<Integer>(); + for (int i : ai) { + al.add(i); + } + Map<Integer, String> am = new HashMap<Integer, String>(); + am.put(2, "two"); + am.put(4, "four"); + am.put(42, "forty-two"); + am.put(54, "fifty-four"); + MatchingContainer ad = new MatchingContainer(ai); + IterableContainer ic = new IterableContainer(ai); + Set<Integer> as = ad.values; + Object[] vars = {ai, al, am, ad, as, ic}; + + for (Object var : vars) { + asserter.setVariable("container", var); + for (int x : ai) { + asserter.setVariable("x", x); + asserter.assertExpression("x =~ container", Boolean.TRUE); + } + asserter.setVariable("x", 169); + asserter.assertExpression("x !~ container", Boolean.TRUE); + } + } + + public void testStartsEndsWith() throws Exception { + asserter.setVariable("x", "foobar"); + asserter.assertExpression("x =^ 'foo'", Boolean.TRUE); + asserter.assertExpression("x =$ 'foo'", Boolean.FALSE); + asserter.setVariable("x", "barfoo"); + asserter.assertExpression("x =^ 'foo'", Boolean.FALSE); + asserter.assertExpression("x =$ 'foo'", Boolean.TRUE); + + int[] ai = {2, 4, 42, 54}; + IterableContainer ic = new IterableContainer(ai); + asserter.setVariable("x", ic); + asserter.assertExpression("x =^ 2", Boolean.TRUE); + asserter.assertExpression("x =$ 54", Boolean.TRUE); + asserter.assertExpression("x =^ 4", Boolean.FALSE); + asserter.assertExpression("x =$ 42", Boolean.FALSE); + asserter.assertExpression("x =^ [2, 4]", Boolean.TRUE); + asserter.assertExpression("x =^ [42, 54]", Boolean.TRUE); + } + + public static class Aggregate { + private Aggregate() {} + public static int sum(Iterable<Integer> ii) { + int sum = 0; + for(Integer i : ii) { + sum += i; + } + return sum; + } + } + + @SuppressWarnings("unchecked") + public void testInterval() throws Exception { + Map<String, Object> ns = new HashMap<String, Object>(); + ns.put("calc", Aggregate.class); + JexlEngine jexl = new JexlBuilder().namespaces(ns).create(); + JexlScript script; + Object result; + + script = jexl.createScript("1 .. 3"); + result = script.execute(null); + assertTrue(result instanceof Iterable<?>); + Iterator<Integer> ii = ((Iterable<Integer>) result).iterator(); + assertEquals(Integer.valueOf(1), ii.next()); + assertEquals(Integer.valueOf(2), ii.next()); + assertEquals(Integer.valueOf(3), ii.next()); + assertNull(ii.next()); + + script = jexl.createScript("(4 - 3) .. (9 / 3)"); + result = script.execute(null); + assertTrue(result instanceof Iterable<?>); + ii = ((Iterable<Integer>) result).iterator(); + assertEquals(Integer.valueOf(1), ii.next()); + assertEquals(Integer.valueOf(2), ii.next()); + assertEquals(Integer.valueOf(3), ii.next()); + assertNull(ii.next()); + + // sum of 1, 2, 3 + script = jexl.createScript("var x = 0; for(var y : ((5 - 4) .. (12 / 4))) { x = x + y }; x"); + result = script.execute(null); + assertEquals(Integer.valueOf(6), result); + + script = jexl.createScript("calc:sum(1 .. 3)"); + result = script.execute(null); + assertEquals(Integer.valueOf(6), result); + + script = jexl.createScript("calc:sum(-3 .. 3)"); + result = script.execute(null); + assertEquals(Integer.valueOf(0), result); } - - public void testOrSimple() throws Exception { - asserter.assertExpression("12 | 3", new Long(15)); - } - - public void testOrVariableNumberCoercion() throws Exception { - asserter.setVariable("x", new Integer(12)); - asserter.setVariable("y", new Short((short) 3)); - asserter.assertExpression("x | y", new Long(15)); - } - - public void testOrVariableStringCoercion() throws Exception { - asserter.setVariable("x", new Integer(12)); - asserter.setVariable("y", "3"); - asserter.assertExpression("x | y", new Long(15)); - } - - public void testXorWithTwoNulls() throws Exception { - asserter.assertExpression("null ^ null", new Long(0)); - } - - public void testXorWithLeftNull() throws Exception { - asserter.assertExpression("null ^ 1", new Long(1)); - } - - public void testXorWithRightNull() throws Exception { - asserter.assertExpression("1 ^ null", new Long(1)); - } - - public void testXorSimple() throws Exception { - asserter.assertExpression("1 ^ 3", new Long(1 ^ 3)); - } - - public void testXorVariableNumberCoercion() throws Exception { - asserter.setVariable("x", new Integer(1)); - asserter.setVariable("y", new Short((short) 3)); - asserter.assertExpression("x ^ y", new Long(1 ^ 3)); - } - - public void testXorVariableStringCoercion() throws Exception { - asserter.setVariable("x", new Integer(1)); - asserter.setVariable("y", "3"); - asserter.assertExpression("x ^ y", new Long(1 ^ 3)); - } - - public void testParenthesized() throws Exception { - asserter.assertExpression("(2 | 1) & 3", Long.valueOf(3L)); - asserter.assertExpression("(2 & 1) | 3", Long.valueOf(3L)); - asserter.assertExpression("~(120 | 42)", new Long( ~(120 | 42) )); - } - }
Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java?rev=1357476&r1=1357475&r2=1357476&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ArithmeticTest.java Thu Jul 5 07:08:06 2012 @@ -16,17 +16,13 @@ */ package org.apache.commons.jexl3; +import org.apache.commons.jexl3.junit.Asserter; + +import java.util.HashMap; import java.util.Map; + import java.math.BigDecimal; import java.math.BigInteger; -import java.util.ArrayList; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Set; -import org.apache.commons.jexl3.junit.Asserter; public class ArithmeticTest extends JexlTestCase { private Asserter asserter; @@ -211,83 +207,64 @@ public class ArithmeticTest extends Jexl asserter.assertExpression("B10 / L2", new BigInteger("5")); } - public static class MatchingContainer { - private final Set<Integer> values; - - public MatchingContainer(int[] is) { - values = new HashSet<Integer>(); - for (int value : is) { - values.add(value); - } - } - - public boolean contains(int value) { - return values.contains(value); - } - } - - public static class IterableContainer implements Iterable<Integer> { - private final Set<Integer> values; - - public IterableContainer(int[] is) { - values = new HashSet<Integer>(); - for (int value : is) { - values.add(value); - } - } - - @Override - public Iterator<Integer> iterator() { - return values.iterator(); - } - } - - public void testRegexp() throws Exception { - asserter.setVariable("str", "abc456"); - asserter.assertExpression("str =~ '.*456'", Boolean.TRUE); - asserter.assertExpression("str !~ 'ABC.*'", Boolean.TRUE); - asserter.setVariable("match", "abc.*"); - asserter.setVariable("nomatch", ".*123"); - asserter.assertExpression("str =~ match", Boolean.TRUE); - asserter.assertExpression("str !~ match", Boolean.FALSE); - asserter.assertExpression("str !~ nomatch", Boolean.TRUE); - asserter.assertExpression("str =~ nomatch", Boolean.FALSE); - asserter.setVariable("match", java.util.regex.Pattern.compile("abc.*")); - asserter.setVariable("nomatch", java.util.regex.Pattern.compile(".*123")); - asserter.assertExpression("str =~ match", Boolean.TRUE); - asserter.assertExpression("str !~ match", Boolean.FALSE); - asserter.assertExpression("str !~ nomatch", Boolean.TRUE); - asserter.assertExpression("str =~ nomatch", Boolean.FALSE); - // check the in/not-in variant - asserter.assertExpression("'a' =~ ['a','b','c','d','e','f']", Boolean.TRUE); - asserter.assertExpression("'a' !~ ['a','b','c','d','e','f']", Boolean.FALSE); - asserter.assertExpression("'z' =~ ['a','b','c','d','e','f']", Boolean.FALSE); - asserter.assertExpression("'z' !~ ['a','b','c','d','e','f']", Boolean.TRUE); - // check in/not-in on array, list, map, set and duck-type collection - int[] ai = {2, 4, 42, 54}; - List<Integer> al = new ArrayList<Integer>(); - for (int i : ai) { - al.add(i); - } - Map<Integer, String> am = new HashMap<Integer, String>(); - am.put(2, "two"); - am.put(4, "four"); - am.put(42, "forty-two"); - am.put(54, "fifty-four"); - MatchingContainer ad = new MatchingContainer(ai); - IterableContainer ic = new IterableContainer(ai); - Set<Integer> as = ad.values; - Object[] vars = {ai, al, am, ad, as, ic}; - - for (Object var : vars) { - asserter.setVariable("container", var); - for (int x : ai) { - asserter.setVariable("x", x); - asserter.assertExpression("x =~ container", Boolean.TRUE); - } - asserter.setVariable("x", 169); - asserter.assertExpression("x !~ container", Boolean.TRUE); - } + // JEXL-24: long integers (and doubles) + public void testLongLiterals() throws Exception { + JexlEvalContext ctxt = new JexlEvalContext(); + ctxt.setStrictArithmetic(true); + String stmt = "{a = 10L; b = 10l; c = 42.0D; d = 42.0d; e=56.3F; f=56.3f; g=63.5; h=0x10; i=010; j=0x10L; k=010l}"; + JexlScript expr = JEXL.createScript(stmt); + /* Object value = */ expr.execute(ctxt); + assertEquals(10L, ctxt.get("a")); + assertEquals(10l, ctxt.get("b")); + assertEquals(42.0D, ctxt.get("c")); + assertEquals(42.0d, ctxt.get("d")); + assertEquals(56.3f, ctxt.get("e")); + assertEquals(56.3f, ctxt.get("f")); + assertEquals(63.5d, ctxt.get("g")); + assertEquals(0x10, ctxt.get("h")); + assertEquals(010, ctxt.get("i")); + assertEquals(0x10L, ctxt.get("j")); + assertEquals(010l, ctxt.get("k")); + } + + // JEXL-24: big integers and big decimals + public void testBigLiterals() throws Exception { + JexlEvalContext ctxt = new JexlEvalContext(); + ctxt.setStrictArithmetic(true); + String stmt = "{a = 10H; b = 10h; c = 42.0B; d = 42.0b;}"; + JexlScript expr = JEXL.createScript(stmt); + /* Object value = */ expr.execute(ctxt); + assertEquals(new BigInteger("10"), ctxt.get("a")); + assertEquals(new BigInteger("10"), ctxt.get("b")); + assertEquals(new BigDecimal("42.0"), ctxt.get("c")); + assertEquals(new BigDecimal("42.0"), ctxt.get("d")); + } + + // JEXL-24: big decimals with exponent + public void testBigExponentLiterals() throws Exception { + JexlEvalContext ctxt = new JexlEvalContext(); + ctxt.setStrictArithmetic(true); + String stmt = "{a = 42.0e1B; b = 42.0E+2B; c = 42.0e-1B; d = 42.0E-2b; e=4242.4242e1b}"; + JexlScript expr = JEXL.createScript(stmt); + /* Object value = */ expr.execute(ctxt); + assertEquals(new BigDecimal("42.0e+1"), ctxt.get("a")); + assertEquals(new BigDecimal("42.0e+2"), ctxt.get("b")); + assertEquals(new BigDecimal("42.0e-1"), ctxt.get("c")); + assertEquals(new BigDecimal("42.0e-2"), ctxt.get("d")); + assertEquals(new BigDecimal("4242.4242e1"), ctxt.get("e")); + } + + // JEXL-24: doubles with exponent + public void test2DoubleLiterals() throws Exception { + JexlEvalContext ctxt = new JexlEvalContext(); + ctxt.setStrictArithmetic(true); + String stmt = "{a = 42.0e1D; b = 42.0E+2D; c = 42.0e-1d; d = 42.0E-2d;}"; + JexlScript expr = JEXL.createScript(stmt); + /* Object value = */ expr.execute(ctxt); + assertEquals(Double.valueOf("42.0e+1"), ctxt.get("a")); + assertEquals(Double.valueOf("42.0e+2"), ctxt.get("b")); + assertEquals(Double.valueOf("42.0e-1"), ctxt.get("c")); + assertEquals(Double.valueOf("42.0e-2"), ctxt.get("d")); } /** @@ -365,4 +342,18 @@ public class ArithmeticTest extends Jexl } debuggerCheck(jexl); } + + public void testNaN() throws Exception { + Map<String, Object> ns = new HashMap<String, Object>(); + ns.put("double", Double.class); + JexlEngine jexl = new JexlBuilder().namespaces(ns).create(); + JexlScript script; + Object result; + script = jexl.createScript("#NaN"); + result = script.execute(null); + assertTrue(Double.isNaN((Double) result)); + script = jexl.createScript("double:isNaN(#NaN)"); + result = script.execute(null); + assertTrue((Boolean) result); + } } \ No newline at end of file Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java?rev=1357476&r1=1357475&r2=1357476&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/IssuesTest.java Thu Jul 5 07:08:06 2012 @@ -39,68 +39,6 @@ public class IssuesTest extends JexlTest java.util.logging.Logger.getLogger(JexlEngine.class.getName()).setLevel(java.util.logging.Level.SEVERE); } - // JEXL-24: long integers (and doubles) - public void test24() throws Exception { - JexlEngine jexl = new Engine(); - Map<String, Object> vars = new HashMap<String, Object>(); - JexlContext ctxt = new MapContext(vars); - String stmt = "{a = 10L; b = 10l; c = 42.0D; d = 42.0d; e=56.3F; f=56.3f; g=63.5; h=0x10; i=010; j=0x10L; k=010l}"; - JexlScript expr = jexl.createScript(stmt); - /* Object value = */ expr.execute(ctxt); - assertEquals(10L, vars.get("a")); - assertEquals(10l, vars.get("b")); - assertEquals(42.0D, vars.get("c")); - assertEquals(42.0d, vars.get("d")); - assertEquals(56.3f, vars.get("e")); - assertEquals(56.3f, vars.get("f")); - assertEquals(63.5d, vars.get("g")); - assertEquals(0x10, vars.get("h")); - assertEquals(010, vars.get("i")); - assertEquals(0x10L, vars.get("j")); - assertEquals(010l, vars.get("k")); - } - - // JEXL-24: big integers and big decimals - public void test24B() throws Exception { - JexlEngine jexl = new Engine(); - Map<String, Object> vars = new HashMap<String, Object>(); - JexlContext ctxt = new MapContext(vars); - String stmt = "{a = 10H; b = 10h; c = 42.0B; d = 42.0b;}"; - JexlScript expr = jexl.createScript(stmt); - /* Object value = */ expr.execute(ctxt); - assertEquals(new BigInteger("10"), vars.get("a")); - assertEquals(new BigInteger("10"), vars.get("b")); - assertEquals(new BigDecimal("42.0"), vars.get("c")); - assertEquals(new BigDecimal("42.0"), vars.get("d")); - } - - // JEXL-24: big decimals with exponent - public void test24C() throws Exception { - JexlEngine jexl = new Engine(); - Map<String, Object> vars = new HashMap<String, Object>(); - JexlContext ctxt = new MapContext(vars); - String stmt = "{a = 42.0e1B; b = 42.0E+2B; c = 42.0e-1B; d = 42.0E-2b;}"; - JexlScript expr = jexl.createScript(stmt); - /* Object value = */ expr.execute(ctxt); - assertEquals(new BigDecimal("42.0e+1"), vars.get("a")); - assertEquals(new BigDecimal("42.0e+2"), vars.get("b")); - assertEquals(new BigDecimal("42.0e-1"), vars.get("c")); - assertEquals(new BigDecimal("42.0e-2"), vars.get("d")); - } - - // JEXL-24: doubles with exponent - public void test24D() throws Exception { - JexlEngine jexl = new Engine(); - Map<String, Object> vars = new HashMap<String, Object>(); - JexlContext ctxt = new MapContext(vars); - String stmt = "{a = 42.0e1D; b = 42.0E+2D; c = 42.0e-1d; d = 42.0E-2d;}"; - JexlScript expr = jexl.createScript(stmt); - /* Object value = */ expr.execute(ctxt); - assertEquals(Double.valueOf("42.0e+1"), vars.get("a")); - assertEquals(Double.valueOf("42.0e+2"), vars.get("b")); - assertEquals(Double.valueOf("42.0e-1"), vars.get("c")); - assertEquals(Double.valueOf("42.0e-2"), vars.get("d")); - } // JEXL-49: blocks not parsed (fixed) public void test49() throws Exception { @@ -151,7 +89,7 @@ public class IssuesTest extends JexlTest jc.set("foo", new Foo()); /* Object o = */ e.evaluate(jc); fail("Should have failed due to invalid assignment"); - } catch (JexlException.Parsing xparse) { + } catch (JexlException.Assignment xparse) { String dbg = xparse.toString(); } catch (JexlException xjexl) { fail("Should have thrown a parse exception"); @@ -869,44 +807,38 @@ public class IssuesTest extends JexlTest } } - JexlEngine createJexl132() { + public void test132a() throws Exception { Map<String, Object> ns = new HashMap<String, Object>(); ns.put("math", Math.class); - return new JexlBuilder().arithmetic(new Arithmetic132()).namespaces(ns).create(); - - } - - public void test132a() throws Exception { - JexlEngine jexl = createJexl132(); - String expr = "1/0"; + JexlEngine jexl = new JexlBuilder().arithmetic(new Arithmetic132()).namespaces(ns).create(); - Object evaluate = jexl.createExpression(expr).evaluate(null); + Object evaluate = jexl.createExpression("1/0").evaluate(null); assertTrue(Double.isInfinite((Double)evaluate)); - expr = "-1/0"; - evaluate = jexl.createExpression(expr).evaluate(null); + evaluate = jexl.createExpression("-1/0").evaluate(null); assertTrue(Double.isInfinite((Double)evaluate)); - } - - public void test132b() throws Exception { - JexlEngine jexl = createJexl132(); - String expr = "1.0/0.0"; - - Object evaluate = jexl.createExpression(expr).evaluate(null); + evaluate = jexl.createExpression("1.0/0.0").evaluate(null); assertTrue(Double.isInfinite((Double)evaluate)); - expr = "-1.0/0"; - evaluate = jexl.createExpression(expr).evaluate(null); + evaluate = jexl.createExpression("-1.0/0.0").evaluate(null); assertTrue(Double.isInfinite((Double)evaluate)); - } - public void test132c() throws Exception { - JexlEngine jexl = createJexl132(); - String expr = "math:abs(-42)"; - - Object evaluate = jexl.createExpression(expr).evaluate(null); + evaluate = jexl.createExpression("math:abs(-42)").evaluate(null); assertEquals(42, evaluate); } + public void test135() throws Exception { + JexlEngine jexl = new Engine(); + JexlScript script; + Object result; + Map<Integer, Object> foo = new HashMap<Integer, Object>(); + foo.put(3, 42); + script = jexl.createScript("var y = state[3]; y", "state"); + result = script.execute(null, foo); + assertEquals(42, result); + script = jexl.createScript("var y = state[1 + 1 + 1]; y", "state"); + result = script.execute(null, foo); + assertEquals(42, result); + } } Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlEvalContext.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlEvalContext.java?rev=1357476&r1=1357475&r2=1357476&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlEvalContext.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/JexlEvalContext.java Thu Jul 5 07:08:06 2012 @@ -17,6 +17,7 @@ package org.apache.commons.jexl3; import java.math.MathContext; +import java.nio.charset.Charset; import java.util.Collections; import java.util.Map; @@ -48,6 +49,11 @@ public class JexlEvalContext implements this(EMPTY_MAP); } + @Override + public Charset getCharset() { + return Charset.defaultCharset(); + } + /** * Creates an evaluation environment wrapping an existing user provided vars. * <p>The supplied vars should be null only in derived classes that override the get/set/has methods. 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=1357476&r1=1357475&r2=1357476&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 Thu Jul 5 07:08:06 2012 @@ -649,7 +649,7 @@ public class JexlTest extends JexlTestCa Foo foo = new Foo(); jc.set("foo", foo); Parser parser = new Parser(new StringReader(";")); - parser.parse(null, new StringReader("aString = 'World';"), null, false); + parser.parse(null, "aString = 'World';", null, false); assertExpression(jc, "hello = 'world'", "world"); assertEquals("hello variable not changed", "world", jc.get("hello")); Copied: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ReadonlyContext.java (from r1338871, commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/ReadonlyContext.java) URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ReadonlyContext.java?p2=commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ReadonlyContext.java&p1=commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/ReadonlyContext.java&r1=1338871&r2=1357476&rev=1357476&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/main/java/org/apache/commons/jexl3/ReadonlyContext.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/ReadonlyContext.java Thu Jul 5 07:08:06 2012 @@ -17,6 +17,9 @@ package org.apache.commons.jexl3; import java.math.MathContext; +import java.nio.charset.Charset; +import org.apache.commons.jexl3.JexlContext; +import org.apache.commons.jexl3.JexlEngine; /** * A readonly context wrapper. @@ -43,7 +46,7 @@ public final class ReadonlyContext imple return wrapped.get(name); } - /** + /** * Will throw an UnsupportedOperationException when called; the JexlEngine deals with it appropriately. * @param name the unused variable name * @param value the unused variable value @@ -68,7 +71,7 @@ public final class ReadonlyContext imple // egnie return options == null? null : options.isStrict(); } - + @Override public Boolean isStrictArithmetic() { return options == null? null : options.isStrict(); @@ -83,4 +86,9 @@ public final class ReadonlyContext imple public int getArithmeticMathScale() { return options == null? -1 : options.getArithmeticMathScale(); } + + @Override + public Charset getCharset() { + return options == null? Charset.defaultCharset() : options.getCharset(); + } } Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/Util.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/Util.java?rev=1357476&r1=1357475&r2=1357476&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/Util.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/internal/Util.java Thu Jul 5 07:08:06 2012 @@ -20,6 +20,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.jexl3.JexlEngine; +import org.apache.commons.jexl3.JexlException; import org.apache.commons.jexl3.JexlScript; import org.apache.commons.jexl3.parser.ASTJexlScript; import org.apache.commons.jexl3.parser.JexlNode; @@ -29,7 +30,6 @@ import org.apache.commons.jexl3.parser.J * @author henri */ public class Util { - /** * Will force testing the debugger for each derived test class by * recreating each expression from the JexlNode in the JexlEngine cache & @@ -37,7 +37,7 @@ public class Util { * @throws Exception */ public static void debuggerCheck(JexlEngine ijexl) throws Exception { - Engine jexl = (Engine) ijexl; + Engine jexl = (Engine) ijexl; // without a cache, nothing to check if (jexl == null || jexl.cache == null) { return; @@ -46,27 +46,35 @@ public class Util { jdbg.parser.allowRegisters(true); Debugger dbg = new Debugger(); // iterate over all expression in cache - Iterator<Map.Entry<String,ASTJexlScript>> inodes = jexl.cache.entrySet().iterator(); + Iterator<Map.Entry<String, ASTJexlScript>> inodes = jexl.cache.entrySet().iterator(); while (inodes.hasNext()) { - Map.Entry<String,ASTJexlScript> entry = inodes.next(); + Map.Entry<String, ASTJexlScript> entry = inodes.next(); JexlNode node = entry.getValue(); // recreate expr string from AST dbg.debug(node); String expressiondbg = dbg.toString(); // recreate expr from string - Script exprdbg = (Script) jdbg.createScript(null, expressiondbg, null); - // make arg cause become the root cause - JexlNode root = exprdbg.script; - while (root.jjtGetParent() != null) { - root = root.jjtGetParent(); - } - // test equality - String reason = checkEquals(root, node); - if (reason != null) { - throw new RuntimeException("debugger equal failed: " - + expressiondbg - +" /**** " +reason+" **** */ " - + entry.getKey()); + try { + Script exprdbg = jdbg.createScript(null, expressiondbg, null); + // make arg cause become the root cause + JexlNode root = exprdbg.script; + while (root.jjtGetParent() != null) { + root = root.jjtGetParent(); + } + // test equality + String reason = checkEquals(root, node); + if (reason != null) { + throw new RuntimeException("check equal failed: " + + expressiondbg + + " /**** " + reason + " **** */ " + + entry.getKey()); + } + } catch (JexlException xjexl) { + throw new RuntimeException("check parse failed: " + + expressiondbg + + " /******** */ " + + entry.getKey(), xjexl); + } } } @@ -84,13 +92,13 @@ public class Util { /** * Recursively adds all children of a script to the list of descendants. - * @param list the list of descendants to add to + * @param list the list of descendants to add to * @param script the script & descendants to add */ private static void flatten(List<JexlNode> list, JexlNode node) { int nc = node.jjtGetNumChildren(); list.add(node); - for(int c = 0; c < nc; ++c) { + for (int c = 0; c < nc; ++c) { flatten(list, node.jjtGetChild(c)); } } @@ -107,9 +115,9 @@ public class Util { ArrayList<JexlNode> lhsl = flatten(lhs); ArrayList<JexlNode> rhsl = flatten(rhs); if (lhsl.size() != rhsl.size()) { - return "size: " + lhsl.size() + " != " + rhsl.size(); + return "size: " + lhsl.size() + " != " + rhsl.size(); } - for(int n = 0; n < lhsl.size(); ++n) { + for (int n = 0; n < lhsl.size(); ++n) { lhs = lhsl.get(n); rhs = rhsl.get(n); if (lhs.getClass() != rhs.getClass()) { @@ -118,7 +126,7 @@ public class Util { String lhss = lhs.toString(); String rhss = rhs.toString(); if ((lhss == null && rhss != null) - || (lhss != null && rhss == null)) { + || (lhss != null && rhss == null)) { return "image: " + lhss + " != " + rhss; } if (lhss != null && !lhss.equals(rhss)) { @@ -147,7 +155,6 @@ public class Util { return strb.toString(); } - private String flattenedStr(JexlNode node) { ArrayList<JexlNode> flattened = flatten(node); StringBuilder strb = new StringBuilder(); Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java?rev=1357476&r1=1357475&r2=1357476&view=diff ============================================================================== --- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java (original) +++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl3/parser/ParserTest.java Thu Jul 5 07:08:06 2012 @@ -19,6 +19,7 @@ package org.apache.commons.jexl3.parser; import java.io.StringReader; import junit.framework.TestCase; +import org.apache.commons.jexl3.JexlException; /** * @since 1.0 @@ -30,23 +31,39 @@ public class ParserTest extends TestCase } /** - * parse test : see if we can parse a little script + * See if we can parse simple scripts */ - public void testParse1() throws Exception { + public void testParse() throws Exception { Parser parser = new Parser(new StringReader(";")); - SimpleNode sn = parser.parse(null, new StringReader("foo = 1;"), null, false); + JexlNode sn; + sn = parser.parse(null, "foo = 1;", null, false); assertNotNull("parsed node is null", sn); - } - - public void testParse2() throws Exception { - Parser parser = new Parser(new StringReader(";")); - SimpleNode sn = parser.parse(null, new StringReader("foo = \"bar\";"), null, false); + sn = parser.parse(null, "foo = \"bar\";", null, false); assertNotNull("parsed node is null", sn); - sn = parser.parse(null, new StringReader("foo = 'bar';"), null, false); + sn = parser.parse(null, "foo = 'bar';", null, false); assertNotNull("parsed node is null", sn); } + public void testErrorAssign() throws Exception { + Parser parser = new Parser(new StringReader(";")); + try { + JexlNode sn = parser.parse(null, "foo() = 1;", null, false); + fail("should have failed on invalid assignment"); + } catch (JexlException.Parsing xparse) { + // ok + } + } + + public void testErrorAmbiguous() throws Exception { + Parser parser = new Parser(new StringReader(";")); + try { + JexlNode sn = parser.parse(null, "x = 1 y = 5", null, false); + fail("should have failed on ambiguous statement"); + } catch (JexlException.Ambiguous xambiguous) { + // ok + } + } }