Skip to content

Commit

Permalink
Merge pull request #76 from TNG/fix-windows-urls
Browse files Browse the repository at this point in the history
URLs should now be resolved in a platform independent way
  • Loading branch information
codecholeric authored Jun 2, 2018
2 parents ebe5130 + ab0623f commit 0730281
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand All @@ -28,7 +29,6 @@
import com.google.common.base.Splitter;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.net.UrlEscapers;
import com.tngtech.archunit.Internal;
import com.tngtech.archunit.base.ArchUnitException.LocationException;
import com.tngtech.archunit.base.Optional;
Expand Down Expand Up @@ -88,22 +88,22 @@ private static Optional<URL> parseClassPathEntry(String path) {

private static Optional<URL> newFileUri(String path) {
path = path.endsWith("/") || path.endsWith(".class") ? path : path + "/";
return newUrl("file", path);
try {
return Optional.of(Paths.get(path).toUri().toURL());
} catch (MalformedURLException e) {
LOG.warn("Cannot parse URL from path " + path, e);
return Optional.absent();
}
}

private static Optional<URL> newJarUri(String path) {
return newUrl("jar:file", path + "!/");
}
Optional<URL> fileUri = newFileUri(path);

private static Optional<URL> newUrl(String protocol, String path) {
try {
return Optional.of(new URL(protocol + "://" + UrlEscapers.urlFragmentEscaper().escape(path)));
return fileUri.isPresent() ? Optional.of(new URL("jar:" + fileUri.get() + "!/")) : Optional.<URL>absent();
} catch (MalformedURLException e) {
LOG.warn("Cannot parse URL from path " + path, e);
return Optional.absent();
} catch (IllegalArgumentException e) {
LOG.warn("Cannot escape fragments from path " + path, e);
return Optional.absent();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void iterate_entries_of_package_of_jar_url() throws Exception {
public void iterate_entries_of_non_existing_jar_url() throws Exception {
File nonExistingJar = new File(createNonExistingFolder(), "not-there.jar");

Location location = Location.of(URI.create("jar:file:" + nonExistingJar.getAbsolutePath() + "!/"));
Location location = Location.of(URI.create("jar:" + nonExistingJar.toURI() + "!/"));

assertThat(location.iterateEntries())
.as("entries of JAR")
Expand All @@ -129,7 +129,7 @@ public void iterate_entries_of_file_url() throws Exception {

@Test
public void iterate_entries_of_non_existing_file_url() throws Exception {
Location location = Location.of(URI.create("file:" + createNonExistingFolder().getAbsolutePath()));
Location location = Location.of(createNonExistingFolder().toURI());

assertThat(location.iterateEntries())
.as("entries of DIR")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,28 @@ public class UrlSourceTest {

@Test
public void resolves_from_system_property() throws MalformedURLException {
String classPath = createClassPathProperty(
"/some/path/classes", "/other/lib/some.jar", "/more/classes", "/my/.m2/repo/greatlib.jar");
Path firstFileEntry = Paths.get("some", "path", "classes");
Path firstJarEntry = Paths.get("other", "lib", "some.jar");
Path secondFileEntry = Paths.get("more", "classes");
Path secondJarEntry = Paths.get("my", ".m2", "repo", "greatlib.jar");
String classPath = createClassPathProperty(firstFileEntry.toString(), firstJarEntry.toString(),
secondFileEntry.toString(), secondJarEntry.toString());
System.setProperty(JAVA_CLASS_PATH_PROP, classPath);
String bootstrapClassPath = createClassPathProperty("/some/bootstrap/classes", "/more/bootstrap/bootlib.jar");

Path bootstrapFileEntry = Paths.get("some", "bootstrap", "classes");
Path bootstrapJarEntry = Paths.get("more", "bootstrap", "bootlib.jar");
String bootstrapClassPath = createClassPathProperty(bootstrapFileEntry.toString(), bootstrapJarEntry.toString());
System.setProperty(JAVA_BOOT_PATH_PROP, bootstrapClassPath);

UrlSource urlSource = UrlSource.From.classPathSystemProperties();

assertThat(urlSource).containsOnly(
new URL("file:/some/path/classes/"),
new URL("jar:file:/other/lib/some.jar!/"),
new URL("file:/more/classes/"),
new URL("jar:file:/my/.m2/repo/greatlib.jar!/"),
new URL("file:/some/bootstrap/classes/"),
new URL("jar:file:/more/bootstrap/bootlib.jar!/")
firstFileEntry.toUri().toURL(),
new URL("jar:" + firstJarEntry.toUri() + "!/"),
secondFileEntry.toUri().toURL(),
new URL("jar:" + secondJarEntry.toUri() + "!/"),
bootstrapFileEntry.toUri().toURL(),
new URL("jar:" + bootstrapJarEntry.toUri() + "!/")
);
}

Expand Down

0 comments on commit 0730281

Please sign in to comment.