You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ra...@apache.org on 2009/08/07 06:55:20 UTC

svn commit: r801881 - in /commons/proper/jexl/branches/2.0: examples/ src/test/java/org/apache/commons/jexl/examples/

Author: rahul
Date: Fri Aug  7 04:55:19 2009
New Revision: 801881

URL: http://svn.apache.org/viewvc?rev=801881&view=rev
Log:
Turn examples directory code into unit tests.
Patch by Henri Biestro <hbiestro at gmail dot com>
JEXL-69

Added:
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/ArrayTest.java   (with props)
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/MethodPropertyTest.java   (with props)
    commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/Output.java   (with props)
Removed:
    commons/proper/jexl/branches/2.0/examples/

Added: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/ArrayTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/ArrayTest.java?rev=801881&view=auto
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/ArrayTest.java (added)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/ArrayTest.java Fri Aug  7 04:55:19 2009
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.jexl.examples;
+
+import org.apache.commons.jexl.*;
+import junit.framework.TestCase;
+import java.util.List;
+import java.util.ArrayList;
+
+/**
+ *  Simple example to show how to access arrays.
+ *
+ *  @since 1.0
+ *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
+ *  @version $Id$
+ */
+public class ArrayTest extends TestCase {
+    /**
+     * An example for array access.
+     */
+    static void example(Output out) throws Exception {
+        /**
+         * First step is to retrieve an instance of a JexlEngine;
+         * it might be already existing and shared or created anew.
+         */
+        JexlEngine jexl = new JexlEngine();
+        /*
+         *  Second make a jexlContext and put stuff in it
+         */
+        JexlContext jc = JexlHelper.createContext();
+
+        List<Object> l = new ArrayList<Object>();
+        l.add("Hello from location 0");
+        Integer two = new Integer(2);
+        l.add(two);
+        jc.getVars().put("array", l);
+
+        Expression e = jexl.createExpression("array[1]");
+        Object o = e.evaluate(jc);
+        out.print("Object @ location 1 = ", o, two);
+
+        e = jexl.createExpression("array[0].length()");
+        o = e.evaluate(jc);
+
+        out.print("The length of the string at location 0 is : ", o, 21);
+    }
+
+    /**
+     * Unit test entry point.
+     * @throws Exception
+     */
+    public void testExample() throws Exception {
+        example(Output.JUNIT);
+    }
+
+    /** 
+     * Command line entry point.
+     * @param args command line arguments
+     * @throws Exception cos jexl does. 
+     */
+    public static void main(String[] args) throws Exception {
+        example(Output.SYSTEM);
+    }
+}
\ No newline at end of file

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/ArrayTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/ArrayTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/MethodPropertyTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/MethodPropertyTest.java?rev=801881&view=auto
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/MethodPropertyTest.java (added)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/MethodPropertyTest.java Fri Aug  7 04:55:19 2009
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.jexl.examples;
+
+import org.apache.commons.jexl.*;
+import junit.framework.TestCase;
+
+/**
+ *  Simple example to show how to access method and properties.
+ *
+ *  @since 1.0
+ *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
+ *  @version $Id$
+ */
+public class MethodPropertyTest extends TestCase {
+    /**
+     * An example for method access.
+     */
+    public static void example(final Output out) throws Exception {
+        /**
+         * First step is to retrieve an instance of a JexlEngine;
+         * it might be already existing and shared or created anew.
+         */
+        JexlEngine jexl = new JexlEngine();
+        /*
+         *  Second make a jexlContext and put stuff in it
+         */
+        JexlContext jc = JexlHelper.createContext();
+
+        /**
+         * The Java equivalents of foo and number for comparison and checking
+         */
+        Foo foo = new Foo();
+        Integer number = new Integer(10);
+
+        jc.getVars().put("foo", foo);
+        jc.getVars().put("number", number);
+
+        /*
+         *  access a method w/o args
+         */
+        Expression e = jexl.createExpression("foo.getFoo()");
+        Object o = e.evaluate(jc);
+        out.print("value returned by the method getFoo() is : ", o, foo.getFoo());
+
+        /*
+         *  access a method w/ args
+         */
+        e = jexl.createExpression("foo.convert(1)");
+        o = e.evaluate(jc);
+        out.print("value of " + e.getExpression() + " is : ", o, foo.convert(1));
+
+        e = jexl.createExpression("foo.convert(1+7)");
+        o = e.evaluate(jc);
+        out.print("value of " + e.getExpression() + " is : ", o, foo.convert(1+7));
+
+        e = jexl.createExpression("foo.convert(1+number)");
+        o = e.evaluate(jc);
+        out.print("value of " + e.getExpression() + " is : ", o, foo.convert(1+number));
+
+        /*
+         * access a property
+         */
+        e = jexl.createExpression("foo.bar");
+        o = e.evaluate(jc);
+        out.print("value returned for the property 'bar' is : ", o, foo.get("bar"));
+
+    }
+
+    /**
+     * Helper example class.
+     */
+    public static class Foo {
+        /**
+         * Gets foo.
+         * @return a string.
+         */
+        public String getFoo() {
+            return "This is from getFoo()";
+        }
+
+        /**
+         * Gets an arbitrary property.
+         * @param arg property name.
+         * @return arg prefixed with 'This is the property '.
+         */
+        public String get(String arg) {
+            return "This is the property " + arg;
+        }
+
+        /**
+         * Gets a string from the argument.
+         * @param i a long.
+         * @return The argument prefixed with 'The value is : '
+         */
+        public String convert(long i) {
+            return "The value is : " + i;
+        }
+    }
+
+
+    /**
+     * Unit test entry point.
+     * @throws Exception
+     */
+    public void testExample() throws Exception {
+        example(Output.JUNIT);
+    }
+
+    /**
+     * Command line entry point.
+     * @param args command line arguments
+     * @throws Exception cos jexl does.
+     */
+    public static void main(String[] args) throws Exception {
+        example(Output.SYSTEM);
+    }
+}
\ No newline at end of file

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/MethodPropertyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/MethodPropertyTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Added: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/Output.java
URL: http://svn.apache.org/viewvc/commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/Output.java?rev=801881&view=auto
==============================================================================
--- commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/Output.java (added)
+++ commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/Output.java Fri Aug  7 04:55:19 2009
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.jexl.examples;
+import junit.framework.TestCase;
+
+/**
+ * Abstracts using a test within Junit or through a main method.
+ */
+public abstract class Output {
+    /**
+     * Creates an output using System.out.
+     */
+    private Output() {
+        // nothing to do
+    }
+
+    /**
+     * Outputs the actual and value or checks the actual equals the expected value.
+     * @param expr the message to output
+     * @param actual the actual value to output
+     * @param expected the expected value
+     */
+    public abstract void print(String expr, Object actual, Object expected);
+
+    /**
+     * The output instance for Junit TestCase calling assertEquals.
+     */
+    public static final Output JUNIT = new Output() {
+        @Override
+        public void print(String expr, Object actual, Object expected) {
+            TestCase.assertEquals(expr, expected, actual);
+        }
+    };
+
+        
+    /**
+     * The output instance for the general outputing to System.out.
+     */
+    public static final Output SYSTEM = new Output() {
+        public void print(String expr, Object actual, Object expected) {
+            System.out.print(expr);
+            System.out.println(actual);
+        }
+    };
+}
\ No newline at end of file

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/Output.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/jexl/branches/2.0/src/test/java/org/apache/commons/jexl/examples/Output.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL