Issue #5264 - Adding demo.mod support to embedded examples tests

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2020-09-23 14:56:53 -05:00
parent 1feb60bb2c
commit bdf30ba437
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
12 changed files with 176 additions and 41 deletions

View File

@ -0,0 +1,153 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.embedded;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A utility test class to locate a Jetty Base for testing purposes by searching:
* <ul>
* <li>The <code>jetty.base</code> system property</li>
* <li>The <code>JETTY_BASE</code> environment variable</li>
* <li>Creating a {@code ${java.io.tmpDir}/jetty-demo-base/} to work with</li>
* </ul>
*/
public class JettyDemoBase
{
private static final Logger LOG = LoggerFactory.getLogger(JettyDemoBase.class);
public static final Path JETTY_BASE;
static
{
Path jettyBase = asDirectory(System.getProperty("jetty.base"));
LOG.debug("JettyDemobase(prop(jetty.home)) = {}", jettyBase);
if (jettyBase == null)
{
jettyBase = asDirectory(System.getenv().get("JETTY_BASE"));
LOG.debug("JettyHome(env(JETTY_BASE)) = {}", jettyBase);
}
if (jettyBase == null || !Files.exists(jettyBase.resolve("start.d/demo.ini")))
{
// Create the demo-base in java.io.tmpdir
ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader();
try
{
Path jettyHome = JettyHome.get();
jettyBase = Files.createTempDirectory("jetty-base");
Path startJar = jettyHome.resolve("start.jar");
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{
startJar.toUri().toURL()
});
Thread.currentThread().setContextClassLoader(urlClassLoader);
Class<?> mainClass = urlClassLoader.loadClass("org.eclipse.jetty.start.Main");
Method mainMethod = mainClass.getMethod("main", String[].class);
String[] args = new String[]{
"jetty.home=" + jettyHome.toString(),
"jetty.base=" + jettyBase.toString(),
"--add-modules=demo"
};
LOG.info("Creating DemoBase in {}", jettyBase);
mainMethod.invoke(mainClass, new Object[]{args});
if (LOG.isDebugEnabled())
LOG.debug("JettyHome(working.resolve(...)) = {}", jettyBase);
}
catch (Throwable th)
{
LOG.warn("Unable to resolve Jetty Distribution location", th);
}
finally
{
Thread.currentThread().setContextClassLoader(origClassLoader);
}
}
JETTY_BASE = jettyBase;
}
private static Path asDirectory(String path)
{
try
{
if (path == null)
{
return null;
}
if (StringUtil.isBlank(path))
{
LOG.debug("asDirectory {} is blank", path);
return null;
}
Path dir = Paths.get(path);
if (!Files.exists(dir))
{
LOG.debug("asDirectory {} does not exist", path);
return null;
}
if (!Files.isDirectory(dir))
{
LOG.debug("asDirectory {} is not a directory", path);
return null;
}
LOG.debug("asDirectory {}", dir);
return dir.toAbsolutePath();
}
catch (Exception e)
{
LOG.trace("IGNORED", e);
}
return null;
}
public static Path get()
{
if (JETTY_BASE == null)
throw new RuntimeException("jetty-base not found");
return JETTY_BASE;
}
public static Path resolve(String path)
{
return get().resolve(path);
}
public static void main(String... arg)
{
System.err.println("Jetty Base is " + JETTY_BASE);
}
}

View File

@ -27,7 +27,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A utility test class to locate a Jetty Distribution for testing purposes by searching:
* A utility test class to locate a Jetty Home for testing purposes by searching:
* <ul>
* <li>The <code>jetty.home</code> system property</li>
* <li>The <code>JETTY_HOME</code> environment variable</li>
@ -37,7 +37,7 @@ import org.slf4j.LoggerFactory;
public class JettyHome
{
private static final Logger LOG = LoggerFactory.getLogger(JettyHome.class);
public static final Path DISTRIBUTION;
public static final Path JETTY_HOME;
static
{
@ -51,13 +51,9 @@ public class JettyHome
Path distro = null;
if (jettyHome != null)
if (jettyHome != null && Files.exists(jettyHome.resolve("start.jar")))
{
Path parent = jettyHome.getParent();
if (hasDemoBase(parent))
{
distro = parent;
}
distro = jettyHome;
}
if (distro == null)
@ -70,7 +66,7 @@ public class JettyHome
while (dir == null && working != null)
{
dir = asDirectory(working.resolve("jetty-home/target/jetty-home").toString());
if (dir != null && hasDemoBase(dir))
if (dir != null && Files.exists(dir.resolve("start.jar")))
{
distro = dir;
}
@ -95,13 +91,7 @@ public class JettyHome
if (LOG.isDebugEnabled())
LOG.debug("JettyHome() FOUND = {}", distro);
}
DISTRIBUTION = distro;
}
private static boolean hasDemoBase(Path path)
{
Path demoBase = path.resolve("demo-base");
return Files.exists(demoBase) && Files.isDirectory(demoBase);
JETTY_HOME = distro;
}
private static Path asDirectory(String path)
@ -144,9 +134,9 @@ public class JettyHome
public static Path get()
{
if (DISTRIBUTION == null)
throw new RuntimeException("jetty-distribution not found");
return DISTRIBUTION;
if (JETTY_HOME == null)
throw new RuntimeException("jetty-home not found");
return JETTY_HOME;
}
public static Path resolve(String path)
@ -156,6 +146,6 @@ public class JettyHome
public static void main(String... arg)
{
System.err.println("Jetty Distribution is " + DISTRIBUTION);
System.err.println("Jetty Home is " + JETTY_HOME);
}
}

