Author: mrdon Date: Sun Feb 20 22:52:04 2005 New Revision: 154644 URL: http://svn.apache.org/viewcvs?view=rev&rev=154644 Log: * Adding a very simple Javascript junit implementation * Adding tests for the List and Map wrappers * Adding tests for File and List extension functions * Fixing errors revealed by the unit tests * Adding description of sugar package
Added: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/package.html (with props) struts/flow/trunk/src/test/ struts/flow/trunk/src/test/assert.js (with props) struts/flow/trunk/src/test/fileTest.js (with props) struts/flow/trunk/src/test/listTest.js (with props) struts/flow/trunk/src/test/org/ struts/flow/trunk/src/test/org/apache/ struts/flow/trunk/src/test/org/apache/struts/ struts/flow/trunk/src/test/org/apache/struts/flow/ struts/flow/trunk/src/test/org/apache/struts/flow/sugar/ struts/flow/trunk/src/test/org/apache/struts/flow/sugar/JavascriptSugarTest.java (with props) struts/flow/trunk/src/test/wrappersTest.js (with props) Modified: struts/flow/trunk/build.xml struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java Modified: struts/flow/trunk/build.xml URL: http://svn.apache.org/viewcvs/struts/flow/trunk/build.xml?view=diff&r1=154643&r2=154644 ============================================================================== --- struts/flow/trunk/build.xml (original) +++ struts/flow/trunk/build.xml Sun Feb 20 22:52:04 2005 @@ -30,13 +30,16 @@ <property name="src.dir" value="src" /> <property name="src.java" value="${src.dir}/java" /> + <property name="src.test" value="${src.dir}/test" /> <property name="src.guess.example" value="${src.dir}/guess-example" /> <property name="src.wizard.example" value="${src.dir}/wizard-example" /> + <property name="build.dir" value="build" /> <property name="build.guess.example" value="${build.dir}/guess-example" /> <property name="build.wizard.example" value="${build.dir}/wizard-example" /> <property name="build.target" value="${build.dir}/target" /> + <property name="build.test" value="${build.dir}/test" /> <property name="javadoc.packages" value="org.apache.struts.flow.*" /> @@ -177,13 +180,19 @@ </copy> </target> -<!-- + <target name="run-tests" depends="compile"> - <javac srcdir="${test.src}" destdir="${test.build}" + <mkdir dir="${build.test}" /> + <javac srcdir="${src.test}" destdir="${build.test}" debug="on" optimize="off" deprecation="off" > <classpath refid="base.classpath" /> </javac> - <junit printsummary="on" fork="on"> + + <java classpathref="base.classpath" classname="org.apache.struts.flow.sugar.JavascriptSugarTest"> + <arg path="${src.test}" /> + </java> + <!-- + <junit printsummary="on" fork="on"> <formatter type="xml" /> <classpath refid="base.classpath" /> @@ -192,9 +201,8 @@ <include name="**/*Test.java" /> </fileset> </batchtest> - </junit> + </junit> --> </target> - --> <!-- The 'javadoc' target creates the API documentation. All javadoc is created in the ${doc.dir}/javadoc directory. Modified: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java?view=diff&r1=154643&r2=154644 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ExtensionFunction.java Sun Feb 20 22:52:04 2005 @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.struts.flow.sugar; import org.mozilla.javascript.*; Modified: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java?view=diff&r1=154643&r2=154644 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/ListExtensions.java Sun Feb 20 22:52:04 2005 @@ -62,7 +62,7 @@ Object o = null; if (list.size() > 0) { o = list.get(list.size() - 1); - list.remove(list.size()); + list.remove(list.size() - 1); } return o; } Modified: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java?view=diff&r1=154643&r2=154644 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java (original) +++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/SugarWrapFactory.java Sun Feb 20 22:52:04 2005 @@ -113,31 +113,6 @@ return map; } - // temporary method for testing - public static final void main(String[] args) throws Exception { - // Creates and enters a Context. The Context stores information - // about the execution environment of a script. - Context cx = Context.enter(); - try { - cx.setWrapFactory(new SugarWrapFactory()); - // Initialize the standard objects (Object, Function, etc.) - // This must be done before scripts can be executed. Returns - // a scope object that we use in later calls. - Scriptable scope = cx.initStandardObjects(); - - // Now evaluate the string we've colected. - FileReader reader = new FileReader(args[0]); - Object result = cx.evaluateReader(scope, reader, args[0], 1, null); - - // Convert the result to a string and print it. - System.err.println(cx.toString(result)); - - } finally { - // Exit from the context. - Context.exit(); - } - } - class ExtensionFunctionEntry { public Class clazz; public String name; Added: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/package.html URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/java/org/apache/struts/flow/sugar/package.html?view=auto&rev=154644 ============================================================================== --- struts/flow/trunk/src/java/org/apache/struts/flow/sugar/package.html (added) +++ struts/flow/trunk/src/java/org/apache/struts/flow/sugar/package.html Sun Feb 20 22:52:04 2005 @@ -0,0 +1,12 @@ +<html> +<head> + <title>Struts Flow - Sugar</title> +</head> +<body> + This package contains syntactic "sugar" in the form of function extensions for + Java core classes. It also contains wrappers for the Collections interfaces to make + their information accessible through normal javascript techniques. This package doesn't depend on + any other Struts Flow classes and can be used with any other embedded instance of Rhino. +</body> +</html> + Propchange: struts/flow/trunk/src/java/org/apache/struts/flow/sugar/package.html ------------------------------------------------------------------------------ svn:executable = * Added: struts/flow/trunk/src/test/assert.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/assert.js?view=auto&rev=154644 ============================================================================== --- struts/flow/trunk/src/test/assert.js (added) +++ struts/flow/trunk/src/test/assert.js Sun Feb 20 22:52:04 2005 @@ -0,0 +1,63 @@ +var errors = new Array(); +var failures = new Array(); +var successes = new Array(); + +function assertTrue(msg, test) { + if (test == undefined) { + test = msg; + msg = "Test failed"; + } + if (test != true) { + this.result = failures.push(msg+" - "+this.curTest); + } else { + successes.push(this.curTest); + } +} + +function runTests() { + + for (id in this) { + if (id.length > 4 && id.substr(0, 4) == 'test') { + try { + if (this.setUp != null) { + this.setUp(); + } + this.curTest = id.substr(4); + this[id](); + } catch (e) { + errors.push(this.curTest + " - "+e+" line:"+e.lineNumber); + } finally { + if (this.tearDown != null) { + this.tearDown(); + } + } + } + } + + printResults() +} + +function printResults() { + print("Test Results"); + print("===============\n"); + print("Successes - "+successes.length); + for (x in successes) { + print("\t"+successes[x]); + } + print("Failures - "+failures.length); + for (x in failures) { + print("\t"+failures[x]); + } + print("Errors - "+errors.length); + for (x in errors) { + print("\t"+errors[x]); + } +} + +function print(val) { + java.lang.System.out.println((val == null ? "null" : val)); +} + +runTests(); + + Propchange: struts/flow/trunk/src/test/assert.js ------------------------------------------------------------------------------ svn:executable = * Added: struts/flow/trunk/src/test/fileTest.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/fileTest.js?view=auto&rev=154644 ============================================================================== --- struts/flow/trunk/src/test/fileTest.js (added) +++ struts/flow/trunk/src/test/fileTest.js Sun Feb 20 22:52:04 2005 @@ -0,0 +1,29 @@ +function setUp() { + this.file = java.io.File.createTempFile("test", ".tmp"); +} + +function testAppend() { + file.append("this is a test"); + txt = new java.io.BufferedReader(new java.io.FileReader(file)).readLine(); + assertTrue("Didn't append text - "+txt, txt == "this is a test"); +} + +function testGetText() { + writer = new java.io.FileWriter(file); + writer.write("foo\nbar"); + writer.close(); + txt = file.getText(); + assertTrue("Didn't get text - "+txt, txt == "foo\nbar"); +} + +function testEachLine() { + writer = new java.io.FileWriter(file); + writer.write("foo\nbar"); + writer.close(); + foo = false; + bar = false; + file.eachLine(function(line) {eval(line+" = true");}); + assertTrue("Didn't read both lines", foo == true && bar == true); +} + + Propchange: struts/flow/trunk/src/test/fileTest.js ------------------------------------------------------------------------------ svn:executable = * Added: struts/flow/trunk/src/test/listTest.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/listTest.js?view=auto&rev=154644 ============================================================================== --- struts/flow/trunk/src/test/listTest.js (added) +++ struts/flow/trunk/src/test/listTest.js Sun Feb 20 22:52:04 2005 @@ -0,0 +1,43 @@ +function setUp() { + this.list = new java.util.ArrayList(); + this.list.add("foo"); + this.list.add("bar"); +} + +function testEach() { + foo = false; + bar = false; + list.each(function(val) { eval(val+" = true;");}); + assertTrue("Didn't iterate over both", foo == true && bar == true); +} + +function testAsImmutable() { + lst = list.asImmutable(); + passed = false; + try { + lst.add("foo"); + } catch (e) { + passed = true; + } + assertTrue("Shouldn't have allowed an add", passed); +} + +function testPop() { + val = list.pop(); + assertTrue("Wrong top value", val == "bar"); + assertTrue("Didn't reduce size", list.size() == 1); +} + +function testSort() { + list.sort(); + assertTrue("Didn't sort, bar should be first", list[0] == "bar"); +} + +function testSortEach() { + list.sortEach(function(val1, val2) { + if (val1 == "bar") return -1; + else return 1; + }); + assertTrue("Didn't sort, bar should be first", list[0] == "bar"); +} + Propchange: struts/flow/trunk/src/test/listTest.js ------------------------------------------------------------------------------ svn:executable = * Added: struts/flow/trunk/src/test/org/apache/struts/flow/sugar/JavascriptSugarTest.java URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/org/apache/struts/flow/sugar/JavascriptSugarTest.java?view=auto&rev=154644 ============================================================================== --- struts/flow/trunk/src/test/org/apache/struts/flow/sugar/JavascriptSugarTest.java (added) +++ struts/flow/trunk/src/test/org/apache/struts/flow/sugar/JavascriptSugarTest.java Sun Feb 20 22:52:04 2005 @@ -0,0 +1,79 @@ +/* + * Copyright 1999-2004 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.struts.flow.sugar; + +import java.io.*; +import java.util.*; +import org.mozilla.javascript.*; + +/** + * Driver class for all the Javascript-based unit tests + */ +public class JavascriptSugarTest { + + /** + * Driver main method for running tests + * + * @param args Array of command line arguments. First argument should be a + * path to the directory where "assert.js" and unit test Javascript + * scripts (*Test.js) can be found. + */ + public static void main(String args[]) throws Exception { + + File baseDir = new File(args[0]); + + String assertCode = readAll(new File(baseDir, "assert.js")); + File[] kids = baseDir.listFiles(); + for (int x=0; x<kids.length; x++) { + if (kids[x].getName().endsWith("Test.js")) { + // Creates and enters a Context. The Context stores information + // about the execution environment of a script. + Context cx = Context.enter(); + try { + cx.setWrapFactory(new SugarWrapFactory()); + // Initialize the standard objects (Object, Function, etc.) + // This must be done before scripts can be executed. Returns + // a scope object that we use in later calls. + Scriptable scope = cx.initStandardObjects(); + + // Now evaluate the string we've colected. + String code = readAll(kids[x]); + StringReader reader = new StringReader(code + "\n" + assertCode); + Object result = cx.evaluateReader(scope, reader, kids[x].getName(), 1, null); + + // Convert the result to a string and print it. + //System.err.println(cx.toString(result)); + } finally { + // Exit from the context. + Context.exit(); + } + } + } + } + + private static String readAll(File file) throws IOException { + StringBuffer sb = new StringBuffer(); + FileReader reader = new FileReader(file); + char[] buffer = new char[4096]; + int len = 0; + while ((len = reader.read(buffer, 0, buffer.length)) > -1) { + sb.append(buffer, 0, len); + } + return sb.toString(); + } + +} + Propchange: struts/flow/trunk/src/test/org/apache/struts/flow/sugar/JavascriptSugarTest.java ------------------------------------------------------------------------------ svn:executable = * Added: struts/flow/trunk/src/test/wrappersTest.js URL: http://svn.apache.org/viewcvs/struts/flow/trunk/src/test/wrappersTest.js?view=auto&rev=154644 ============================================================================== --- struts/flow/trunk/src/test/wrappersTest.js (added) +++ struts/flow/trunk/src/test/wrappersTest.js Sun Feb 20 22:52:04 2005 @@ -0,0 +1,25 @@ +function testMapIndex() { + map = new java.util.HashMap(); + map.put("foo", "bar"); + assertTrue("brackets didn't work", map["foo"] == "bar"); + assertTrue("dot notation didn't work", map.foo == "bar"); + assertTrue("normal map get() didn't work", map.get("foo") == "bar"); +} + +function testListIndex() { + list = new java.util.ArrayList(); + list.add("foo"); + list.add("bar"); + assertTrue("Index didn't work", list[0] == "foo"); +} + +function testListForIn() { + list = new java.util.ArrayList(); + list.add("foo"); + list.add("bar"); + count = 0; + for (x in list) { + if (x == 0) assertTrue("first value wrong", list[x] == "foo"); + if (x == 1) assertTrue("second value wrong", list[x] == "bar"); + } +} Propchange: struts/flow/trunk/src/test/wrappersTest.js ------------------------------------------------------------------------------ svn:executable = * --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]