Issue #1743 WIP add unit test; stopOnShutdown

Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Jan Bartel 2019-09-25 12:43:51 +10:00
parent 557cd83713
commit 747315d3dc
7 changed files with 177 additions and 17 deletions

View File

@ -283,7 +283,7 @@ public abstract class AbstractWebAppMojo extends AbstractMojo
* Optional.
*/
@Parameter
protected ContextHandler[] contextHandlers;
protected List<ContextHandler> contextHandlers;
/**
* List of security realms to set up. Consider using instead
@ -291,7 +291,7 @@ public abstract class AbstractWebAppMojo extends AbstractMojo
* Optional.
*/
@Parameter
protected LoginService[] loginServices;
protected List<LoginService> loginServices;
/**
* A RequestLog implementation to use for the webapp at runtime.
@ -526,7 +526,6 @@ public abstract class AbstractWebAppMojo extends AbstractMojo
jetty.setJettyXmlFiles(jettyXmls);
jetty.setHttpConnector(httpConnector);
jetty.setJettyProperties(jettyProperties);
jetty.setRequestLog(requestLog);
jetty.setLoginServices(loginServices);
jetty.setContextXml(contextXml);
jetty.setWebApp(webApp);
@ -817,7 +816,6 @@ public abstract class AbstractWebAppMojo extends AbstractMojo
//only unpack if the overlay is newer
if (!unpackDir.exists() || (overlay.getResource().lastModified() > unpackDir.lastModified()))
{
boolean made = unpackDir.mkdirs();
overlay.getResource().copyTo(unpackDir);
}

View File

@ -19,6 +19,7 @@
package org.eclipse.jetty.maven.plugin;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@ -30,8 +31,6 @@ import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ShutdownMonitor;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
@ -47,9 +46,9 @@ public class JettyEmbedder extends AbstractLifeCycle
{
private static final Logger LOG = Log.getLogger(JettyEmbedder.class);
protected ContextHandler[] contextHandlers;
protected List<ContextHandler> contextHandlers;
protected LoginService[] loginServices;
protected List<LoginService> loginServices;
protected RequestLog requestLog;
@ -77,24 +76,30 @@ public class JettyEmbedder extends AbstractLifeCycle
private Properties webAppProperties;
public ContextHandler[] getContextHandlers()
public List<ContextHandler> getContextHandlers()
{
return contextHandlers;
}
public void setContextHandlers(ContextHandler[] contextHandlers)
public void setContextHandlers(List<ContextHandler> contextHandlers)
{
this.contextHandlers = contextHandlers;
if (contextHandlers == null)
this.contextHandlers = null;
else
this.contextHandlers = new ArrayList<>(contextHandlers);
}
public LoginService[] getLoginServices()
public List<LoginService> getLoginServices()
{
return loginServices;
}
public void setLoginServices(LoginService[] loginServices)
public void setLoginServices(List<LoginService> loginServices)
{
this.loginServices = loginServices;
if (loginServices == null)
this.loginServices = null;
else
this.loginServices = new ArrayList<>(loginServices);
}
public RequestLog getRequestLog()
@ -275,6 +280,8 @@ public class JettyEmbedder extends AbstractLifeCycle
if (server == null)
server = new Server();
server.setStopAtShutdown(stopAtShutdown);
//ensure there's a connector
if (httpConnector != null)
@ -297,7 +304,7 @@ public class JettyEmbedder extends AbstractLifeCycle
applyWebAppProperties();
//TODO- this might be duplicating WebAppPropertyConverter. make it a quickstart if the quickstart-web.xml file exists
//If there is a quickstart file, then quickstart the webapp.
if (webApp.getTempDirectory() != null)
{
File qs = new File(webApp.getTempDirectory(), "quickstart-web.xml");

View File

@ -62,7 +62,7 @@ public class ServerSupport
* @param requestLog the request log
* @throws Exception if unable to configure the handlers
*/
public static void configureHandlers (Server server, ContextHandler[] contextHandlers, RequestLog requestLog) throws Exception
public static void configureHandlers (Server server, List<ContextHandler> contextHandlers, RequestLog requestLog) throws Exception
{
if (server == null)
throw new IllegalArgumentException("Server is null");
@ -143,7 +143,7 @@ public class ServerSupport
* @param server the server
* @param loginServices the login services
*/
public static void configureLoginServices(Server server, LoginService[] loginServices)
public static void configureLoginServices(Server server, List<LoginService> loginServices)
{
if (server == null)
throw new IllegalArgumentException("Server is null");

View File

@ -0,0 +1,121 @@
//
// ========================================================================
// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// 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.maven.plugin;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.Resource;
import org.junit.jupiter.api.Test;
/**
*
*
*/
public class TestJettyEmbedder
{
//@Test
public void testJettyEmbedderFromDefaults()
throws Exception
{
JettyWebAppContext webApp = new JettyWebAppContext();
JettyEmbedder jetty = new JettyEmbedder();
jetty.setExitVm(false);
jetty.setServer(null);
jetty.setContextHandlers(null);
jetty.setRequestLog(null);
jetty.setJettyXmlFiles(null);
jetty.setHttpConnector(null);
jetty.setJettyProperties(null);
jetty.setLoginServices(null);
jetty.setContextXml(MavenTestingUtils.getTestResourceFile("embedder-context.xml").getAbsolutePath());
jetty.setWebApp(webApp);
try
{
jetty.start();
assertEquals("/embedder", webApp.getContextPath());
assertTrue(webApp.isStarted());
assertNotNull(jetty.getServer());
assertTrue(jetty.getServer().isStarted());
assertNotNull(jetty.getServer().getConnectors());
assertNotNull(ServerSupport.findContextHandlerCollection(jetty.getServer()));
}
finally
{
jetty.stop();
}
}
//@Test
public void testJettyEmbedder()
throws Exception
{
JettyWebAppContext webApp = new JettyWebAppContext();
Server server = new Server();
Map<String,String> jettyProperties = new HashMap<>();
jettyProperties.put("jetty.server.dumpAfterStart", "true");
ContextHandler otherHandler = new ContextHandler();
otherHandler.setContextPath("/other");
otherHandler.setBaseResource(Resource.newResource(MavenTestingUtils.getTestResourceDir("root")));
JettyEmbedder jetty = new JettyEmbedder();
jetty.setExitVm(false);
jetty.setServer(server);
jetty.setContextHandlers(Arrays.asList(otherHandler));
jetty.setRequestLog(null);
jetty.setJettyXmlFiles(Arrays.asList(MavenTestingUtils.getTestResourceFile("embedder-jetty.xml")));
jetty.setHttpConnector(null);
jetty.setJettyProperties(jettyProperties);
jetty.setLoginServices(null);
jetty.setContextXml(MavenTestingUtils.getTestResourceFile("embedder-context.xml").getAbsolutePath());
jetty.setWebApp(webApp);
try
{
jetty.start();
assertEquals("/embedder", webApp.getContextPath());
assertTrue(webApp.isStarted());
assertNotNull(jetty.getServer());
assertTrue(jetty.getServer().isStarted());
assertNotNull(jetty.getServer().getConnectors());
ContextHandlerCollection contexts = ServerSupport.findContextHandlerCollection(jetty.getServer());
assertNotNull(contexts);
assertTrue(contexts.contains(otherHandler));
assertTrue(contexts.contains(webApp));
}
finally
{
jetty.stop();
}
}
}

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/embedder</Set>
</Configure>

View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
</Item>
<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
<Set name="stopAtShutdown" property="jetty.server.stopAtShutdown"/>
<Set name="stopTimeout"><Property name="jetty.server.stopTimeout" default="5000"/></Set>
<Set name="dumpAfterStart" property="jetty.server.dumpAfterStart"/>
<Set name="dumpBeforeStop" property="jetty.server.dumpBeforeStop"/>
</Configure>

View File

@ -0,0 +1 @@
<H1>ROOT</H1>