Skip to content

Commit 6ae70b8

Browse files
committed
Fix path resolving on Windows
Make all tests also run on Windows except for docx converter tests from flexmark-docx-converter module.
1 parent a7cb1e6 commit 6ae70b8

3 files changed

Lines changed: 14 additions & 38 deletions

File tree

flexmark-test-util/src/main/java/com/vladsch/flexmark/test/util/TestUtils.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,6 @@ public static String getFormattedSection(String section, int exampleNumber) {
534534
return section == null ? "" : section.trim() + ": " + exampleNumber;
535535
}
536536

537-
@NotNull
538-
public static String getResolvedSpecResourcePath(@NotNull String testClassName, @NotNull String resourcePath) {
539-
File specInfo = new File(resourcePath);
540-
File classInfo = new File("/" + testClassName.replace('.', '/'));
541-
return !specInfo.isAbsolute() ? new File(classInfo.getParent(), resourcePath).getAbsolutePath() : resourcePath;
542-
}
543-
544537
@NotNull
545538
public static String getAbsoluteSpecResourcePath(@NotNull String testClassPath, @NotNull String resourceRootPath, @NotNull String resourcePath) {
546539
File resourceFile = resourcePath.startsWith("/") ? new File(resourceRootPath, resourcePath.substring(1)) : new File(new File(testClassPath).getParent(), resourcePath);
@@ -552,9 +545,8 @@ public static String getSpecResourceFileUrl(@NotNull Class<?> resourceClass, @No
552545
if (resourcePath.isEmpty()) {
553546
throw new IllegalStateException("Empty resource paths not supported");
554547
} else {
555-
String resolvedResourcePath = getResolvedSpecResourcePath(resourceClass.getName(), resourcePath);
556-
URL url = resourceClass.getResource(resolvedResourcePath);
557-
assert url != null : "Resource path: '" + resolvedResourcePath + "' not found.";
548+
URL url = resourceClass.getResource(resourcePath);
549+
assert url != null : "Resource path: '" + resourcePath + "' not found.";
558550
return adjustedFileUrl(url);
559551
}
560552
}

flexmark-test-util/src/main/java/com/vladsch/flexmark/test/util/spec/ResourceLocation.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@ public class ResourceLocation {
1313
final private @NotNull Class<?> resourceClass;
1414
final private @NotNull String resourcePath;
1515
final private @NotNull String fileUrl;
16-
final private @NotNull String resolvedResourcePath;
1716

1817
public ResourceLocation(@NotNull Class<?> resourceClass, @NotNull String resourcePath, @NotNull String fileUrl) {
19-
this(resourceClass, resourcePath, fileUrl, TestUtils.getResolvedSpecResourcePath(resourceClass.getName(), resourcePath));
20-
}
21-
22-
private ResourceLocation(@NotNull Class<?> resourceClass, @NotNull String resourcePath, @NotNull String fileUrl, @NotNull String resolvedResourcePath) {
2318
this.resourceClass = resourceClass;
2419
this.resourcePath = resourcePath;
2520
this.fileUrl = fileUrl;
26-
this.resolvedResourcePath = resolvedResourcePath;
2721
}
2822

2923
@NotNull
@@ -55,11 +49,6 @@ public String getFileUrl(int lineNumber) {
5549
return TestUtils.getUrlWithLineNumber(getFileUrl(), lineNumber);
5650
}
5751

58-
@NotNull
59-
public String getResolvedResourcePath() {
60-
return resolvedResourcePath;
61-
}
62-
6352
public boolean isNull() {
6453
return this == NULL;
6554
}
@@ -83,23 +72,21 @@ public boolean equals(Object o) {
8372

8473
if (!resourceClass.equals(location.resourceClass)) return false;
8574
if (!resourcePath.equals(location.resourcePath)) return false;
86-
if (!fileUrl.equals(location.fileUrl)) return false;
87-
return resolvedResourcePath.equals(location.resolvedResourcePath);
75+
return fileUrl.equals(location.fileUrl);
8876
}
8977

9078
// @formatter:off
91-
@NotNull public ResourceLocation withResourceClass(@NotNull Class<?> resourceClass) { return new ResourceLocation(resourceClass, resourcePath, fileUrl, resolvedResourcePath); };
92-
@NotNull public ResourceLocation withResourcePath(@NotNull String resourcePath) { return new ResourceLocation(resourceClass, resourcePath, fileUrl, resolvedResourcePath); };
93-
@NotNull public ResourceLocation withFileUrl(@NotNull String fileUrl) { return new ResourceLocation(resourceClass, resourcePath, fileUrl, resolvedResourcePath); };
94-
@NotNull public ResourceLocation withResolvedResourcePath(@NotNull String resolvedResourcePath) { return new ResourceLocation(resourceClass, resourcePath, fileUrl, resolvedResourcePath); };
79+
@NotNull public ResourceLocation withResourceClass(@NotNull Class<?> resourceClass) { return new ResourceLocation(resourceClass, resourcePath, fileUrl); };
80+
@NotNull public ResourceLocation withResourcePath(@NotNull String resourcePath) { return new ResourceLocation(resourceClass, resourcePath, fileUrl); };
81+
@NotNull public ResourceLocation withFileUrl(@NotNull String fileUrl) { return new ResourceLocation(resourceClass, resourcePath, fileUrl); };
82+
@NotNull public ResourceLocation withResolvedResourcePath(@NotNull String resolvedResourcePath) { return new ResourceLocation(resourceClass, resourcePath, fileUrl); };
9583
// @formatter:on
9684

9785
@Override
9886
public int hashCode() {
9987
int result = resourceClass.hashCode();
10088
result = 31 * result + resourcePath.hashCode();
10189
result = 31 * result + fileUrl.hashCode();
102-
result = 31 * result + resolvedResourcePath.hashCode();
10390
return result;
10491
}
10592

@@ -113,15 +100,13 @@ public String toString() {
113100

114101
public static @NotNull ResourceLocation of(@NotNull String resourcePath) {
115102
return new ResourceLocation(ComboSpecTestCase.class, resourcePath,
116-
TestUtils.getSpecResourceFileUrl(ComboSpecTestCase.class, resourcePath),
117-
TestUtils.getResolvedSpecResourcePath(ComboSpecTestCase.class.getName(), resourcePath)
103+
TestUtils.getSpecResourceFileUrl(ComboSpecTestCase.class, resourcePath)
118104
);
119105
}
120106

121107
public static @NotNull ResourceLocation of(@NotNull Class<?> resourceClass, @NotNull String resourcePath) {
122108
return new ResourceLocation(resourceClass, resourcePath,
123-
TestUtils.getSpecResourceFileUrl(resourceClass, resourcePath),
124-
TestUtils.getResolvedSpecResourcePath(resourceClass.getName(), resourcePath)
109+
TestUtils.getSpecResourceFileUrl(resourceClass, resourcePath)
125110
);
126111
}
127112

@@ -152,8 +137,7 @@ public static String getResourceText(@NotNull ResourceLocation location) {
152137

153138
@NotNull
154139
public static InputStream getResourceInputStream(@NotNull ResourceLocation location) {
155-
String useSpecResource = location.getResolvedResourcePath();
156-
InputStream stream = location.getResourceClass().getResourceAsStream(useSpecResource);
140+
InputStream stream = location.getResourceClass().getResourceAsStream(location.getResourcePath());
157141
if (stream == null) {
158142
throw new IllegalStateException("Could not load " + location);
159143
}

flexmark-test-util/src/main/java/com/vladsch/flexmark/test/util/spec/SpecExample.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,14 @@ public String toString() {
162162
String fileName = traceElement.getFileName();
163163
// need path to class, so fake it with class resource file
164164
String javaFilePath = resourceClass.getName().replace('.', '/');
165-
File javaPathFile = new File("/" + javaFilePath).getParentFile();
166-
String javaPath = javaPathFile.getPath() + "/" + fileName;
165+
File javaPathFile = new File('/' + javaFilePath).getParentFile();
166+
String javaPath = new File(javaPathFile, fileName).getPath().replace('\\', '/');
167167
URL url = null;
168168
String prefix = null;
169169
String resourcePath = null;
170170
while (true) {
171-
String absolutePath = Utils.removeSuffix(javaPathFile.getPath(), "/");
172-
resourcePath = Utils.removePrefix(absolutePath, '/').replace('/', '.') + ".txt";
171+
String absolutePath = Utils.removeSuffix(javaPathFile.getPath(), File.separator);
172+
resourcePath = Utils.removePrefix(absolutePath, File.separatorChar).replace(File.separatorChar, '.') + ".txt";
173173
url = resourceClass.getResource("/" + resourcePath);
174174
if (url != null) {
175175
prefix = Utils.getResourceAsString(resourceClass, "/" + resourcePath).trim();

0 commit comments

Comments
 (0)