Author: mrdon Date: Thu Jul 27 11:38:11 2006 New Revision: 426191 URL: http://svn.apache.org/viewvc?rev=426191&view=rev Log: Adding list and file tests, added a few more file methods, reorganized test structure, cleaned up file handing in file extensions, auto-unwrap args before call
Added: jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/FileExtensionsTest.java jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/ListExtensionsTest.java Modified: jakarta/commons/sandbox/js2j/trunk/pom.xml jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ExtensionFunction.java jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java 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/pom.xml URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/pom.xml?rev=426191&r1=426190&r2=426191&view=diff ============================================================================== --- jakarta/commons/sandbox/js2j/trunk/pom.xml (original) +++ jakarta/commons/sandbox/js2j/trunk/pom.xml Thu Jul 27 11:38:11 2006 @@ -121,5 +121,6 @@ </configuration> </plugin> </plugins> + <defaultGoal>install</defaultGoal> </build> </project> Modified: jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ExtensionFunction.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ExtensionFunction.java?rev=426191&r1=426190&r2=426191&view=diff ============================================================================== --- jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ExtensionFunction.java (original) +++ jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/ExtensionFunction.java Thu Jul 27 11:38:11 2006 @@ -17,6 +17,8 @@ import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; +import org.mozilla.javascript.IdScriptableObject; +import org.mozilla.javascript.NativeJavaObject; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject; @@ -31,7 +33,21 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) { try { + // Unwrap args and convert to Java types as appropriate + for (int x=0; x<args.length; x++) { + if (args[x] instanceof NativeJavaObject) { + args[x] = ((NativeJavaObject)args[x]).unwrap(); + } else if (args[x] instanceof IdScriptableObject) { + if ("String".equals(((IdScriptableObject)args[x]).getClassName())) { + args[x] = args[x].toString(); + } + } + } + + // Execute the function call Object o = execute(cx, scope, thisObj, args); + + // Wrap the result as necessary if (o instanceof Scriptable) { return o; } else if (o == target) { Modified: jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java?rev=426191&r1=426190&r2=426191&view=diff ============================================================================== --- jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java (original) +++ jakarta/commons/sandbox/js2j/trunk/src/main/java/org/apache/commons/js2j/FileExtensions.java Thu Jul 27 11:38:11 2006 @@ -25,6 +25,9 @@ import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; +import org.mozilla.javascript.NativeJavaObject; +import org.mozilla.javascript.RegExpProxy; +import org.mozilla.javascript.ScriptRuntime; import org.mozilla.javascript.Scriptable; /** @@ -34,7 +37,7 @@ public class FileExtensions { - /** + /** * Appends text to the file. * * @funcParams String text @@ -46,15 +49,19 @@ public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws IOException { String text = args[0].toString(); - BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); - writer.write(text); - writer.close(); + FileWriter writer = null; + try { + writer = new FileWriter(file, true); + writer.write(text); + } finally { + writer.close(); + } return file; } }; } - /** + /** * Gets the contents of the file as a String. * * @funcParams @@ -66,17 +73,53 @@ public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(file)); - StringBuffer answer = new StringBuffer(); - // reading the content of the file within a char buffer allow to keep the correct line endings - char[] charBuffer = new char[4096]; - int nbCharRead = 0; - while ((nbCharRead = reader.read(charBuffer)) != -1) { - // appends buffer - answer.append(charBuffer, 0, nbCharRead); + return readFile(file); + } + }; + } + + /** + * Copies the contents of the file into a new File. + * + * @funcParams java.io.File targetFile + * @funcReturn java.io.File + * @example file.copy(new File("newFile.txt")); + */ + public static ExtensionFunction copy(final File file) { + return new ExtensionFunction() { + public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) + throws IOException { + + File tFile = null; + if (args[0] instanceof File) { + tFile = (File) args[0]; + } else { + tFile = new File((String)args[0]); + } + FileWriter writer = null; + BufferedReader reader = null; + + try { + writer = new FileWriter(tFile); + reader = new BufferedReader(new FileReader(file)); + // reading the content of the file within a char buffer allow to keep the correct line endings + char[] charBuffer = new char[4096]; + int nbCharRead = 0; + while ((nbCharRead = reader.read(charBuffer)) != -1) { + writer.write(charBuffer, 0, nbCharRead); + } + } finally { + try { + if (writer != null) { + writer.close(); + } + } finally { + if (reader != null) { + reader.close(); + } + } } - reader.close(); - return answer.toString(); + return tFile; } }; } @@ -95,20 +138,80 @@ public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(file)); - Function func = (Function)args[0]; - Object[] params = new Object[1]; - String line = null; - while ((line = reader.readLine()) != null) { - params[0] = line; - func.call(cx, scope, thisObj, params); + FileReader freader = null; + try { + freader = new FileReader(file); + BufferedReader reader = new BufferedReader(freader); + Function func = (Function)args[0]; + Object[] params = new Object[1]; + String line = null; + while ((line = reader.readLine()) != null) { + params[0] = line; + func.call(cx, scope, thisObj, params); + } + } finally { + freader.close(); } - reader.close(); return null; } }; } + /** + * Replaces text in a file. + * + * @funcParams Regexp pattern, String replacementText + * @funcReturn java.io.File + * @example file.replace(/foo/, "bar") + */ + public static ExtensionFunction replace(final File file) { + return new ExtensionFunction() { + public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) + throws IOException { + + // Read original file contents + String orig = readFile(file); + + // Replace context with regexp + Scriptable l = cx.getWrapFactory().wrapAsJavaObject(cx, scope, orig.toString(), String.class); + String text = ScriptRuntime.toString( + ScriptRuntime.checkRegExpProxy(cx).action(cx, scope, l, args, RegExpProxy.RA_REPLACE)); + + // Write new contents to original file + FileWriter writer = null; + try { + writer = new FileWriter(file); + writer.write(text); + } finally { + writer.close(); + } + return file; + } + }; + } + + /** + * Searches a file for a regular expression. + * + * @funcParams Regexp pattern + * @funcReturn int + * @example file.search(/foo/) + */ + public static ExtensionFunction search(final File file) { + return new ExtensionFunction() { + public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) + throws IOException { + + // Read original file contents + String orig = readFile(file); + + // Replace context with regexp + Scriptable l = cx.getWrapFactory().wrapAsJavaObject(cx, scope, orig.toString(), String.class); + return ScriptRuntime.checkRegExpProxy(cx).action(cx, scope, l, args, RegExpProxy.RA_SEARCH); + } + }; + } + /** * Collects the contents of the file as an array of lines. The file is opened, and * interpreted as a text file using the default encoding. @@ -122,14 +225,19 @@ public Object execute(Context cx, Scriptable scope, Scriptable thisObj, java.lang.Object[] args) throws IOException { - BufferedReader reader = new BufferedReader(new FileReader(file)); ArrayList list = new ArrayList(); - String line = null; - - while ((line = reader.readLine()) != null) { - list.add(line); + FileReader freader = null; + try { + freader = new FileReader(file); + BufferedReader reader = new BufferedReader(freader); + String line = null; + + while ((line = reader.readLine()) != null) { + list.add(line); + } + } finally { + freader.close(); } - reader.close(); Object[] lines = new Object[list.size()]; for (int x=0; x<lines.length; x++) { @@ -155,6 +263,23 @@ return new Boolean(file.delete()); } }; + } + + private static String readFile(File file) throws IOException { + StringBuffer orig = new StringBuffer(); + FileReader freader = null; + try { + freader = new FileReader(file); + BufferedReader reader = new BufferedReader(freader); + char[] charBuffer = new char[4096]; + int nbCharRead = 0; + while ((nbCharRead = reader.read(charBuffer)) != -1) { + orig.append(charBuffer, 0, nbCharRead); + } + } finally { + freader.close(); + } + return orig.toString(); } } Added: jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/FileExtensionsTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/FileExtensionsTest.java?rev=426191&view=auto ============================================================================== --- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/FileExtensionsTest.java (added) +++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/FileExtensionsTest.java Thu Jul 27 11:38:11 2006 @@ -0,0 +1,114 @@ +/* + * 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 FileExtensionsTest extends TestScript { + + public void setUp() throws Exception { + super.setUp(); + run("file = java.io.File.createTempFile('test', '.tmp');"); + } + + public void testAppend() throws Exception { + run("file.append('this is a test');"); + run("txt = new java.io.BufferedReader(new java.io.FileReader(file)).readLine();"); + + test("txt", "this is a test"); + } + + public void testCopy() throws Exception { + run("writer = new java.io.FileWriter(file);"); + run("writer.write('this is a test');"); + run("writer.close();"); + run("tfile = file.copy(java.io.File.createTempFile('test', '.tmp').toString());"); + run("txt = new java.io.BufferedReader(new java.io.FileReader(tfile)).readLine();"); + + test("txt", "this is a test"); + + run("tfile = file.copy(java.io.File.createTempFile('test', '.tmp').toString());"); + run("txt = new java.io.BufferedReader(new java.io.FileReader(tfile)).readLine();"); + test("txt", "this is a test"); + + run("tfile = file.copy(new String(java.io.File.createTempFile('test', '.tmp').toString()));"); + run("txt = new java.io.BufferedReader(new java.io.FileReader(tfile)).readLine();"); + test("txt", "this is a test"); + } + + public void testReplace() throws Exception { + run("writer = new java.io.FileWriter(file);"); + run("writer.write('this is a test');"); + run("writer.close();"); + run("tfile = file.replace(/this/, 'foo');"); + run("txt = new java.io.BufferedReader(new java.io.FileReader(tfile)).readLine();"); + + test("txt", "foo is a test"); + + run("tfile = file.replace('foo', 'this');"); + run("txt = new java.io.BufferedReader(new java.io.FileReader(tfile)).readLine();"); + + test("txt", "this is a test"); + } + + public void testSearch() throws Exception { + run("writer = new java.io.FileWriter(file);"); + run("writer.write('this is a test');"); + run("writer.close();"); + run("ret = file.search(/this/);"); + + test("ret", "0"); + test("file.search(/asdf/)", "-1"); + } + + public void testGetText() throws Exception { + run("writer = new java.io.FileWriter(file);"); + run("writer.write('foo\\nbar');"); + run("writer.close();"); + run("txt = file.getText();"); + + test("txt", "foo\nbar"); + } + + public void testGetLines() throws Exception { + run("writer = new java.io.FileWriter(file);"); + run("writer.write('foo\\nbar');"); + run("writer.close();"); + run("txt = file.getLines();"); + + test("txt.length", "2.0"); + test("txt[1]", "bar"); + } + + public void testRemove() throws Exception { + run("writer = new java.io.FileWriter(file);"); + run("writer.write('foo\\nbar');"); + run("writer.close();"); + run("ret = file.remove();"); + + test("ret", "true"); + test("!file.exists();", "true"); + } + + public void testEachLine() throws Exception { + run("writer = new java.io.FileWriter(file);"); + run("writer.write('foo\\nbar');"); + run("writer.close();"); + run("foo = false;"); + run("bar = false;"); + run("file.eachLine(function(line) {eval(line+' = true');});"); + + test("foo == true && bar == true;", "true"); + } +} Added: jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/ListExtensionsTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/ListExtensionsTest.java?rev=426191&view=auto ============================================================================== --- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/ListExtensionsTest.java (added) +++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/ListExtensionsTest.java Thu Jul 27 11:38:11 2006 @@ -0,0 +1,82 @@ +/* + * 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 ListExtensionsTest extends TestScript { + + public void setUp() throws Exception { + super.setUp(); + run("this.list = new java.util.ArrayList();"); + run("this.list.add('foo');"); + run("this.list.add('bar');"); + } + + public void testEach() throws Exception { + run("foo = false;"); + run("bar = false;"); + run("list.each(function(val) { eval(val+' = true;');});"); + + test("foo == true && bar == true", "true"); + } + + public void testAsImmutable() throws Exception { + run("lst = list.asImmutable();"); + run("passed = false;"); + run("try {lst.add('foo'); }catch (e) {passed = true;}"); + + test("passed", "true"); + } + + public void testPop() throws Exception { + run("val = list.pop();"); + + test("val", "bar"); + test("list.size()", "1"); + } + + public void testSort() throws Exception { + run("list.sort();"); + + test("list[0]", "bar"); + } + + public void testSortEach() throws Exception { + script += "list.sortEach(function(val1, val2) {"; + script += " if (val1 == 'bar') return -1;"; + script += " else return 1;"; + script += " });"; + run(script); + + test("list[0]", "bar"); + } + + public void testLength() throws Exception { + test("list.length", "2"); + } + + public void testFind() throws Exception { + run("result = list.find(function(val) { return (val == 'bar' ? val : null);});"); + + test("result", "bar"); + } + + public void testFindAll() throws Exception { + run("list.add('bar');"); + run("result = list.findAll(function(val) { return (val == 'bar' ? val : null);});"); + + test("result.size()", "2"); + } +} Modified: 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=426191&r1=426190&r2=426191&view=diff ============================================================================== --- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java (original) +++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/TestScript.java Thu Jul 27 11:38:11 2006 @@ -27,14 +27,6 @@ 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()); @@ -42,19 +34,20 @@ } protected void test(String script, String expected) throws Exception { - String val = run(script); + Object obj = run(script); + String val = (obj == null ? null : obj.toString()); if (!expected.equals(val)) { fail("Expression '"+script+"' should have returned '"+expected+"', "+ "but instead returned '"+val+"'"); } } - protected String run(String script) throws Exception { + protected Object 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()); + return ret; } protected void tearDown() throws Exception { Modified: 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=426191&r1=426190&r2=426191&view=diff ============================================================================== --- jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/WrappersTest.java (original) +++ jakarta/commons/sandbox/js2j/trunk/src/test/java/org/apache/commons/js2j/WrappersTest.java Thu Jul 27 11:38:11 2006 @@ -17,10 +17,6 @@ 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"; --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]