Return-Path: Delivered-To: apmail-sling-commits-archive@www.apache.org Received: (qmail 36121 invoked from network); 18 May 2010 12:27:47 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 18 May 2010 12:27:47 -0000 Received: (qmail 57539 invoked by uid 500); 18 May 2010 12:27:46 -0000 Delivered-To: apmail-sling-commits-archive@sling.apache.org Received: (qmail 57477 invoked by uid 500); 18 May 2010 12:27:46 -0000 Mailing-List: contact commits-help@sling.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@sling.apache.org Delivered-To: mailing list commits@sling.apache.org Received: (qmail 57469 invoked by uid 99); 18 May 2010 12:27:46 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 18 May 2010 12:27:46 +0000 X-ASF-Spam-Status: No, hits=-1371.1 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 18 May 2010 12:27:44 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 9DB7D2388993; Tue, 18 May 2010 12:27:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r945618 - in /sling/trunk/bundles/commons/testing/src: main/java/org/apache/sling/commons/testing/integration/ main/java/org/apache/sling/commons/testing/util/ test/java/org/apache/sling/commons/testing/util/ Date: Tue, 18 May 2010 12:27:24 -0000 To: commits@sling.apache.org From: bdelacretaz@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100518122724.9DB7D2388993@eris.apache.org> Author: bdelacretaz Date: Tue May 18 12:27:24 2010 New Revision: 945618 URL: http://svn.apache.org/viewvc?rev=945618&view=rev Log: SLING-1523 - decouple assertJavascript from HttpTestBase Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java (with props) sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/ sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/JavascriptEngineTest.java (with props) Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java?rev=945618&r1=945617&r2=945618&view=diff ============================================================================== --- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java (original) +++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java Tue May 18 12:27:24 2010 @@ -42,6 +42,7 @@ import org.apache.commons.httpclient.Use import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.sling.commons.testing.util.JavascriptEngine; import org.mozilla.javascript.Context; import org.mozilla.javascript.ScriptableObject; @@ -80,6 +81,9 @@ public class HttpTestBase extends TestCa /** URLs stored here are deleted in tearDown */ protected final List urlsToDelete = new LinkedList(); + + /** Need to execute javascript code */ + private final JavascriptEngine javascriptEngine = new JavascriptEngine(); /** Class that creates a test node under the given parentPath, and * stores useful values for testing. Created for JspScriptingTest, @@ -400,30 +404,13 @@ public class HttpTestBase extends TestCa /** Evaluate given code using given jsonData as the "data" object */ protected void assertJavascript(String expectedOutput, String jsonData, String code, String testInfo) throws IOException { - // build the code, something like - // data = ; - // - final String jsCode = "data=" + jsonData + ";\n" + code; - final Context rhinoContext = Context.enter(); - final ScriptableObject scope = rhinoContext.initStandardObjects(); - - // execute the script, out script variable maps to sw - final StringWriter sw = new StringWriter(); - final PrintWriter pw = new PrintWriter(sw, true); - ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope)); - final int lineNumber = 1; - final Object securityDomain = null; - rhinoContext.evaluateString(scope, jsCode, getClass().getSimpleName(), - lineNumber, securityDomain); - - // check script output - pw.flush(); - final String result = sw.toString().trim(); + final String result = javascriptEngine.execute(code, jsonData); if(!result.equals(expectedOutput)) { fail( "Expected '" + expectedOutput + "' but got '" + result - + "' for script='" + jsCode + "'" + + "' for script='" + code + "'" + + "' and data='" + jsonData + "'" + (testInfo==null ? "" : ", test info=" + testInfo) ); } Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java?rev=945618&view=auto ============================================================================== --- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java (added) +++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java Tue May 18 12:27:24 2010 @@ -0,0 +1,59 @@ +/* + * 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.sling.commons.testing.util; + +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.mozilla.javascript.Context; +import org.mozilla.javascript.ScriptableObject; + +/** Simplistic Javascript engine using Rhino, meant + * for automated tests */ +public class JavascriptEngine { + /** Execute supplied code against supplied data, + * see JavascriptEngineTest for examples */ + public String execute(String code, String jsonData) throws IOException { + final String jsCode = "data=" + jsonData + ";\n" + code; + final Context rhinoContext = Context.enter(); + final ScriptableObject scope = rhinoContext.initStandardObjects(); + + // execute the script, out script variable maps to sw + final StringWriter sw = new StringWriter(); + final PrintWriter pw = new PrintWriter(sw, true); + ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope)); + final int lineNumber = 1; + final Object securityDomain = null; + try { + rhinoContext.evaluateString( + scope, + jsCode, + getClass().getSimpleName(), + lineNumber, + securityDomain); + } catch(Exception e) { + final IOException ioe = new IOException("While executing [" + code + "]:" + e); + ioe.initCause(e); + throw ioe; + } + + // check script output + pw.flush(); + return sw.toString().trim(); + } +} Propchange: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/util/JavascriptEngine.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev URL Added: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/JavascriptEngineTest.java URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/JavascriptEngineTest.java?rev=945618&view=auto ============================================================================== --- sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/JavascriptEngineTest.java (added) +++ sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/JavascriptEngineTest.java Tue May 18 12:27:24 2010 @@ -0,0 +1,46 @@ +/* + * 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.sling.commons.testing.util; + +import java.io.IOException; + +import org.apache.sling.commons.testing.util.JavascriptEngine; +import org.junit.Test; +import static org.junit.Assert.assertEquals; + +public class JavascriptEngineTest { + private final JavascriptEngine engine = new JavascriptEngine(); + + @Test + public void testSimpleScripts() throws IOException { + + // Each triplet of data is: json data, code, expected output + final String [] data = { + "{}", "out.print('hello')", "hello", + "{ i:26, j:'a' }", "out.print(data.i + data.j)", "26a", + "{ i:'ab', j:'cd' }", "out.print(data.i) ; out.print('+') ; out.print(data.j)", "ab+cd" + }; + + for(int i=0; i < data.length; i+= 3) { + final String json = data[i]; + final String code = data[i+1]; + final String expected = data[i+2]; + final String actual = engine.execute(code, json); + assertEquals("At index " + i, expected, actual); + } + } +} Propchange: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/JavascriptEngineTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: sling/trunk/bundles/commons/testing/src/test/java/org/apache/sling/commons/testing/util/JavascriptEngineTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision Rev URL