Skip to content

Commit

Permalink
Issue #5137 - Cleanup of WebAppContextTest
Browse files Browse the repository at this point in the history
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
  • Loading branch information
joakime committed Aug 12, 2020
1 parent e632d24 commit fbefe0b
Show file tree
Hide file tree
Showing 5 changed files with 448 additions and 402 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//
// ========================================================================
// 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.servlet;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HotSwapHandler;
import org.eclipse.jetty.toolchain.test.FS;
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 static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;

public class ServletContextListenerTest
{
private Server server;

@AfterEach
public void tearDown()
{
LifeCycle.stop(server);
}

private Server newServer()
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0);
server.addConnector(connector);
return server;
}

@Test
public void testServletContextListener() throws Exception
{
Server server = newServer();
HotSwapHandler swap = new HotSwapHandler();
server.setHandler(swap);
server.start();

Path tempDir = MavenTestingUtils.getTargetTestingPath("testServletContextListener");
FS.ensureEmpty(tempDir);

ServletContextHandler context = new ServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
context.setBaseResource(new PathResource(tempDir));

final List<String> 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"));
}
}
7 changes: 7 additions & 0 deletions jetty-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-http</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// ========================================================================
// 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.util.Collection;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.eclipse.jetty.server.Server;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class HttpSessionListenerTest
{
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();

WebAppContext wac = new WebAppContext();

wac.setServer(server);
server.setHandler(wac);
wac.addEventListener(new MyHttpSessionListener());

Collection<MyHttpSessionListener> listeners = wac.getSessionHandler().getBeans(MyHttpSessionListener.class);
assertNotNull(listeners);
assertEquals(1, listeners.size());
}
}
Loading

0 comments on commit fbefe0b

Please sign in to comment.