Author: musachy
Date: Wed Aug 5 17:49:14 2009
New Revision: 801334
URL: http://svn.apache.org/viewvc?rev=801334&view=rev
Log:
add tests
Added:
struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/broken0.jsp
Modified:
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPLoader.java
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPRuntime.java
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/ServletCache.java
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryClassLoader.java
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryJavaFileObject.java
struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java
Modified:
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPLoader.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPLoader.java?rev=801334&r1=801333&r2=801334&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPLoader.java
(original)
+++
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPLoader.java
Wed Aug 5 17:49:14 2009
@@ -91,6 +91,10 @@
return servlet;
}
+ /**
+ * Compiles the given source code into java bytecode
+ *
+ */
private void compileJava(String className, final String source,
Set<String> extraClassPath) {
JavaCompiler compiler =
ToolProvider.getSystemJavaCompiler();
@@ -98,6 +102,7 @@
DiagnosticCollector<JavaFileObject> diagnostics =
new DiagnosticCollector<JavaFileObject>();
+ //the generated bytecode is fed to the class loader
JavaFileManager jfm = new
ForwardingJavaFileManager<StandardJavaFileManager>(
compiler.getStandardFileManager(diagnostics, null,
null)) {
@@ -111,9 +116,9 @@
classLoader.addMemoryJavaFileObject(name, fileObject);
return fileObject;
}
-
};
+ //read java source code from memory
String fileName = className.replace('.', '/') + ".java";
SimpleJavaFileObject sourceCodeObject = new
SimpleJavaFileObject(toURI(fileName), JavaFileObject.Kind.SOURCE) {
@Override
@@ -152,12 +157,13 @@
optionList.addAll(Arrays.asList("-classpath", classPath.toString()));
+ //compile
JavaCompiler.CompilationTask task = compiler.getTask(
null, jfm, diagnostics, optionList, null,
Arrays.asList(sourceCodeObject));
if (!task.call()) {
- throw new RuntimeException("Compilation failed:" +
diagnostics.getDiagnostics().get(0).toString());
+ throw new StrutsException("Compilation failed:" +
diagnostics.getDiagnostics().get(0).toString());
}
}
Modified:
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPRuntime.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPRuntime.java?rev=801334&r1=801333&r2=801334&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPRuntime.java
(original)
+++
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/JSPRuntime.java
Wed Aug 5 17:49:14 2009
@@ -1,3 +1,23 @@
+/*
+ * $Id$
+ *
+ * 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.struts2;
import com.opensymphony.xwork2.ActionContext;
@@ -15,7 +35,7 @@
*/
public abstract class JSPRuntime {
//maps from jsp path -> pagelet
- private static final ServletCache servletCache = new ServletCache();
+ protected static final ServletCache servletCache = new ServletCache();
public static void handle(String location) throws Exception {
handle(location, false);
Modified:
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/ServletCache.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/ServletCache.java?rev=801334&r1=801333&r2=801334&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/ServletCache.java
(original)
+++
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/ServletCache.java
Wed Aug 5 17:49:14 2009
@@ -28,7 +28,7 @@
* will block and wait for the jsp to be loaded
*/
public class ServletCache {
- private final ConcurrentMap<String, Future<Servlet>> cache
+ protected final ConcurrentMap<String, Future<Servlet>> cache
= new ConcurrentHashMap<String, Future<Servlet>>();
private final JSPLoader jspLoader = new JSPLoader();
@@ -65,7 +65,7 @@
else if (t instanceof Error)
throw (Error) t;
else
- throw new IllegalStateException("Not unchecked", t);
+ throw new IllegalStateException(t);
}
}
Modified:
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryClassLoader.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryClassLoader.java?rev=801334&r1=801333&r2=801334&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryClassLoader.java
(original)
+++
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryClassLoader.java
Wed Aug 5 17:49:14 2009
@@ -23,6 +23,10 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+/**
+ * Keeps a cache of class name -> MemoryJavaFileObject. If the requested class
name is in the cache
+ * a new class is defined for it, otherwise findClass delegates to the parent
class loader
+ */
public class MemoryClassLoader extends ClassLoader {
private Map<String, MemoryJavaFileObject> cachedObjects = new
ConcurrentHashMap<String, MemoryJavaFileObject>();
Modified:
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryJavaFileObject.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryJavaFileObject.java?rev=801334&r1=801333&r2=801334&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryJavaFileObject.java
(original)
+++
struts/sandbox/trunk/struts2-jsp-plugin/src/main/java/org/apache/struts2/compiler/MemoryJavaFileObject.java
Wed Aug 5 17:49:14 2009
@@ -26,6 +26,9 @@
import java.net.URI;
import java.net.URISyntaxException;
+/**
+ * Captures the output of the java compiler in memory
+ */
public class MemoryJavaFileObject extends SimpleJavaFileObject {
private ByteArrayOutputStream out;
Modified:
struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java?rev=801334&r1=801333&r2=801334&view=diff
==============================================================================
---
struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java
(original)
+++
struts/sandbox/trunk/struts2-jsp-plugin/src/test/java/org/apache/struts2/EmbeddedJSPResultTest.java
Wed Aug 5 17:49:14 2009
@@ -1,3 +1,23 @@
+/*
+ * $Id$
+ *
+ * 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.struts2;
import com.opensymphony.xwork2.ActionContext;
@@ -13,6 +33,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
+import javax.servlet.Servlet;
import java.util.HashMap;
import java.util.Map;
@@ -20,11 +41,9 @@
private HttpServletRequest request;
private MockHttpServletResponse response;
private MockServletContext context;
+ private EmbeddedJSPResult result;
public void testSimple() throws Exception {
- //mock objects
- EmbeddedJSPResult result = new EmbeddedJSPResult();
-
result.setLocation("org/apache/struts2/simple0.jsp");
result.execute(null);
@@ -32,54 +51,59 @@
}
public void testTag0() throws Exception {
- //mock objects
- EmbeddedJSPResult result = new EmbeddedJSPResult();
-
result.setLocation("org/apache/struts2/tag0.jsp");
result.execute(null);
assertEquals("Thissessionisnotsecure.OtherText",
cleanup(response.getContentAsString()));
}
- public void testIncludeSimple() throws Exception {
- //mock objects
- EmbeddedJSPResult result = new EmbeddedJSPResult();
-
+ public void testIncludeSimple() throws Exception {
result.setLocation("org/apache/struts2/includes0.jsp");
result.execute(null);
assertEquals("helloTest", cleanup(response.getContentAsString()));
}
- public void testIncludeSimpleWithDirective() throws Exception {
- //mock objects
- EmbeddedJSPResult result = new EmbeddedJSPResult();
-
+ public void testIncludeSimpleWithDirective() throws Exception {
result.setLocation("org/apache/struts2/includes3.jsp");
result.execute(null);
assertEquals("helloTest", cleanup(response.getContentAsString()));
}
- public void testIncludeWithSubdir() throws Exception {
- EmbeddedJSPResult result = new EmbeddedJSPResult();
-
+ public void testIncludeWithSubdir() throws Exception {
result.setLocation("org/apache/struts2/includes1.jsp");
result.execute(null);
assertEquals("subTest", cleanup(response.getContentAsString()));
}
- public void testIncludeWithParam() throws Exception {
- //mock objects
- EmbeddedJSPResult result = new EmbeddedJSPResult();
-
+ public void testIncludeWithParam() throws Exception {
result.setLocation("org/apache/struts2/includes2.jsp");
result.execute(null);
assertEquals("JGTest", cleanup(response.getContentAsString()));
}
+ public void testBroken0() throws Exception {
+ try {
+ result.setLocation("org/apache/struts2/broken0.jsp");
+ result.execute(null);
+ fail("should have failed with broken jsp");
+ } catch (IllegalStateException ex) {
+ //ok
+ }
+ }
+
+
+ public void testCachedInstances() throws InterruptedException {
+ ServletCache cache = new ServletCache();
+ Servlet servlet1 = cache.get("org/apache/struts2/simple0.jsp");
+ Servlet servlet2 = cache.get("org/apache/struts2/simple0.jsp");
+
+ assertSame(servlet1, servlet2);
+ }
+
private String cleanup(String str) {
return str.replaceAll("\\s", "");
}
@@ -88,7 +112,9 @@
@Override
protected void setUp() throws Exception {
super.setUp();
-
+
+ result = new EmbeddedJSPResult();
+
request = EasyMock.createNiceMock(HttpServletRequest.class);
response = new MockHttpServletResponse();
context = new MockServletContext();
@@ -103,7 +129,7 @@
EasyMock.expect(request.getParameter("username")).andAnswer(new
IAnswer<String>() {
@Override
public String answer() throws Throwable {
- return ((String[])params.get("username"))[0];
+ return ((String[]) params.get("username"))[0];
}
});
@@ -135,7 +161,7 @@
actionContext.setValueStack(valueStack);
//XWorkConverter conv =
((Container)stack.getContext().get(ActionContext.CONTAINER)).getInstance(XWorkConverter.class);
-
+
if (JSPLoader.JSP_DIR.exists())
FileUtils.forceDelete(JSPLoader.JSP_DIR);
}
@@ -143,5 +169,5 @@
//converter has a protected default constructor...meh
class DummyConverter extends XWorkConverter {
-
+
}
Added:
struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/broken0.jsp
URL:
http://svn.apache.org/viewvc/struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/broken0.jsp?rev=801334&view=auto
==============================================================================
---
struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/broken0.jsp
(added)
+++
struts/sandbox/trunk/struts2-jsp-plugin/src/test/resources/org/apache/struts2/broken0.jsp
Wed Aug 5 17:49:14 2009
@@ -0,0 +1,2 @@
+<jsp:include page="org/apache/struts2/printParam.jsp" >
+
\ No newline at end of file