From a99a832206d52b23bd6c905473e463ee9a1bd691 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 12 Aug 2020 11:26:50 -0500 Subject: [PATCH] Issue #5137 - Cleanup of WebAppContextTest + Cleanup of test-servlet-spec webapp + Removing unused methods + Commented System.err.println moved to java.util.logging + General checkstyle cleanup (empty lines, spacing, etc) + Eliminate bogus WebApp LIB ordering tests. + No ClassLoader has a order guarantee. + Moving HttpSessionListenerTest contents to existing SessionListenerTest + Cleanup of SessionListenerTest with eye to resource cleanup + Removing ServletContextListenerTest as it duplicates tests + Restoring WebAppClassLoaderTest test method + `ordering()` is now `testClashingResource()` and properly tests assumptions when dealing with a ClassLoader. The old test assumed guaranteed order from a ClassLoader, which is not a feature of any Java ClassLoader. Especially so for URLClassLoader. + Restoring clashing resource test Signed-off-by: Joakim Erdfelt --- jetty-webapp/pom.xml | 7 + .../jetty/webapp/WebAppClassLoaderTest.java | 2 +- .../WebAppContextExtraClasspathTest.java | 216 --------- .../jetty/webapp/WebAppContextTest.java | 435 +++++++++--------- .../server/session/SessionListenerTest.java | 172 ++++--- .../java/com/acme/test/AnnotatedListener.java | 52 +-- .../com/acme/test/AsyncListenerServlet.java | 8 - .../src/main/java/com/acme/test/Bar.java | 1 - .../com/acme/test/ClassLoaderServlet.java | 1 - .../com/acme/test/RoleAnnotationTest.java | 1 - .../java/com/acme/test/SecuredServlet.java | 4 +- .../main/java/com/acme/test/TestListener.java | 53 +-- 12 files changed, 392 insertions(+), 560 deletions(-) delete mode 100644 jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextExtraClasspathTest.java diff --git a/jetty-webapp/pom.xml b/jetty-webapp/pom.xml index 10ccfdce33a..290755183e0 100644 --- a/jetty-webapp/pom.xml +++ b/jetty-webapp/pom.xml @@ -69,6 +69,13 @@ ${project.version} test + + org.eclipse.jetty + jetty-http + ${project.version} + tests + test + org.eclipse.jetty.toolchain jetty-test-helper diff --git a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java index fe3d08b6f4b..fd3031173c8 100644 --- a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java +++ b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppClassLoaderTest.java @@ -342,7 +342,7 @@ public class WebAppClassLoaderTest } @Test - public void ordering() throws Exception + public void testClashingResource() throws Exception { // The existence of a URLStreamHandler changes the behavior assumeTrue(URLStreamHandlerUtil.getFactory() == null, "URLStreamHandler changes behavior, skip test"); diff --git a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextExtraClasspathTest.java b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextExtraClasspathTest.java deleted file mode 100644 index 93167f07a87..00000000000 --- a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextExtraClasspathTest.java +++ /dev/null @@ -1,216 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.webapp; - -import java.io.File; -import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.ServerConnector; -import org.eclipse.jetty.toolchain.test.MavenTestingUtils; -import org.eclipse.jetty.util.component.LifeCycle; -import org.eclipse.jetty.util.resource.PathResource; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class WebAppContextExtraClasspathTest -{ - private Server server; - - private Server newServer() - { - server = new Server(); - ServerConnector connector = new ServerConnector(server); - connector.setPort(0); - server.addConnector(connector); - return server; - } - - @AfterEach - public void tearDown() - { - LifeCycle.stop(server); - } - - @Test - public void testBaseResourceAbsolutePath() throws Exception - { - Server server = newServer(); - - WebAppContext context = new WebAppContext(); - context.setContextPath("/"); - - Path warPath = MavenTestingUtils.getTestResourcePathFile("wars/dump.war"); - warPath = warPath.toAbsolutePath(); - assertTrue(warPath.isAbsolute(), "Path should be absolute: " + warPath); - // Use String reference to war - // On Unix / Linux this should have no issue. - // On Windows with fully qualified paths such as "E:\mybase\webapps\dump.war" the - // resolution of the Resource can trigger various URI issues with the "E:" portion of the provided String. - context.setResourceBase(warPath.toString()); - - server.setHandler(context); - server.start(); - - assertTrue(context.isAvailable(), "WebAppContext should be available"); - } - - public static Stream extraClasspathGlob() - { - List references = new ArrayList<>(); - - Path extLibs = MavenTestingUtils.getTestResourcePathDir("ext"); - extLibs = extLibs.toAbsolutePath(); - - // Absolute reference with trailing slash and glob - references.add(Arguments.of(extLibs.toString() + File.separator + "*")); - - // Establish a relative extraClassPath reference - String relativeExtLibsDir = MavenTestingUtils.getBasePath().relativize(extLibs).toString(); - - // This will be in the String form similar to "src/test/resources/ext/*" (with trailing slash and glob) - references.add(Arguments.of(relativeExtLibsDir + File.separator + "*")); - - return references.stream(); - } - - /** - * Test using WebAppContext.setExtraClassPath(String) with a reference to a glob - */ - @ParameterizedTest - @MethodSource("extraClasspathGlob") - public void testExtraClasspathGlob(String extraClasspathGlobReference) throws Exception - { - Server server = newServer(); - - WebAppContext context = new WebAppContext(); - context.setContextPath("/"); - Path warPath = MavenTestingUtils.getTestResourcePathFile("wars/dump.war"); - context.setBaseResource(new PathResource(warPath)); - context.setExtraClasspath(extraClasspathGlobReference); - - server.setHandler(context); - server.start(); - - // Should not have failed the start of the WebAppContext - assertTrue(context.isAvailable(), "WebAppContext should be available"); - - // Test WebAppClassLoader contents for expected jars - ClassLoader contextClassLoader = context.getClassLoader(); - assertThat(contextClassLoader, instanceOf(WebAppClassLoader.class)); - WebAppClassLoader webAppClassLoader = (WebAppClassLoader)contextClassLoader; - Path extLibsDir = MavenTestingUtils.getTestResourcePathDir("ext"); - extLibsDir = extLibsDir.toAbsolutePath(); - List expectedPaths = Files.list(extLibsDir) - .filter(Files::isRegularFile) - .filter((path) -> path.toString().endsWith(".jar")) - .collect(Collectors.toList()); - List actualPaths = new ArrayList<>(); - for (URL url : webAppClassLoader.getURLs()) - { - actualPaths.add(Paths.get(url.toURI())); - } - assertThat("WebAppClassLoader.urls.length", actualPaths.size(), is(expectedPaths.size())); - for (Path expectedPath : expectedPaths) - { - boolean found = false; - for (Path actualPath : actualPaths) - { - if (Files.isSameFile(actualPath, expectedPath)) - { - found = true; - } - } - assertTrue(found, "Not able to find expected jar in WebAppClassLoader: " + expectedPath); - } - } - - public static Stream extraClasspathDir() - { - List references = new ArrayList<>(); - - Path extLibs = MavenTestingUtils.getTestResourcePathDir("ext"); - extLibs = extLibs.toAbsolutePath(); - - // Absolute reference with trailing slash - references.add(Arguments.of(extLibs.toString() + File.separator)); - - // Absolute reference without trailing slash - references.add(Arguments.of(extLibs.toString())); - - // Establish a relative extraClassPath reference - String relativeExtLibsDir = MavenTestingUtils.getBasePath().relativize(extLibs).toString(); - - // This will be in the String form similar to "src/test/resources/ext/" (with trailing slash) - references.add(Arguments.of(relativeExtLibsDir + File.separator)); - - // This will be in the String form similar to "src/test/resources/ext/" (without trailing slash) - references.add(Arguments.of(relativeExtLibsDir)); - - return references.stream(); - } - - /** - * Test using WebAppContext.setExtraClassPath(String) with a reference to a directory - */ - @ParameterizedTest - @MethodSource("extraClasspathDir") - public void testExtraClasspathDir(String extraClassPathReference) throws Exception - { - Server server = newServer(); - - WebAppContext context = new WebAppContext(); - context.setContextPath("/"); - Path warPath = MavenTestingUtils.getTestResourcePathFile("wars/dump.war"); - context.setBaseResource(new PathResource(warPath)); - - context.setExtraClasspath(extraClassPathReference); - - server.setHandler(context); - server.start(); - - // Should not have failed the start of the WebAppContext - assertTrue(context.isAvailable(), "WebAppContext should be available"); - - // Test WebAppClassLoader contents for expected directory reference - ClassLoader contextClassLoader = context.getClassLoader(); - assertThat(contextClassLoader, instanceOf(WebAppClassLoader.class)); - WebAppClassLoader webAppClassLoader = (WebAppClassLoader)contextClassLoader; - URL[] urls = webAppClassLoader.getURLs(); - assertThat("URLs", urls.length, is(1)); - Path extLibs = MavenTestingUtils.getTestResourcePathDir("ext"); - extLibs = extLibs.toAbsolutePath(); - assertThat("URL[0]", urls[0].toURI(), is(extLibs.toUri())); - } -} diff --git a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextTest.java b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextTest.java index 5b205ecc21a..b6b0447e1ee 100644 --- a/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextTest.java +++ b/jetty-webapp/src/test/java/org/eclipse/jetty/webapp/WebAppContextTest.java @@ -19,94 +19,89 @@ package org.eclipse.jetty.webapp; import java.io.File; -import java.io.IOException; +import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import javax.servlet.GenericServlet; import javax.servlet.ServletContext; -import javax.servlet.ServletContextEvent; -import javax.servlet.ServletContextListener; -import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; -import javax.servlet.http.HttpSessionEvent; -import javax.servlet.http.HttpSessionListener; +import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.http.HttpTester; import org.eclipse.jetty.server.LocalConnector; import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.HandlerList; -import org.eclipse.jetty.server.handler.HotSwapHandler; import org.eclipse.jetty.servlet.ErrorPageErrorHandler; import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.toolchain.test.FS; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; +import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.resource.PathResource; import org.eclipse.jetty.util.resource.Resource; -import org.eclipse.jetty.util.resource.ResourceCollection; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +@ExtendWith(WorkDirExtension.class) public class WebAppContextTest { - public class MySessionListener implements HttpSessionListener + public static final Logger LOG = Log.getLogger(WebAppContextTest.class); + public WorkDir workDir; + private final List lifeCycles = new ArrayList<>(); + + @AfterEach + public void tearDown() { - - @Override - public void sessionCreated(HttpSessionEvent se) - { - // TODO Auto-generated method stub - - } - - @Override - public void sessionDestroyed(HttpSessionEvent se) - { - // TODO Auto-generated method stub - - } + lifeCycles.forEach(LifeCycle::stop); } - @Test - public void testSessionListeners() - throws Exception + private Server newServer() { Server server = new Server(); - - WebAppContext wac = new WebAppContext(); - - wac.setServer(server); - server.setHandler(wac); - wac.addEventListener(new MySessionListener()); - - Collection listeners = wac.getSessionHandler().getBeans(org.eclipse.jetty.webapp.WebAppContextTest.MySessionListener.class); - assertNotNull(listeners); - assertEquals(1, listeners.size()); + ServerConnector connector = new ServerConnector(server); + connector.setPort(0); + server.addConnector(connector); + lifeCycles.add(server); + return server; } @Test public void testConfigurationClassesFromDefault() { - Server server = new Server(); - //test if no classnames set, its the defaults + Server server = newServer(); + // test if no classnames set, its the defaults WebAppContext wac = new WebAppContext(); assertEquals(0, wac.getConfigurations().length); String[] classNames = wac.getConfigurationClasses(); assertNotNull(classNames); - //test if no classname set, and none from server its the defaults + // test if no classname set, and none from server its the defaults wac.setServer(server); assertTrue(Arrays.equals(classNames, wac.getConfigurationClasses())); } @@ -116,7 +111,7 @@ public class WebAppContextTest { String[] classNames = {"x.y.z"}; - Server server = new Server(); + Server server = newServer(); server.setAttribute(Configuration.ATTR, classNames); //test an explicitly set classnames list overrides that from the server @@ -136,7 +131,7 @@ public class WebAppContextTest } catch (Exception e) { - Log.getRootLogger().ignore(e); + LOG.ignore(e); } assertTrue(Arrays.equals(classNames, wac2.getConfigurationClasses())); } @@ -151,7 +146,7 @@ public class WebAppContextTest //test that explicit config instances override any from server String[] classNames = {"x.y.z"}; - Server server = new Server(); + Server server = newServer(); server.setAttribute(Configuration.ATTR, classNames); wac.setServer(server); assertTrue(Arrays.equals(configs, wac.getConfigurations())); @@ -160,7 +155,7 @@ public class WebAppContextTest @Test public void testRealPathDoesNotExist() throws Exception { - Server server = new Server(0); + Server server = newServer(); WebAppContext context = new WebAppContext(".", "/"); server.setHandler(context); server.start(); @@ -178,15 +173,31 @@ public class WebAppContextTest @Test public void testContextWhiteList() throws Exception { - Server server = new Server(0); + Server server = newServer(); HandlerList handlers = new HandlerList(); WebAppContext contextA = new WebAppContext(".", "/A"); - contextA.addServlet(ServletA.class, "/s"); + ServletHolder servletAHolder = new ServletHolder(new GenericServlet() + { + @Override + public void service(ServletRequest req, ServletResponse res) + { + this.getServletContext().getContext("/A/s"); + } + }); + contextA.addServlet(servletAHolder, "/s"); handlers.addHandler(contextA); WebAppContext contextB = new WebAppContext(".", "/B"); - contextB.addServlet(ServletB.class, "/s"); + ServletHolder servletBHolder = new ServletHolder(new GenericServlet() + { + @Override + public void service(ServletRequest req, ServletResponse res) + { + this.getServletContext().getContext("/B/s"); + } + }); + contextB.addServlet(servletBHolder, "/s"); contextB.setContextWhiteList(new String[]{"/doesnotexist", "/B/s"}); handlers.addHandler(contextB); @@ -205,22 +216,20 @@ public class WebAppContextTest @Test public void testAlias() throws Exception { - File dir = File.createTempFile("dir", null); - dir.delete(); - dir.mkdir(); - dir.deleteOnExit(); + Path tempDir = workDir.getEmptyPathDir().resolve("dir"); + FS.ensureEmpty(tempDir); - File webinf = new File(dir, "WEB-INF"); - webinf.mkdir(); + Path webinf = tempDir.resolve("WEB-INF"); + FS.ensureEmpty(webinf); - File classes = new File(dir, "classes"); - classes.mkdir(); + Path classes = tempDir.resolve("classes"); + FS.ensureEmpty(classes); - File someclass = new File(classes, "SomeClass.class"); - someclass.createNewFile(); + Path someClass = classes.resolve("SomeClass.class"); + FS.touch(someClass); WebAppContext context = new WebAppContext(); - context.setBaseResource(new ResourceCollection(dir.getAbsolutePath())); + context.setBaseResource(new PathResource(tempDir)); context.setResourceAlias("/WEB-INF/classes/", "/classes/"); @@ -229,9 +238,10 @@ public class WebAppContextTest } @Test - public void testIsProtected() throws Exception + public void testIsProtected() { WebAppContext context = new WebAppContext(); + assertTrue(context.isProtectedTarget("/web-inf/lib/foo.jar")); assertTrue(context.isProtectedTarget("/meta-inf/readme.txt")); assertFalse(context.isProtectedTarget("/something-else/web-inf")); @@ -240,11 +250,13 @@ public class WebAppContextTest @Test public void testNullPath() throws Exception { - Server server = new Server(0); + Server server = newServer(); + HandlerList handlers = new HandlerList(); ContextHandlerCollection contexts = new ContextHandlerCollection(); WebAppContext context = new WebAppContext(); - context.setBaseResource(Resource.newResource("./src/test/webapp")); + Path testWebapp = MavenTestingUtils.getProjectDirPath("src/test/webapp"); + context.setBaseResource(new PathResource(testWebapp)); context.setContextPath("/"); server.setHandler(handlers); handlers.addHandler(contexts); @@ -254,27 +266,25 @@ public class WebAppContextTest server.addConnector(connector); server.start(); - try - { - String response = connector.getResponse("GET http://localhost:8080 HTTP/1.1\r\nHost: localhost:8080\r\nConnection: close\r\n\r\n"); - assertThat("Response OK", response, containsString("200 OK")); - } - finally - { - server.stop(); - } + + String rawResponse = connector.getResponse("GET http://localhost:8080 HTTP/1.1\r\nHost: localhost:8080\r\nConnection: close\r\n\r\n"); + HttpTester.Response response = HttpTester.parseResponse(rawResponse); + assertThat("Response OK", response.getStatus(), is(HttpStatus.OK_200)); } @Test public void testNullSessionAndSecurityHandler() throws Exception { - Server server = new Server(0); + Server server = newServer(); + HandlerList handlers = new HandlerList(); ContextHandlerCollection contexts = new ContextHandlerCollection(); WebAppContext context = new WebAppContext(null, null, null, null, null, new ErrorPageErrorHandler(), ServletContextHandler.NO_SESSIONS | ServletContextHandler.NO_SECURITY); context.setContextPath("/"); - context.setBaseResource(Resource.newResource("./src/test/webapp")); + + Path testWebapp = MavenTestingUtils.getProjectDirPath("src/test/webapp"); + context.setBaseResource(new PathResource(testWebapp)); server.setHandler(handlers); handlers.addHandler(contexts); contexts.addHandler(context); @@ -282,149 +292,158 @@ public class WebAppContextTest LocalConnector connector = new LocalConnector(server); server.addConnector(connector); - try - { - server.start(); - assertTrue(context.isAvailable()); - } - finally - { - server.stop(); - } - } - - class ServletA extends GenericServlet - { - @Override - public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException - { - this.getServletContext().getContext("/A/s"); - } - } - - class ServletB extends GenericServlet - { - @Override - public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException - { - this.getServletContext().getContext("/B/s"); - } + server.start(); + assertTrue(context.isAvailable()); } @Test - public void testServletContextListener() throws Exception + public void testBaseResourceAbsolutePath() throws Exception { - Server server = new Server(); - HotSwapHandler swap = new HotSwapHandler(); - server.setHandler(swap); + Server server = newServer(); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + + Path warPath = MavenTestingUtils.getTestResourcePathFile("wars/dump.war"); + warPath = warPath.toAbsolutePath(); + assertTrue(warPath.isAbsolute(), "Path should be absolute: " + warPath); + // Use String reference to war + // On Unix / Linux this should have no issue. + // On Windows with fully qualified paths such as "E:\mybase\webapps\dump.war" the + // resolution of the Resource can trigger various URI issues with the "E:" portion of the provided String. + context.setResourceBase(warPath.toString()); + + server.setHandler(context); server.start(); - ServletContextHandler context = new ServletContextHandler( - ServletContextHandler.SESSIONS); - context.setContextPath("/"); - context.setResourceBase(System.getProperty("java.io.tmpdir")); - - final List history = new ArrayList<>(); - - context.addEventListener(new ServletContextListener() - { - @Override - public void contextInitialized(ServletContextEvent servletContextEvent) - { - history.add("I0"); - } - - @Override - public void contextDestroyed(ServletContextEvent servletContextEvent) - { - history.add("D0"); - } - }); - context.addEventListener(new ServletContextListener() - { - @Override - public void contextInitialized(ServletContextEvent servletContextEvent) - { - history.add("I1"); - } - - @Override - public void contextDestroyed(ServletContextEvent servletContextEvent) - { - history.add("D1"); - throw new RuntimeException("Listener1 destroy broken"); - } - }); - context.addEventListener(new ServletContextListener() - { - @Override - public void contextInitialized(ServletContextEvent servletContextEvent) - { - history.add("I2"); - throw new RuntimeException("Listener2 init broken"); - } - - @Override - public void contextDestroyed(ServletContextEvent servletContextEvent) - { - history.add("D2"); - } - }); - context.addEventListener(new ServletContextListener() - { - @Override - public void contextInitialized(ServletContextEvent servletContextEvent) - { - history.add("I3"); - } - - @Override - public void contextDestroyed(ServletContextEvent servletContextEvent) - { - history.add("D3"); - } - }); - - try - { - swap.setHandler(context); - context.start(); - } - catch (Exception e) - { - history.add(e.getMessage()); - } - finally - { - try - { - swap.setHandler(null); - } - catch (Exception e) - { - while (e.getCause() instanceof Exception) - { - e = (Exception)e.getCause(); - } - history.add(e.getMessage()); - } - } - - assertThat(history, contains("I0", "I1", "I2", "Listener2 init broken", "D1", "D0", "Listener1 destroy broken")); - - server.stop(); + assertTrue(context.isAvailable(), "WebAppContext should be available"); } - @Test - public void ordering() throws Exception + public static Stream extraClasspathGlob() { - Path testWebappDir = MavenTestingUtils.getProjectDirPath("src/test/webapp"); - Resource webapp = new PathResource(testWebappDir); + List references = new ArrayList<>(); + + Path extLibs = MavenTestingUtils.getTestResourcePathDir("ext"); + extLibs = extLibs.toAbsolutePath(); + + // Absolute reference with trailing slash and glob + references.add(Arguments.of(extLibs.toString() + File.separator + "*")); + + // Establish a relative extraClassPath reference + String relativeExtLibsDir = MavenTestingUtils.getBasePath().relativize(extLibs).toString(); + + // This will be in the String form similar to "src/test/resources/ext/*" (with trailing slash and glob) + references.add(Arguments.of(relativeExtLibsDir + File.separator + "*")); + + return references.stream(); + } + + /** + * Test using WebAppContext.setExtraClassPath(String) with a reference to a glob + */ + @ParameterizedTest + @MethodSource("extraClasspathGlob") + public void testExtraClasspathGlob(String extraClasspathGlobReference) throws Exception + { + Server server = newServer(); + WebAppContext context = new WebAppContext(); - context.setBaseResource(webapp); - context.setContextPath("/test"); - new WebInfConfiguration().preConfigure(context); - assertEquals(Arrays.asList("acme.jar", "alpha.jar", "omega.jar"), - context.getMetaData().getWebInfJars().stream().map(r -> r.getURI().toString().replaceFirst(".+/", "")).collect(Collectors.toList())); + context.setContextPath("/"); + Path warPath = MavenTestingUtils.getTestResourcePathFile("wars/dump.war"); + context.setBaseResource(new PathResource(warPath)); + context.setExtraClasspath(extraClasspathGlobReference); + + server.setHandler(context); + server.start(); + + // Should not have failed the start of the WebAppContext + assertTrue(context.isAvailable(), "WebAppContext should be available"); + + // Test WebAppClassLoader contents for expected jars + ClassLoader contextClassLoader = context.getClassLoader(); + assertThat(contextClassLoader, instanceOf(WebAppClassLoader.class)); + WebAppClassLoader webAppClassLoader = (WebAppClassLoader)contextClassLoader; + Path extLibsDir = MavenTestingUtils.getTestResourcePathDir("ext"); + extLibsDir = extLibsDir.toAbsolutePath(); + List expectedPaths = Files.list(extLibsDir) + .filter(Files::isRegularFile) + .filter((path) -> path.toString().endsWith(".jar")) + .collect(Collectors.toList()); + List actualPaths = new ArrayList<>(); + for (URL url : webAppClassLoader.getURLs()) + { + actualPaths.add(Paths.get(url.toURI())); + } + assertThat("WebAppClassLoader.urls.length", actualPaths.size(), is(expectedPaths.size())); + for (Path expectedPath : expectedPaths) + { + boolean found = false; + for (Path actualPath : actualPaths) + { + if (Files.isSameFile(actualPath, expectedPath)) + { + found = true; + } + } + assertTrue(found, "Not able to find expected jar in WebAppClassLoader: " + expectedPath); + } + } + + public static Stream extraClasspathDir() + { + List references = new ArrayList<>(); + + Path extLibs = MavenTestingUtils.getTestResourcePathDir("ext"); + extLibs = extLibs.toAbsolutePath(); + + // Absolute reference with trailing slash + references.add(Arguments.of(extLibs.toString() + File.separator)); + + // Absolute reference without trailing slash + references.add(Arguments.of(extLibs.toString())); + + // Establish a relative extraClassPath reference + String relativeExtLibsDir = MavenTestingUtils.getBasePath().relativize(extLibs).toString(); + + // This will be in the String form similar to "src/test/resources/ext/" (with trailing slash) + references.add(Arguments.of(relativeExtLibsDir + File.separator)); + + // This will be in the String form similar to "src/test/resources/ext/" (without trailing slash) + references.add(Arguments.of(relativeExtLibsDir)); + + return references.stream(); + } + + /** + * Test using WebAppContext.setExtraClassPath(String) with a reference to a directory + */ + @ParameterizedTest + @MethodSource("extraClasspathDir") + public void testExtraClasspathDir(String extraClassPathReference) throws Exception + { + Server server = newServer(); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + Path warPath = MavenTestingUtils.getTestResourcePathFile("wars/dump.war"); + context.setBaseResource(new PathResource(warPath)); + + context.setExtraClasspath(extraClassPathReference); + + server.setHandler(context); + server.start(); + + // Should not have failed the start of the WebAppContext + assertTrue(context.isAvailable(), "WebAppContext should be available"); + + // Test WebAppClassLoader contents for expected directory reference + ClassLoader contextClassLoader = context.getClassLoader(); + assertThat(contextClassLoader, instanceOf(WebAppClassLoader.class)); + WebAppClassLoader webAppClassLoader = (WebAppClassLoader)contextClassLoader; + URL[] urls = webAppClassLoader.getURLs(); + assertThat("URLs", urls.length, is(1)); + Path extLibs = MavenTestingUtils.getTestResourcePathDir("ext"); + extLibs = extLibs.toAbsolutePath(); + assertThat("URL[0]", urls[0].toURI(), is(extLibs.toUri())); } } diff --git a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java index 8ec42555884..6e45e322d6d 100644 --- a/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java +++ b/tests/test-sessions/test-sessions-common/src/test/java/org/eclipse/jetty/server/session/SessionListenerTest.java @@ -18,14 +18,16 @@ package org.eclipse.jetty.server.session; -import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.Serializable; import java.net.HttpCookie; import java.net.URL; import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Collection; import java.util.concurrent.TimeUnit; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -34,17 +36,25 @@ import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; +import javax.servlet.http.HttpSessionEvent; +import javax.servlet.http.HttpSessionListener; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.ContentResponse; import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.toolchain.test.IO; -import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; +import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; +import org.eclipse.jetty.util.component.LifeCycle; +import org.eclipse.jetty.webapp.WebAppContext; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.in; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -58,8 +68,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue; * * Test that session listeners are called. */ +@ExtendWith(WorkDirExtension.class) public class SessionListenerTest { + public WorkDir workDir; + /** * Test that listeners are called when a session is deliberately invalidated. */ @@ -73,8 +86,8 @@ public class SessionListenerTest DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory(); cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT); - SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory(); - ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod); + TestSessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory(); + storeFactory.setGracePeriodSec(scavengePeriod); TestServer server = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory); @@ -99,7 +112,7 @@ public class SessionListenerTest ContentResponse response1 = client.GET(url + "?action=init"); assertEquals(HttpServletResponse.SC_OK, response1.getStatus()); String sessionCookie = response1.getHeaders().get("Set-Cookie"); - assertTrue(sessionCookie != null); + assertNotNull(sessionCookie); assertTrue(TestServlet.bindingListener.bound); String sessionId = TestServer.extractSessionId(sessionCookie); @@ -115,12 +128,12 @@ public class SessionListenerTest } finally { - client.stop(); + LifeCycle.stop(client); } } finally { - server.stop(); + LifeCycle.stop(server); } } @@ -131,17 +144,20 @@ public class SessionListenerTest @Test public void testSessionExpiresWithListener() throws Exception { + Path foodir = workDir.getEmptyPathDir(); + Path fooClass = foodir.resolve("Foo.class"); + //Use a class that would only be known to the webapp classloader - InputStream foostream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Foo.clazz"); - File foodir = new File(MavenTestingUtils.getTargetDir(), "foo"); - foodir.mkdirs(); - File fooclass = new File(foodir, "Foo.class"); - IO.copy(foostream, new FileOutputStream(fooclass)); + try (InputStream foostream = Thread.currentThread().getContextClassLoader().getResourceAsStream("Foo.clazz"); + OutputStream out = Files.newOutputStream(fooClass)) + { + IO.copy(foostream, out); + } - assertTrue(fooclass.exists()); - assertTrue(fooclass.length() != 0); + assertTrue(Files.exists(fooClass)); + assertThat(Files.size(fooClass), greaterThan(0L)); - URL[] foodirUrls = new URL[]{foodir.toURI().toURL()}; + URL[] foodirUrls = new URL[]{foodir.toUri().toURL()}; URLClassLoader contextClassLoader = new URLClassLoader(foodirUrls, Thread.currentThread().getContextClassLoader()); String contextPath = "/"; @@ -151,8 +167,8 @@ public class SessionListenerTest DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory(); cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT); - SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory(); - ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod); + TestSessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory(); + storeFactory.setGracePeriodSec(scavengePeriod); TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory); @@ -164,31 +180,38 @@ public class SessionListenerTest TestHttpSessionListener listener = new TestHttpSessionListenerWithWebappClasses(true); context.getSessionHandler().addEventListener(listener); - server1.start(); - int port1 = server1.getPort(); - try { + server1.start(); + int port1 = server1.getPort(); + HttpClient client = new HttpClient(); - client.start(); - String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1); + try + { + client.start(); + String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1); - //make a request to set up a session on the server - ContentResponse response1 = client.GET(url + "?action=init"); - assertEquals(HttpServletResponse.SC_OK, response1.getStatus()); - String sessionCookie = response1.getHeaders().get("Set-Cookie"); - assertTrue(sessionCookie != null); + //make a request to set up a session on the server + ContentResponse response1 = client.GET(url + "?action=init"); + assertEquals(HttpServletResponse.SC_OK, response1.getStatus()); + String sessionCookie = response1.getHeaders().get("Set-Cookie"); + assertNotNull(sessionCookie); - String sessionId = TestServer.extractSessionId(sessionCookie); + String sessionId = TestServer.extractSessionId(sessionCookie); - assertThat(sessionId, is(in(listener.createdSessions))); + assertThat(sessionId, is(in(listener.createdSessions))); - //and wait until the session should have expired - Thread.currentThread().sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + (2 * scavengePeriod))); + //and wait until the session should have expired + Thread.sleep(TimeUnit.SECONDS.toMillis(inactivePeriod + (2 * scavengePeriod))); - assertThat(sessionId, is(in(listener.destroyedSessions))); + assertThat(sessionId, is(in(listener.destroyedSessions))); - assertNull(listener.ex); + assertNull(listener.ex); + } + finally + { + LifeCycle.stop(client); + } } finally { @@ -209,8 +232,8 @@ public class SessionListenerTest DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory(); cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT); - SessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory(); - ((AbstractSessionDataStoreFactory)storeFactory).setGracePeriodSec(scavengePeriod); + TestSessionDataStoreFactory storeFactory = new TestSessionDataStoreFactory(); + storeFactory.setGracePeriodSec(scavengePeriod); TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory); @@ -222,11 +245,11 @@ public class SessionListenerTest context.getSessionHandler().addEventListener(listener); - server1.start(); - int port1 = server1.getPort(); - try { + server1.start(); + int port1 = server1.getPort(); + //save a session that has already expired long now = System.currentTimeMillis(); SessionData data = context.getSessionHandler().getSessionCache().getSessionDataStore().newSessionData("1234", now - 10, now - 5, now - 10, 30000); @@ -234,24 +257,31 @@ public class SessionListenerTest context.getSessionHandler().getSessionCache().getSessionDataStore().store("1234", data); HttpClient client = new HttpClient(); - client.start(); + try + { + client.start(); - port1 = server1.getPort(); - String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1); + port1 = server1.getPort(); + String url = "http://localhost:" + port1 + contextPath + servletMapping.substring(1); - //make another request using the id of the expired session - Request request = client.newRequest(url + "?action=test"); - request.cookie(new HttpCookie("JSESSIONID", "1234")); - ContentResponse response = request.send(); - assertEquals(HttpServletResponse.SC_OK, response.getStatus()); + //make another request using the id of the expired session + Request request = client.newRequest(url + "?action=test"); + request.cookie(new HttpCookie("JSESSIONID", "1234")); + ContentResponse response = request.send(); + assertEquals(HttpServletResponse.SC_OK, response.getStatus()); - //should be a new session id - String cookie2 = response.getHeaders().get("Set-Cookie"); - assertNotEquals("1234", TestServer.extractSessionId(cookie2)); + //should be a new session id + String cookie2 = response.getHeaders().get("Set-Cookie"); + assertNotEquals("1234", TestServer.extractSessionId(cookie2)); - assertTrue(listener.destroyedSessions.contains("1234")); + assertTrue(listener.destroyedSessions.contains("1234")); - assertNull(listener.ex); + assertNull(listener.ex); + } + finally + { + LifeCycle.stop(client); + } } finally { @@ -259,6 +289,42 @@ public class SessionListenerTest } } + public static class MyHttpSessionListener implements HttpSessionListener + { + @Override + public void sessionCreated(HttpSessionEvent se) + { + } + + @Override + public void sessionDestroyed(HttpSessionEvent se) + { + } + } + + @Test + public void testSessionListeners() + { + Server server = new Server(); + try + { + WebAppContext wac = new WebAppContext(); + + wac.setServer(server); + server.setHandler(wac); + wac.addEventListener(new MyHttpSessionListener()); + + Collection listeners = wac.getSessionHandler().getBeans(MyHttpSessionListener.class); + assertNotNull(listeners); + + assertEquals(1, listeners.size()); + } + finally + { + LifeCycle.stop(server); + } + } + public static class MySessionBindingListener implements HttpSessionBindingListener, Serializable { private static final long serialVersionUID = 1L; @@ -314,7 +380,7 @@ public class SessionListenerTest if ("test".equals(action)) { HttpSession session = request.getSession(true); - assertTrue(session != null); + assertNotNull(session); } } } diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AnnotatedListener.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AnnotatedListener.java index 3ca3fb7862a..a1d314b266a 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AnnotatedListener.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AnnotatedListener.java @@ -18,6 +18,7 @@ package com.acme.test; +import java.util.logging.Logger; import javax.annotation.Resource; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; @@ -37,6 +38,7 @@ import javax.servlet.http.HttpSessionListener; @WebListener public class AnnotatedListener implements HttpSessionListener, HttpSessionAttributeListener, HttpSessionActivationListener, ServletContextListener, ServletContextAttributeListener, ServletRequestListener, ServletRequestAttributeListener { + private static final Logger LOG = Logger.getLogger(AnnotatedListener.class.getName()); @Resource(mappedName = "maxAmount") private Double maxAmount; @@ -44,31 +46,31 @@ public class AnnotatedListener implements HttpSessionListener, HttpSessionAttrib @Override public void attributeAdded(HttpSessionBindingEvent se) { - // System.err.println("attributedAdded "+se); + LOG.fine("attributedAdded " + se); } @Override public void attributeRemoved(HttpSessionBindingEvent se) { - // System.err.println("attributeRemoved "+se); + LOG.fine("attributeRemoved " + se); } @Override public void attributeReplaced(HttpSessionBindingEvent se) { - // System.err.println("attributeReplaced "+se); + LOG.fine("attributeReplaced " + se); } @Override public void sessionWillPassivate(HttpSessionEvent se) { - // System.err.println("sessionWillPassivate "+se); + LOG.fine("sessionWillPassivate " + se); } @Override public void sessionDidActivate(HttpSessionEvent se) { - // System.err.println("sessionDidActivate "+se); + LOG.fine("sessionDidActivate " + se); } @Override @@ -83,84 +85,66 @@ public class AnnotatedListener implements HttpSessionListener, HttpSessionAttrib @Override public void contextDestroyed(ServletContextEvent sce) { - // System.err.println("contextDestroyed "+sce); + LOG.fine("contextDestroyed " + sce); } @Override public void attributeAdded(ServletContextAttributeEvent scab) { - // System.err.println("attributeAdded "+scab); + LOG.fine("attributeAdded " + scab); } @Override public void attributeRemoved(ServletContextAttributeEvent scab) { - // System.err.println("attributeRemoved "+scab); + LOG.fine("attributeRemoved " + scab); } @Override public void attributeReplaced(ServletContextAttributeEvent scab) { - // System.err.println("attributeReplaced "+scab); + LOG.fine("attributeReplaced " + scab); } @Override public void requestDestroyed(ServletRequestEvent sre) { - // System.err.println("requestDestroyed "+sre); + LOG.fine("requestDestroyed " + sre); } @Override public void requestInitialized(ServletRequestEvent sre) { - // System.err.println("requestInitialized "+sre); + LOG.fine("requestInitialized " + sre); } @Override public void attributeAdded(ServletRequestAttributeEvent srae) { - // System.err.println("attributeAdded "+srae); + LOG.fine("attributeAdded " + srae); } @Override public void attributeRemoved(ServletRequestAttributeEvent srae) { - // System.err.println("attributeRemoved "+srae); + LOG.fine("attributeRemoved " + srae); } @Override public void attributeReplaced(ServletRequestAttributeEvent srae) { - // System.err.println("attributeReplaced "+srae); + LOG.fine("attributeReplaced " + srae); } @Override public void sessionCreated(HttpSessionEvent se) { - // System.err.println("sessionCreated "+se); + LOG.fine("sessionCreated " + se); } @Override public void sessionDestroyed(HttpSessionEvent se) { - // System.err.println("sessionDestroyed "+se); - } - - public void requestCompleted(ServletRequestEvent rre) - { - // TODO Auto-generated method stub - - } - - public void requestResumed(ServletRequestEvent rre) - { - // TODO Auto-generated method stub - - } - - public void requestSuspended(ServletRequestEvent rre) - { - // TODO Auto-generated method stub - + LOG.fine("sessionDestroyed " + se); } } diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AsyncListenerServlet.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AsyncListenerServlet.java index 00744b29254..95814c29eda 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AsyncListenerServlet.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/AsyncListenerServlet.java @@ -62,29 +62,21 @@ public class AsyncListenerServlet extends HttpServlet @Override public void onComplete(AsyncEvent event) throws IOException { - // TODO Auto-generated method stub - } @Override public void onTimeout(AsyncEvent event) throws IOException { - // TODO Auto-generated method stub - } @Override public void onError(AsyncEvent event) throws IOException { - // TODO Auto-generated method stub - } @Override public void onStartAsync(AsyncEvent event) throws IOException { - // TODO Auto-generated method stub - } } diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/Bar.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/Bar.java index 3f556b96b1d..77903978394 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/Bar.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/Bar.java @@ -20,7 +20,6 @@ package com.acme.test; public class Bar { - @com.acme.initializer.Foo(2) public void someMethod() { diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java index f13f3afd332..7af05a97285 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/ClassLoaderServlet.java @@ -40,7 +40,6 @@ public class ClassLoaderServlet extends HttpServlet { try { - PrintWriter writer = resp.getWriter(); writer.println(""); writer.println(""); diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/RoleAnnotationTest.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/RoleAnnotationTest.java index 2d0fa4b4506..bc734a6822b 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/RoleAnnotationTest.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/RoleAnnotationTest.java @@ -32,7 +32,6 @@ import javax.servlet.http.HttpServletResponse; * * Use DeclareRolesAnnotations from within Jetty. */ - @DeclareRoles({"server-administrator", "user"}) public class RoleAnnotationTest extends HttpServlet { diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java index 08dfa47f0d1..33620211aa5 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/SecuredServlet.java @@ -20,7 +20,6 @@ package com.acme.test; import java.io.IOException; import java.io.PrintWriter; -import javax.servlet.ServletException; import javax.servlet.annotation.HttpConstraint; import javax.servlet.annotation.ServletSecurity; import javax.servlet.annotation.WebServlet; @@ -32,10 +31,9 @@ import javax.servlet.http.HttpServletResponse; @ServletSecurity(@HttpConstraint(rolesAllowed = "admin")) public class SecuredServlet extends HttpServlet { - @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException + throws IOException { PrintWriter writer = resp.getWriter(); writer.println(""); diff --git a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/TestListener.java b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/TestListener.java index 21c458679fa..19f0d38932b 100644 --- a/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/TestListener.java +++ b/tests/test-webapps/test-servlet-spec/test-spec-webapp/src/main/java/com/acme/test/TestListener.java @@ -19,6 +19,7 @@ package com.acme.test; import java.util.EventListener; +import java.util.logging.Logger; import javax.annotation.Resource; import javax.servlet.ServletContextAttributeEvent; import javax.servlet.ServletContextAttributeListener; @@ -40,6 +41,8 @@ import javax.servlet.http.HttpSessionListener; @WebListener public class TestListener implements HttpSessionListener, HttpSessionAttributeListener, HttpSessionActivationListener, ServletContextListener, ServletContextAttributeListener, ServletRequestListener, ServletRequestAttributeListener { + private static final Logger LOG = Logger.getLogger(TestListener.class.getName()); + public static class NaughtyServletContextListener implements ServletContextListener { @@ -85,31 +88,31 @@ public class TestListener implements HttpSessionListener, HttpSessionAttributeLi @Override public void attributeAdded(HttpSessionBindingEvent se) { - // System.err.println("attributedAdded "+se); + LOG.fine("attributeAdded " + se); } @Override public void attributeRemoved(HttpSessionBindingEvent se) { - // System.err.println("attributeRemoved "+se); + LOG.fine("attributeRemoved " + se); } @Override public void attributeReplaced(HttpSessionBindingEvent se) { - // System.err.println("attributeReplaced "+se); + LOG.fine("attributeReplaced " + se); } @Override public void sessionWillPassivate(HttpSessionEvent se) { - // System.err.println("sessionWillPassivate "+se); + LOG.fine("sessionWillPassivate " + se); } @Override public void sessionDidActivate(HttpSessionEvent se) { - // System.err.println("sessionDidActivate "+se); + LOG.fine("sessionDidActivate " + se); } @Override @@ -165,84 +168,66 @@ public class TestListener implements HttpSessionListener, HttpSessionAttributeLi @Override public void contextDestroyed(ServletContextEvent sce) { - // System.err.println("contextDestroyed "+sce); + LOG.fine("contextDestroyed " + sce); } @Override public void attributeAdded(ServletContextAttributeEvent scab) { - // System.err.println("attributeAdded "+scab); + LOG.fine("attributeAdded " + scab); } @Override public void attributeRemoved(ServletContextAttributeEvent scab) { - // System.err.println("attributeRemoved "+scab); + LOG.fine("attributeRemoved " + scab); } @Override public void attributeReplaced(ServletContextAttributeEvent scab) { - // System.err.println("attributeReplaced "+scab); + LOG.fine("attributeReplaced " + scab); } @Override public void requestDestroyed(ServletRequestEvent sre) { - // System.err.println("requestDestroyed "+sre); + LOG.fine("requestDestroyed " + sre); } @Override public void requestInitialized(ServletRequestEvent sre) { - // System.err.println("requestInitialized "+sre); + LOG.fine("requestInitialized " + sre); } @Override public void attributeAdded(ServletRequestAttributeEvent srae) { - // System.err.println("attributeAdded "+srae); + LOG.fine("attributeAdded " + srae); } @Override public void attributeRemoved(ServletRequestAttributeEvent srae) { - // System.err.println("attributeRemoved "+srae); + LOG.fine("attributeRemoved " + srae); } @Override public void attributeReplaced(ServletRequestAttributeEvent srae) { - // System.err.println("attributeReplaced "+srae); + LOG.fine("attributeReplaced " + srae); } @Override public void sessionCreated(HttpSessionEvent se) { - // System.err.println("sessionCreated "+se); + LOG.fine("sessionCreated " + se); } @Override public void sessionDestroyed(HttpSessionEvent se) { - // System.err.println("sessionDestroyed "+se); - } - - public void requestCompleted(ServletRequestEvent rre) - { - // TODO Auto-generated method stub - - } - - public void requestResumed(ServletRequestEvent rre) - { - // TODO Auto-generated method stub - - } - - public void requestSuspended(ServletRequestEvent rre) - { - // TODO Auto-generated method stub - + LOG.fine("sessionDestroyed " + se); } }