From 6cc10d611dde53ca4947a39bff5b2bacc9fc3d6c Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Mon, 15 Aug 2022 19:57:45 +1000 Subject: [PATCH] Added tests for ServletContext resources from collections --- .../jetty/ee10/webapp/WebAppContextTest.java | 42 +++++++++++++++++++ .../resources/wars/layer0/WEB-INF/zero.xml | 0 .../resources/wars/layer1/WEB-INF/one.xml | 0 3 files changed, 42 insertions(+) create mode 100644 jetty-ee10/jetty-ee10-webapp/src/test/resources/wars/layer0/WEB-INF/zero.xml create mode 100644 jetty-ee10/jetty-ee10-webapp/src/test/resources/wars/layer1/WEB-INF/one.xml diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java index 081d434ab1b..8b96fbd2816 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java @@ -49,6 +49,7 @@ import org.eclipse.jetty.util.FileID; import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.resource.FileSystemPool; +import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; @@ -64,9 +65,11 @@ import org.slf4j.LoggerFactory; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.sameInstance; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -484,6 +487,10 @@ public class WebAppContextTest WebAppContext context = new WebAppContext(); context.setContextPath("/"); + // TODO this is not testing what it looks like. The war should be set with + // setWar (or converted to a jar:file before calling setBaseResource) + // However the war is currently a javax war, so it needs to be converted. + new Throwable("fixme").printStackTrace(); Path warPath = MavenTestingUtils.getTestResourcePathFile("wars/dump.war"); warPath = warPath.toAbsolutePath(); assertTrue(warPath.isAbsolute(), "Path should be absolute: " + warPath); @@ -499,6 +506,41 @@ public class WebAppContextTest assertTrue(context.isAvailable(), "WebAppContext should be available"); } + @Test + public void testGetResourceFromCollection() throws Exception + { + Server server = newServer(); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + context.setBaseResource(Resource.combine( + ResourceFactory.root().newResource(MavenTestingUtils.getTestResourcePath("wars/layer0/")), + ResourceFactory.root().newResource(MavenTestingUtils.getTestResourcePath("wars/layer1/")))); + server.setHandler(context); + server.start(); + + ServletContext servletContext = context.getServletContext(); + assertThat(servletContext.getResource("/WEB-INF/zero.xml"), notNullValue()); + assertThat(servletContext.getResource("/WEB-INF/one.xml"), notNullValue()); + } + + @Test + public void testGetResourcePathsFromCollection() throws Exception + { + Server server = newServer(); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + context.setBaseResource(Resource.combine( + ResourceFactory.root().newResource(MavenTestingUtils.getTestResourcePath("wars/layer0/")), + ResourceFactory.root().newResource(MavenTestingUtils.getTestResourcePath("wars/layer1/")))); + server.setHandler(context); + server.start(); + + ServletContext servletContext = context.getServletContext(); + assertThat(servletContext.getResourcePaths("/WEB-INF"), containsInAnyOrder("/WEB-INF/zero.xml", "/WEB-INF/one.xml")); + } + public static Stream extraClasspathGlob() { List references = new ArrayList<>(); diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/resources/wars/layer0/WEB-INF/zero.xml b/jetty-ee10/jetty-ee10-webapp/src/test/resources/wars/layer0/WEB-INF/zero.xml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/resources/wars/layer1/WEB-INF/one.xml b/jetty-ee10/jetty-ee10-webapp/src/test/resources/wars/layer1/WEB-INF/one.xml new file mode 100644 index 00000000000..e69de29bb2d