Skip to content

Commit

Permalink
#37: Introduce --classpath to add engines
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Dec 4, 2015
1 parent eb1f9f4 commit 37ba9a2
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2015 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.console;

import static java.util.stream.Collectors.toList;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

class ClasspathEntriesParser {

URL[] toURLs(List<String> additionalClasspathEntries) {
String pathSeparatorPattern = Pattern.quote(File.pathSeparator);
// @formatter:off
List<URL> urls = additionalClasspathEntries.stream()
.map(entry -> entry.split(pathSeparatorPattern))
.flatMap(Arrays::stream)
.map(this::toURL)
.collect(toList());
// @formatter:on
return urls.toArray(new URL[urls.size()]);
}

private URL toURL(String value) {
try {
return new File(value).toURI().toURL();
}
catch (MalformedURLException e) {
throw new RuntimeException("Erroneous classpath entry: " + value, e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import java.io.File;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -73,6 +75,8 @@ public ConsoleRunner(CommandLineOptions options) {
}

private TestExecutionSummary run() {
updateClassLoader();

// TODO Configure launcher?
Launcher launcher = new Launcher();

Expand All @@ -86,6 +90,16 @@ private TestExecutionSummary run() {
return summary;
}

private void updateClassLoader() {
List<String> additionalClasspathEntries = options.getAdditionalClasspathEntries();
if (!additionalClasspathEntries.isEmpty()) {
URL[] urls = new ClasspathEntriesParser().toURLs(additionalClasspathEntries);
ClassLoader parentClassLoader = ReflectionUtils.getDefaultClassLoader();
URLClassLoader customClassLoader = URLClassLoader.newInstance(urls, parentClassLoader);
Thread.currentThread().setContextClassLoader(customClassLoader);
}
}

private void registerListeners(Launcher launcher, TestExecutionSummary summary) {
SummaryCreatingTestListener testSummaryListener = new SummaryCreatingTestListener(summary);
launcher.registerTestPlanExecutionListeners(testSummaryListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class AvailableOptions {
final OptionSpec<Void> hideDetails;
final OptionSpec<String> classnameFilter;
final OptionSpec<String> tagFilter;
final OptionSpec<String> additionalClasspathEntries;
final OptionSpec<String> arguments;

AvailableOptions() {
Expand All @@ -45,6 +46,9 @@ class AvailableOptions {
tagFilter = parser.acceptsAll(asList("t", "filter-tags"),
"Give a tag to include in the test run. This option can be repeated.") //
.withRequiredArg();
additionalClasspathEntries = parser.acceptsAll(asList("p", "classpath"), //
"Additional classpath entries, e.g. for adding engines and their dependencies") //
.withRequiredArg();
arguments = parser.nonOptions("Test classes, methods or packages to execute. If --all|-a has been chosen, "
+ "arguments can list all classpath roots that should be considered for test scanning, "
+ "or none if the full classpath shall be scanned.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ public interface CommandLineOptions {

List<String> getArguments();

List<String> getAdditionalClasspathEntries();

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public List<String> getTagsFilter() {
return detectedOptions.valuesOf(options.tagFilter);
}

@Override
public List<String> getAdditionalClasspathEntries() {
return detectedOptions.valuesOf(options.additionalClasspathEntries);
}

@Override
public List<String> getArguments() {
return detectedOptions.valuesOf(options.arguments);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2015 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.junit.gen5.console;

import static java.lang.String.join;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.gen5.api.Assertions.assertEquals;

import java.io.File;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;

import org.junit.Test;

public class ClasspathEntriesParserTest {

@Test
public void toURLsWithoutEntries() {
URL[] urls = new ClasspathEntriesParser().toURLs(emptyList());

assertEquals(0, urls.length);
}

@Test
public void toURLsWithSinglePath() throws Exception {
Path root = getFileSystemRoot();
Path path = root.resolve("bin");

URL[] urls = new ClasspathEntriesParser().toURLs(singletonList(path.toString()));

assertEquals(1, urls.length);
assertEquals(path.toUri().toURL(), urls[0]);
}

@Test
public void toURLsWithMultiplePathsInMultipleEntries() throws Exception {
Path root = getFileSystemRoot();
Path path1 = root.resolve("foo");
Path path2 = root.resolve("bar");

URL[] urls = new ClasspathEntriesParser().toURLs(asList(path1.toString(), path2.toString()));

assertEquals(2, urls.length);
assertEquals(path1.toUri().toURL(), urls[0]);
assertEquals(path2.toUri().toURL(), urls[1]);
}

@Test
public void toURLsWithMultiplePathsInSingleEntry() throws Exception {
Path root = getFileSystemRoot();
Path path1 = root.resolve("foo");
Path path2 = root.resolve("bar");

URL[] urls = new ClasspathEntriesParser().toURLs(
singletonList(join(File.pathSeparator, path1.toString(), path2.toString())));

assertEquals(2, urls.length);
assertEquals(path1.toUri().toURL(), urls[0]);
assertEquals(path2.toUri().toURL(), urls[1]);
}

private Path getFileSystemRoot() {
return FileSystems.getDefault().getRootDirectories().iterator().next();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.gen5.api.Assertions.assertThrows;
import static org.junit.gen5.api.Assertions.assertTrue;

import java.io.File;
import java.util.Optional;
import java.util.function.Predicate;

Expand All @@ -39,6 +40,7 @@ public void parseNoArguments() {
() -> assertFalse(options.isRunAllTests()),
() -> assertEquals(Optional.empty(), options.getClassnameFilter()),
() -> assertEquals(emptyList(), options.getTagsFilter()),
() -> assertEquals(emptyList(), options.getAdditionalClasspathEntries()),
() -> assertEquals(emptyList(), options.getArguments())
);
// @formatter:on
Expand Down Expand Up @@ -100,6 +102,29 @@ public void parseInvalidTagFilter() {
// @formatter:on
}

@Test
public void parseValidAdditionalClasspathEntries() {
// @formatter:off
assertAll(
() -> assertEquals(asList("."), parseArgLine("-p .").getAdditionalClasspathEntries()),
() -> assertEquals(asList("."), parseArgLine("--classpath .").getAdditionalClasspathEntries()),
() -> assertEquals(asList("."), parseArgLine("--classpath=.").getAdditionalClasspathEntries()),
() -> assertEquals(asList(".", "lib/some.jar"), parseArgLine("-p . -p lib/some.jar").getAdditionalClasspathEntries()),
() -> assertEquals(asList("." + File.pathSeparator + "lib/some.jar"), parseArgLine("-p ." + File.pathSeparator + "lib/some.jar").getAdditionalClasspathEntries())
);
// @formatter:on
}

@Test
public void parseInvalidAdditionalClasspathEntries() {
// @formatter:off
assertAll(
() -> assertThrows(Exception.class, () -> parseArgLine("-p")),
() -> assertThrows(Exception.class, () -> parseArgLine("--classpath"))
);
// @formatter:on
}

@Test
public void parseExtraArguments() {
// @formatter:off
Expand Down

0 comments on commit 37ba9a2

Please sign in to comment.