Tests for Servlet 3.0 support of bundling static resources in META-INF/resources...
authorkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 29 Apr 2010 10:24:14 +0000 (10:24 +0000)
committerkkolinko <kkolinko@13f79535-47bb-0310-9956-ffa450edef68>
Thu, 29 Apr 2010 10:24:14 +0000 (10:24 +0000)
It is a separate web application, because the old "webapp-3.0" has metadata-complete="true".

The tests in TestStandardContextResources are currently failing,
and thus are disabled until a solution is found. See the comment in TestStandardContextResources.java

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@939253 13f79535-47bb-0310-9956-ffa450edef68

test/org/apache/catalina/core/TestStandardContextResources.java [new file with mode: 0644]
test/webapp-3.0-fragments/WEB-INF/lib/resources.jar [new file with mode: 0644]
test/webapp-3.0-fragments/WEB-INF/web.xml [new file with mode: 0644]
test/webapp-3.0-fragments/folder/resourceC.jsp [new file with mode: 0644]
test/webapp-3.0-fragments/folder/resourceE.jsp [new file with mode: 0644]
test/webapp-3.0-fragments/resourceA.jsp [new file with mode: 0644]

diff --git a/test/org/apache/catalina/core/TestStandardContextResources.java b/test/org/apache/catalina/core/TestStandardContextResources.java
new file mode 100644 (file)
index 0000000..5f6d72d
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ *  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.catalina.core;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.catalina.startup.Tomcat;
+import org.apache.catalina.startup.TomcatBaseTest;
+import org.apache.tomcat.util.buf.ByteChunk;
+
+public class TestStandardContextResources extends TomcatBaseTest {
+
+    public void testResources() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File appDir = new File("test/webapp-3.0-fragments");
+        // app dir is relative to server home
+        tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
+
+        tomcat.start();
+
+        if (false) {
+            // FIXME: These tests are currently failing. See comment in testResources2() below.
+            
+        assertPageContains("/test/resourceA.jsp",
+                "<p>resourceA.jsp in the web application</p>");
+        assertPageContains("/test/resourceB.jsp",
+                "<p>resourceB.jsp in resources.jar</p>");
+        assertPageContains("/test/folder/resourceC.jsp",
+                "<p>resourceC.jsp in the web application</p>");
+        assertPageContains("/test/folder/resourceD.jsp",
+                "<p>resourceD.jsp in resources.jar</p>");
+        assertPageContains("/test/folder/resourceE.jsp",
+                "<p>resourceE.jsp in the web application</p>");
+        }
+    }
+
+    public void testResources2() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        File appDir = new File("test/webapp-3.0-fragments");
+        // app dir is relative to server home
+        StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test",
+                appDir.getAbsolutePath());
+
+        Tomcat.addServlet(ctx, "getresource", new GetResourceServlet());
+        ctx.addServletMapping("/getresource", "getresource");
+
+        tomcat.start();
+
+        // FIXME: These tests are currently failing.
+        //
+        // I do not have a fix yet, but I know the following:
+        // when trying to get "/resourceB.jsp" in ApplicationContext#getResource()
+        // an Exception is caught and silently swallowed. That exception is
+        //
+        // java.lang.IllegalStateException: zip file closed
+        // at java.util.jar.JarFile.getMetaInfEntryNames(Native Method)
+        // at java.util.jar.JarFile.maybeInstantiateVerifier(JarFile.java:277)
+        // at java.util.jar.JarFile.getInputStream(JarFile.java:381)
+        // at org.apache.naming.resources.WARDirContext$WARResource.streamContent(WARDirContext.java:951)
+        // at org.apache.naming.resources.ProxyDirContext.cacheLoad(ProxyDirContext.java:1578)
+        // at org.apache.naming.resources.ProxyDirContext.cacheLookup(ProxyDirContext.java:1458)
+        // at org.apache.naming.resources.ProxyDirContext.lookup(ProxyDirContext.java:292)
+        // at org.apache.catalina.core.ApplicationContext.getResource(ApplicationContext.java:506)
+        // at org.apache.catalina.core.ApplicationContextFacade.getResource(ApplicationContextFacade.java:196)
+        // at org.apache.catalina.core.TestStandardContextResources$GetResourceServlet.doGet(TestStandardContextResources.java:126)
+        //
+        if (false) {
+        
+        assertPageContains("/test/getresource?path=/resourceA.jsp",
+                "<p>resourceA.jsp in the web application</p>");
+        assertPageContains("/test/getresource?path=/resourceB.jsp",
+                "<p>resourceB.jsp in resources.jar</p>");
+        assertPageContains("/test/getresource?path=/folder/resourceC.jsp",
+                "<p>resourceC.jsp in the web application</p>");
+        assertPageContains("/test/getresource?path=/folder/resourceD.jsp",
+                "<p>resourceD.jsp in resources.jar</p>");
+        assertPageContains("/test/getresource?path=/folder/resourceE.jsp",
+                "<p>resourceE.jsp in the web application</p>");
+        }
+    }
+
+    /**
+     * A servlet that prints the requested resource. The path to the requested
+     * resource is passed as a parameter, <code>path</code>.
+     */
+    public static class GetResourceServlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+
+            resp.setContentType("text/plain");
+
+            ServletContext context = getServletContext();
+
+            // Check resources individually
+            URL url = context.getResource(req.getParameter("path"));
+            if (url == null) {
+                resp.getWriter().println("Not found");
+                return;
+            }
+
+            InputStream input = url.openStream();
+            OutputStream output = resp.getOutputStream();
+            try {
+                byte[] buffer = new byte[4000];
+                for (int len; (len = input.read(buffer)) > 0;) {
+                    output.write(buffer, 0, len);
+                }
+            } finally {
+                input.close();
+                output.close();
+            }
+        }
+    }
+
+    private void assertPageContains(String pageUrl, String expected)
+            throws IOException {
+        ByteChunk res = getUrl("http://localhost:" + getPort() + pageUrl);
+
+        String result = res.toString();
+        assertTrue(result, result.indexOf(expected) > 0);
+    }
+}
diff --git a/test/webapp-3.0-fragments/WEB-INF/lib/resources.jar b/test/webapp-3.0-fragments/WEB-INF/lib/resources.jar
new file mode 100644 (file)
index 0000000..77ee6e9
Binary files /dev/null and b/test/webapp-3.0-fragments/WEB-INF/lib/resources.jar differ
diff --git a/test/webapp-3.0-fragments/WEB-INF/web.xml b/test/webapp-3.0-fragments/WEB-INF/web.xml
new file mode 100644 (file)
index 0000000..024539c
--- /dev/null
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  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.
+-->
+<web-app xmlns="http://java.sun.com/xml/ns/javaee"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
+  version="3.0"
+  metadata-complete="false">  
+
+  <display-name>Tomcat Test Application</display-name>
+  <description>
+     Used as part of the Tomcat unit tests when a full web application is
+     required.
+  </description>
+</web-app>
\ No newline at end of file
diff --git a/test/webapp-3.0-fragments/folder/resourceC.jsp b/test/webapp-3.0-fragments/folder/resourceC.jsp
new file mode 100644 (file)
index 0000000..df079ab
--- /dev/null
@@ -0,0 +1,21 @@
+<%--
+ 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.
+--%>
+<%--
+  Resource file that is present both in the web application and in the
+  WEB-INF/lib/resources.jar file. The one in the web application should win.
+--%>
+<p>resourceC.jsp in the web application</p>
diff --git a/test/webapp-3.0-fragments/folder/resourceE.jsp b/test/webapp-3.0-fragments/folder/resourceE.jsp
new file mode 100644 (file)
index 0000000..f94a9ca
--- /dev/null
@@ -0,0 +1,20 @@
+<%--
+ 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.
+--%>
+<%--
+  Resource file that is present in the web application only.
+--%>
+<p>resourceE.jsp in the web application</p>
diff --git a/test/webapp-3.0-fragments/resourceA.jsp b/test/webapp-3.0-fragments/resourceA.jsp
new file mode 100644 (file)
index 0000000..8a8d423
--- /dev/null
@@ -0,0 +1,21 @@
+<%--
+ 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.
+--%>
+<%--
+  Resource file that is present both in the web application and in the
+  WEB-INF/lib/resources.jar file. The one in the web application should win.
+--%>
+<p>resourceA.jsp in the web application</p>