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

svn commit: r567632 - in /commons/proper/el/trunk: ./ src/test/ src/test/org/ src/test/org/apache/ src/test/org/apache/commons/ src/test/org/apache/commons/el/

Author: bayard
Date: Mon Aug 20 03:06:48 2007
New Revision: 567632

URL: http://svn.apache.org/viewvc?rev=567632&view=rev
Log:
Adding a unit test structure, with a few basic unit tests

Added:
    commons/proper/el/trunk/src/test/
    commons/proper/el/trunk/src/test/org/
    commons/proper/el/trunk/src/test/org/apache/
    commons/proper/el/trunk/src/test/org/apache/commons/
    commons/proper/el/trunk/src/test/org/apache/commons/el/
    commons/proper/el/trunk/src/test/org/apache/commons/el/ELTest.java   (with props)
    commons/proper/el/trunk/src/test/org/apache/commons/el/MockFunctionMapper.java   (with props)
    commons/proper/el/trunk/src/test/org/apache/commons/el/MockVariableResolver.java   (with props)
Modified:
    commons/proper/el/trunk/project.xml

Modified: commons/proper/el/trunk/project.xml
URL: http://svn.apache.org/viewvc/commons/proper/el/trunk/project.xml?rev=567632&r1=567631&r2=567632&view=diff
==============================================================================
--- commons/proper/el/trunk/project.xml (original)
+++ commons/proper/el/trunk/project.xml Mon Aug 20 03:06:48 2007
@@ -104,6 +104,15 @@
   </developers>
   
   <dependencies>
+      <dependency>
+        <groupId>junit</groupId>
+        <artifactId>junit</artifactId>
+        <version>3.8.1</version>
+        <properties>
+         <scope>test</scope>
+         <comment>Required only for testing.</comment>
+        </properties>
+      </dependency>
     <dependency>
       <groupId>servletapi</groupId>
       <artifactId>servletapi</artifactId>

Added: commons/proper/el/trunk/src/test/org/apache/commons/el/ELTest.java
URL: http://svn.apache.org/viewvc/commons/proper/el/trunk/src/test/org/apache/commons/el/ELTest.java?rev=567632&view=auto
==============================================================================
--- commons/proper/el/trunk/src/test/org/apache/commons/el/ELTest.java (added)
+++ commons/proper/el/trunk/src/test/org/apache/commons/el/ELTest.java Mon Aug 20 03:06:48 2007
@@ -0,0 +1,84 @@
+/*
+ * 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.el;
+
+import org.apache.commons.el.parser.ELParser;
+import org.apache.commons.el.parser.ParseException;
+
+import java.io.StringReader;
+import java.io.IOException;
+
+import javax.servlet.jsp.el.FunctionMapper;
+import javax.servlet.jsp.el.VariableResolver;
+import javax.servlet.jsp.el.ELException;
+
+import junit.framework.TestCase;
+
+public class ELTest extends TestCase {
+
+    private MockVariableResolver resolver;
+    private MockFunctionMapper mapper;
+
+    protected void setUp() {
+        this.resolver = new MockVariableResolver();
+        this.mapper = new MockFunctionMapper();
+    }
+
+    protected void tearDown() {
+        this.resolver = null;
+        this.mapper = null;
+    }
+
+    public void testMath() {
+        assertEquals("2", evaluate("${1 + 1}"));
+        assertEquals("5", evaluate("${10 - 5}"));
+    }
+
+    public void testLiteral() {
+        assertEquals("ac", evaluate("${'ac'}"));
+        assertEquals("\"", evaluate("${'\"'}"));
+        assertEquals("true", evaluate("${true}"));
+    }
+
+    private String evaluate(String input) {
+        try {
+            StringReader rdr = new StringReader(input);
+            ELParser parser = new ELParser(rdr);
+            Object result = parser.ExpressionString();
+            if(result instanceof String) {
+                return (String) result;
+            } else if(result instanceof Expression) {
+                Expression expr = (Expression) result;
+                result = expr.evaluate(this.resolver, this.mapper);
+                return result == null ? null : result.toString();
+            } else if(result instanceof ExpressionString) {
+                Expression expr = (Expression) result;
+                result = expr.evaluate(this.resolver, this.mapper);
+                return result == null ? null : result.toString();
+            } else {
+                throw new RuntimeException("Incorrect type returned; not String, Expression or ExpressionString");
+            }
+        } catch(ParseException pe) {
+            throw new RuntimeException("ParseException thrown: " + pe.getMessage(), pe);
+        } catch(ELException ele) {
+            throw new RuntimeException("ELException thrown: " + ele.getMessage(), ele);
+        }
+    }
+
+}

Propchange: commons/proper/el/trunk/src/test/org/apache/commons/el/ELTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/el/trunk/src/test/org/apache/commons/el/MockFunctionMapper.java
URL: http://svn.apache.org/viewvc/commons/proper/el/trunk/src/test/org/apache/commons/el/MockFunctionMapper.java?rev=567632&view=auto
==============================================================================
--- commons/proper/el/trunk/src/test/org/apache/commons/el/MockFunctionMapper.java (added)
+++ commons/proper/el/trunk/src/test/org/apache/commons/el/MockFunctionMapper.java Mon Aug 20 03:06:48 2007
@@ -0,0 +1,56 @@
+/*
+ * 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.el;
+
+import javax.servlet.jsp.el.FunctionMapper;
+import javax.servlet.jsp.el.ELException;
+import java.lang.reflect.Method;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public class MockFunctionMapper implements FunctionMapper {
+
+    private Map functionMap;
+
+    public MockFunctionMapper() {
+        this.functionMap = new HashMap();
+    }
+
+    public MockFunctionMapper(Map map) {
+        this.functionMap = map;
+    }
+
+    public void addIdentityMethod(String name) {
+        try {
+            getClass().getMethod("identity", new Class[] { Object.class });
+        } catch(Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public Object identity(Object input) {
+        return input;
+    }
+
+    public Method resolveFunction(String prefix, String localName) {
+        return (Method) this.functionMap.get(localName);
+    }
+
+}

Propchange: commons/proper/el/trunk/src/test/org/apache/commons/el/MockFunctionMapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: commons/proper/el/trunk/src/test/org/apache/commons/el/MockVariableResolver.java
URL: http://svn.apache.org/viewvc/commons/proper/el/trunk/src/test/org/apache/commons/el/MockVariableResolver.java?rev=567632&view=auto
==============================================================================
--- commons/proper/el/trunk/src/test/org/apache/commons/el/MockVariableResolver.java (added)
+++ commons/proper/el/trunk/src/test/org/apache/commons/el/MockVariableResolver.java Mon Aug 20 03:06:48 2007
@@ -0,0 +1,43 @@
+/*
+ * 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.el;
+
+import javax.servlet.jsp.el.VariableResolver;
+import javax.servlet.jsp.el.ELException;
+
+import java.util.Map;
+import java.util.HashMap;
+
+public class MockVariableResolver implements VariableResolver {
+
+    private Map variableMap;
+
+    public MockVariableResolver() {
+        this.variableMap = new HashMap();
+    }
+
+    public void addVariable(String variable, Object value) {
+        this.variableMap.put(variable, value);
+    }
+
+    public Object resolveVariable(String pName) throws ELException {
+        return this.variableMap.get(pName);
+    }
+ 
+}

Propchange: commons/proper/el/trunk/src/test/org/apache/commons/el/MockVariableResolver.java
------------------------------------------------------------------------------
    svn:eol-style = native