diff --git a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationConfiguration.java b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationConfiguration.java index 5df80b85fece..fea17fc15ec8 100644 --- a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationConfiguration.java +++ b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationConfiguration.java @@ -13,10 +13,10 @@ package org.eclipse.jetty.ee10.annotations; -import java.io.File; import java.net.URL; import java.net.URLClassLoader; -import java.util.Arrays; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import jakarta.servlet.ServletContainerInitializer; @@ -26,6 +26,7 @@ import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.JAR; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.util.resource.FileSystemPool; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; @@ -38,12 +39,13 @@ import static org.hamcrest.Matchers.empty; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestAnnotationConfiguration { - public class TestableAnnotationConfiguration extends AnnotationConfiguration + public static class TestableAnnotationConfiguration extends AnnotationConfiguration { public void assertAnnotationDiscovery(boolean b) { @@ -52,67 +54,55 @@ public void assertAnnotationDiscovery(boolean b) else assertFalse(_discoverableAnnotationHandlers.isEmpty()); } - } - - public File web25; - - public File web31false; - - public File web31true; - - public File jarDir; - - public File testSciJar; - - public File testContainerSciJar; - - public File testWebInfClassesJar; - - public File unpacked; + } + public WorkDir workDir; + public Path web25; + public Path web31false; + public Path web31true; + public Path jarDir; + public Path testSciJar; + public Path testContainerSciJar; + public Path testWebInfClassesJar; + public Path unpacked; public URLClassLoader containerLoader; - public URLClassLoader webAppLoader; - public List classes; - public Resource targetClasses; - public Resource webInfClasses; @BeforeEach public void setup() throws Exception { assertThat(FileSystemPool.INSTANCE.mounts(), empty()); - web25 = MavenTestingUtils.getTestResourceFile("web25.xml"); - web31false = MavenTestingUtils.getTestResourceFile("web31false.xml"); - web31true = MavenTestingUtils.getTestResourceFile("web31true.xml"); + web25 = MavenTestingUtils.getTestResourcePathFile("web25.xml"); + web31false = MavenTestingUtils.getTestResourcePathFile("web31false.xml"); + web31true = MavenTestingUtils.getTestResourcePathFile("web31true.xml"); // prepare an sci that will be on the webapp's classpath - jarDir = new File(MavenTestingUtils.getTestResourcesDir().getParentFile(), "jar"); - testSciJar = new File(jarDir, "test-sci.jar"); - assertTrue(testSciJar.exists()); + jarDir = MavenTestingUtils.getProjectDirPath("src/test/jar"); + testSciJar = jarDir.resolve("test-sci.jar"); + assertTrue(Files.exists(testSciJar)); - testContainerSciJar = new File(jarDir, "test-sci-for-container-path.jar"); - testWebInfClassesJar = new File(jarDir, "test-sci-for-webinf.jar"); + testContainerSciJar = jarDir.resolve("test-sci-for-container-path.jar"); + testWebInfClassesJar = jarDir.resolve("test-sci-for-webinf.jar"); // unpack some classes to pretend that are in WEB-INF/classes - unpacked = new File(MavenTestingUtils.getTargetTestingDir(), "test-sci-for-webinf"); - unpacked.mkdirs(); + unpacked = workDir.getEmptyPathDir(); FS.cleanDirectory(unpacked); - JAR.unpack(testWebInfClassesJar, unpacked); - webInfClasses = ResourceFactory.root().newResource(unpacked.toPath()); + JAR.unpack(testWebInfClassesJar.toFile(), unpacked.toFile()); + webInfClasses = ResourceFactory.root().newResource(unpacked); containerLoader = new URLClassLoader(new URL[]{ - testContainerSciJar.toURI().toURL() + testContainerSciJar.toUri().toURL() }, Thread.currentThread().getContextClassLoader()); targetClasses = ResourceFactory.root().newResource(MavenTestingUtils.getTargetDir().toURI()).resolve("/test-classes"); - classes = Arrays.asList(new Resource[]{webInfClasses, targetClasses}); + classes = List.of(webInfClasses, targetClasses); webAppLoader = new URLClassLoader(new URL[]{ - testSciJar.toURI().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() + testSciJar.toUri().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() }, containerLoader); } @@ -133,7 +123,7 @@ public void testAnnotationScanControl() throws Exception context25.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context25.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); context25.setConfigurationDiscovered(false); - context25.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25.getMetaData().setWebDescriptor(new WebDescriptor(context25.getResourceFactory().newResource(web25))); context25.getContext().getServletContext().setEffectiveMajorVersion(2); context25.getContext().getServletContext().setEffectiveMinorVersion(5); config25.configure(context25); @@ -145,7 +135,7 @@ public void testAnnotationScanControl() throws Exception context25b.setClassLoader(Thread.currentThread().getContextClassLoader()); context25b.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context25b.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); - context25b.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25b.getMetaData().setWebDescriptor(new WebDescriptor(context25b.getResourceFactory().newResource(web25))); context25b.getContext().getServletContext().setEffectiveMajorVersion(2); context25b.getContext().getServletContext().setEffectiveMinorVersion(5); config25b.configure(context25b); @@ -157,7 +147,7 @@ public void testAnnotationScanControl() throws Exception context31.setClassLoader(Thread.currentThread().getContextClassLoader()); context31.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context31.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); - context31.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context31.getMetaData().setWebDescriptor(new WebDescriptor(context31.getResourceFactory().newResource(web31true))); context31.getContext().getServletContext().setEffectiveMajorVersion(3); context31.getContext().getServletContext().setEffectiveMinorVersion(1); config31.configure(context31); @@ -169,7 +159,7 @@ public void testAnnotationScanControl() throws Exception context31b.setClassLoader(Thread.currentThread().getContextClassLoader()); context31b.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context31b.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); - context31b.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31false.toPath()))); + context31b.getMetaData().setWebDescriptor(new WebDescriptor(context31b.getResourceFactory().newResource(web31false))); context31b.getContext().getServletContext().setEffectiveMajorVersion(3); context31b.getContext().getServletContext().setEffectiveMinorVersion(1); config31b.configure(context31b); @@ -191,8 +181,8 @@ public void testServerAndWebappSCIs() throws Exception //test 3.1 webapp loads both server and app scis context.setClassLoader(webAppLoader); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31true))); context.getMetaData().setWebInfClassesResources(classes); context.getContext().getServletContext().setEffectiveMajorVersion(3); context.getContext().getServletContext().setEffectiveMinorVersion(1); @@ -208,7 +198,7 @@ public void testServerAndWebappSCIs() throws Exception Thread.currentThread().setContextClassLoader(old); } } - + @Test public void testClassScanHandlersForSCIs() throws Exception { @@ -236,24 +226,24 @@ public void createServletContainerInitializerAnnotationHandlers(WebAppContext co assertEquals("com.acme.initializer.Foo", handler._annotation.getName()); } } - + MyAnnotationConfiguration config = new MyAnnotationConfiguration(); - + WebAppContext context = new WebAppContext(); config.preConfigure(context); List scis; context.setClassLoader(webAppLoader); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31true))); context.getMetaData().setWebInfClassesResources(classes); context.getContext().getServletContext().setEffectiveMajorVersion(3); context.getContext().getServletContext().setEffectiveMinorVersion(1); scis = config.getNonExcludedInitializers(context); assertNotNull(scis); assertEquals(3, scis.size()); - - config.createServletContainerInitializerAnnotationHandlers(context, scis); + + config.createServletContainerInitializerAnnotationHandlers(context, scis); } finally { @@ -276,9 +266,9 @@ public void testMetaDataCompleteSCIs() throws Exception // test a 3.1 webapp with metadata-complete=false loads both server // and webapp scis context.setClassLoader(webAppLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31false.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31false))); context.getMetaData().setWebInfClassesResources(classes); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getContext().getServletContext().setEffectiveMajorVersion(3); context.getContext().getServletContext().setEffectiveMinorVersion(1); scis = config.getNonExcludedInitializers(context); @@ -306,10 +296,10 @@ public void testRelativeOrderingWithSCIs() throws Exception ClassLoader old = Thread.currentThread().getContextClassLoader(); - File orderedFragmentJar = new File(jarDir, "test-sci-with-ordering.jar"); - assertTrue(orderedFragmentJar.exists()); + Path orderedFragmentJar = jarDir.resolve("test-sci-with-ordering.jar"); + assertTrue(Files.exists(orderedFragmentJar)); URLClassLoader orderedLoader = new URLClassLoader(new URL[]{ - orderedFragmentJar.toURI().toURL(), testSciJar.toURI().toURL(), + orderedFragmentJar.toUri().toURL(), testSciJar.toUri().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() }, containerLoader); @@ -322,11 +312,11 @@ public void testRelativeOrderingWithSCIs() throws Exception config.preConfigure(context); List scis; context.setClassLoader(orderedLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31true))); RelativeOrdering ordering = new RelativeOrdering(context.getMetaData()); context.getMetaData().setOrdering(ordering); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(orderedFragmentJar.toURI().toURL())); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(orderedFragmentJar)); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getMetaData().setWebInfClassesResources(classes); context.getMetaData().orderFragments(); context.getContext().getServletContext().setEffectiveMajorVersion(3); @@ -359,9 +349,9 @@ public void testDiscoveredFalseWithSCIs() throws Exception List scis; context.setConfigurationDiscovered(false); context.setClassLoader(webAppLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web25))); context.getMetaData().setWebInfClassesResources(classes); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getContext().getServletContext().setEffectiveMajorVersion(2); context.getContext().getServletContext().setEffectiveMinorVersion(5); scis = config.getNonExcludedInitializers(context); @@ -369,8 +359,8 @@ public void testDiscoveredFalseWithSCIs() throws Exception for (ServletContainerInitializer s : scis) { //should not have any of the web-inf lib scis in here - assertFalse(s.getClass().getName().equals("com.acme.ordering.AcmeServletContainerInitializer")); - assertFalse(s.getClass().getName().equals("com.acme.initializer.FooInitializer")); + assertNotEquals("com.acme.ordering.AcmeServletContainerInitializer", s.getClass().getName()); + assertNotEquals("com.acme.initializer.FooInitializer", s.getClass().getName()); //NOTE: should also not have the web-inf classes scis in here either, but due to the //way the test is set up, the sci we're pretending is in web-inf classes will actually //NOT be loaded by the webapp's classloader, but rather by the junit classloader, so @@ -397,14 +387,14 @@ public void testDiscoveredTrueWithSCIs() throws Exception List scis; context.setConfigurationDiscovered(true); context.setClassLoader(webAppLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web25))); context.getMetaData().setWebInfClassesResources(classes); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getContext().getServletContext().setEffectiveMajorVersion(2); context.getContext().getServletContext().setEffectiveMinorVersion(5); scis = config.getNonExcludedInitializers(context); assertNotNull(scis); - assertEquals(3, scis.size(), () -> scis.toString()); + assertEquals(3, scis.size(), scis::toString); assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); //web-inf jar no web-fragment diff --git a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java index 11aa7128eb8c..79d51f2fa9b8 100644 --- a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java +++ b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationDecorator.java @@ -23,13 +23,11 @@ import org.eclipse.jetty.ee10.webapp.WebAppContext; import org.eclipse.jetty.ee10.webapp.WebDescriptor; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; -import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; import org.eclipse.jetty.util.DecoratedObjectFactory; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; import org.eclipse.jetty.xml.XmlParser; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -37,16 +35,15 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -@ExtendWith(WorkDirExtension.class) public class TestAnnotationDecorator { public WorkDir workDir; public class TestWebDescriptor extends WebDescriptor { - public TestWebDescriptor(Resource resource, MetaData.Complete metadata) + public TestWebDescriptor(Resource xml, MetaData.Complete metadata) { - super(resource); + super(xml); _metaDataComplete = metadata; } @@ -86,9 +83,10 @@ public int getMinorVersion() @Test public void testAnnotationDecorator() throws Exception { - Path dummyXml = workDir.getEmptyPathDir().resolve("dummy.xml"); - Files.createFile(dummyXml); - Resource dummyXmlResource = ResourceFactory.root().newResource(dummyXml); + Path docroot = workDir.getEmptyPathDir(); + Path dummyDescriptor = docroot.resolve("dummy.xml"); + Files.createFile(dummyDescriptor); + Resource dummyResource = ResourceFactory.root().newResource(dummyDescriptor); assertThrows(NullPointerException.class, () -> { @@ -108,7 +106,7 @@ public void testAnnotationDecorator() throws Exception context.removeAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); //test with BaseHolder metadata, should not introspect with metdata-complete==true - context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyXmlResource, MetaData.Complete.True)); + context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyResource, MetaData.Complete.True)); assertTrue(context.getMetaData().isMetaDataComplete()); ServletHolder holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, "")); holder.setHeldClass(ServletE.class); @@ -124,7 +122,7 @@ public void testAnnotationDecorator() throws Exception context.removeAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); //test with BaseHolder metadata, should introspect with metadata-complete==false - context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyXmlResource, MetaData.Complete.False)); + context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyResource, MetaData.Complete.False)); DecoratedObjectFactory.associateInfo(holder); decorator = new AnnotationDecorator(context); decorator.decorate(servlet); diff --git a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationIntrospector.java b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationIntrospector.java index 3db21edb9191..7181edd72b4f 100644 --- a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationIntrospector.java +++ b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestAnnotationIntrospector.java @@ -13,7 +13,7 @@ package org.eclipse.jetty.ee10.annotations; -import java.io.File; +import java.nio.file.Path; import org.eclipse.jetty.ee10.servlet.ServletHolder; import org.eclipse.jetty.ee10.servlet.Source; @@ -23,7 +23,6 @@ import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -63,31 +62,33 @@ public void testIsIntrospectable() throws Exception assertTrue(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet can be introspected if web.xml metdata-complete==false - File file = MavenTestingUtils.getTestResourceFile("web31false.xml"); - Resource resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().setWebDescriptor(new WebDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + Path xml = MavenTestingUtils.getTestResourcePathFile("web31false.xml"); + Resource xmlResource = wac.getResourceFactory().newResource(xml); + wac.getMetaData().setWebDescriptor(new WebDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertTrue(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet can be introspected if web-fragment.xml medata-complete==false && web.xml metadata-complete==false - file = MavenTestingUtils.getTestResourceFile("web-fragment4false.xml"); - resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().addFragmentDescriptor(ResourceFactory.root().newResource(file.getParentFile().toPath()), new FragmentDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + xml = MavenTestingUtils.getTestResourcePathFile("web-fragment4false.xml"); + xmlResource = wac.getResourceFactory().newResource(xml); + Resource parent = wac.getResourceFactory().newResource(xml.getParent()); + wac.getMetaData().addFragmentDescriptor(parent, new FragmentDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertTrue(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet cannot be introspected if web-fragment.xml medata-complete==true (&& web.xml metadata-complete==false) - file = MavenTestingUtils.getTestResourceFile("web-fragment4true.xml"); - resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().addFragmentDescriptor(ResourceFactory.root().newResource(file.getParentFile().toPath()), new FragmentDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + xml = MavenTestingUtils.getTestResourcePathFile("web-fragment4true.xml"); + xmlResource = wac.getResourceFactory().newResource(xml); + parent = wac.getResourceFactory().newResource(xml.getParent()); + wac.getMetaData().addFragmentDescriptor(parent, new FragmentDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertFalse(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet cannot be introspected if web.xml medata-complete==true - file = MavenTestingUtils.getTestResourceFile("web31true.xml"); - resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().setWebDescriptor(new WebDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + xml = MavenTestingUtils.getTestResourcePathFile("web31true.xml"); + xmlResource = wac.getResourceFactory().newResource(xml); + wac.getMetaData().setWebDescriptor(new WebDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertFalse(introspector.isIntrospectable(new ServletE(), holder)); } } diff --git a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestRunAsAnnotation.java b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestRunAsAnnotation.java index 7fab0584f3ab..61dfb0c741d5 100644 --- a/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestRunAsAnnotation.java +++ b/jetty-ee10/jetty-ee10-annotations/src/test/java/org/eclipse/jetty/ee10/annotations/TestRunAsAnnotation.java @@ -20,15 +20,11 @@ import org.eclipse.jetty.ee10.webapp.WebAppContext; import org.eclipse.jetty.ee10.webapp.WebDescriptor; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; -import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; -import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; -@ExtendWith(WorkDirExtension.class) public class TestRunAsAnnotation { public WorkDir workDir; @@ -36,10 +32,6 @@ public class TestRunAsAnnotation @Test public void testRunAsAnnotation() throws Exception { - Path dummyXml = workDir.getEmptyPathDir().resolve("dummy.xml"); - Files.createFile(dummyXml); - Resource dummyXmlResource = ResourceFactory.root().newResource(dummyXml); - WebAppContext wac = new WebAppContext(); //pre-add a servlet but not by descriptor @@ -55,7 +47,9 @@ public void testRunAsAnnotation() throws Exception holder2.setHeldClass(ServletC.class); holder2.setInitOrder(1); wac.getServletHandler().addServletWithMapping(holder2, "/foo2/*"); - wac.getMetaData().setOrigin(holder2.getName() + ".servlet.run-as", new WebDescriptor(dummyXmlResource)); + Path fakeXml = workDir.getEmptyPathDir().resolve("fake.xml"); + Files.createFile(fakeXml); + wac.getMetaData().setOrigin(holder2.getName() + ".servlet.run-as", new WebDescriptor(wac.getResourceFactory().newResource(fakeXml))); AnnotationIntrospector parser = new AnnotationIntrospector(wac); RunAsAnnotationHandler handler = new RunAsAnnotationHandler(wac); @@ -63,6 +57,6 @@ public void testRunAsAnnotation() throws Exception parser.introspect(new ServletC(), null); assertEquals("admin", holder.getRunAsRole()); - assertEquals(null, holder2.getRunAsRole()); + assertNull(holder2.getRunAsRole()); } } diff --git a/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/PlusDescriptorProcessor.java b/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/PlusDescriptorProcessor.java index 0bfb4ff59a62..49fe9aaaa45d 100644 --- a/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/PlusDescriptorProcessor.java +++ b/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/PlusDescriptorProcessor.java @@ -142,7 +142,7 @@ public void visitEnvEntry(WebAppContext context, Descriptor descriptor, XmlParse case WebFragment: { //ServletSpec p.75. No declaration in web.xml, but in multiple web-fragments. Error. - throw new IllegalStateException("Conflicting env-entry " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting env-entry " + name + " in " + descriptor); } default: break; @@ -293,7 +293,7 @@ public void visitResourceRef(WebAppContext context, Descriptor descriptor, XmlPa //ServletSpec p.75. No declaration of resource-ref in web xml, but different in multiple web-fragments. Error. if (!type.equals(otherType) || !auth.equals(otherAuth) || !shared.equals(otherShared)) - throw new IllegalStateException("Conflicting resource-ref " + jndiName + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting resource-ref " + jndiName + " in " + descriptor); //same in multiple web-fragments, merge the injections addInjections(context, descriptor, node, jndiName, TypeUtil.fromName(type)); } @@ -401,7 +401,7 @@ public void visitResourceEnvRef(WebAppContext context, Descriptor descriptor, Xm //ServletSpec p.75. No declaration of resource-ref in web xml, but different in multiple web-fragments. Error. if (!type.equals(otherType)) - throw new IllegalStateException("Conflicting resource-env-ref " + jndiName + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting resource-env-ref " + jndiName + " in " + descriptor); //same in multiple web-fragments, merge the injections addInjections(context, descriptor, node, jndiName, TypeUtil.fromName(type)); @@ -503,7 +503,7 @@ public void visitMessageDestinationRef(WebAppContext context, Descriptor descrip type = (type == null ? "" : type); usage = (usage == null ? "" : usage); if (!type.equals(otherType) || !usage.equalsIgnoreCase(otherUsage)) - throw new IllegalStateException("Conflicting message-destination-ref " + jndiName + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting message-destination-ref " + jndiName + " in " + descriptor); //same in multiple web-fragments, merge the injections addInjections(context, descriptor, node, jndiName, TypeUtil.fromName(type)); diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Descriptor.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Descriptor.java index 690a4a9162cd..cc15d2f281a6 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Descriptor.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Descriptor.java @@ -15,8 +15,6 @@ import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; import java.util.Objects; import org.eclipse.jetty.util.resource.Resource; @@ -33,13 +31,11 @@ public abstract class Descriptor protected XmlParser.Node _root; protected String _dtd; - public Descriptor(Resource xml) + public Descriptor(Resource resource) { - _xml = Objects.requireNonNull(xml); - if (!_xml.exists()) - throw new IllegalArgumentException("Descriptor does not exist: " + xml); + _xml = Objects.requireNonNull(resource, "Resource must not be null"); if (_xml.isDirectory()) - throw new IllegalArgumentException("Descriptor is not a file: " + xml); + throw new IllegalArgumentException("Descriptor cannot be a directory"); } public void parse(XmlParser parser) @@ -48,7 +44,7 @@ public void parse(XmlParser parser) if (_root == null) { Objects.requireNonNull(parser); - try (InputStream is = Files.newInputStream(_xml.getPath(), StandardOpenOption.READ)) + try (InputStream is = _xml.newInputStream()) { _root = parser.parse(is); _dtd = parser.getDTD(); @@ -61,9 +57,9 @@ public void parse(XmlParser parser) } } - public boolean isParsed() + public String getURI() { - return _root != null; + return _xml.getURI().toASCIIString(); } public Resource getResource() @@ -79,6 +75,6 @@ public XmlParser.Node getRoot() @Override public String toString() { - return this.getClass().getSimpleName() + "(" + _xml + ")"; + return this.getClass().getSimpleName() + "(" + getURI() + ")"; } } diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentConfiguration.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentConfiguration.java index 454f3dd213d0..42d5a9d4436f 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentConfiguration.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentConfiguration.java @@ -24,6 +24,9 @@ */ public class FragmentConfiguration extends AbstractConfiguration { + // Holds a Map . + // key: Resource to the Jar + // value: Resource to the web fragment xml public static final String FRAGMENT_RESOURCES = "org.eclipse.jetty.webFragments"; public FragmentConfiguration() diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentDescriptor.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentDescriptor.java index 7ef6b1f230f3..92c6fadcef8e 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentDescriptor.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/FragmentDescriptor.java @@ -36,7 +36,6 @@ public enum OtherType None, Before, After } - ; protected OtherType _otherType = OtherType.None; protected List _befores = new ArrayList(); @@ -44,7 +43,6 @@ public enum OtherType protected String _name; public FragmentDescriptor(Resource xml) - throws Exception { super(xml); } @@ -109,7 +107,7 @@ public void processBefores(XmlParser.Node ordering) if (node.getTag().equalsIgnoreCase("others")) { if (_otherType != OtherType.None) - throw new IllegalStateException("Duplicate clause detected in " + _xml.getURI()); + throw new IllegalStateException("Duplicate clause detected in " + _xml); _otherType = OtherType.Before; } @@ -136,7 +134,7 @@ public void processAfters(XmlParser.Node ordering) if (node.getTag().equalsIgnoreCase("others")) { if (_otherType != OtherType.None) - throw new IllegalStateException("Duplicate clause detected in " + _xml.getURI()); + throw new IllegalStateException("Duplicate clause detected in " + _xml); _otherType = OtherType.After; } diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java index 709833c24b9c..2d7f2566bb93 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaData.java @@ -314,7 +314,7 @@ public void addFragmentDescriptor(Resource jarResource, FragmentDescriptor descr Descriptor existing = _webFragmentNameMap.get(descriptor.getName()); if (existing != null && !isAllowDuplicateFragmentNames()) { - throw new IllegalStateException("Duplicate fragment name: " + descriptor.getName() + " for " + existing.getResource() + " and " + descriptor.getResource()); + throw new IllegalStateException("Duplicate fragment name: " + descriptor.getName() + " for " + existing.getURI() + " and " + descriptor.getURI()); } else _webFragmentNameMap.put(descriptor.getName(), descriptor); @@ -597,7 +597,7 @@ public FragmentDescriptor getFragmentDescriptor(String name) */ public FragmentDescriptor getFragmentDescriptor(Resource descriptorResource) { - return _webFragmentRoots.stream().filter(d -> d.getResource().equals(descriptorResource)).findFirst().orElse(null); + return _webFragmentRoots.stream().filter(d -> d.getRoot().equals(descriptorResource)).findFirst().orElse(null); } /** diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java index ff3c6ccf768c..14691b49b17f 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/MetaInfConfiguration.java @@ -376,10 +376,8 @@ public void scanJars(final WebAppContext context, Collection jars, boo * @param context the context for the scan * @param target the target resource to scan for * @param cache the resource cache - * @throws Exception if unable to scan for resources */ public void scanForResources(WebAppContext context, Resource target, ConcurrentHashMap cache) - throws Exception { Resource resourcesDir = null; if (cache != null && cache.containsKey(target)) @@ -450,10 +448,8 @@ private static boolean isEmptyResource(Resource resourcesDir) * @param context the context for the scan * @param jar the jar resource to scan for fragements in * @param cache the resource cache - * @throws Exception if unable to scan for fragments */ public void scanForFragment(WebAppContext context, Resource jar, ConcurrentHashMap cache) - throws Exception { Resource webFrag = null; if (cache != null && cache.containsKey(jar)) @@ -485,7 +481,7 @@ else if (LOG.isDebugEnabled()) if (cache != null) { - //web-fragment.xml doesn't exist: put token in cache to signal we've seen the jar + //web-fragment.xml doesn't exist: put token in cache to signal we've seen the jar Resource old = cache.putIfAbsent(jar, webFrag); if (old != null) webFrag = old; @@ -569,7 +565,6 @@ public void scanForTlds(WebAppContext context, Resource jar, ConcurrentHashMap metaInfTlds = (Collection)context.getAttribute(METAINF_TLDS); if (metaInfTlds == null) { @@ -692,7 +687,7 @@ protected List findWebInfLibJars(WebAppContext context) throws Exception { Resource webInf = context.getWebInf(); - if (webInf == null) + if (webInf == null || !webInf.exists() || webInf.isDirectory()) return null; Resource webInfLib = webInf.resolve("/lib"); diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/StandardDescriptorProcessor.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/StandardDescriptorProcessor.java index e1ca8a0b9e27..c0033c85619a 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/StandardDescriptorProcessor.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/StandardDescriptorProcessor.java @@ -176,7 +176,7 @@ public void visitContextParam(WebAppContext context, Descriptor descriptor, XmlP if (descriptor instanceof FragmentDescriptor) { if (!((String)context.getInitParams().get(name)).equals(value)) - throw new IllegalStateException("Conflicting context-param " + name + "=" + value + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting context-param " + name + "=" + value + " in " + descriptor); } break; } @@ -206,7 +206,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser //If servlet of that name does not already exist, create it. if (holder == null) { - holder = context.getServletHandler().newServletHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + holder = context.getServletHandler().newServletHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); holder.setName(name); _servletHolderMap.put(name, holder); _servletHolders.add(holder); @@ -250,7 +250,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //previously set by a web-fragment, make sure that the value matches, otherwise its an error if ((descriptor != originDescriptor) && !holder.getInitParameter(pname).equals(pvalue)) - throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor.getResource()); + throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor); break; } default: @@ -306,7 +306,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //the class was set by another fragment, ensure this fragment's value is the same if (!servletClass.equals(holder.getClassName())) - throw new IllegalStateException("Conflicting servlet-class " + servletClass + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting servlet-class " + servletClass + " in " + descriptor); break; } default: @@ -370,7 +370,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //it was already set by another fragment, if we're parsing a fragment, the values must match if (order != holder.getInitOrder()) - throw new IllegalStateException("Conflicting load-on-startup value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting load-on-startup value in " + descriptor); break; } default: @@ -413,7 +413,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser case WebFragment: { if (!holder.getUserRoleLink(roleName).equals(roleLink)) - throw new IllegalStateException("Conflicting role-link for role-name " + roleName + " for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting role-link for role-name " + roleName + " for servlet " + name + " in " + descriptor); break; } default: @@ -459,7 +459,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //run-as was set by another fragment, this fragment must show the same value if (!holder.getRunAsRole().equals(roleName)) - throw new IllegalStateException("Conflicting run-as role " + roleName + " for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting run-as role " + roleName + " for servlet " + name + " in " + descriptor); break; } default: @@ -498,7 +498,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //async-supported set by another fragment, this fragment's value must match if (holder.isAsyncSupported() != val) - throw new IllegalStateException("Conflicting async-supported=" + async + " for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting async-supported=" + async + " for servlet " + name + " in " + descriptor); break; } default: @@ -536,7 +536,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //was set by another fragment, this fragment's value must match if (holder.isEnabled() != isEnabled) - throw new IllegalStateException("Conflicting value of servlet enabled for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting value of servlet enabled for servlet " + name + " in " + descriptor); break; } default: @@ -589,14 +589,14 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser MultipartConfigElement cfg = ((ServletHolder.Registration)holder.getRegistration()).getMultipartConfig(); if (cfg.getMaxFileSize() != element.getMaxFileSize()) - throw new IllegalStateException("Conflicting multipart-config max-file-size for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config max-file-size for servlet " + name + " in " + descriptor); if (cfg.getMaxRequestSize() != element.getMaxRequestSize()) - throw new IllegalStateException("Conflicting multipart-config max-request-size for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config max-request-size for servlet " + name + " in " + descriptor); if (cfg.getFileSizeThreshold() != element.getFileSizeThreshold()) - throw new IllegalStateException("Conflicting multipart-config file-size-threshold for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config file-size-threshold for servlet " + name + " in " + descriptor); if ((cfg.getLocation() != null && (element.getLocation() == null || element.getLocation().length() == 0)) || (cfg.getLocation() == null && (element.getLocation() != null || element.getLocation().length() > 0))) - throw new IllegalStateException("Conflicting multipart-config location for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config location for servlet " + name + " in " + descriptor); break; } default: @@ -805,7 +805,7 @@ public void addSessionConfigAttribute(WebAppContext context, Descriptor descript { //a web-fragment set an attribute of the same name, all web-fragments must have the same value if (!StringUtil.nonNull(value).equals(StringUtil.nonNull(context.getSessionHandler().getSessionCookieConfig().getAttribute(name)))) - throw new IllegalStateException("Conflicting attribute " + name + "=" + value + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting attribute " + name + "=" + value + " in " + descriptor.getURI()); break; } default: @@ -847,7 +847,7 @@ public void visitMimeMapping(WebAppContext context, Descriptor descriptor, XmlPa { //a web-fragment set the value, all web-fragments must have the same value if (!context.getMimeTypes().getMimeByExtension("." + extension).equals(mimeType)) - throw new IllegalStateException("Conflicting mime-type " + mimeType + " for extension " + extension + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting mime-type " + mimeType + " for extension " + extension + " in " + descriptor); break; } default: @@ -938,7 +938,7 @@ public void visitLocaleEncodingList(WebAppContext context, Descriptor descriptor { //a value was set by a web-fragment, all fragments must have the same value if (!encoding.equals(context.getLocaleEncoding(locale))) - throw new IllegalStateException("Conflicting loacle-encoding mapping for locale " + locale + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting locale-encoding mapping for locale " + locale + " in " + descriptor); break; } default: @@ -1004,7 +1004,7 @@ public void visitErrorPage(WebAppContext context, Descriptor descriptor, XmlPars { //another web fragment set the same error code or exception, if its different its an error if (!handler.getErrorPages().get(error).equals(location)) - throw new IllegalStateException("Conflicting error-code or exception-type " + error + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting error-code or exception-type " + error + " in " + descriptor); break; } default: @@ -1028,7 +1028,7 @@ public void addWelcomeFiles(WebAppContext context, XmlParser.Node node, Descript public ServletMapping addServletMapping(String servletName, XmlParser.Node node, WebAppContext context, Descriptor descriptor) { - ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); mapping.setServletName(servletName); mapping.setFromDefaultDescriptor(descriptor instanceof DefaultsDescriptor); context.getMetaData().setOrigin(servletName + ".servlet.mapping." + Long.toHexString(mapping.hashCode()), descriptor); @@ -1271,7 +1271,7 @@ public void visitJspConfig(WebAppContext context, Descriptor descriptor, XmlPars else { //no mapping for jsp yet, make one - ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); mapping.setServletName("jsp"); mapping.setPathSpecs(paths.toArray(new String[paths.size()])); _servletMappings.add(mapping); @@ -1429,7 +1429,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //it was already set by another fragment, if we're parsing a fragment, the values must match if (!context.getSecurityHandler().getAuthMethod().equals(method.toString(false, true))) - throw new IllegalStateException("Conflicting auth-method value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting auth-method value in " + descriptor); break; } default: @@ -1465,7 +1465,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //a fragment set it, and we must be parsing another fragment, so the values must match if (!context.getSecurityHandler().getRealmName().equals(nameStr)) - throw new IllegalStateException("Conflicting realm-name value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting realm-name value in " + descriptor); break; } default: @@ -1513,7 +1513,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //a web-fragment previously set it. We must be parsing yet another web-fragment, so the values must agree if (!context.getSecurityHandler().getInitParameter(FormAuthenticator.__FORM_LOGIN_PAGE).equals(loginPageName)) - throw new IllegalStateException("Conflicting form-login-page value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting form-login-page value in " + descriptor); break; } default: @@ -1547,7 +1547,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //a web-fragment previously set it. We must be parsing yet another web-fragment, so the values must agree if (!context.getSecurityHandler().getInitParameter(FormAuthenticator.__FORM_ERROR_PAGE).equals(errorPageName)) - throw new IllegalStateException("Conflicting form-error-page value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting form-error-page value in " + descriptor); break; } default: @@ -1582,7 +1582,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. FilterHolder holder = _filterHolderMap.get(name); if (holder == null) { - holder = context.getServletHandler().newFilterHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + holder = context.getServletHandler().newFilterHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); holder.setName(name); _filterHolderMap.put(name, holder); _filterHolders.add(holder); @@ -1619,7 +1619,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. { //the filter class was set up by a web fragment, all fragments must be the same if (!holder.getClassName().equals(filterClass)) - throw new IllegalStateException("Conflicting filter-class for filter " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting filter-class for filter " + name + " in " + descriptor); break; } default: @@ -1661,7 +1661,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. { //previously set by a web-fragment, make sure that the value matches, otherwise its an error if (!holder.getInitParameter(pname).equals(pvalue)) - throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor.getResource()); + throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor); break; } default: @@ -1701,7 +1701,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. { //async-supported set by another fragment, this fragment's value must match if (holder.isAsyncSupported() != val) - throw new IllegalStateException("Conflicting async-supported=" + async + " for filter " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting async-supported=" + async + " for filter " + name + " in " + descriptor); break; } default: @@ -1766,7 +1766,7 @@ public void visitListener(WebAppContext context, Descriptor descriptor, XmlParse ((WebDescriptor)descriptor).addClassName(className); - ListenerHolder h = context.getServletHandler().newListenerHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + ListenerHolder h = context.getServletHandler().newListenerHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); h.setClassName(className); context.getServletHandler().addListener(h); context.getMetaData().setOrigin(className + ".listener", descriptor); @@ -1775,7 +1775,6 @@ public void visitListener(WebAppContext context, Descriptor descriptor, XmlParse catch (Exception e) { LOG.warn("Could not instantiate listener {}", className, e); - return; } } diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebXmlConfiguration.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebXmlConfiguration.java index 5d0f25e3318e..64532103001a 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebXmlConfiguration.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebXmlConfiguration.java @@ -128,7 +128,7 @@ public void deconfigure(WebAppContext context) throws Exception { context.setWelcomeFiles(null); - //TODO: ErrorPageErorrHandler is not an ErrorProcessor + //TODO: ErrorPageErrorHandler is not an ErrorProcessor if (context.getErrorProcessor() instanceof ErrorPageErrorHandler errorPageErrorHandler) errorPageErrorHandler.setErrorPages(null); diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/MetaInfConfigurationTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/MetaInfConfigurationTest.java index 83d17e766d09..6c736785ca07 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/MetaInfConfigurationTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/MetaInfConfigurationTest.java @@ -13,7 +13,7 @@ package org.eclipse.jetty.ee10.webapp; -import java.io.File; +import java.nio.file.Path; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -22,7 +22,6 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.resource.FileSystemPool; import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -94,16 +93,16 @@ public void afterEach() @Test public void testScanTypes() throws Exception { - File web25 = MavenTestingUtils.getTestResourceFile("web25.xml"); - File web31 = MavenTestingUtils.getTestResourceFile("web31.xml"); - File web31false = MavenTestingUtils.getTestResourceFile("web31false.xml"); + Path web25 = MavenTestingUtils.getTestResourcePathFile("web25.xml"); + Path web31 = MavenTestingUtils.getTestResourcePathFile("web31.xml"); + Path web31false = MavenTestingUtils.getTestResourcePathFile("web31false.xml"); //test a 2.5 webapp will not look for fragments as manually configured MetaInfConfiguration meta25 = new TestableMetaInfConfiguration(MetaInfConfiguration.__allScanTypes, Arrays.asList(MetaInfConfiguration.METAINF_TLDS, MetaInfConfiguration.METAINF_RESOURCES)); WebAppContext context25 = new WebAppContext(); context25.setConfigurationDiscovered(false); - context25.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25.getMetaData().setWebDescriptor(new WebDescriptor(context25.getResourceFactory().newResource(web25))); context25.getContext().getServletContext().setEffectiveMajorVersion(2); context25.getContext().getServletContext().setEffectiveMinorVersion(5); meta25.preConfigure(context25); @@ -112,7 +111,7 @@ public void testScanTypes() throws Exception MetaInfConfiguration meta25b = new TestableMetaInfConfiguration(MetaInfConfiguration.__allScanTypes, MetaInfConfiguration.__allScanTypes); WebAppContext context25b = new WebAppContext(); - context25b.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25b.getMetaData().setWebDescriptor(new WebDescriptor(context25b.getResourceFactory().newResource(web25))); context25b.getContext().getServletContext().setEffectiveMajorVersion(2); context25b.getContext().getServletContext().setEffectiveMinorVersion(5); meta25b.preConfigure(context25b); @@ -121,7 +120,7 @@ public void testScanTypes() throws Exception MetaInfConfiguration meta31 = new TestableMetaInfConfiguration(MetaInfConfiguration.__allScanTypes, Arrays.asList(MetaInfConfiguration.METAINF_TLDS, MetaInfConfiguration.METAINF_RESOURCES)); WebAppContext context31 = new WebAppContext(); - context31.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31.toPath()))); + context31.getMetaData().setWebDescriptor(new WebDescriptor(context31.getResourceFactory().newResource(web31))); context31.getContext().getServletContext().setEffectiveMajorVersion(3); context31.getContext().getServletContext().setEffectiveMinorVersion(1); meta31.preConfigure(context31); @@ -131,7 +130,7 @@ public void testScanTypes() throws Exception MetaInfConfiguration.__allScanTypes); WebAppContext context31false = new WebAppContext(); context31false.setConfigurationDiscovered(true); - context31false.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31false.toPath()))); + context31false.getMetaData().setWebDescriptor(new WebDescriptor(context31false.getResourceFactory().newResource(web31false))); context31false.getContext().getServletContext().setEffectiveMajorVersion(3); context31false.getContext().getServletContext().setEffectiveMinorVersion(1); meta31false.preConfigure(context31false); diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/OrderingTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/OrderingTest.java index 9388af43970e..94fc10c73915 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/OrderingTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/OrderingTest.java @@ -14,15 +14,15 @@ package org.eclipse.jetty.ee10.webapp; import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.util.resource.FileSystemPool; import org.eclipse.jetty.util.resource.Resource; +import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,85 +39,29 @@ */ public class OrderingTest { - public class TestResource extends Resource - { - public String _name; - - public TestResource(String name) - { - _name = name; - } - - @Override - public Resource resolve(String subUriPath) - { - return null; - } - - @Override - public boolean exists() - { - return true; - } - - @Override - public Path getPath() - { - return null; - } - - @Override - public InputStream newInputStream() throws IOException - { - return null; - } - - @Override - public ReadableByteChannel newReadableByteChannel() throws IOException - { - return null; - } + WorkDir workDir; - @Override - public String getName() - { - return _name; - } - - @Override - public String getFileName() - { - return null; - } - - @Override - public URI getURI() - { - return null; - } - - @Override - public boolean isContainedIn(Resource r) - { - return false; - } - - @Override - public boolean isDirectory() - { - return false; - } + private Resource newTestableDirResource(String name) throws IOException + { + Path dir = workDir.getPath().resolve(name); + if (!Files.exists(dir)) + Files.createDirectories(dir); + return ResourceFactory.root().newResource(dir); + } - @Override - public long length() - { - return 0; - } + private Resource newTestableFileResource(String name) throws IOException + { + Path file = workDir.getPath().resolve(name); + if (!Files.exists(file)) + Files.createFile(file); + return ResourceFactory.root().newResource(file); } @BeforeEach public void beforeEach() { + // ensure work dir exists, and is empty + workDir.getEmptyPathDir(); assertThat(FileSystemPool.INSTANCE.mounts(), empty()); } @@ -133,13 +77,13 @@ public void testRelativeOrdering0() { //Example from ServletSpec p.70 MetaData metaData = new MetaData(); - List resources = new ArrayList(); + List resources = new ArrayList<>(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, after C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -149,9 +93,9 @@ public void testRelativeOrdering0() f1._afters.add("C"); //B: before others - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -160,9 +104,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addBeforeOthers(r2); //C: after others - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -171,9 +115,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addAfterOthers(r3); //D: no ordering - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); @@ -182,9 +126,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addNoOthers(r4); //E: no ordering - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); @@ -193,9 +137,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addNoOthers(r5); //F: before others, before B - TestResource jar6 = new TestResource("F"); + Resource jar6 = newTestableDirResource("F"); resources.add(jar6); - TestResource r6 = new TestResource("F/web-fragment.xml"); + Resource r6 = newTestableFileResource("F/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = "F"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -210,13 +154,13 @@ public void testRelativeOrdering0() String[] outcomes = {"FBDECA"}; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -224,15 +168,15 @@ public void testRelativeOrdering0() public void testRelativeOrdering1() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //Example from ServletSpec p.70-71 //No name: after others, before C - TestResource jar1 = new TestResource("plain"); + Resource jar1 = newTestableDirResource("plain"); resources.add(jar1); - TestResource r1 = new TestResource("plain/web-fragment.xml"); + Resource r1 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -242,9 +186,9 @@ public void testRelativeOrdering1() f1._befores.add("C"); //B: before others - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -253,9 +197,9 @@ public void testRelativeOrdering1() //((RelativeOrdering)metaData._ordering).addBeforeOthers(f2); //C: no ordering - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -264,9 +208,9 @@ public void testRelativeOrdering1() f3._otherType = FragmentDescriptor.OtherType.None; //D: after others - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); @@ -275,9 +219,9 @@ public void testRelativeOrdering1() f4._otherType = FragmentDescriptor.OtherType.After; //E: before others - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); @@ -286,9 +230,9 @@ public void testRelativeOrdering1() f5._otherType = FragmentDescriptor.OtherType.Before; //F: no ordering - TestResource jar6 = new TestResource("F"); + Resource jar6 = newTestableDirResource("F"); resources.add(jar6); - TestResource r6 = new TestResource("F/web-fragment.xml"); + Resource r6 = newTestableFileResource("F/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = "F"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -313,13 +257,13 @@ public void testRelativeOrdering1() "EBFDplainC" }; - String orderedNames = ""; + StringBuilder orderedNames = new StringBuilder(); for (Resource r : orderedList) { - orderedNames += (((TestResource)r)._name); + orderedNames.append(r.getFileName()); } - if (!checkResult(orderedNames, outcomes)) + if (!checkResult(orderedNames.toString(), outcomes)) fail("No outcome matched " + orderedNames); } @@ -327,16 +271,16 @@ public void testRelativeOrdering1() public void testRelativeOrdering2() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //Example from Spec p. 71-72 //A: after B - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -346,9 +290,9 @@ public void testRelativeOrdering2() f1._afters.add("B"); //B: no order - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -357,9 +301,9 @@ public void testRelativeOrdering2() f2._otherType = FragmentDescriptor.OtherType.None; //C: before others - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -368,9 +312,9 @@ public void testRelativeOrdering2() f3._otherType = FragmentDescriptor.OtherType.Before; //D: no order - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); @@ -390,13 +334,13 @@ public void testRelativeOrdering2() }; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -404,14 +348,14 @@ public void testRelativeOrdering2() public void testRelativeOrdering3() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, before C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -421,9 +365,9 @@ public void testRelativeOrdering3() f1._befores.add("C"); //B: before others, before C - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -433,9 +377,9 @@ public void testRelativeOrdering3() f2._befores.add("C"); //C: no ordering - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -447,13 +391,13 @@ public void testRelativeOrdering3() String[] outcomes = {"BAC"}; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -461,7 +405,7 @@ public void testRelativeOrdering3() public void testOrderFragments() throws Exception { final MetaData metadata = new MetaData(); - final Resource jarResource = new TestResource("A"); + final Resource jarResource = newTestableDirResource("A"); metadata.setOrdering(new RelativeOrdering(metadata)); metadata.addWebInfResource(jarResource); @@ -478,14 +422,14 @@ public void testCircular1() //A: after B //B: after A - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after B - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -495,9 +439,9 @@ public void testCircular1() f1._afters.add("B"); //B: after A - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -517,14 +461,14 @@ public void testCircular1() public void testInvalid1() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, before C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -534,9 +478,9 @@ public void testInvalid1() f1._befores.add("C"); //B: before others, after C - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -546,9 +490,9 @@ public void testInvalid1() f2._afters.add("C"); //C: no ordering - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -559,10 +503,10 @@ public void testInvalid1() assertThrows(IllegalStateException.class, () -> { List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } System.err.println("Invalid Result = " + result); fail("A and B have an impossible relationship to C"); @@ -576,7 +520,7 @@ public void testAbsoluteOrdering1() // // A,B,C,others // - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); ((AbsoluteOrdering)metaData._ordering).add("A"); @@ -584,49 +528,49 @@ public void testAbsoluteOrdering1() ((AbsoluteOrdering)metaData._ordering).add("C"); ((AbsoluteOrdering)metaData._ordering).addOthers(); - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); metaData._webFragmentResourceMap.put(jar1, f1); - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); metaData._webFragmentResourceMap.put(jar2, f2); - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); metaData._webFragmentResourceMap.put(jar3, f3); - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); metaData._webFragmentResourceMap.put(jar4, f4); - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); metaData._webFragmentResourceMap.put(jar5, f5); - TestResource jar6 = new TestResource("plain"); + Resource jar6 = newTestableDirResource("plain"); resources.add(jar6); - TestResource r6 = new TestResource("plain/web-fragment.xml"); + Resource r6 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -635,13 +579,13 @@ public void testAbsoluteOrdering1() List list = metaData._ordering.order(resources); String[] outcomes = {"ABCDEplain"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : list) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -650,7 +594,7 @@ public void testAbsoluteOrdering2() throws Exception { // C,B,A - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); @@ -658,49 +602,49 @@ public void testAbsoluteOrdering2() ((AbsoluteOrdering)metaData._ordering).add("B"); ((AbsoluteOrdering)metaData._ordering).add("A"); - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); metaData._webFragmentResourceMap.put(jar1, f1); - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); metaData._webFragmentResourceMap.put(jar2, f2); - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); metaData._webFragmentResourceMap.put(jar3, f3); - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); metaData._webFragmentResourceMap.put(jar4, f4); - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); metaData._webFragmentResourceMap.put(jar5, f5); - TestResource jar6 = new TestResource("plain"); + Resource jar6 = newTestableDirResource("plain"); resources.add(jar6); - TestResource r6 = new TestResource("plain/web-fragment.xml"); + Resource r6 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -708,13 +652,13 @@ public void testAbsoluteOrdering2() List list = metaData._ordering.order(resources); String[] outcomes = {"CBA"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : list) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -726,10 +670,10 @@ public void testAbsoluteOrdering3() MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); - List resources = new ArrayList(); + List resources = new ArrayList<>(); - resources.add(new TestResource("A")); - resources.add(new TestResource("B")); + resources.add(newTestableDirResource("A")); + resources.add(newTestableDirResource("B")); List list = metaData._ordering.order(resources); assertThat(list, is(empty())); @@ -740,14 +684,14 @@ public void testRelativeOrderingWithPlainJars() throws Exception { //B,A,C other jars with no fragments - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, before C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -757,9 +701,9 @@ public void testRelativeOrderingWithPlainJars() f1._befores.add("C"); //B: before others, before C - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -769,9 +713,9 @@ public void testRelativeOrderingWithPlainJars() f2._befores.add("C"); //C: after A - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -781,24 +725,24 @@ public void testRelativeOrderingWithPlainJars() f3._afters.add("A"); //No fragment jar 1 - TestResource r4 = new TestResource("plain1"); + Resource r4 = newTestableFileResource("plain1"); resources.add(r4); //No fragment jar 2 - TestResource r5 = new TestResource("plain2"); + Resource r5 = newTestableFileResource("plain2"); resources.add(r5); //result: BAC String[] outcomes = {"Bplain1plain2AC"}; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -807,14 +751,14 @@ public void testRelativeOrderingWithPlainJars2() throws Exception { //web.xml has no ordering, jar A has fragment after others, jar B is plain, jar C is plain - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A has after others - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -822,22 +766,22 @@ public void testRelativeOrderingWithPlainJars2() f1._otherType = FragmentDescriptor.OtherType.After; //No fragment jar B - TestResource r4 = new TestResource("plainB"); + Resource r4 = newTestableFileResource("plainB"); resources.add(r4); //No fragment jar C - TestResource r5 = new TestResource("plainC"); + Resource r5 = newTestableFileResource("plainC"); resources.add(r5); List orderedList = metaData._ordering.order(resources); String[] outcomes = {"plainBplainCA"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -848,7 +792,7 @@ public void testAbsoluteOrderingWithPlainJars() // // A,B,C,others // - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); ((AbsoluteOrdering)metaData._ordering).add("A"); @@ -856,82 +800,81 @@ public void testAbsoluteOrderingWithPlainJars() ((AbsoluteOrdering)metaData._ordering).add("C"); ((AbsoluteOrdering)metaData._ordering).addOthers(); - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); metaData._webFragmentResourceMap.put(jar1, f1); - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); metaData._webFragmentResourceMap.put(jar2, f2); - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); metaData._webFragmentResourceMap.put(jar3, f3); - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); metaData._webFragmentResourceMap.put(jar4, f4); - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); metaData._webFragmentResourceMap.put(jar5, f5); - TestResource jar6 = new TestResource("plain"); + Resource jar6 = newTestableDirResource("plain"); resources.add(jar6); - TestResource r6 = new TestResource("plain/web-fragment.xml"); + Resource r6 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f6._name, f6); metaData._webFragmentResourceMap.put(jar6, f6); //plain jar - TestResource r7 = new TestResource("plain1"); + Resource r7 = newTestableFileResource("plain1"); resources.add(r7); - TestResource r8 = new TestResource("plain2"); + Resource r8 = newTestableFileResource("plain2"); resources.add(r8); List list = metaData._ordering.order(resources); String[] outcomes = {"ABCDEplainplain1plain2"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : list) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } public boolean checkResult(String result, String[] outcomes) { - boolean matched = false; for (String s : outcomes) { if (s.equals(result)) - matched = true; + return true; } - return matched; + return false; } } diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebDescriptorTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebDescriptorTest.java index a70b76a9fee7..9040d1b14eaa 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebDescriptorTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebDescriptorTest.java @@ -19,7 +19,6 @@ import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; -import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; import org.eclipse.jetty.xml.XmlParser; import org.junit.jupiter.api.Test; @@ -48,8 +47,7 @@ public void testXmlWithXsd() throws Exception """, StandardCharsets.UTF_8); - Resource xmlRes = ResourceFactory.root().newResource(xml); - WebDescriptor webDescriptor = new WebDescriptor(xmlRes); + WebDescriptor webDescriptor = new WebDescriptor(ResourceFactory.root().newResource(xml)); XmlParser xmlParser = WebDescriptor.newParser(true); // This should not throw an exception, if it does then you have a bad state. // Such as missing required XML resource entities. diff --git a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationConfiguration.java b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationConfiguration.java index 90f820e86cab..7119b2c62bb4 100644 --- a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationConfiguration.java +++ b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationConfiguration.java @@ -13,10 +13,10 @@ package org.eclipse.jetty.ee9.annotations; -import java.io.File; import java.net.URL; import java.net.URLClassLoader; -import java.util.Arrays; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import jakarta.servlet.ServletContainerInitializer; @@ -26,6 +26,7 @@ import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.JAR; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.util.resource.FileSystemPool; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; @@ -38,12 +39,13 @@ import static org.hamcrest.Matchers.empty; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; public class TestAnnotationConfiguration { - public class TestableAnnotationConfiguration extends AnnotationConfiguration + public static class TestableAnnotationConfiguration extends AnnotationConfiguration { public void assertAnnotationDiscovery(boolean b) { @@ -52,67 +54,55 @@ public void assertAnnotationDiscovery(boolean b) else assertFalse(_discoverableAnnotationHandlers.isEmpty()); } - } - - public File web25; - - public File web31false; - - public File web31true; - - public File jarDir; - - public File testSciJar; - - public File testContainerSciJar; - - public File testWebInfClassesJar; - - public File unpacked; + } + public WorkDir workDir; + public Path web25; + public Path web31false; + public Path web31true; + public Path jarDir; + public Path testSciJar; + public Path testContainerSciJar; + public Path testWebInfClassesJar; + public Path unpacked; public URLClassLoader containerLoader; - public URLClassLoader webAppLoader; - public List classes; - public Resource targetClasses; - public Resource webInfClasses; @BeforeEach public void setup() throws Exception { assertThat(FileSystemPool.INSTANCE.mounts(), empty()); - web25 = MavenTestingUtils.getTargetFile("test-classes/web25.xml"); - web31false = MavenTestingUtils.getTargetFile("test-classes/web31false.xml"); - web31true = MavenTestingUtils.getTargetFile("test-classes/web31true.xml"); + web25 = MavenTestingUtils.getTargetPath().resolve("test-classes/web25.xml"); + web31false = MavenTestingUtils.getTargetPath().resolve("web31false.xml"); + web31true = MavenTestingUtils.getTargetPath().resolve("web31true.xml"); // prepare an sci that will be on the webapp's classpath - jarDir = new File(MavenTestingUtils.getTargetPath("test-classes").toFile(), "jar"); - testSciJar = new File(jarDir, "test-sci.jar"); - assertTrue(testSciJar.exists()); + jarDir = MavenTestingUtils.getProjectDirPath("src/test/jar"); + testSciJar = jarDir.resolve("test-sci.jar"); + assertTrue(Files.exists(testSciJar)); - testContainerSciJar = new File(jarDir, "test-sci-for-container-path.jar"); - testWebInfClassesJar = new File(jarDir, "test-sci-for-webinf.jar"); + testContainerSciJar = jarDir.resolve("test-sci-for-container-path.jar"); + testWebInfClassesJar = jarDir.resolve("test-sci-for-webinf.jar"); // unpack some classes to pretend that are in WEB-INF/classes - unpacked = new File(MavenTestingUtils.getTargetTestingDir(), "test-sci-for-webinf"); - unpacked.mkdirs(); + unpacked = workDir.getEmptyPathDir(); FS.cleanDirectory(unpacked); - JAR.unpack(testWebInfClassesJar, unpacked); - webInfClasses = ResourceFactory.root().newResource(unpacked.toPath()); + JAR.unpack(testWebInfClassesJar.toFile(), unpacked.toFile()); + webInfClasses = ResourceFactory.root().newResource(unpacked); containerLoader = new URLClassLoader(new URL[]{ - testContainerSciJar.toURI().toURL() + testContainerSciJar.toUri().toURL() }, Thread.currentThread().getContextClassLoader()); targetClasses = ResourceFactory.root().newResource(MavenTestingUtils.getTargetDir().toURI()).resolve("/test-classes"); - classes = Arrays.asList(new Resource[]{webInfClasses, targetClasses}); + classes = List.of(webInfClasses, targetClasses); webAppLoader = new URLClassLoader(new URL[]{ - testSciJar.toURI().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() + testSciJar.toUri().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() }, containerLoader); } @@ -133,7 +123,7 @@ public void testAnnotationScanControl() throws Exception context25.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context25.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); context25.setConfigurationDiscovered(false); - context25.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25.getMetaData().setWebDescriptor(new WebDescriptor(context25.getResourceFactory().newResource(web25))); context25.getServletContext().setEffectiveMajorVersion(2); context25.getServletContext().setEffectiveMinorVersion(5); config25.configure(context25); @@ -145,7 +135,7 @@ public void testAnnotationScanControl() throws Exception context25b.setClassLoader(Thread.currentThread().getContextClassLoader()); context25b.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context25b.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); - context25b.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25b.getMetaData().setWebDescriptor(new WebDescriptor(context25b.getResourceFactory().newResource(web25))); context25b.getServletContext().setEffectiveMajorVersion(2); context25b.getServletContext().setEffectiveMinorVersion(5); config25b.configure(context25b); @@ -157,7 +147,7 @@ public void testAnnotationScanControl() throws Exception context31.setClassLoader(Thread.currentThread().getContextClassLoader()); context31.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context31.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); - context31.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context31.getMetaData().setWebDescriptor(new WebDescriptor(context31.getResourceFactory().newResource(web31true))); context31.getServletContext().setEffectiveMajorVersion(3); context31.getServletContext().setEffectiveMinorVersion(1); config31.configure(context31); @@ -169,7 +159,7 @@ public void testAnnotationScanControl() throws Exception context31b.setClassLoader(Thread.currentThread().getContextClassLoader()); context31b.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE); context31b.setAttribute(AnnotationConfiguration.MAX_SCAN_WAIT, 0); - context31b.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31false.toPath()))); + context31b.getMetaData().setWebDescriptor(new WebDescriptor(context31b.getResourceFactory().newResource(web31false))); context31b.getServletContext().setEffectiveMajorVersion(3); context31b.getServletContext().setEffectiveMinorVersion(1); config31b.configure(context31b); @@ -191,8 +181,8 @@ public void testServerAndWebappSCIs() throws Exception //test 3.1 webapp loads both server and app scis context.setClassLoader(webAppLoader); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31true))); context.getMetaData().setWebInfClassesResources(classes); context.getServletContext().setEffectiveMajorVersion(3); context.getServletContext().setEffectiveMinorVersion(1); @@ -208,7 +198,7 @@ public void testServerAndWebappSCIs() throws Exception Thread.currentThread().setContextClassLoader(old); } } - + @Test public void testClassScanHandlersForSCIs() throws Exception { @@ -236,24 +226,24 @@ public void createServletContainerInitializerAnnotationHandlers(WebAppContext co assertEquals("com.acme.initializer.Foo", handler._annotation.getName()); } } - + MyAnnotationConfiguration config = new MyAnnotationConfiguration(); - + WebAppContext context = new WebAppContext(); config.preConfigure(context); List scis; context.setClassLoader(webAppLoader); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31true))); context.getMetaData().setWebInfClassesResources(classes); context.getServletContext().setEffectiveMajorVersion(3); context.getServletContext().setEffectiveMinorVersion(1); scis = config.getNonExcludedInitializers(context); assertNotNull(scis); assertEquals(3, scis.size()); - - config.createServletContainerInitializerAnnotationHandlers(context, scis); + + config.createServletContainerInitializerAnnotationHandlers(context, scis); } finally { @@ -276,9 +266,9 @@ public void testMetaDataCompleteSCIs() throws Exception // test a 3.1 webapp with metadata-complete=false loads both server // and webapp scis context.setClassLoader(webAppLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31false.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31false))); context.getMetaData().setWebInfClassesResources(classes); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getServletContext().setEffectiveMajorVersion(3); context.getServletContext().setEffectiveMinorVersion(1); scis = config.getNonExcludedInitializers(context); @@ -306,10 +296,10 @@ public void testRelativeOrderingWithSCIs() throws Exception ClassLoader old = Thread.currentThread().getContextClassLoader(); - File orderedFragmentJar = new File(jarDir, "test-sci-with-ordering.jar"); - assertTrue(orderedFragmentJar.exists()); + Path orderedFragmentJar = jarDir.resolve("test-sci-with-ordering.jar"); + assertTrue(Files.exists(orderedFragmentJar)); URLClassLoader orderedLoader = new URLClassLoader(new URL[]{ - orderedFragmentJar.toURI().toURL(), testSciJar.toURI().toURL(), + orderedFragmentJar.toUri().toURL(), testSciJar.toUri().toURL(), targetClasses.getURI().toURL(), webInfClasses.getURI().toURL() }, containerLoader); @@ -322,11 +312,11 @@ public void testRelativeOrderingWithSCIs() throws Exception config.preConfigure(context); List scis; context.setClassLoader(orderedLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31true.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web31true))); RelativeOrdering ordering = new RelativeOrdering(context.getMetaData()); context.getMetaData().setOrdering(ordering); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(orderedFragmentJar.toURI().toURL())); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(orderedFragmentJar)); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getMetaData().setWebInfClassesResources(classes); context.getMetaData().orderFragments(); context.getServletContext().setEffectiveMajorVersion(3); @@ -359,9 +349,9 @@ public void testDiscoveredFalseWithSCIs() throws Exception List scis; context.setConfigurationDiscovered(false); context.setClassLoader(webAppLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web25))); context.getMetaData().setWebInfClassesResources(classes); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getServletContext().setEffectiveMajorVersion(2); context.getServletContext().setEffectiveMinorVersion(5); scis = config.getNonExcludedInitializers(context); @@ -369,8 +359,8 @@ public void testDiscoveredFalseWithSCIs() throws Exception for (ServletContainerInitializer s : scis) { //should not have any of the web-inf lib scis in here - assertFalse(s.getClass().getName().equals("com.acme.ordering.AcmeServletContainerInitializer")); - assertFalse(s.getClass().getName().equals("com.acme.initializer.FooInitializer")); + assertNotEquals("com.acme.ordering.AcmeServletContainerInitializer", s.getClass().getName()); + assertNotEquals("com.acme.initializer.FooInitializer", s.getClass().getName()); //NOTE: should also not have the web-inf classes scis in here either, but due to the //way the test is set up, the sci we're pretending is in web-inf classes will actually //NOT be loaded by the webapp's classloader, but rather by the junit classloader, so @@ -397,14 +387,14 @@ public void testDiscoveredTrueWithSCIs() throws Exception List scis; context.setConfigurationDiscovered(true); context.setClassLoader(webAppLoader); - context.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context.getMetaData().setWebDescriptor(new WebDescriptor(context.getResourceFactory().newResource(web25))); context.getMetaData().setWebInfClassesResources(classes); - context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar.toURI().toURL())); + context.getMetaData().addWebInfResource(ResourceFactory.root().newResource(testSciJar)); context.getServletContext().setEffectiveMajorVersion(2); context.getServletContext().setEffectiveMinorVersion(5); scis = config.getNonExcludedInitializers(context); assertNotNull(scis); - assertEquals(3, scis.size(), () -> scis.toString()); + assertEquals(3, scis.size(), scis::toString); assertEquals("com.acme.ServerServletContainerInitializer", scis.get(0).getClass().getName()); //container path assertEquals("com.acme.webinf.WebInfClassServletContainerInitializer", scis.get(1).getClass().getName()); // web-inf assertEquals("com.acme.initializer.FooInitializer", scis.get(2).getClass().getName()); //web-inf jar no web-fragment diff --git a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java index 6067a029efc1..63f81f467241 100644 --- a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java +++ b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationDecorator.java @@ -13,6 +13,7 @@ package org.eclipse.jetty.ee9.annotations; +import java.nio.file.Files; import java.nio.file.Path; import org.eclipse.jetty.ee9.plus.annotation.LifeCycleCallbackCollection; @@ -21,7 +22,9 @@ import org.eclipse.jetty.ee9.webapp.MetaData; import org.eclipse.jetty.ee9.webapp.WebAppContext; import org.eclipse.jetty.ee9.webapp.WebDescriptor; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.util.DecoratedObjectFactory; +import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; import org.eclipse.jetty.xml.XmlParser; import org.junit.jupiter.api.Test; @@ -34,11 +37,13 @@ public class TestAnnotationDecorator { + public WorkDir workDir; + public class TestWebDescriptor extends WebDescriptor { - public TestWebDescriptor(MetaData.Complete metadata) + public TestWebDescriptor(Resource xml, MetaData.Complete metadata) { - super(ResourceFactory.root().newResource(Path.of("."))); + super(xml); _metaDataComplete = metadata; } @@ -78,6 +83,11 @@ public int getMinorVersion() @Test public void testAnnotationDecorator() throws Exception { + Path docroot = workDir.getEmptyPathDir(); + Path dummyDescriptor = docroot.resolve("dummy.xml"); + Files.createFile(dummyDescriptor); + Resource dummyResource = ResourceFactory.root().newResource(dummyDescriptor); + assertThrows(NullPointerException.class, () -> { new AnnotationDecorator(null); @@ -96,7 +106,7 @@ public void testAnnotationDecorator() throws Exception context.removeAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); //test with BaseHolder metadata, should not introspect with metdata-complete==true - context.getMetaData().setWebDescriptor(new TestWebDescriptor(MetaData.Complete.True)); + context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyResource, MetaData.Complete.True)); assertTrue(context.getMetaData().isMetaDataComplete()); ServletHolder holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, "")); holder.setHeldClass(ServletE.class); @@ -112,7 +122,7 @@ public void testAnnotationDecorator() throws Exception context.removeAttribute(LifeCycleCallbackCollection.LIFECYCLE_CALLBACK_COLLECTION); //test with BaseHolder metadata, should introspect with metadata-complete==false - context.getMetaData().setWebDescriptor(new TestWebDescriptor(MetaData.Complete.False)); + context.getMetaData().setWebDescriptor(new TestWebDescriptor(dummyResource, MetaData.Complete.False)); DecoratedObjectFactory.associateInfo(holder); decorator = new AnnotationDecorator(context); decorator.decorate(servlet); diff --git a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationIntrospector.java b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationIntrospector.java index 9b482c965f79..952ce222d1d5 100644 --- a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationIntrospector.java +++ b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestAnnotationIntrospector.java @@ -13,7 +13,7 @@ package org.eclipse.jetty.ee9.annotations; -import java.io.File; +import java.nio.file.Path; import org.eclipse.jetty.ee9.servlet.ServletHolder; import org.eclipse.jetty.ee9.servlet.Source; @@ -23,7 +23,6 @@ import org.eclipse.jetty.logging.StacklessLogging; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -63,31 +62,33 @@ public void testIsIntrospectable() throws Exception assertTrue(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet can be introspected if web.xml metdata-complete==false - File file = MavenTestingUtils.getTargetFile("test-classes/web31false.xml"); - Resource resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().setWebDescriptor(new WebDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + Path xml = MavenTestingUtils.getTargetPath().resolve("test-classes/web31false.xml"); + Resource xmlResource = wac.getResourceFactory().newResource(xml); + wac.getMetaData().setWebDescriptor(new WebDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertTrue(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet can be introspected if web-fragment.xml medata-complete==false && web.xml metadata-complete==false - file = MavenTestingUtils.getTargetFile("test-classes/web-fragment4false.xml"); - resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().addFragmentDescriptor(ResourceFactory.root().newResource(file.getParentFile().toPath()), new FragmentDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + xml = MavenTestingUtils.getTargetPath().resolve("test-classes/web-fragment4false.xml"); + xmlResource = wac.getResourceFactory().newResource(xml); + Resource parent = wac.getResourceFactory().newResource(xml.getParent()); + wac.getMetaData().addFragmentDescriptor(parent, new FragmentDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertTrue(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet cannot be introspected if web-fragment.xml medata-complete==true (&& web.xml metadata-complete==false) - file = MavenTestingUtils.getTargetFile("test-classes/web-fragment4true.xml"); - resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().addFragmentDescriptor(ResourceFactory.root().newResource(file.getParentFile().toPath()), new FragmentDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + xml = MavenTestingUtils.getTargetPath().resolve("test-classes/web-fragment4true.xml"); + xmlResource = wac.getResourceFactory().newResource(xml); + parent = wac.getResourceFactory().newResource(xml.getParent()); + wac.getMetaData().addFragmentDescriptor(parent, new FragmentDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertFalse(introspector.isIntrospectable(new ServletE(), holder)); //a DESCRIPTOR sourced servlet cannot be introspected if web.xml medata-complete==true - file = MavenTestingUtils.getTargetFile("test-classes/web31true.xml"); - resource = ResourceFactory.root().newResource(file.toPath()); - wac.getMetaData().setWebDescriptor(new WebDescriptor(resource)); - holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, resource.toString())); + xml = MavenTestingUtils.getTargetPath().resolve("test-classes/web31true.xml"); + xmlResource = wac.getResourceFactory().newResource(xml); + wac.getMetaData().setWebDescriptor(new WebDescriptor(xmlResource)); + holder = new ServletHolder(new Source(Source.Origin.DESCRIPTOR, xml.toUri().toASCIIString())); assertFalse(introspector.isIntrospectable(new ServletE(), holder)); } } diff --git a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestRunAsAnnotation.java b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestRunAsAnnotation.java index 0a9c5b918dcb..5668c606b210 100644 --- a/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestRunAsAnnotation.java +++ b/jetty-ee9/jetty-ee9-annotations/src/test/java/org/eclipse/jetty/ee9/annotations/TestRunAsAnnotation.java @@ -13,20 +13,22 @@ package org.eclipse.jetty.ee9.annotations; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import org.eclipse.jetty.ee9.servlet.ServletHolder; import org.eclipse.jetty.ee9.webapp.WebAppContext; import org.eclipse.jetty.ee9.webapp.WebDescriptor; -import org.eclipse.jetty.toolchain.test.MavenTestingUtils; -import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.resource.ResourceFactory; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; public class TestRunAsAnnotation { + public WorkDir workDir; + @Test public void testRunAsAnnotation() throws Exception { @@ -45,8 +47,9 @@ public void testRunAsAnnotation() throws Exception holder2.setHeldClass(ServletC.class); holder2.setInitOrder(1); wac.getServletHandler().addServletWithMapping(holder2, "/foo2/*"); - Resource fakeXml = ResourceFactory.root().newResource(new File(MavenTestingUtils.getTargetTestingDir("run-as"), "fake.xml").toPath()); - wac.getMetaData().setOrigin(holder2.getName() + ".servlet.run-as", new WebDescriptor(fakeXml)); + Path fakeXml = workDir.getEmptyPathDir().resolve("fake.xml"); + Files.createFile(fakeXml); + wac.getMetaData().setOrigin(holder2.getName() + ".servlet.run-as", new WebDescriptor(wac.getResourceFactory().newResource(fakeXml))); AnnotationIntrospector parser = new AnnotationIntrospector(wac); RunAsAnnotationHandler handler = new RunAsAnnotationHandler(wac); @@ -54,6 +57,6 @@ public void testRunAsAnnotation() throws Exception parser.introspect(new ServletC(), null); assertEquals("admin", holder.getRunAsRole()); - assertEquals(null, holder2.getRunAsRole()); + assertNull(holder2.getRunAsRole()); } } diff --git a/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/PlusDescriptorProcessor.java b/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/PlusDescriptorProcessor.java index 930770795b2d..9cc33620e13d 100644 --- a/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/PlusDescriptorProcessor.java +++ b/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/PlusDescriptorProcessor.java @@ -142,7 +142,7 @@ public void visitEnvEntry(WebAppContext context, Descriptor descriptor, XmlParse case WebFragment: { //ServletSpec p.75. No declaration in web.xml, but in multiple web-fragments. Error. - throw new IllegalStateException("Conflicting env-entry " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting env-entry " + name + " in " + descriptor); } default: break; @@ -293,7 +293,7 @@ public void visitResourceRef(WebAppContext context, Descriptor descriptor, XmlPa //ServletSpec p.75. No declaration of resource-ref in web xml, but different in multiple web-fragments. Error. if (!type.equals(otherType) || !auth.equals(otherAuth) || !shared.equals(otherShared)) - throw new IllegalStateException("Conflicting resource-ref " + jndiName + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting resource-ref " + jndiName + " in " + descriptor); //same in multiple web-fragments, merge the injections addInjections(context, descriptor, node, jndiName, TypeUtil.fromName(type)); } @@ -401,7 +401,7 @@ public void visitResourceEnvRef(WebAppContext context, Descriptor descriptor, Xm //ServletSpec p.75. No declaration of resource-ref in web xml, but different in multiple web-fragments. Error. if (!type.equals(otherType)) - throw new IllegalStateException("Conflicting resource-env-ref " + jndiName + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting resource-env-ref " + jndiName + " in " + descriptor); //same in multiple web-fragments, merge the injections addInjections(context, descriptor, node, jndiName, TypeUtil.fromName(type)); @@ -503,7 +503,7 @@ public void visitMessageDestinationRef(WebAppContext context, Descriptor descrip type = (type == null ? "" : type); usage = (usage == null ? "" : usage); if (!type.equals(otherType) || !usage.equalsIgnoreCase(otherUsage)) - throw new IllegalStateException("Conflicting message-destination-ref " + jndiName + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting message-destination-ref " + jndiName + " in " + descriptor); //same in multiple web-fragments, merge the injections addInjections(context, descriptor, node, jndiName, TypeUtil.fromName(type)); diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/Descriptor.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/Descriptor.java index 125ad96c3f50..6ca012526f49 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/Descriptor.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/Descriptor.java @@ -15,8 +15,6 @@ import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.StandardOpenOption; import java.util.Objects; import org.eclipse.jetty.util.resource.Resource; @@ -33,19 +31,20 @@ public abstract class Descriptor protected XmlParser.Node _root; protected String _dtd; - public Descriptor(Resource xml) + public Descriptor(Resource resource) { - _xml = Objects.requireNonNull(xml); + _xml = Objects.requireNonNull(resource, "Resource must not be null"); + if (_xml.isDirectory()) + throw new IllegalArgumentException("Descriptor cannot be a directory"); } public void parse(XmlParser parser) throws Exception { - if (_root == null) { Objects.requireNonNull(parser); - try (InputStream is = Files.newInputStream(_xml.getPath(), StandardOpenOption.READ)) + try (InputStream is = _xml.newInputStream()) { _root = parser.parse(is); _dtd = parser.getDTD(); @@ -58,9 +57,9 @@ public void parse(XmlParser parser) } } - public boolean isParsed() + public String getURI() { - return _root != null; + return _xml.getURI().toASCIIString(); } public Resource getResource() @@ -76,6 +75,6 @@ public XmlParser.Node getRoot() @Override public String toString() { - return this.getClass().getSimpleName() + "(" + _xml + ")"; + return this.getClass().getSimpleName() + "(" + getURI() + ")"; } } diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentConfiguration.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentConfiguration.java index a5330d1aaf55..1db3f12d1eed 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentConfiguration.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentConfiguration.java @@ -24,6 +24,9 @@ */ public class FragmentConfiguration extends AbstractConfiguration { + // Holds a Map . + // key: Resource to the Jar + // value: Resource to the web fragment xml public static final String FRAGMENT_RESOURCES = "org.eclipse.jetty.webFragments"; public FragmentConfiguration() diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentDescriptor.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentDescriptor.java index a6a796665151..217c405c4c34 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentDescriptor.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/FragmentDescriptor.java @@ -36,7 +36,6 @@ public enum OtherType None, Before, After } - ; protected OtherType _otherType = OtherType.None; protected List _befores = new ArrayList(); @@ -44,7 +43,6 @@ public enum OtherType protected String _name; public FragmentDescriptor(Resource xml) - throws Exception { super(xml); } @@ -109,7 +107,7 @@ public void processBefores(XmlParser.Node ordering) if (node.getTag().equalsIgnoreCase("others")) { if (_otherType != OtherType.None) - throw new IllegalStateException("Duplicate clause detected in " + _xml.getURI()); + throw new IllegalStateException("Duplicate clause detected in " + _xml); _otherType = OtherType.Before; } @@ -136,7 +134,7 @@ public void processAfters(XmlParser.Node ordering) if (node.getTag().equalsIgnoreCase("others")) { if (_otherType != OtherType.None) - throw new IllegalStateException("Duplicate clause detected in " + _xml.getURI()); + throw new IllegalStateException("Duplicate clause detected in " + _xml); _otherType = OtherType.After; } diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java index 0fcd0056473e..986d9ee1abbe 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaData.java @@ -314,7 +314,7 @@ public void addFragmentDescriptor(Resource jarResource, FragmentDescriptor descr Descriptor existing = _webFragmentNameMap.get(descriptor.getName()); if (existing != null && !isAllowDuplicateFragmentNames()) { - throw new IllegalStateException("Duplicate fragment name: " + descriptor.getName() + " for " + existing.getResource() + " and " + descriptor.getResource()); + throw new IllegalStateException("Duplicate fragment name: " + descriptor.getName() + " for " + existing.getURI() + " and " + descriptor.getURI()); } else _webFragmentNameMap.put(descriptor.getName(), descriptor); @@ -448,7 +448,7 @@ public void resolve(WebAppContext context) { LOG.debug("metadata resolve {}", context); - // Ensure origins is fresh + //Ensure origins is fresh _origins.clear(); // Set the ordered lib attribute @@ -597,7 +597,7 @@ public FragmentDescriptor getFragmentDescriptor(String name) */ public FragmentDescriptor getFragmentDescriptor(Resource descriptorResource) { - return _webFragmentRoots.stream().filter(d -> d.getResource().equals(descriptorResource)).findFirst().orElse(null); + return _webFragmentRoots.stream().filter(d -> d.getRoot().equals(descriptorResource)).findFirst().orElse(null); } /** diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java index ad433476bf57..760a5a10fbce 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/MetaInfConfiguration.java @@ -250,6 +250,7 @@ protected List getAllContainerJars(final WebAppContext context) } loader = loader.getParent(); } + return uris; } @@ -325,6 +326,7 @@ public void scanJars(final WebAppContext context, Collection jars, boo * @param scanTypes the type of things to look for in the jars * @throws Exception if unable to scan the jars */ + @SuppressWarnings("unchecked") public void scanJars(final WebAppContext context, Collection jars, boolean useCaches, List scanTypes) throws Exception { @@ -374,10 +376,8 @@ public void scanJars(final WebAppContext context, Collection jars, boo * @param context the context for the scan * @param target the target resource to scan for * @param cache the resource cache - * @throws Exception if unable to scan for resources */ public void scanForResources(WebAppContext context, Resource target, ConcurrentHashMap cache) - throws Exception { Resource resourcesDir = null; if (cache != null && cache.containsKey(target)) @@ -404,7 +404,7 @@ else if (LOG.isDebugEnabled()) } else { - //Resource represents a packed jar + // Resource represents a packed jar URI uri = target.getURI(); resourcesDir = _resourceFactory.newResource(URIUtil.uriJarPrefix(uri, "!/META-INF/resources")); } @@ -448,10 +448,8 @@ private static boolean isEmptyResource(Resource resourcesDir) * @param context the context for the scan * @param jar the jar resource to scan for fragements in * @param cache the resource cache - * @throws Exception if unable to scan for fragments */ public void scanForFragment(WebAppContext context, Resource jar, ConcurrentHashMap cache) - throws Exception { Resource webFrag = null; if (cache != null && cache.containsKey(jar)) @@ -543,7 +541,7 @@ public void scanForTlds(WebAppContext context, Resource jar, ConcurrentHashMap(); + tlds = new HashSet<>(); if (jar.isDirectory()) { tlds.addAll(getTlds(jar.getPath())); @@ -613,7 +611,6 @@ public Collection getTlds(Path dir) throws IOException tlds.add(entry.toUri().toURL()); } } - return tlds; } @@ -654,9 +651,7 @@ protected List findClassDirs(WebAppContext context) Resource webInfClasses = findWebInfClassesDir(context); if (webInfClasses != null) classDirs.add(webInfClasses); - List extraClassDirs = findExtraClasspathDirs(context); - if (extraClassDirs != null) - classDirs.addAll(extraClassDirs); + classDirs.addAll(findExtraClasspathDirs(context)); return classDirs; } @@ -692,7 +687,7 @@ protected List findWebInfLibJars(WebAppContext context) throws Exception { Resource webInf = context.getWebInf(); - if (webInf == null || !webInf.exists()) + if (webInf == null || !webInf.exists() || webInf.isDirectory()) return null; Resource webInfLib = webInf.resolve("/lib"); @@ -754,15 +749,14 @@ protected Resource findWebInfClassesDir(WebAppContext context) * * @param context the context to look for extra classpaths in * @return the list of Resources to the extra classpath - * @throws Exception if unable to resolve the extra classpath resources */ protected List findExtraClasspathDirs(WebAppContext context) - throws Exception { if (context == null || context.getExtraClasspath() == null) - return null; + return List.of(); - return context.getExtraClasspath().getResources() + return context.getExtraClasspath() + .getResources() .stream() .filter(Resource::isDirectory) .collect(Collectors.toList()); diff --git a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/StandardDescriptorProcessor.java b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/StandardDescriptorProcessor.java index f4811d71725d..1420fbdb17d5 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/StandardDescriptorProcessor.java +++ b/jetty-ee9/jetty-ee9-webapp/src/main/java/org/eclipse/jetty/ee9/webapp/StandardDescriptorProcessor.java @@ -175,7 +175,7 @@ public void visitContextParam(WebAppContext context, Descriptor descriptor, XmlP if (descriptor instanceof FragmentDescriptor) { if (!((String)context.getInitParams().get(name)).equals(value)) - throw new IllegalStateException("Conflicting context-param " + name + "=" + value + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting context-param " + name + "=" + value + " in " + descriptor); } break; } @@ -205,7 +205,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser //If servlet of that name does not already exist, create it. if (holder == null) { - holder = context.getServletHandler().newServletHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + holder = context.getServletHandler().newServletHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); holder.setName(name); _servletHolderMap.put(name, holder); _servletHolders.add(holder); @@ -249,7 +249,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //previously set by a web-fragment, make sure that the value matches, otherwise its an error if ((descriptor != originDescriptor) && !holder.getInitParameter(pname).equals(pvalue)) - throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor.getResource()); + throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor); break; } default: @@ -305,7 +305,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //the class was set by another fragment, ensure this fragment's value is the same if (!servletClass.equals(holder.getClassName())) - throw new IllegalStateException("Conflicting servlet-class " + servletClass + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting servlet-class " + servletClass + " in " + descriptor); break; } default: @@ -369,7 +369,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //it was already set by another fragment, if we're parsing a fragment, the values must match if (order != holder.getInitOrder()) - throw new IllegalStateException("Conflicting load-on-startup value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting load-on-startup value in " + descriptor); break; } default: @@ -412,7 +412,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser case WebFragment: { if (!holder.getUserRoleLink(roleName).equals(roleLink)) - throw new IllegalStateException("Conflicting role-link for role-name " + roleName + " for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting role-link for role-name " + roleName + " for servlet " + name + " in " + descriptor); break; } default: @@ -458,7 +458,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //run-as was set by another fragment, this fragment must show the same value if (!holder.getRunAsRole().equals(roleName)) - throw new IllegalStateException("Conflicting run-as role " + roleName + " for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting run-as role " + roleName + " for servlet " + name + " in " + descriptor); break; } default: @@ -497,7 +497,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //async-supported set by another fragment, this fragment's value must match if (holder.isAsyncSupported() != val) - throw new IllegalStateException("Conflicting async-supported=" + async + " for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting async-supported=" + async + " for servlet " + name + " in " + descriptor); break; } default: @@ -535,7 +535,7 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser { //was set by another fragment, this fragment's value must match if (holder.isEnabled() != isEnabled) - throw new IllegalStateException("Conflicting value of servlet enabled for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting value of servlet enabled for servlet " + name + " in " + descriptor); break; } default: @@ -588,14 +588,14 @@ public void visitServlet(WebAppContext context, Descriptor descriptor, XmlParser MultipartConfigElement cfg = ((ServletHolder.Registration)holder.getRegistration()).getMultipartConfig(); if (cfg.getMaxFileSize() != element.getMaxFileSize()) - throw new IllegalStateException("Conflicting multipart-config max-file-size for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config max-file-size for servlet " + name + " in " + descriptor); if (cfg.getMaxRequestSize() != element.getMaxRequestSize()) - throw new IllegalStateException("Conflicting multipart-config max-request-size for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config max-request-size for servlet " + name + " in " + descriptor); if (cfg.getFileSizeThreshold() != element.getFileSizeThreshold()) - throw new IllegalStateException("Conflicting multipart-config file-size-threshold for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config file-size-threshold for servlet " + name + " in " + descriptor); if ((cfg.getLocation() != null && (element.getLocation() == null || element.getLocation().length() == 0)) || (cfg.getLocation() == null && (element.getLocation() != null || element.getLocation().length() > 0))) - throw new IllegalStateException("Conflicting multipart-config location for servlet " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting multipart-config location for servlet " + name + " in " + descriptor); break; } default: @@ -741,7 +741,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml { //a web-fragment set the value, all web-fragments must have the same value if (!name.equals(context.getSessionHandler().getSessionCookieConfig().getName())) - throw new IllegalStateException("Conflicting cookie-config name " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting cookie-config name " + name + " in " + descriptor); break; } default: @@ -779,7 +779,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml { //a web-fragment set the value, all web-fragments must have the same value if (!context.getSessionHandler().getSessionCookieConfig().getDomain().equals(domain)) - throw new IllegalStateException("Conflicting cookie-config domain " + domain + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting cookie-config domain " + domain + " in " + descriptor); break; } default: @@ -817,7 +817,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml { //a web-fragment set the value, all web-fragments must have the same value if (!path.equals(context.getSessionHandler().getSessionCookieConfig().getPath())) - throw new IllegalStateException("Conflicting cookie-config path " + path + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting cookie-config path " + path + " in " + descriptor); break; } default: @@ -855,7 +855,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml { //a web-fragment set the value, all web-fragments must have the same value if (!context.getSessionHandler().getSessionCookieConfig().getComment().equals(comment)) - throw new IllegalStateException("Conflicting cookie-config comment " + comment + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting cookie-config comment " + comment + " in " + descriptor); break; } default: @@ -894,7 +894,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml { //a web-fragment set the value, all web-fragments must have the same value if (context.getSessionHandler().getSessionCookieConfig().isHttpOnly() != httpOnly) - throw new IllegalStateException("Conflicting cookie-config http-only " + httpOnly + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting cookie-config http-only " + httpOnly + " in " + descriptor); break; } default: @@ -933,7 +933,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml { //a web-fragment set the value, all web-fragments must have the same value if (context.getSessionHandler().getSessionCookieConfig().isSecure() != secure) - throw new IllegalStateException("Conflicting cookie-config secure " + secure + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting cookie-config secure " + secure + " in " + descriptor); break; } default: @@ -972,7 +972,7 @@ public void visitSessionConfig(WebAppContext context, Descriptor descriptor, Xml { //a web-fragment set the value, all web-fragments must have the same value if (context.getSessionHandler().getSessionCookieConfig().getMaxAge() != maxAge) - throw new IllegalStateException("Conflicting cookie-config max-age " + maxAge + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting cookie-config max-age " + maxAge + " in " + descriptor); break; } default: @@ -1016,7 +1016,7 @@ public void visitMimeMapping(WebAppContext context, Descriptor descriptor, XmlPa { //a web-fragment set the value, all web-fragments must have the same value if (!context.getMimeTypes().getMimeByExtension("." + extension).equals(mimeType)) - throw new IllegalStateException("Conflicting mime-type " + mimeType + " for extension " + extension + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting mime-type " + mimeType + " for extension " + extension + " in " + descriptor); break; } default: @@ -1107,7 +1107,7 @@ public void visitLocaleEncodingList(WebAppContext context, Descriptor descriptor { //a value was set by a web-fragment, all fragments must have the same value if (!encoding.equals(context.getLocaleEncoding(locale))) - throw new IllegalStateException("Conflicting loacle-encoding mapping for locale " + locale + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting locale-encoding mapping for locale " + locale + " in " + descriptor); break; } default: @@ -1172,7 +1172,7 @@ public void visitErrorPage(WebAppContext context, Descriptor descriptor, XmlPars { //another web fragment set the same error code or exception, if its different its an error if (!handler.getErrorPages().get(error).equals(location)) - throw new IllegalStateException("Conflicting error-code or exception-type " + error + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting error-code or exception-type " + error + " in " + descriptor); break; } default: @@ -1196,7 +1196,7 @@ public void addWelcomeFiles(WebAppContext context, XmlParser.Node node, Descript public ServletMapping addServletMapping(String servletName, XmlParser.Node node, WebAppContext context, Descriptor descriptor) { - ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); mapping.setServletName(servletName); mapping.setFromDefaultDescriptor(descriptor instanceof DefaultsDescriptor); context.getMetaData().setOrigin(servletName + ".servlet.mapping." + Long.toHexString(mapping.hashCode()), descriptor); @@ -1439,7 +1439,7 @@ public void visitJspConfig(WebAppContext context, Descriptor descriptor, XmlPars else { //no mapping for jsp yet, make one - ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + ServletMapping mapping = new ServletMapping(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); mapping.setServletName("jsp"); mapping.setPathSpecs(paths.toArray(new String[paths.size()])); _servletMappings.add(mapping); @@ -1597,7 +1597,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //it was already set by another fragment, if we're parsing a fragment, the values must match if (!context.getSecurityHandler().getAuthMethod().equals(method.toString(false, true))) - throw new IllegalStateException("Conflicting auth-method value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting auth-method value in " + descriptor); break; } default: @@ -1633,7 +1633,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //a fragment set it, and we must be parsing another fragment, so the values must match if (!context.getSecurityHandler().getRealmName().equals(nameStr)) - throw new IllegalStateException("Conflicting realm-name value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting realm-name value in " + descriptor); break; } default: @@ -1681,7 +1681,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //a web-fragment previously set it. We must be parsing yet another web-fragment, so the values must agree if (!context.getSecurityHandler().getInitParameter(FormAuthenticator.__FORM_LOGIN_PAGE).equals(loginPageName)) - throw new IllegalStateException("Conflicting form-login-page value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting form-login-page value in " + descriptor); break; } default: @@ -1715,7 +1715,7 @@ public void visitLoginConfig(WebAppContext context, Descriptor descriptor, XmlPa { //a web-fragment previously set it. We must be parsing yet another web-fragment, so the values must agree if (!context.getSecurityHandler().getInitParameter(FormAuthenticator.__FORM_ERROR_PAGE).equals(errorPageName)) - throw new IllegalStateException("Conflicting form-error-page value in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting form-error-page value in " + descriptor); break; } default: @@ -1750,7 +1750,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. FilterHolder holder = _filterHolderMap.get(name); if (holder == null) { - holder = context.getServletHandler().newFilterHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + holder = context.getServletHandler().newFilterHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); holder.setName(name); _filterHolderMap.put(name, holder); _filterHolders.add(holder); @@ -1787,7 +1787,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. { //the filter class was set up by a web fragment, all fragments must be the same if (!holder.getClassName().equals(filterClass)) - throw new IllegalStateException("Conflicting filter-class for filter " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting filter-class for filter " + name + " in " + descriptor); break; } default: @@ -1829,7 +1829,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. { //previously set by a web-fragment, make sure that the value matches, otherwise its an error if (!holder.getInitParameter(pname).equals(pvalue)) - throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor.getResource()); + throw new IllegalStateException("Mismatching init-param " + pname + "=" + pvalue + " in " + descriptor); break; } default: @@ -1869,7 +1869,7 @@ public void visitFilter(WebAppContext context, Descriptor descriptor, XmlParser. { //async-supported set by another fragment, this fragment's value must match if (holder.isAsyncSupported() != val) - throw new IllegalStateException("Conflicting async-supported=" + async + " for filter " + name + " in " + descriptor.getResource()); + throw new IllegalStateException("Conflicting async-supported=" + async + " for filter " + name + " in " + descriptor); break; } default: @@ -1934,7 +1934,7 @@ public void visitListener(WebAppContext context, Descriptor descriptor, XmlParse ((WebDescriptor)descriptor).addClassName(className); - ListenerHolder h = context.getServletHandler().newListenerHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getResource().toString())); + ListenerHolder h = context.getServletHandler().newListenerHolder(new Source(Source.Origin.DESCRIPTOR, descriptor.getURI())); h.setClassName(className); context.getServletHandler().addListener(h); context.getMetaData().setOrigin(className + ".listener", descriptor); @@ -1943,7 +1943,6 @@ public void visitListener(WebAppContext context, Descriptor descriptor, XmlParse catch (Exception e) { LOG.warn("Could not instantiate listener {}", className, e); - return; } } diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/MetaInfConfigurationTest.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/MetaInfConfigurationTest.java index d2c5427dab8f..d1d32513138a 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/MetaInfConfigurationTest.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/MetaInfConfigurationTest.java @@ -13,7 +13,7 @@ package org.eclipse.jetty.ee9.webapp; -import java.io.File; +import java.nio.file.Path; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -22,7 +22,6 @@ import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.util.resource.FileSystemPool; import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -94,16 +93,16 @@ public void afterEach() @Test public void testScanTypes() throws Exception { - File web25 = MavenTestingUtils.getTargetFile("test-classes/web25.xml"); - File web31 = MavenTestingUtils.getTargetFile("test-classes/web31.xml"); - File web31false = MavenTestingUtils.getTargetFile("test-classes/web31false.xml"); + Path web25 = MavenTestingUtils.getTargetPath().resolve("test-classes/web25.xml"); + Path web31 = MavenTestingUtils.getTargetPath().resolve("test-classes/web31.xml"); + Path web31false = MavenTestingUtils.getTargetPath().resolve("test-classes/web31false.xml"); //test a 2.5 webapp will not look for fragments as manually configured MetaInfConfiguration meta25 = new TestableMetaInfConfiguration(MetaInfConfiguration.__allScanTypes, Arrays.asList(MetaInfConfiguration.METAINF_TLDS, MetaInfConfiguration.METAINF_RESOURCES)); WebAppContext context25 = new WebAppContext(); context25.setConfigurationDiscovered(false); - context25.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25.getMetaData().setWebDescriptor(new WebDescriptor(context25.getResourceFactory().newResource(web25))); context25.getServletContext().setEffectiveMajorVersion(2); context25.getServletContext().setEffectiveMinorVersion(5); meta25.preConfigure(context25); @@ -112,7 +111,7 @@ public void testScanTypes() throws Exception MetaInfConfiguration meta25b = new TestableMetaInfConfiguration(MetaInfConfiguration.__allScanTypes, MetaInfConfiguration.__allScanTypes); WebAppContext context25b = new WebAppContext(); - context25b.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web25.toPath()))); + context25b.getMetaData().setWebDescriptor(new WebDescriptor(context25b.getResourceFactory().newResource(web25))); context25b.getServletContext().setEffectiveMajorVersion(2); context25b.getServletContext().setEffectiveMinorVersion(5); meta25b.preConfigure(context25b); @@ -121,7 +120,7 @@ public void testScanTypes() throws Exception MetaInfConfiguration meta31 = new TestableMetaInfConfiguration(MetaInfConfiguration.__allScanTypes, Arrays.asList(MetaInfConfiguration.METAINF_TLDS, MetaInfConfiguration.METAINF_RESOURCES)); WebAppContext context31 = new WebAppContext(); - context31.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31.toPath()))); + context31.getMetaData().setWebDescriptor(new WebDescriptor(context31.getResourceFactory().newResource(web31))); context31.getServletContext().setEffectiveMajorVersion(3); context31.getServletContext().setEffectiveMinorVersion(1); meta31.preConfigure(context31); @@ -131,7 +130,7 @@ public void testScanTypes() throws Exception MetaInfConfiguration.__allScanTypes); WebAppContext context31false = new WebAppContext(); context31false.setConfigurationDiscovered(true); - context31false.getMetaData().setWebDescriptor(new WebDescriptor(ResourceFactory.root().newResource(web31false.toPath()))); + context31false.getMetaData().setWebDescriptor(new WebDescriptor(context31false.getResourceFactory().newResource(web31false))); context31false.getServletContext().setEffectiveMajorVersion(3); context31false.getServletContext().setEffectiveMinorVersion(1); meta31false.preConfigure(context31false); @@ -162,6 +161,7 @@ public void testFindAndFilterContainerPathsJDK9() throws Exception context.setClassLoader(loader); config.findAndFilterContainerPaths(context); List containerResources = context.getMetaData().getContainerResources(); + assertEquals(2, containerResources.size()); for (Resource r : containerResources) { diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/OrderingTest.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/OrderingTest.java index b88fdc5479c7..5e3ba31652c3 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/OrderingTest.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/OrderingTest.java @@ -14,15 +14,15 @@ package org.eclipse.jetty.ee9.webapp; import java.io.IOException; -import java.io.InputStream; -import java.net.URI; -import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.util.resource.FileSystemPool; import org.eclipse.jetty.util.resource.Resource; +import org.eclipse.jetty.util.resource.ResourceFactory; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -39,85 +39,29 @@ */ public class OrderingTest { - public class TestResource extends Resource - { - public String _name; - - public TestResource(String name) - { - _name = name; - } - - @Override - public Resource resolve(String subUriPath) - { - return null; - } - - @Override - public boolean exists() - { - return false; - } - - @Override - public Path getPath() - { - return null; - } - - @Override - public InputStream newInputStream() throws IOException - { - return null; - } - - @Override - public ReadableByteChannel newReadableByteChannel() throws IOException - { - return null; - } + WorkDir workDir; - @Override - public String getName() - { - return _name; - } - - @Override - public String getFileName() - { - return null; - } - - @Override - public URI getURI() - { - return null; - } - - @Override - public boolean isContainedIn(Resource r) - { - return false; - } - - @Override - public boolean isDirectory() - { - return false; - } + private Resource newTestableDirResource(String name) throws IOException + { + Path dir = workDir.getPath().resolve(name); + if (!Files.exists(dir)) + Files.createDirectories(dir); + return ResourceFactory.root().newResource(dir); + } - @Override - public long length() - { - return 0; - } + private Resource newTestableFileResource(String name) throws IOException + { + Path file = workDir.getPath().resolve(name); + if (!Files.exists(file)) + Files.createFile(file); + return ResourceFactory.root().newResource(file); } @BeforeEach public void beforeEach() { + // ensure work dir exists, and is empty + workDir.getEmptyPathDir(); assertThat(FileSystemPool.INSTANCE.mounts(), empty()); } @@ -133,13 +77,13 @@ public void testRelativeOrdering0() { //Example from ServletSpec p.70 MetaData metaData = new MetaData(); - List resources = new ArrayList(); + List resources = new ArrayList<>(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, after C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -149,9 +93,9 @@ public void testRelativeOrdering0() f1._afters.add("C"); //B: before others - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -160,9 +104,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addBeforeOthers(r2); //C: after others - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -171,9 +115,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addAfterOthers(r3); //D: no ordering - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); @@ -182,9 +126,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addNoOthers(r4); //E: no ordering - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); @@ -193,9 +137,9 @@ public void testRelativeOrdering0() //((RelativeOrdering)metaData._ordering).addNoOthers(r5); //F: before others, before B - TestResource jar6 = new TestResource("F"); + Resource jar6 = newTestableDirResource("F"); resources.add(jar6); - TestResource r6 = new TestResource("F/web-fragment.xml"); + Resource r6 = newTestableFileResource("F/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = "F"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -210,13 +154,13 @@ public void testRelativeOrdering0() String[] outcomes = {"FBDECA"}; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -224,15 +168,15 @@ public void testRelativeOrdering0() public void testRelativeOrdering1() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //Example from ServletSpec p.70-71 //No name: after others, before C - TestResource jar1 = new TestResource("plain"); + Resource jar1 = newTestableDirResource("plain"); resources.add(jar1); - TestResource r1 = new TestResource("plain/web-fragment.xml"); + Resource r1 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -242,9 +186,9 @@ public void testRelativeOrdering1() f1._befores.add("C"); //B: before others - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -253,9 +197,9 @@ public void testRelativeOrdering1() //((RelativeOrdering)metaData._ordering).addBeforeOthers(f2); //C: no ordering - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -264,9 +208,9 @@ public void testRelativeOrdering1() f3._otherType = FragmentDescriptor.OtherType.None; //D: after others - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); @@ -275,9 +219,9 @@ public void testRelativeOrdering1() f4._otherType = FragmentDescriptor.OtherType.After; //E: before others - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); @@ -286,9 +230,9 @@ public void testRelativeOrdering1() f5._otherType = FragmentDescriptor.OtherType.Before; //F: no ordering - TestResource jar6 = new TestResource("F"); + Resource jar6 = newTestableDirResource("F"); resources.add(jar6); - TestResource r6 = new TestResource("F/web-fragment.xml"); + Resource r6 = newTestableFileResource("F/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = "F"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -313,13 +257,13 @@ public void testRelativeOrdering1() "EBFDplainC" }; - String orderedNames = ""; + StringBuilder orderedNames = new StringBuilder(); for (Resource r : orderedList) { - orderedNames += (((TestResource)r)._name); + orderedNames.append(r.getFileName()); } - if (!checkResult(orderedNames, outcomes)) + if (!checkResult(orderedNames.toString(), outcomes)) fail("No outcome matched " + orderedNames); } @@ -327,16 +271,16 @@ public void testRelativeOrdering1() public void testRelativeOrdering2() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //Example from Spec p. 71-72 //A: after B - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -346,9 +290,9 @@ public void testRelativeOrdering2() f1._afters.add("B"); //B: no order - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -357,9 +301,9 @@ public void testRelativeOrdering2() f2._otherType = FragmentDescriptor.OtherType.None; //C: before others - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -368,9 +312,9 @@ public void testRelativeOrdering2() f3._otherType = FragmentDescriptor.OtherType.Before; //D: no order - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); @@ -390,13 +334,13 @@ public void testRelativeOrdering2() }; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -404,14 +348,14 @@ public void testRelativeOrdering2() public void testRelativeOrdering3() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, before C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -421,9 +365,9 @@ public void testRelativeOrdering3() f1._befores.add("C"); //B: before others, before C - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -433,9 +377,9 @@ public void testRelativeOrdering3() f2._befores.add("C"); //C: no ordering - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -447,13 +391,13 @@ public void testRelativeOrdering3() String[] outcomes = {"BAC"}; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -461,7 +405,7 @@ public void testRelativeOrdering3() public void testOrderFragments() throws Exception { final MetaData metadata = new MetaData(); - final Resource jarResource = new TestResource("A"); + final Resource jarResource = newTestableDirResource("A"); metadata.setOrdering(new RelativeOrdering(metadata)); metadata.addWebInfResource(jarResource); @@ -478,14 +422,14 @@ public void testCircular1() //A: after B //B: after A - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after B - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -495,9 +439,9 @@ public void testCircular1() f1._afters.add("B"); //B: after A - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -517,14 +461,14 @@ public void testCircular1() public void testInvalid1() throws Exception { - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, before C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -534,9 +478,9 @@ public void testInvalid1() f1._befores.add("C"); //B: before others, after C - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -546,9 +490,9 @@ public void testInvalid1() f2._afters.add("C"); //C: no ordering - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -559,10 +503,10 @@ public void testInvalid1() assertThrows(IllegalStateException.class, () -> { List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } System.err.println("Invalid Result = " + result); fail("A and B have an impossible relationship to C"); @@ -576,7 +520,7 @@ public void testAbsoluteOrdering1() // // A,B,C,others // - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); ((AbsoluteOrdering)metaData._ordering).add("A"); @@ -584,49 +528,49 @@ public void testAbsoluteOrdering1() ((AbsoluteOrdering)metaData._ordering).add("C"); ((AbsoluteOrdering)metaData._ordering).addOthers(); - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); metaData._webFragmentResourceMap.put(jar1, f1); - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); metaData._webFragmentResourceMap.put(jar2, f2); - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); metaData._webFragmentResourceMap.put(jar3, f3); - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); metaData._webFragmentResourceMap.put(jar4, f4); - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); metaData._webFragmentResourceMap.put(jar5, f5); - TestResource jar6 = new TestResource("plain"); + Resource jar6 = newTestableDirResource("plain"); resources.add(jar6); - TestResource r6 = new TestResource("plain/web-fragment.xml"); + Resource r6 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -635,13 +579,13 @@ public void testAbsoluteOrdering1() List list = metaData._ordering.order(resources); String[] outcomes = {"ABCDEplain"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : list) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -650,7 +594,7 @@ public void testAbsoluteOrdering2() throws Exception { // C,B,A - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); @@ -658,49 +602,49 @@ public void testAbsoluteOrdering2() ((AbsoluteOrdering)metaData._ordering).add("B"); ((AbsoluteOrdering)metaData._ordering).add("A"); - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); metaData._webFragmentResourceMap.put(jar1, f1); - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); metaData._webFragmentResourceMap.put(jar2, f2); - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); metaData._webFragmentResourceMap.put(jar3, f3); - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); metaData._webFragmentResourceMap.put(jar4, f4); - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); metaData._webFragmentResourceMap.put(jar5, f5); - TestResource jar6 = new TestResource("plain"); + Resource jar6 = newTestableDirResource("plain"); resources.add(jar6); - TestResource r6 = new TestResource("plain/web-fragment.xml"); + Resource r6 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f6._name, f6); @@ -708,13 +652,13 @@ public void testAbsoluteOrdering2() List list = metaData._ordering.order(resources); String[] outcomes = {"CBA"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : list) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -726,10 +670,10 @@ public void testAbsoluteOrdering3() MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); - List resources = new ArrayList(); + List resources = new ArrayList<>(); - resources.add(new TestResource("A")); - resources.add(new TestResource("B")); + resources.add(newTestableDirResource("A")); + resources.add(newTestableDirResource("B")); List list = metaData._ordering.order(resources); assertThat(list, is(empty())); @@ -740,14 +684,14 @@ public void testRelativeOrderingWithPlainJars() throws Exception { //B,A,C other jars with no fragments - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A: after others, before C - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -757,9 +701,9 @@ public void testRelativeOrderingWithPlainJars() f1._befores.add("C"); //B: before others, before C - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); @@ -769,9 +713,9 @@ public void testRelativeOrderingWithPlainJars() f2._befores.add("C"); //C: after A - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); @@ -781,24 +725,24 @@ public void testRelativeOrderingWithPlainJars() f3._afters.add("A"); //No fragment jar 1 - TestResource r4 = new TestResource("plain1"); + Resource r4 = newTestableFileResource("plain1"); resources.add(r4); //No fragment jar 2 - TestResource r5 = new TestResource("plain2"); + Resource r5 = newTestableFileResource("plain2"); resources.add(r5); //result: BAC String[] outcomes = {"Bplain1plain2AC"}; List orderedList = metaData._ordering.order(resources); - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -807,14 +751,14 @@ public void testRelativeOrderingWithPlainJars2() throws Exception { //web.xml has no ordering, jar A has fragment after others, jar B is plain, jar C is plain - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new RelativeOrdering(metaData); //A has after others - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); @@ -822,22 +766,22 @@ public void testRelativeOrderingWithPlainJars2() f1._otherType = FragmentDescriptor.OtherType.After; //No fragment jar B - TestResource r4 = new TestResource("plainB"); + Resource r4 = newTestableFileResource("plainB"); resources.add(r4); //No fragment jar C - TestResource r5 = new TestResource("plainC"); + Resource r5 = newTestableFileResource("plainC"); resources.add(r5); List orderedList = metaData._ordering.order(resources); String[] outcomes = {"plainBplainCA"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : orderedList) { - result += (((TestResource)r)._name); + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } @@ -848,7 +792,7 @@ public void testAbsoluteOrderingWithPlainJars() // // A,B,C,others // - List resources = new ArrayList(); + List resources = new ArrayList<>(); MetaData metaData = new MetaData(); metaData._ordering = new AbsoluteOrdering(metaData); ((AbsoluteOrdering)metaData._ordering).add("A"); @@ -856,82 +800,81 @@ public void testAbsoluteOrderingWithPlainJars() ((AbsoluteOrdering)metaData._ordering).add("C"); ((AbsoluteOrdering)metaData._ordering).addOthers(); - TestResource jar1 = new TestResource("A"); + Resource jar1 = newTestableDirResource("A"); resources.add(jar1); - TestResource r1 = new TestResource("A/web-fragment.xml"); + Resource r1 = newTestableFileResource("A/web-fragment.xml"); FragmentDescriptor f1 = new FragmentDescriptor(r1); f1._name = "A"; metaData._webFragmentNameMap.put(f1._name, f1); metaData._webFragmentResourceMap.put(jar1, f1); - TestResource jar2 = new TestResource("B"); + Resource jar2 = newTestableDirResource("B"); resources.add(jar2); - TestResource r2 = new TestResource("B/web-fragment.xml"); + Resource r2 = newTestableFileResource("B/web-fragment.xml"); FragmentDescriptor f2 = new FragmentDescriptor(r2); f2._name = "B"; metaData._webFragmentNameMap.put(f2._name, f2); metaData._webFragmentResourceMap.put(jar2, f2); - TestResource jar3 = new TestResource("C"); + Resource jar3 = newTestableDirResource("C"); resources.add(jar3); - TestResource r3 = new TestResource("C/web-fragment.xml"); + Resource r3 = newTestableFileResource("C/web-fragment.xml"); FragmentDescriptor f3 = new FragmentDescriptor(r3); f3._name = "C"; metaData._webFragmentNameMap.put(f3._name, f3); metaData._webFragmentResourceMap.put(jar3, f3); - TestResource jar4 = new TestResource("D"); + Resource jar4 = newTestableDirResource("D"); resources.add(jar4); - TestResource r4 = new TestResource("D/web-fragment.xml"); + Resource r4 = newTestableFileResource("D/web-fragment.xml"); FragmentDescriptor f4 = new FragmentDescriptor(r4); f4._name = "D"; metaData._webFragmentNameMap.put(f4._name, f4); metaData._webFragmentResourceMap.put(jar4, f4); - TestResource jar5 = new TestResource("E"); + Resource jar5 = newTestableDirResource("E"); resources.add(jar5); - TestResource r5 = new TestResource("E/web-fragment.xml"); + Resource r5 = newTestableFileResource("E/web-fragment.xml"); FragmentDescriptor f5 = new FragmentDescriptor(r5); f5._name = "E"; metaData._webFragmentNameMap.put(f5._name, f5); metaData._webFragmentResourceMap.put(jar5, f5); - TestResource jar6 = new TestResource("plain"); + Resource jar6 = newTestableDirResource("plain"); resources.add(jar6); - TestResource r6 = new TestResource("plain/web-fragment.xml"); + Resource r6 = newTestableFileResource("plain/web-fragment.xml"); FragmentDescriptor f6 = new FragmentDescriptor(r6); f6._name = FragmentDescriptor.NAMELESS + "1"; metaData._webFragmentNameMap.put(f6._name, f6); metaData._webFragmentResourceMap.put(jar6, f6); //plain jar - TestResource r7 = new TestResource("plain1"); + Resource r7 = newTestableFileResource("plain1"); resources.add(r7); - TestResource r8 = new TestResource("plain2"); + Resource r8 = newTestableFileResource("plain2"); resources.add(r8); List list = metaData._ordering.order(resources); String[] outcomes = {"ABCDEplainplain1plain2"}; - String result = ""; + StringBuilder result = new StringBuilder(); for (Resource r : list) { - result += ((TestResource)r)._name; + result.append(r.getFileName()); } - if (!checkResult(result, outcomes)) + if (!checkResult(result.toString(), outcomes)) fail("No outcome matched " + result); } public boolean checkResult(String result, String[] outcomes) { - boolean matched = false; for (String s : outcomes) { if (s.equals(result)) - matched = true; + return true; } - return matched; + return false; } } diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebDescriptorTest.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebDescriptorTest.java index 846539efa662..920d686c69ef 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebDescriptorTest.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebDescriptorTest.java @@ -17,11 +17,14 @@ import java.nio.file.Files; import java.nio.file.Path; +import org.eclipse.jetty.ee9.nested.ContextHandler; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; +import org.eclipse.jetty.util.component.Environment; import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.ResourceFactory; import org.eclipse.jetty.xml.XmlParser; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -36,17 +39,13 @@ public class WebDescriptorTest @Test public void testXmlWithXsd() throws Exception { + // TODO: need to address ee8 issues with missing jsp-configType from the that seems to be a missing resource + // org.xml.sax.SAXParseException; systemId: jar:file:///path/to/jetty-servlet-api-4.0.6.jar!/javax/servlet/resources/web-common_4_0.xsd; lineNumber: 142; columnNumber: 50; + // src-resolve: Cannot resolve the name 'javaee:jsp-configType' to a(n) 'type definition' component. + Assumptions.assumeTrue(ContextHandler.ENVIRONMENT.getName().endsWith("9")); + Path xml = workDir.getEmptyPathDir().resolve("test.xml"); - Files.writeString(xml, """ - - - Empty WebApp Descriptor - - """, StandardCharsets.UTF_8); + Files.writeString(xml, getWebAppXml(ContextHandler.ENVIRONMENT), StandardCharsets.UTF_8); Resource xmlRes = ResourceFactory.root().newResource(xml); WebDescriptor webDescriptor = new WebDescriptor(xmlRes); @@ -55,4 +54,29 @@ public void testXmlWithXsd() throws Exception // Such as missing required XML resource entities. webDescriptor.parse(xmlParser); } + + private String getWebAppXml(Environment env) + { + String namespace = "https://jakarta.ee/xml/ns/jakartaee"; + String schemaLocation = "https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"; + String version = "5.0"; + + if (env.getName().equals("ee8")) + { + namespace = "http://xmlns.jcp.org/xml/ns/javaee"; + schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"; + version = "4.0"; + } + + return """ + + + Empty WebApp Descriptor + + """.formatted(namespace, schemaLocation, version); + } }