You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by mr...@apache.org on 2006/07/27 08:33:50 UTC

svn commit: r425975 - in /jakarta/commons/sandbox/js2j/trunk/src: main/java/org/apache/commons/js2j/ test/ test/java/ test/java/org/ test/java/org/apache/ test/java/org/apache/commons/ test/java/org/apache/commons/js2j/

Author: mrdon
Date: Wed Jul 26 23:33:49 2006
New Revision: 425975

URL: http://svn.apache.org/viewvc?rev=425975&view=rev
Log:
Adding testing framework, started bringing over struts flow js tests

Added:
    jakarta/commons/sandbox/js2j/trunk/src/test/
    jakarta/commons/sandbox/js2j/trunk/src/test/java/
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java
    jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/WrappersTest.java
Modified:
    jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ScriptableMap.java
    jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java

Modified: jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ScriptableMap.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ScriptableMap.java?rev=425975&r1=425974&r2=425975&view=diff
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ScriptableMap.java (original)
+++ jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ScriptableMap.java Wed Jul 26 23:33:49 2006
@@ -79,6 +79,7 @@
         if (value instanceof NativeJavaObject) {
             value = ((NativeJavaObject)value).unwrap();
         }
+        System.out.println("putting:"+value+" class:"+value.getClass());
         map.put(name, value);
     }
 

Modified: jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java?rev=425975&r1=425974&r2=425975&view=diff
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java (original)
+++ jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/SugarWrapFactory.java Wed Jul 26 23:33:49 2006
@@ -104,7 +104,7 @@
         } else if (javaObject instanceof List) {
             wrap = new ScriptableList(scope, javaObject, staticType, map);
         } else if (dynabeanClass != null && dynabeanClass.isAssignableFrom(javaObject.getClass())) {
-            new ScriptableDynaBean(scope, javaObject, staticType, map);
+            wrap = new ScriptableDynaBean(scope, javaObject, staticType, map);
         } else {
             wrap = new JavaObjectWrapper(scope, javaObject, staticType, map);
         }

Added: jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java?rev=425975&view=auto
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java (added)
+++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java Wed Jul 26 23:33:49 2006
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.js2j;
+
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.tools.shell.Global;
+
+import junit.framework.TestCase;
+
+public abstract class TestScript extends TestCase {
+
+    Context cx = null;
+    Scriptable scope = null;
+    protected String script = "";
+
+    public TestScript() {
+        super();
+    }
+
+    public TestScript(String arg0) {
+        super(arg0);
+    }
+
+    protected void setUp() throws Exception {
+        cx = Context.enter();
+        cx.setWrapFactory(new SugarWrapFactory());
+        scope = new Global(cx);
+    }
+
+    protected void test(String script, String expected) throws Exception {
+        String val = run(script);
+        if (!expected.equals(val)) {
+            fail("Expression '"+script+"' should have returned '"+expected+"', "+
+                 "but instead returned '"+val+"'");
+        }
+    }
+
+    protected String run(String script) throws Exception {
+        Object ret = cx.evaluateString(scope, script, "test", 1, null);
+        if (ret instanceof JavaObjectWrapper) {
+            ret = ((JavaObjectWrapper)ret).unwrap();
+        }
+        return (ret == null ? null : ret.toString());
+    }
+
+    protected void tearDown() throws Exception {
+        Context.exit();
+    }
+
+}
\ No newline at end of file

Added: jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/WrappersTest.java
URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/WrappersTest.java?rev=425975&view=auto
==============================================================================
--- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/WrappersTest.java (added)
+++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/WrappersTest.java Wed Jul 26 23:33:49 2006
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ * 
+ * Licensed 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.js2j;
+
+public class WrappersTest extends TestScript {
+
+    public WrappersTest(String arg0) {
+        super(arg0);
+    }
+    
+    public void testMapIndex() throws Exception {
+        script += "map = new java.util.HashMap();\n";
+        script += "map.put(\"foo\", \"bar\");\n";
+        run(script);
+        
+        test("map[\"foo\"]", "bar");
+        test("map.foo", "bar");
+        test("map.get(\"foo\")", "bar");
+    }
+    
+    public void testMapFuncPropCollide() throws Exception {
+        script += "map = new java.util.HashMap();\n";
+        script += "map.put(\"foo\", \"bar\");\n";
+        script += "map.put(\"size\", \"100\");\n";
+        run(script);
+        
+        test("map.size()", "2");
+        test("map.get(\"size\")", "100");
+    }
+    
+    public void testBeanIndex() throws Exception {
+        script += "bean = new Packages.org.apache.commons.beanutils.LazyDynaBean();\n";
+        script += "bean.set(\"foo\", \"bar\");\n";
+        script += "bean[\"jim\"] = \"bar\";\n";
+        script += "bean.sara = 'friend';\n";
+        run(script);
+        
+        test("bean['foo']", "bar");
+        test("bean.jim", "bar");
+        test("bean.get('sara')", "friend");
+        
+        script =  "jimFound = false;\n";
+        script += "for (x in bean) \n";
+        script += "if (x == 'jim') jimFound = true;\n";
+        run(script);
+        test("jimFound", "true");
+    }
+    
+    public void testDynaBeanFuncPropCollide() throws Exception {
+        script += "bean = new Packages.org.apache.commons.beanutils.LazyDynaBean();\n";
+        script += "bean.set('foo', 'bar');\n";
+        script += "bean.set('get', '100');\n";
+        run(script);
+        
+        test("bean.get('foo')", "bar");
+        test("bean.get('get')", "100");
+    }
+    
+    public void testListIndex() throws Exception {
+        script += "list = new java.util.ArrayList();\n";
+        script += "list.add('foo');\n";
+        script += "list.add('bar');\n";
+        run(script);
+        
+        test("list[0]", "foo");
+    }
+    
+    public void testListForIn() throws Exception {
+        script += "list = new java.util.ArrayList();\n";
+        script += "list.add('foo');\n";
+        script += "list.add('bar');\n";
+        
+        script += "pass = 0;\n";
+        script += "for (x in list) {\n";
+        script += "    if (x == 0) pass += (list[x] == 'foo');\n";
+        script += "    if (x == 1) pass += (list[x] == 'bar');\n";
+        script += "}";
+        run(script);
+        
+        test("pass", "2.0");
+    }
+    
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org