Skip to content

Commit 2cbaab8

Browse files
authored
Normalize backslashes of Windows paths TNG#357
Now that we uniformly search all classpath resources by iterating the classpath URLs and matching them against the specified classes to import (where we used `ClassLoader.getResources(..)` in the past), we also need to normalize URIs by replacing Windows path separators `\` with Unix path separators `/`. Otherwise the mechanism does not work for file URI classpath resources like `file:/C:\foo\bar\..`. Fixes TNG#356
2 parents 0c23de0 + 30f4f95 commit 2cbaab8

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

archunit/src/main/java/com/tngtech/archunit/core/importer/NormalizedResourceName.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ class NormalizedResourceName {
2121
private final String resourceName;
2222

2323
private NormalizedResourceName(String resourceName) {
24-
resourceName = resourceName.replaceAll("^/*", "").replaceAll("/*$", "");
25-
this.resourceName = resourceName;
24+
this.resourceName = resourceName
25+
// normalize Windows backslashes
26+
.replace('\\', '/')
27+
// remove leading slashes
28+
.replaceAll("^/*", "")
29+
// remove trailing slashes
30+
.replaceAll("/*$", "");
2631
}
2732

2833
static NormalizedResourceName from(String resourceName) {

archunit/src/test/java/com/tngtech/archunit/core/importer/NormalizedResourceNameTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static Object[][] resource_name_starts_with_cases() {
1818
$("com", "com", true),
1919
$("com/foo", "com", true),
2020
$("/com/", "/com", true),
21+
$("\\com\\foo", "/com/foo", true),
2122
$("com", "bar", false),
2223
$("com", "co", false),
2324
$("co/m", "co", true),

0 commit comments

Comments
 (0)