View File

@ -65,21 +65,13 @@ public class LikeJettyXml
{
public static Server createServer(int port, int securePort, boolean addDebugListener) throws Exception
{
// Path to as-built jetty-distribution directory
Path jettyDistro = JettyHome.get();
// Find jetty home and base directories
String homePath = System.getProperty("jetty.home", jettyDistro.resolve("jetty-home").toString());
Path homeDir = Paths.get(homePath);
String basePath = System.getProperty("jetty.base", jettyDistro.resolve("demo-base").toString());
Path baseDir = Paths.get(basePath);
Path jettyHome = JettyHome.get();
Path jettyBase = JettyDemoBase.get();
// Configure jetty.home and jetty.base system properties
String jettyHome = homeDir.toAbsolutePath().toString();
String jettyBase = baseDir.toAbsolutePath().toString();
System.setProperty("jetty.home", jettyHome);
System.setProperty("jetty.base", jettyBase);
System.setProperty("jetty.home", jettyHome.toString());
System.setProperty("jetty.base", jettyBase.toString());
// === jetty.xml ===
// Setup Threadpool

View File

@ -43,7 +43,7 @@ public class OneWebApp
// PlusConfiguration) to choosing where the webapp will unpack itself.
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
File warFile = JettyHome.resolve("demo-base/webapps/async-rest.war").toFile();
File warFile = JettyDemoBase.resolve("webapps/demo-async-rest.war").toFile();
webapp.setWar(warFile.getAbsolutePath());
// A WebAppContext is a ContextHandler as well so it needs to be set to

View File

@ -51,7 +51,7 @@ public class OneWebAppWithJsp
// the webapp will unpack itself.
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
Path warFile = JettyHome.resolve("demo-base/webapps/test.war");
Path warFile = JettyDemoBase.resolve("webapps/demo-jetty.war");
if (!Files.exists(warFile))
{
throw new FileNotFoundException(warFile.toString());

View File

@ -51,7 +51,7 @@ public class ServerWithAnnotations
webapp.addConfiguration(new EnvConfiguration(), new PlusConfiguration(), new AnnotationConfiguration());
webapp.setContextPath("/");
File warFile = JettyHome.resolve("demo-base/webapps/test-spec.war").toFile();
File warFile = JettyDemoBase.resolve("webapps/demo-spec.war").toFile();
webapp.setWar(warFile.getAbsolutePath());
webapp.setAttribute(
"org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",

View File

@ -41,7 +41,7 @@ public class ServerWithJNDI
// Create a WebApp
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
Path testJndiWar = JettyHome.resolve("demo-base/webapps/test-jndi.war");
Path testJndiWar = JettyDemoBase.resolve("webapps/demo-jndi.war");
webapp.setWarResource(new PathResource(testJndiWar));
server.setHandler(webapp);

View File

@ -44,7 +44,7 @@ public class LikeJettyXmlTest extends AbstractEmbeddedTest
@BeforeEach
public void startServer() throws Exception
{
assumeTrue(JettyHome.DISTRIBUTION != null, "jetty-distribution not found");
assumeTrue(JettyHome.JETTY_HOME != null, "jetty-distribution not found");
server = LikeJettyXml.createServer(0, 0, false);
server.start();

View File

@ -42,7 +42,7 @@ public class OneWebAppTest extends AbstractEmbeddedTest
@BeforeEach
public void startServer() throws Exception
{
assumeTrue(JettyHome.DISTRIBUTION != null, "jetty-distribution not found");
assumeTrue(JettyHome.JETTY_HOME != null, "jetty-distribution not found");
server = OneWebApp.createServer(0);
server.start();

View File

@ -42,7 +42,7 @@ public class OneWebAppWithJspTest extends AbstractEmbeddedTest
@BeforeEach
public void startServer() throws Exception
{
assumeTrue(JettyHome.DISTRIBUTION != null, "jetty-distribution not found");
assumeTrue(JettyHome.JETTY_HOME != null, "jetty-distribution not found");
server = OneWebAppWithJsp.createServer(0);
server.start();

View File

@ -41,7 +41,7 @@ public class ServerWithAnnotationsTest extends AbstractEmbeddedTest
@BeforeEach
public void startServer() throws Exception
{
assumeTrue(JettyHome.DISTRIBUTION != null, "jetty-distribution not found");
assumeTrue(JettyHome.JETTY_HOME != null, "jetty-distribution not found");
server = ServerWithAnnotations.createServer(0);
server.start();

View File

@ -42,7 +42,7 @@ public class ServerWithJNDITest extends AbstractEmbeddedTest
@BeforeEach
public void startServer() throws Exception
{
assumeTrue(JettyHome.DISTRIBUTION != null, "jetty-distribution not found");
assumeTrue(JettyHome.JETTY_HOME != null, "jetty-distribution not found");
server = ServerWithJNDI.createServer(0);
server.start();