commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hen...@apache.org
Subject svn commit: r1196677 [2/2] - in /commons/proper/jexl/trunk/src: main/java/org/apache/commons/jexl2/ main/java/org/apache/commons/jexl2/introspection/ main/java/org/apache/commons/jexl2/parser/ main/java/org/apache/commons/jexl2/scripting/ test/java/org...
Date Wed, 02 Nov 2011 16:32:40 GMT
Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/UnifiedJEXLTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/UnifiedJEXLTest.java?rev=1196677&r1=1196676&r2=1196677&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/UnifiedJEXLTest.java
(original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/UnifiedJEXLTest.java
Wed Nov  2 16:32:39 2011
@@ -15,6 +15,9 @@
  * limitations under the License.
  */
 package org.apache.commons.jexl2;
+
+import java.io.StringReader;
+import java.io.StringWriter;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
@@ -22,11 +25,13 @@ import java.util.Map;
 import java.util.Set;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+
 /**
  * Test cases for the UnifiedEL.
  */
 public class UnifiedJEXLTest extends JexlTestCase {
     private static final JexlEngine ENGINE = createEngine(false);
+
     static {
         ENGINE.setSilent(false);
         ENGINE.setCache(128);
@@ -34,13 +39,13 @@ public class UnifiedJEXLTest extends Jex
     private static final UnifiedJEXL EL = new UnifiedJEXL(ENGINE);
     private static final Log LOG = LogFactory.getLog(UnifiedJEXL.class);
     private JexlContext context = null;
-    private Map<String,Object> vars =null;
+    private Map<String, Object> vars = null;
 
     @Override
     public void setUp() throws Exception {
         // ensure jul logging is only error
         java.util.logging.Logger.getLogger(JexlEngine.class.getName()).setLevel(java.util.logging.Level.SEVERE);
-        vars = new HashMap<String,Object>();
+        vars = new HashMap<String, Object>();
         context = new MapContext(vars);
     }
 
@@ -49,18 +54,38 @@ public class UnifiedJEXLTest extends Jex
         debuggerCheck(ENGINE);
         super.tearDown();
     }
+    
+    /** Extract the source from a toString-ed expression. */
+    private String getSource(String tostring) {
+        int len = tostring.length();
+        int sc = tostring.lastIndexOf(" /*= ");
+        if (sc >= 0)  {
+            sc += " /*= ".length();
+        }
+        int ec = tostring.lastIndexOf(" */");
+        if (sc >= 0 && ec >= 0 && ec > sc && ec < len)
{
+            return tostring.substring(sc, ec);
+        } else {
+            return tostring;
+        }
+
+    }
 
     public static class Froboz {
         int value;
+
         public Froboz(int v) {
             value = v;
         }
+
         public void setValue(int v) {
             value = v;
         }
+
         public int getValue() {
             return value;
         }
+
         public int plus10() {
             int i = value;
             value += 10;
@@ -89,9 +114,10 @@ public class UnifiedJEXLTest extends Jex
         o = check.evaluate(context);
         assertEquals("Result is not 10", new Integer(10), o);
     }
-    
+
     public void testComposite() throws Exception {
-        UnifiedJEXL.Expression expr = EL.parse("Dear ${p} ${name};");
+        String source = "Dear ${p} ${name};";
+        UnifiedJEXL.Expression expr = EL.parse(source);
         vars.put("p", "Mr");
         vars.put("name", "Doe");
         assertTrue("expression should be immediate", expr.isImmediate());
@@ -101,18 +127,20 @@ public class UnifiedJEXLTest extends Jex
         vars.put("name", "Jones");
         o = expr.evaluate(context);
         assertEquals("Dear Ms Jones;", o);
+        assertEquals(source, getSource(expr.toString()));
     }
 
     public void testPrepareEvaluate() throws Exception {
+        final String source = "Dear #{p} ${name};";
         UnifiedJEXL.Expression expr = EL.parse("Dear #{p} ${name};");
         assertTrue("expression should be deferred", expr.isDeferred());
-        
+
         Set<List<String>> evars = expr.getVariables();
         assertEquals(1, evars.size());
         assertTrue(evars.contains(Arrays.asList("name")));
         vars.put("name", "Doe");
-        UnifiedJEXL.Expression  phase1 = expr.prepare(context);
-        String as = phase1.toString();
+        UnifiedJEXL.Expression phase1 = expr.prepare(context);
+        String as = phase1.asString();
         assertEquals("Dear ${p} Doe;", as);
         Set<List<String>> evars1 = phase1.getVariables();
         assertEquals(1, evars1.size());
@@ -121,45 +149,65 @@ public class UnifiedJEXLTest extends Jex
         vars.put("name", "Should not be used in 2nd phase");
         Object o = phase1.evaluate(context);
         assertEquals("Dear Mr Doe;", o);
+        
+        String p1 = getSource(phase1.toString());
+        assertEquals(source, getSource(phase1.toString()));
+        assertEquals(source, getSource(expr.toString()));
     }
-    
+
     public void testNested() throws Exception {
-        UnifiedJEXL.Expression expr = EL.parse("#{${hi}+'.world'}");
-        vars.put("hi", "hello");
-        vars.put("hello.world", "Hello World!");
+        final String source = "#{${hi}+'.world'}";
+        UnifiedJEXL.Expression expr = EL.parse(source);
+
+        Set<List<String>> evars = expr.getVariables();
+        assertEquals(1, evars.size());
+        assertTrue(evars.contains(Arrays.asList("hi")));
+
+        vars.put("hi", "greeting");
+        vars.put("greeting.world", "Hello World!");
+        assertTrue("expression should be deferred", expr.isDeferred());
         Object o = expr.evaluate(context);
-        assertTrue("source should not same expression", expr.getSource() != expr.prepare(context));
-        assertTrue("expression should be immediate", expr.isImmediate());
         assertEquals("Hello World!", o);
+        
+        assertEquals(source, getSource(expr.toString()));
     }
 
     public void testImmediate() throws Exception {
         JexlContext none = null;
-        UnifiedJEXL.Expression expr = EL.parse("${'Hello ' + 'World!'}");
+        final String source = "${'Hello ' + 'World!'}";
+        UnifiedJEXL.Expression expr = EL.parse(source);
         UnifiedJEXL.Expression prepared = expr.prepare(none);
-        assertEquals("prepare should return same expression","Hello World!",prepared.toString());
+        assertEquals("prepare should return same expression", "Hello World!", prepared.asString());
         Object o = expr.evaluate(none);
         assertTrue("expression should be immediate", expr.isImmediate());
         assertEquals("Hello World!", o);
+        
+        assertEquals(source, getSource(expr.toString()));
     }
 
     public void testConstant() throws Exception {
         JexlContext none = null;
-        UnifiedJEXL.Expression expr = EL.parse("Hello World!");
+        final String source = "Hello World!";
+        UnifiedJEXL.Expression expr = EL.parse(source);
         assertTrue("prepare should return same expression", expr.prepare(none) == expr);
         Object o = expr.evaluate(none);
         assertTrue("expression should be immediate", expr.isImmediate());
         assertEquals("Hello World!", o);
+        
+        assertEquals(source, getSource(expr.toString()));
     }
 
     public void testDeferred() throws Exception {
         JexlContext none = null;
-        UnifiedJEXL.Expression expr = EL.parse("#{'world'}");
+        final String source = "#{'world'}";
+        UnifiedJEXL.Expression expr = EL.parse(source);
+        assertTrue("expression should be deferred", expr.isDeferred());
         String as = expr.prepare(none).asString();
         assertEquals("prepare should return immediate version", "${'world'}", as);
         Object o = expr.evaluate(none);
-        assertTrue("expression should be immediate", expr.isImmediate());
         assertEquals("world", o);
+        
+        assertEquals(source, getSource(expr.toString()));
     }
 
     public void testEscape() throws Exception {
@@ -195,48 +243,45 @@ public class UnifiedJEXLTest extends Jex
             JexlContext none = null;
             expr.evaluate(none);
             fail("should be malformed");
-        }
-        catch(UnifiedJEXL.Exception xjexl) {
+        } catch (UnifiedJEXL.Exception xjexl) {
             // expected
             String xmsg = xjexl.getMessage();
             LOG.warn(xmsg);
         }
     }
-    
+
     public void testMalformedNested() throws Exception {
         try {
             UnifiedJEXL.Expression expr = EL.parse("#{${hi} world}");
             JexlContext none = null;
             expr.evaluate(none);
             fail("should be malformed");
-        }
-        catch(UnifiedJEXL.Exception xjexl) {
+        } catch (UnifiedJEXL.Exception xjexl) {
             // expected
             String xmsg = xjexl.getMessage();
             LOG.warn(xmsg);
         }
     }
-    
+
     public void testBadContextNested() throws Exception {
         try {
             UnifiedJEXL.Expression expr = EL.parse("#{${hi}+'.world'}");
             JexlContext none = null;
             expr.evaluate(none);
             fail("should be malformed");
-        }
-        catch(UnifiedJEXL.Exception xjexl) {
+        } catch (UnifiedJEXL.Exception xjexl) {
             // expected
             String xmsg = xjexl.getMessage();
             LOG.warn(xmsg);
         }
     }
-    
+
     public void testCharAtBug() throws Exception {
         vars.put("foo", "abcdef");
         UnifiedJEXL.Expression expr = EL.parse("${foo.substring(2,4)/*comment*/}");
         Object o = expr.evaluate(context);
         assertEquals("cd", o);
-        
+
         vars.put("bar", "foo");
         try {
             ENGINE.setSilent(true);
@@ -244,11 +289,108 @@ public class UnifiedJEXLTest extends Jex
             expr = expr.prepare(context);
             o = expr.evaluate(context);
             assertEquals(null, o);
-        }
-        finally {
+        } finally {
             ENGINE.setSilent(false);
         }
 
     }
 
+    public void testTemplate0() throws Exception {
+        String source = "   $$ if(x) {\nx is ${x}\n   $$ } else {\n${'no x'}\n$$ }\n";
+        StringWriter strw;
+        String output;
+
+        UnifiedJEXL.Template t = EL.createTemplate(source);
+
+        vars.put("x", 42);
+        strw = new StringWriter();
+        t.evaluate(context, strw);
+        output = strw.toString();
+        assertEquals("x is 42\n", output);
+
+        strw = new StringWriter();
+        vars.put("x", "");
+        t.evaluate(context, strw);
+        output = strw.toString();
+        assertEquals("no x\n", output);
+        
+        String dstr = t.toString();
+        assertNotNull(dstr);
+    }
+
+    public void testTemplate1() throws Exception {
+        String source = "$$ if(x) {\nx is ${x}\n$$ } else {\n${'no x'}\n$$ }\n";
+        StringWriter strw;
+        String output;
+
+        UnifiedJEXL.Template t = EL.createTemplate("$$", new StringReader(source), "x");
+        String dstr = t.asString();
+        assertNotNull(dstr);
+
+        strw = new StringWriter();
+        t.evaluate(context, strw, 42);
+        output = strw.toString();
+        assertEquals("x is 42\n", output);
+
+        strw = new StringWriter();
+        t.evaluate(context, strw, "");
+        output = strw.toString();
+        assertEquals("no x\n", output);
+    }
+    
+    public void testPrepareTemplate() throws Exception {
+        String source =
+                 "$$ for(var x : list) {\n"
+               + "${l10n}=#{x}\n"
+               + "$$ }\n";
+        int[] args = { 42 };
+        UnifiedJEXL.Template tl10n = EL.createTemplate(source, "list");
+        String dstr = tl10n.asString();
+        assertNotNull(dstr);
+        context.set("l10n", "valeur");
+        UnifiedJEXL.Template tpFR = tl10n.prepare(context);
+        context.set("l10n", "value");
+        UnifiedJEXL.Template tpEN = tl10n.prepare(context);
+        context.set("l10n", null);
+        
+        StringWriter strw;
+        strw = new StringWriter();
+        tpFR.evaluate(context, strw, args);
+        String outFR = strw.toString();
+        assertEquals("valeur=42\n", outFR);
+        
+        context.set("l10n", null);
+        strw = new StringWriter();
+        tpEN.evaluate(context, strw, args);
+        String outEN = strw.toString();
+        assertEquals("value=42\n", outEN);
+    }
+
+    public void test42() throws Exception {
+        String test42 =
+                  "$$ for(var x : list) {\n"
+                + "$$   if (x == 42) {\n"
+                + "Life, the universe, and everything\n"
+                + "$$   } else if (x > 42) {\n"
+                + "The value ${x} is over fourty-two\n"
+                + "$$   } else {\n"
+                + "The value ${x} is under fourty-two\n"
+                + "$$   }\n"
+                + "$$ }\n";
+        UnifiedJEXL.Template t = EL.createTemplate("$$", new StringReader(test42), "list");
+        StringWriter strw = new StringWriter();
+        int[] list = {1, 3, 5, 42, 169};
+        t.evaluate(context, strw, list);
+        String output = strw.toString();
+        String out42 =
+                  "The value 1 is under fourty-two\n"
+                + "The value 3 is under fourty-two\n"
+                + "The value 5 is under fourty-two\n"
+                + "Life, the universe, and everything\n"
+                + "The value 169 is over fourty-two\n";
+        assertEquals(out42, output);
+        
+        String dstr = t.asString();
+        assertNotNull(dstr);
+    }
 }

Modified: commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/parser/ParserTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/parser/ParserTest.java?rev=1196677&r1=1196676&r2=1196677&view=diff
==============================================================================
--- commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/parser/ParserTest.java
(original)
+++ commons/proper/jexl/trunk/src/test/java/org/apache/commons/jexl2/parser/ParserTest.java
Wed Nov  2 16:32:39 2011
@@ -49,21 +49,4 @@ public class ParserTest extends TestCase
         assertNotNull("parsed node is null", sn);
     }
 
-    public void testReadLines() throws Exception {
-        String input = "foo bar\n\rquux\n\r\npizza";
-        StringBuilder output = new StringBuilder(32);
-        int read = StringParser.readLines(output, input, 0, null);
-        assertEquals(input.length(), read);
-        String outstr = output.toString();
-        assertEquals("foo bar quux pizza ", outstr);
-    }
-
-    public void testReadLines$$() throws Exception {
-        String input = "$$foo bar\n\r$$quux\n\r\n$$pizza";
-        StringBuilder output = new StringBuilder(32);
-        int read = StringParser.readLines(output, input, 0, "$$");
-        assertEquals(input.length(), read);
-        String outstr = output.toString();
-        assertEquals("foo bar quux pizza ", outstr);
-    }
 }



Mime
View raw message