-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#37: Introduce --classpath to add engines
- Loading branch information
1 parent
eb1f9f4
commit 37ba9a2
Showing
7 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
junit-console/src/main/java/org/junit/gen5/console/ClasspathEntriesParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
junit-tests/src/test/java/org/junit/gen5/console/ClasspathEntriesParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters