From: kkolinko Date: Thu, 29 Apr 2010 10:24:14 +0000 (+0000) Subject: Tests for Servlet 3.0 support of bundling static resources in META-INF/resources... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=019a2b172e37e8e51da0c2c3ffcce12294ef8e64;p=tomcat7.0 Tests for Servlet 3.0 support of bundling static resources in META-INF/resources subdirectory of a jar file. 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 --- diff --git a/test/org/apache/catalina/core/TestStandardContextResources.java b/test/org/apache/catalina/core/TestStandardContextResources.java new file mode 100644 index 000000000..5f6d72d96 --- /dev/null +++ b/test/org/apache/catalina/core/TestStandardContextResources.java @@ -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", + "

resourceA.jsp in the web application

"); + assertPageContains("/test/resourceB.jsp", + "

resourceB.jsp in resources.jar

"); + assertPageContains("/test/folder/resourceC.jsp", + "

resourceC.jsp in the web application

"); + assertPageContains("/test/folder/resourceD.jsp", + "

resourceD.jsp in resources.jar

"); + assertPageContains("/test/folder/resourceE.jsp", + "

resourceE.jsp in the web application

"); + } + } + + 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", + "

resourceA.jsp in the web application

"); + assertPageContains("/test/getresource?path=/resourceB.jsp", + "

resourceB.jsp in resources.jar

"); + assertPageContains("/test/getresource?path=/folder/resourceC.jsp", + "

resourceC.jsp in the web application

"); + assertPageContains("/test/getresource?path=/folder/resourceD.jsp", + "

resourceD.jsp in resources.jar

"); + assertPageContains("/test/getresource?path=/folder/resourceE.jsp", + "

resourceE.jsp in the web application

"); + } + } + + /** + * A servlet that prints the requested resource. The path to the requested + * resource is passed as a parameter, path. + */ + 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 index 000000000..77ee6e974 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 index 000000000..024539c7b --- /dev/null +++ b/test/webapp-3.0-fragments/WEB-INF/web.xml @@ -0,0 +1,30 @@ + + + + + Tomcat Test Application + + Used as part of the Tomcat unit tests when a full web application is + required. + + \ 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 index 000000000..df079ab0a --- /dev/null +++ b/test/webapp-3.0-fragments/folder/resourceC.jsp @@ -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. +--%> +

resourceC.jsp in the web application

diff --git a/test/webapp-3.0-fragments/folder/resourceE.jsp b/test/webapp-3.0-fragments/folder/resourceE.jsp new file mode 100644 index 000000000..f94a9cac8 --- /dev/null +++ b/test/webapp-3.0-fragments/folder/resourceE.jsp @@ -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. +--%> +

resourceE.jsp in the web application

diff --git a/test/webapp-3.0-fragments/resourceA.jsp b/test/webapp-3.0-fragments/resourceA.jsp new file mode 100644 index 000000000..8a8d423c2 --- /dev/null +++ b/test/webapp-3.0-fragments/resourceA.jsp @@ -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. +--%> +

resourceA.jsp in the web application