apply some comments from review
Signed-off-by: olivier lamy <oliver.lamy@gmail.com>
This commit is contained in:
parent
214bc03270
commit
cbc9a9cff3
|
@ -11,7 +11,7 @@
|
|||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<jetty.home>${project.build.outputDirectory}/jetty_home</jetty.home>
|
||||
<bundle-symbolic-name>${project.groupId}.test_distribution</bundle-symbolic-name>
|
||||
<mavenResolver.version>1.3.1</mavenResolver.version>
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
|||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
@ -83,85 +84,71 @@ import org.eclipse.jetty.util.log.Logger;
|
|||
* <b>So API can change without any further notice.</b>
|
||||
* </p>
|
||||
*/
|
||||
public class DistributionTester {
|
||||
|
||||
private Process pid;
|
||||
|
||||
private URI baseUri;
|
||||
|
||||
private String jmxUrl;
|
||||
|
||||
private long waitStartTime = 60;
|
||||
|
||||
private long maxWaitToStop = 60000;
|
||||
|
||||
private TimeUnit timeUnit = TimeUnit.SECONDS;
|
||||
|
||||
private Path jettyBase;
|
||||
|
||||
private File jettyHomeDir;
|
||||
|
||||
private String jettyVersion;
|
||||
private String jettyHome;
|
||||
private String mavenLocalRepository = System.getProperty("user.home") + "/.m2/repository";
|
||||
public class DistributionTester implements Closeable
|
||||
{
|
||||
|
||||
private static final Logger LOGGER = Log.getLogger(DistributionTester.class);
|
||||
|
||||
private Process pid;
|
||||
private URI baseUri;
|
||||
private String jmxUrl;
|
||||
private Config config;
|
||||
private List<ConsoleStreamer> consoleStreamers = new ArrayList<>();
|
||||
private List<String> logs = new ArrayList<>();
|
||||
|
||||
private HttpClient httpClient;
|
||||
|
||||
private Map<String,String> mavenRemoteRepositories = new HashMap<>();
|
||||
private static class Config
|
||||
{
|
||||
private long waitStartTime = 60;
|
||||
|
||||
private DistributionTester(Path jettyBase)
|
||||
throws Exception {
|
||||
this.jettyBase = jettyBase;
|
||||
private long maxWaitToStop = 60000;
|
||||
|
||||
private TimeUnit timeUnit = TimeUnit.SECONDS;
|
||||
|
||||
private Path jettyBase;
|
||||
|
||||
private File jettyHomeDir;
|
||||
|
||||
private String jettyVersion;
|
||||
private String jettyHome;
|
||||
private String mavenLocalRepository = System.getProperty("user.home") + "/.m2/repository";
|
||||
private Map<String, String> mavenRemoteRepositories = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return DistributionTester setup jettyHome directory and start httpClient.
|
||||
* @throws Exception
|
||||
*/
|
||||
private DistributionTester initialise() throws Exception {
|
||||
|
||||
if (StringUtils.isNotEmpty(jettyHome)) {
|
||||
jettyHomeDir = Paths.get(jettyHome).toFile();
|
||||
} else {
|
||||
jettyHomeDir = resolveDistribution(this.jettyVersion);
|
||||
}
|
||||
|
||||
this.httpClient = new HttpClient();
|
||||
httpClient.start();
|
||||
|
||||
return this;
|
||||
|
||||
private DistributionTester(Config config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* start the instance with no arguments
|
||||
* @throws Exception
|
||||
*/
|
||||
public void start() throws Exception {
|
||||
public DistributionTester start() throws Exception
|
||||
{
|
||||
start(Collections.emptyList());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* start the instance with the arguments
|
||||
*
|
||||
* @param args arguments to use to start the distribution
|
||||
* @throws Exception
|
||||
*/
|
||||
public void start(String... args) throws Exception {
|
||||
public DistributionTester start(String... args) throws Exception
|
||||
{
|
||||
start(Arrays.asList(args));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* start the instance with the arguments
|
||||
*
|
||||
* @param args arguments to use to start the distribution
|
||||
* @throws Exception
|
||||
*/
|
||||
public void start(List<String> args)
|
||||
throws Exception {
|
||||
public DistributionTester start(List<String> args)
|
||||
throws Exception
|
||||
{
|
||||
|
||||
// do we want to be sure and use "--testing-mode" to not break surefire with a System.exit ???
|
||||
|
||||
|
@ -170,20 +157,20 @@ public class DistributionTester {
|
|||
List<String> commands = new ArrayList<>();
|
||||
commands.add(getJavaBin());
|
||||
|
||||
commands.add("-Djetty.home=" + jettyHomeDir.getAbsolutePath());
|
||||
commands.add("-Djetty.base=" + jettyBase.toAbsolutePath().toString());
|
||||
commands.add("-Djetty.home=" + config.jettyHomeDir.getAbsolutePath());
|
||||
commands.add("-Djetty.base=" + config.jettyBase.toAbsolutePath().toString());
|
||||
|
||||
commands.add("-jar");
|
||||
commands.add(jettyHomeDir.getAbsolutePath() + "/start.jar");
|
||||
commands.add(config.jettyHomeDir.getAbsolutePath() + "/start.jar");
|
||||
commands.add("jetty.http.port=0");
|
||||
|
||||
commands.addAll(args);
|
||||
|
||||
ProcessBuilder pbCmd = new ProcessBuilder(commands);
|
||||
pbCmd.directory(jettyHomeDir);
|
||||
pbCmd.directory(config.jettyHomeDir);
|
||||
|
||||
LOGGER.info("Executing: {}", commands);
|
||||
LOGGER.info("Working Dir: {}", jettyHomeDir.getAbsolutePath());
|
||||
LOGGER.info("Working Dir: {}", config.jettyHomeDir.getAbsolutePath());
|
||||
|
||||
pbCmd = new ProcessBuilder(commands);
|
||||
pid = pbCmd.start();
|
||||
|
@ -194,12 +181,12 @@ public class DistributionTester {
|
|||
List<String[]> connList =
|
||||
parser.newPattern("[A-Za-z]*Connector@.*\\{.*\\}\\{(.*)\\:([0-9]*)}", 1);
|
||||
|
||||
startPump("STDOUT", parser, this.pid.getInputStream());
|
||||
startPump("STDERR", parser, this.pid.getErrorStream());
|
||||
consoleStreamers.add(startPump("STDOUT", parser, this.pid.getInputStream()));
|
||||
consoleStreamers.add(startPump("STDERR", parser, this.pid.getErrorStream()));
|
||||
|
||||
try {
|
||||
long start = System.currentTimeMillis();
|
||||
parser.waitForDone(this.waitStartTime, this.timeUnit);
|
||||
parser.waitForDone(config.waitStartTime, config.timeUnit);
|
||||
LOGGER.info("wait start {}", System.currentTimeMillis() - start);
|
||||
|
||||
|
||||
|
@ -219,42 +206,169 @@ public class DistributionTester {
|
|||
} catch (InterruptedException e) {
|
||||
pid.destroy();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void installWarFile(File warFile, String context) throws IOException
|
||||
{
|
||||
//webapps
|
||||
Path webapps = Paths.get(config.jettyBase.toString(), "webapps", context);
|
||||
if (!Files.exists(webapps))
|
||||
{
|
||||
Files.createDirectories(webapps);
|
||||
}
|
||||
unzip(warFile, webapps.toFile());
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
// Assert methods
|
||||
//---------------------------------------
|
||||
|
||||
public DistributionTester assertLogsContains(String txt)
|
||||
{
|
||||
assertTrue(logs.stream().filter(s -> StringUtils.contains(s, txt)).count() > 0);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
private void startPump(String mode, ConsoleParser parser, InputStream inputStream) {
|
||||
public DistributionTester assertUrlStatus(String url, int expectedStatus)
|
||||
{
|
||||
try
|
||||
{
|
||||
ContentResponse contentResponse = httpClient.GET(getBaseUri() + url);
|
||||
int status = contentResponse.getStatus();
|
||||
assertEquals(expectedStatus, status, () -> "status not " + expectedStatus + " but " + status);
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException e)
|
||||
{
|
||||
fail(e.getMessage(), e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public DistributionTester assertUrlContains(String url, String content)
|
||||
{
|
||||
try
|
||||
{
|
||||
ContentResponse contentResponse = httpClient.GET(getBaseUri() + url);
|
||||
String contentResponseStr = contentResponse.getContentAsString();
|
||||
assertTrue(StringUtils.contains(contentResponseStr, content), () -> "content not containing '" + content + "'");
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException e)
|
||||
{
|
||||
fail(e.getMessage(), e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param coordinates <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
|
||||
* @return the artifact
|
||||
*/
|
||||
public File resolveArtifact(String coordinates) throws ArtifactResolutionException
|
||||
{
|
||||
RepositorySystem repositorySystem = newRepositorySystem();
|
||||
|
||||
Artifact artifact = new DefaultArtifact(coordinates);
|
||||
|
||||
RepositorySystemSession session = newRepositorySystemSession(repositorySystem);
|
||||
|
||||
ArtifactRequest artifactRequest = new ArtifactRequest();
|
||||
artifactRequest.setArtifact(artifact);
|
||||
artifactRequest.setRepositories(newRepositories(repositorySystem, newRepositorySystemSession(repositorySystem)));
|
||||
ArtifactResult artifactResult = repositorySystem.resolveArtifact(session, artifactRequest);
|
||||
|
||||
artifact = artifactResult.getArtifact();
|
||||
return artifact.getFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the directory used as JettyBase
|
||||
*/
|
||||
public Path getJettyBase()
|
||||
{
|
||||
return config.jettyBase;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the {@link URI} to use to access the jetty instance (random port has been detected)
|
||||
*/
|
||||
public URI getBaseUri()
|
||||
{
|
||||
return baseUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the connection to use to access JMX (if configured)
|
||||
*/
|
||||
public String getJmxUrl()
|
||||
{
|
||||
return jmxUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return DistributionTester setup jettyHome directory and start httpClient.
|
||||
*/
|
||||
private DistributionTester initialise() throws Exception
|
||||
{
|
||||
if (config.jettyBase == null)
|
||||
{
|
||||
config.jettyBase = Files.createTempDirectory("jetty_base_test");
|
||||
config.jettyBase.toFile().deleteOnExit();
|
||||
}
|
||||
|
||||
if (StringUtils.isNotEmpty(config.jettyHome))
|
||||
{
|
||||
config.jettyHomeDir = Paths.get(config.jettyHome).toFile();
|
||||
} else
|
||||
{
|
||||
config.jettyHomeDir = resolveDistribution(config.jettyVersion);
|
||||
}
|
||||
|
||||
this.httpClient = new HttpClient();
|
||||
httpClient.start();
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
private ConsoleStreamer startPump(String mode, ConsoleParser parser, InputStream inputStream)
|
||||
{
|
||||
ConsoleStreamer pump = new ConsoleStreamer(mode, inputStream);
|
||||
pump.setParser(parser);
|
||||
Thread thread = new Thread(pump, "ConsoleStreamer/" + mode);
|
||||
thread.start();
|
||||
return pump;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple streamer for the console output from a Process
|
||||
*/
|
||||
private class ConsoleStreamer
|
||||
implements Runnable {
|
||||
implements Runnable
|
||||
{
|
||||
private String mode;
|
||||
|
||||
private BufferedReader reader;
|
||||
|
||||
private ConsoleParser parser;
|
||||
|
||||
public ConsoleStreamer(String mode, InputStream is) {
|
||||
private volatile boolean stop;
|
||||
|
||||
public ConsoleStreamer(String mode, InputStream is)
|
||||
{
|
||||
this.mode = mode;
|
||||
this.reader = new BufferedReader(new InputStreamReader(is));
|
||||
}
|
||||
|
||||
public void setParser(ConsoleParser connector) {
|
||||
public void setParser(ConsoleParser connector)
|
||||
{
|
||||
this.parser = connector;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
public void run()
|
||||
{
|
||||
String line;
|
||||
try {
|
||||
while ((line = reader.readLine()) != (null)) {
|
||||
while ((line = reader.readLine()) != null && !stop) {
|
||||
if (parser != null) {
|
||||
parser.parse(line);
|
||||
}
|
||||
|
@ -271,32 +385,39 @@ public class DistributionTester {
|
|||
}
|
||||
}
|
||||
|
||||
private static class ConsoleParser {
|
||||
private static class ConsoleParser
|
||||
{
|
||||
private List<ConsolePattern> patterns = new ArrayList<>();
|
||||
|
||||
private CountDownLatch latch;
|
||||
|
||||
private int count;
|
||||
|
||||
public List<String[]> newPattern(String exp, int cnt) {
|
||||
public List<String[]> newPattern(String exp, int cnt)
|
||||
{
|
||||
ConsolePattern pat = new ConsolePattern(exp, cnt);
|
||||
patterns.add(pat);
|
||||
count += cnt;
|
||||
return pat.getMatches();
|
||||
}
|
||||
|
||||
public void parse(String line) {
|
||||
for (ConsolePattern pat : patterns) {
|
||||
public void parse(String line)
|
||||
{
|
||||
for (ConsolePattern pat : patterns)
|
||||
{
|
||||
Matcher mat = pat.getMatcher(line);
|
||||
if (mat.find()) {
|
||||
if (mat.find())
|
||||
{
|
||||
int num = 0, count = mat.groupCount();
|
||||
String[] match = new String[count];
|
||||
while (num++ < count) {
|
||||
while (num++ < count)
|
||||
{
|
||||
match[num - 1] = mat.group(num);
|
||||
}
|
||||
pat.getMatches().add(match);
|
||||
|
||||
if (pat.getCount() > 0) {
|
||||
if (pat.getCount() > 0)
|
||||
{
|
||||
getLatch().countDown();
|
||||
}
|
||||
}
|
||||
|
@ -304,13 +425,17 @@ public class DistributionTester {
|
|||
}
|
||||
|
||||
public void waitForDone(long timeout, TimeUnit unit)
|
||||
throws InterruptedException {
|
||||
throws InterruptedException
|
||||
{
|
||||
getLatch().await(timeout, unit);
|
||||
}
|
||||
|
||||
private CountDownLatch getLatch() {
|
||||
synchronized (this) {
|
||||
if (latch == null) {
|
||||
private CountDownLatch getLatch()
|
||||
{
|
||||
synchronized (this)
|
||||
{
|
||||
if (latch == null)
|
||||
{
|
||||
latch = new CountDownLatch(count);
|
||||
}
|
||||
}
|
||||
|
@ -318,91 +443,63 @@ public class DistributionTester {
|
|||
}
|
||||
}
|
||||
|
||||
private static class ConsolePattern {
|
||||
private static class ConsolePattern
|
||||
{
|
||||
private Pattern pattern;
|
||||
|
||||
private List<String[]> matches;
|
||||
|
||||
private int count;
|
||||
|
||||
ConsolePattern(String exp, int cnt) {
|
||||
ConsolePattern(String exp, int cnt)
|
||||
{
|
||||
pattern = Pattern.compile(exp);
|
||||
matches = new ArrayList<>();
|
||||
count = cnt;
|
||||
}
|
||||
|
||||
public Matcher getMatcher(String line) {
|
||||
public Matcher getMatcher(String line)
|
||||
{
|
||||
return pattern.matcher(line);
|
||||
}
|
||||
|
||||
public List<String[]> getMatches() {
|
||||
public List<String[]> getMatches()
|
||||
{
|
||||
return matches;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
public int getCount()
|
||||
{
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void installWarFile(File warFile, String context) throws IOException {
|
||||
//webapps
|
||||
Path webapps = Paths.get(jettyBase.toString(),"webapps", context);
|
||||
if (!Files.exists(webapps)) {
|
||||
Files.createDirectories(webapps);
|
||||
}
|
||||
unzip(warFile, webapps.toFile());
|
||||
}
|
||||
|
||||
//---------------------------------------
|
||||
// Assert methods
|
||||
//---------------------------------------
|
||||
|
||||
public void assertLogsContains(String txt){
|
||||
assertTrue(logs.stream().filter(s -> StringUtils.contains(s, txt)).count()>0);
|
||||
}
|
||||
|
||||
|
||||
public void assertUrlStatus(String url, int expectedStatus){
|
||||
try {
|
||||
ContentResponse contentResponse = httpClient.GET(getBaseUri() + url);
|
||||
int status = contentResponse.getStatus();
|
||||
assertEquals(expectedStatus, status, () -> "status not " + expectedStatus + " but " + status);
|
||||
} catch (InterruptedException|ExecutionException|TimeoutException e) {
|
||||
fail(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
public void assertUrlContains(String url, String content){
|
||||
try {
|
||||
ContentResponse contentResponse = httpClient.GET(getBaseUri() + url);
|
||||
String contentResponseStr = contentResponse.getContentAsString();
|
||||
assertTrue(StringUtils.contains(contentResponseStr, content), () -> "content not containing '" + content + "'");
|
||||
} catch (InterruptedException|ExecutionException|TimeoutException e) {
|
||||
fail(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void unzip(File zipFile, File output) throws IOException {
|
||||
private void unzip(File zipFile, File output) throws IOException
|
||||
{
|
||||
try (InputStream fileInputStream = Files.newInputStream(zipFile.toPath());
|
||||
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream))
|
||||
ZipInputStream zipInputStream = new ZipInputStream(fileInputStream))
|
||||
{
|
||||
ZipEntry entry = zipInputStream.getNextEntry();
|
||||
while (entry != null) {
|
||||
if (entry.isDirectory()) {
|
||||
while (entry != null)
|
||||
{
|
||||
if (entry.isDirectory())
|
||||
{
|
||||
File dir = new File(output, entry.getName());
|
||||
if (!Files.exists(dir.toPath())) {
|
||||
Files.createDirectories(dir.toPath());
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
{
|
||||
// Read zipEntry and write to a file.
|
||||
File file = new File(output, entry.getName());
|
||||
if(!Files.exists(file.getParentFile().toPath())){
|
||||
if (!Files.exists(file.getParentFile().toPath()))
|
||||
{
|
||||
Files.createDirectories(file.getParentFile().toPath());
|
||||
}
|
||||
try (OutputStream outputStream = Files.newOutputStream(file.toPath())) {
|
||||
IOUtil.copy(zipInputStream,outputStream);
|
||||
try (OutputStream outputStream = Files.newOutputStream(file.toPath()))
|
||||
{
|
||||
IOUtil.copy(zipInputStream, outputStream);
|
||||
}
|
||||
}
|
||||
// Get next entry
|
||||
|
@ -415,29 +512,8 @@ public class DistributionTester {
|
|||
// Maven Utils methods
|
||||
//---------------------------------------
|
||||
|
||||
/**
|
||||
*
|
||||
* @param coordinates <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>
|
||||
* @return the artifact
|
||||
* @throws ArtifactResolutionException
|
||||
*/
|
||||
public File resolveArtifact(String coordinates) throws ArtifactResolutionException {
|
||||
RepositorySystem repositorySystem = newRepositorySystem();
|
||||
|
||||
Artifact artifact = new DefaultArtifact(coordinates);
|
||||
|
||||
RepositorySystemSession session = newRepositorySystemSession(repositorySystem);
|
||||
|
||||
ArtifactRequest artifactRequest = new ArtifactRequest();
|
||||
artifactRequest.setArtifact(artifact);
|
||||
artifactRequest.setRepositories(newRepositories(repositorySystem, newRepositorySystemSession(repositorySystem)));
|
||||
ArtifactResult artifactResult = repositorySystem.resolveArtifact(session, artifactRequest);
|
||||
|
||||
artifact = artifactResult.getArtifact();
|
||||
return artifact.getFile();
|
||||
}
|
||||
|
||||
private File resolveDistribution(String version) throws Exception {
|
||||
private File resolveDistribution(String version) throws Exception
|
||||
{
|
||||
File artifactFile = resolveArtifact("org.eclipse.jetty:jetty-distribution:zip:" + version);
|
||||
|
||||
// create tmp directory to unzip distribution
|
||||
|
@ -449,16 +525,18 @@ public class DistributionTester {
|
|||
}
|
||||
|
||||
|
||||
|
||||
private RepositorySystem newRepositorySystem() {
|
||||
private RepositorySystem newRepositorySystem()
|
||||
{
|
||||
DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
|
||||
locator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
|
||||
locator.addService(TransporterFactory.class, FileTransporterFactory.class);
|
||||
locator.addService(TransporterFactory.class, HttpTransporterFactory.class);
|
||||
|
||||
locator.setErrorHandler(new DefaultServiceLocator.ErrorHandler() {
|
||||
locator.setErrorHandler(new DefaultServiceLocator.ErrorHandler()
|
||||
{
|
||||
@Override
|
||||
public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception) {
|
||||
public void serviceCreationFailed(Class<?> type, Class<?> impl, Throwable exception)
|
||||
{
|
||||
LOGGER.warn("Service creation failed for {} implementation {}: {}",
|
||||
type, impl, exception.getMessage(), exception);
|
||||
}
|
||||
|
@ -467,24 +545,27 @@ public class DistributionTester {
|
|||
return locator.getService(RepositorySystem.class);
|
||||
}
|
||||
|
||||
private List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session) {
|
||||
List<RemoteRepository> remoteRepositories = new ArrayList<>(this.mavenRemoteRepositories.size()+1);
|
||||
this.mavenRemoteRepositories.entrySet().stream().forEach(stringStringEntry -> {
|
||||
remoteRepositories.add(new RemoteRepository.Builder(stringStringEntry.getKey(), "default", stringStringEntry.getValue()).build());
|
||||
});
|
||||
private List<RemoteRepository> newRepositories(RepositorySystem system, RepositorySystemSession session)
|
||||
{
|
||||
List<RemoteRepository> remoteRepositories = new ArrayList<>(config.mavenRemoteRepositories.size() + 1);
|
||||
config.mavenRemoteRepositories.entrySet().stream().forEach( stringStringEntry ->
|
||||
remoteRepositories.add(new RemoteRepository.Builder(stringStringEntry.getKey(), "default", stringStringEntry.getValue()).build())
|
||||
);
|
||||
remoteRepositories.add(newCentralRepository());
|
||||
return remoteRepositories;
|
||||
}
|
||||
|
||||
private static RemoteRepository newCentralRepository() {
|
||||
private static RemoteRepository newCentralRepository()
|
||||
{
|
||||
return new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2/").build();
|
||||
}
|
||||
|
||||
private DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system) {
|
||||
private DefaultRepositorySystemSession newRepositorySystemSession(RepositorySystem system)
|
||||
{
|
||||
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
|
||||
|
||||
|
||||
LocalRepository localRepo = new LocalRepository(mavenLocalRepository);
|
||||
LocalRepository localRepo = new LocalRepository(config.mavenLocalRepository);
|
||||
session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
|
||||
|
||||
session.setTransferListener(new LogTransferListener());
|
||||
|
@ -493,11 +574,13 @@ public class DistributionTester {
|
|||
return session;
|
||||
}
|
||||
|
||||
private static class LogTransferListener extends AbstractTransferListener {
|
||||
private static class LogTransferListener extends AbstractTransferListener
|
||||
{
|
||||
// no op
|
||||
}
|
||||
|
||||
private static class LogRepositoryListener extends AbstractRepositoryListener {
|
||||
private static class LogRepositoryListener extends AbstractRepositoryListener
|
||||
{
|
||||
|
||||
@Override
|
||||
public void artifactDownloaded(RepositoryEvent event) {
|
||||
|
@ -511,12 +594,13 @@ public class DistributionTester {
|
|||
}
|
||||
|
||||
|
||||
|
||||
private String getJavaBin() {
|
||||
private String getJavaBin()
|
||||
{
|
||||
String javaexes[] = new String[]{"java", "java.exe"};
|
||||
|
||||
File javaHomeDir = new File(System.getProperty("java.home"));
|
||||
for (String javaexe : javaexes) {
|
||||
for (String javaexe : javaexes)
|
||||
{
|
||||
File javabin = new File(javaHomeDir, FS.separators("bin/" + javaexe));
|
||||
if (javabin.exists() && javabin.isFile()) {
|
||||
return javabin.getAbsolutePath();
|
||||
|
@ -527,85 +611,72 @@ public class DistributionTester {
|
|||
|
||||
/**
|
||||
* Stop the distribution
|
||||
* @throws Exception
|
||||
*/
|
||||
public void stop()
|
||||
throws Exception {
|
||||
|
||||
throws IOException
|
||||
{
|
||||
long start = System.currentTimeMillis();
|
||||
while (this.pid.isAlive()&&(System.currentTimeMillis()-start<maxWaitToStop)) {
|
||||
while (this.pid.isAlive() && (System.currentTimeMillis() - start < config.maxWaitToStop))
|
||||
{
|
||||
this.pid.destroy();
|
||||
if(this.pid.isAlive()){
|
||||
if (this.pid.isAlive())
|
||||
{
|
||||
// wait a bit to try again
|
||||
Thread.sleep(500);
|
||||
try
|
||||
{
|
||||
Thread.sleep(500);
|
||||
} catch (InterruptedException e)
|
||||
{
|
||||
throw new IOException(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
}
|
||||
// still alive so force stop
|
||||
if(this.pid.isAlive()){
|
||||
if (this.pid.isAlive())
|
||||
{
|
||||
LOGGER.info("still alive so force destroy process");
|
||||
this.pid.destroyForcibly();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* remove the content from JettyBase directory
|
||||
* @throws IOException
|
||||
*/
|
||||
public void cleanJettyBase() throws IOException {
|
||||
FileUtils.cleanDirectory(this.jettyBase.toFile());
|
||||
consoleStreamers.forEach(consoleStreamer -> consoleStreamer.stop=true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to use in finally block of a test and when using @After in a unit test.
|
||||
* if running, it stops the distribution.
|
||||
* Cleanup JettyBase and JettyHome directories
|
||||
* @throws Exception
|
||||
*/
|
||||
public void cleanup() throws Exception {
|
||||
public void close() throws IOException
|
||||
{
|
||||
stop();
|
||||
if(httpClient!=null&&httpClient.isRunning()){
|
||||
httpClient.stop();
|
||||
if (httpClient != null && httpClient.isRunning()) {
|
||||
try
|
||||
{
|
||||
httpClient.stop();
|
||||
} catch (Exception e)
|
||||
{
|
||||
throw new IOException(e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
if (Files.exists(this.jettyBase)) {
|
||||
if (Files.exists(config.jettyBase))
|
||||
{
|
||||
// cleanup jetty base
|
||||
IO.delete(this.jettyBase.toFile());
|
||||
IO.delete(config.jettyBase.toFile());
|
||||
}
|
||||
if (Files.exists(this.jettyHomeDir.toPath())) {
|
||||
if (Files.exists(config.jettyHomeDir.toPath()))
|
||||
{
|
||||
// cleanup jetty distribution
|
||||
IO.delete(this.jettyHomeDir);
|
||||
IO.delete(config.jettyHomeDir);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the directory used as JettyBase
|
||||
*/
|
||||
public Path getJettyBase() {
|
||||
return jettyBase;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the {@link URI} to use to access the jetty instance (random port has been detected)
|
||||
*/
|
||||
public URI getBaseUri() {
|
||||
return baseUri;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return the connection to use to access JMX (if configured)
|
||||
*/
|
||||
public String getJmxUrl() {
|
||||
return jmxUrl;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------
|
||||
// Builder class
|
||||
//---------------------------------------
|
||||
public static class Builder {
|
||||
private Builder() {
|
||||
public static class Builder
|
||||
{
|
||||
private Builder()
|
||||
{
|
||||
// no op
|
||||
}
|
||||
|
||||
|
@ -621,102 +692,100 @@ public class DistributionTester {
|
|||
|
||||
private long maxWaitToStop = 60000;
|
||||
|
||||
private Map<String,String> mavenRemoteRepositories = new HashMap<>();
|
||||
private Map<String, String> mavenRemoteRepositories = new HashMap<>();
|
||||
|
||||
/**
|
||||
*
|
||||
* @param jettyVersion the version to use (format: 9.4.14.v20181114 9.4.15-SNAPSHOT).
|
||||
* The distribution will be downloaded from local repository or remote
|
||||
* The distribution will be downloaded from local repository or remote
|
||||
* @return the {@link Builder}
|
||||
*/
|
||||
public Builder jettyVersion(String jettyVersion) {
|
||||
public Builder jettyVersion(String jettyVersion)
|
||||
{
|
||||
this.jettyVersion = jettyVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param jettyHome Path to the local exploded jetty distribution
|
||||
* if configured the jettyVersion parameter will not be used
|
||||
* @return the {@link Builder}
|
||||
*/
|
||||
public Builder jettyHome(String jettyHome) {
|
||||
public Builder jettyHome(String jettyHome)
|
||||
{
|
||||
this.jettyHome = jettyHome;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param mavenLocalRepository Path to the local maven repository
|
||||
* @return the {@link Builder}
|
||||
*/
|
||||
public Builder mavenLocalRepository(String mavenLocalRepository) {
|
||||
public Builder mavenLocalRepository(String mavenLocalRepository)
|
||||
{
|
||||
this.mavenLocalRepository = mavenLocalRepository;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param waitStartTime the maximum time in seconds to wait the start of the distribution
|
||||
* @return the {@link Builder}
|
||||
*/
|
||||
public Builder waitStartTime(long waitStartTime) {
|
||||
public Builder waitStartTime(long waitStartTime)
|
||||
{
|
||||
this.waitStartTime = waitStartTime;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param maxWaitToStop the maximum time in seconds to wait after stop the distribution
|
||||
* process before forcing the stop.
|
||||
* @return the {@link Builder}
|
||||
*/
|
||||
public Builder maxWaitToStop(long maxWaitToStop) {
|
||||
public Builder maxWaitToStop(long maxWaitToStop)
|
||||
{
|
||||
this.maxWaitToStop = maxWaitToStop;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* If needed to resolve JettyDistribtion from another Maven remote repositories
|
||||
*
|
||||
* @param id the id
|
||||
* @param url the Maven remote repository url
|
||||
* @return the {@link Builder}
|
||||
*/
|
||||
public Builder addRemoteRepository(String id, String url) {
|
||||
public Builder addRemoteRepository(String id, String url)
|
||||
{
|
||||
this.mavenRemoteRepositories.put(id, url);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return an empty instance of {@link Builder}
|
||||
*/
|
||||
public static Builder newInstance() {
|
||||
public static Builder newInstance()
|
||||
{
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return a new configured instance of {@link DistributionTester}
|
||||
* @throws Exception
|
||||
*/
|
||||
public DistributionTester build()
|
||||
throws Exception {
|
||||
if (jettyBase == null) {
|
||||
this.jettyBase = Files.createTempDirectory("jetty_base_test");
|
||||
this.jettyBase.toFile().deleteOnExit();
|
||||
throws Exception
|
||||
{
|
||||
Config config = new Config();
|
||||
config.jettyBase = jettyBase;
|
||||
config.jettyVersion = jettyVersion;
|
||||
config.jettyHome = jettyHome;
|
||||
config.mavenLocalRepository = mavenLocalRepository;
|
||||
config.waitStartTime = waitStartTime;
|
||||
config.maxWaitToStop = maxWaitToStop;
|
||||
if (!this.mavenRemoteRepositories.isEmpty())
|
||||
{
|
||||
config.mavenRemoteRepositories.putAll(this.mavenRemoteRepositories);
|
||||
}
|
||||
DistributionTester distributionTester = new DistributionTester(jettyBase);
|
||||
distributionTester.jettyVersion = jettyVersion;
|
||||
distributionTester.jettyHome = jettyHome;
|
||||
distributionTester.mavenLocalRepository = mavenLocalRepository;
|
||||
distributionTester.waitStartTime = waitStartTime;
|
||||
distributionTester.maxWaitToStop = maxWaitToStop;
|
||||
if(!this.mavenRemoteRepositories.isEmpty()) {
|
||||
distributionTester.mavenRemoteRepositories.putAll(this.mavenRemoteRepositories);
|
||||
}
|
||||
return distributionTester.initialise();
|
||||
return new DistributionTester(config).initialise();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,25 +27,22 @@ public class HttpModuleTests
|
|||
@Test
|
||||
public void http_module() throws Exception
|
||||
{
|
||||
DistributionTester distributionTester = DistributionTester.Builder.newInstance() //
|
||||
.jettyVersion(System.getProperty("jetty_version")) //
|
||||
.mavenLocalRepository(System.getProperty("mavenRepoPath")) //
|
||||
.waitStartTime(30) //
|
||||
.build(); //
|
||||
try
|
||||
|
||||
try(DistributionTester distributionTester = DistributionTester.Builder.newInstance() //
|
||||
.jettyVersion(System.getProperty("jetty_version")) //
|
||||
.mavenLocalRepository(System.getProperty("mavenRepoPath")) //
|
||||
.waitStartTime(30) //
|
||||
.build())
|
||||
{
|
||||
distributionTester.start("--create-startd", "--approve-all-licenses", "--add-to-start=resources,server,http,webapp,deploy,jsp,jmx,jmx-remote,servlet,servlets");
|
||||
distributionTester.stop();
|
||||
|
||||
File war = distributionTester.resolveArtifact("org.eclipse.jetty.tests:test-simple-webapp:war:" + System.getProperty("jetty_version"));
|
||||
distributionTester.installWarFile(war, "test");
|
||||
distributionTester.start();
|
||||
distributionTester.assertLogsContains("Started @");
|
||||
distributionTester.assertUrlStatus("/test/index.jsp", 200);
|
||||
distributionTester.assertUrlContains("/test/index.jsp", "Hello");
|
||||
} finally {
|
||||
distributionTester.stop();
|
||||
distributionTester.cleanup();
|
||||
distributionTester.start() //
|
||||
.assertLogsContains("Started @") //
|
||||
.assertUrlStatus("/test/index.jsp", 200) //
|
||||
.assertUrlContains("/test/index.jsp", "Hello");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -30,25 +30,22 @@ public class JpmsActivatedTests
|
|||
@DisabledOnJre(JRE.JAVA_8)
|
||||
public void jpms_activated() throws Exception
|
||||
{
|
||||
DistributionTester distributionTester = DistributionTester.Builder.newInstance() //
|
||||
.jettyVersion(System.getProperty("jetty_version")) //
|
||||
.mavenLocalRepository(System.getProperty("mavenRepoPath")) //
|
||||
.waitStartTime(30) //
|
||||
.build(); //
|
||||
try
|
||||
|
||||
try(DistributionTester distributionTester = DistributionTester.Builder.newInstance() //
|
||||
.jettyVersion(System.getProperty("jetty_version")) //
|
||||
.mavenLocalRepository(System.getProperty("mavenRepoPath")) //
|
||||
.waitStartTime(30) //
|
||||
.build())
|
||||
{
|
||||
distributionTester.start("--create-startd", "--approve-all-licenses", "--add-to-start=resources,server,http,webapp,deploy,jsp,jmx,jmx-remote,servlet,servlets");
|
||||
distributionTester.stop();
|
||||
|
||||
File war = distributionTester.resolveArtifact("org.eclipse.jetty.tests:test-simple-webapp:war:" + System.getProperty("jetty_version"));
|
||||
distributionTester.installWarFile(war, "test");
|
||||
distributionTester.start("--jpms");
|
||||
distributionTester.assertLogsContains("Started @");
|
||||
distributionTester.assertUrlStatus("/test/index.jsp", 200);
|
||||
distributionTester.assertUrlContains("/test/index.jsp", "Hello");
|
||||
} finally {
|
||||
distributionTester.stop();
|
||||
distributionTester.cleanup();
|
||||
distributionTester.start("--jpms") //
|
||||
.assertLogsContains("Started @") //
|
||||
.assertUrlStatus("/test/index.jsp", 200)
|
||||
.assertUrlContains("/test/index.jsp", "Hello");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue