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<String> urlsToDelete = new LinkedList<String>();
+
+ /** 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 = <jsonData> ;
- // <code>
- 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