Skip to content

Commit

Permalink
Revert simplification to be more compatible with various class loader…
Browse files Browse the repository at this point in the history
…s, improve code documentation
  • Loading branch information
FWest98 committed May 15, 2024
1 parent 8c290cf commit 9c71d7a
Showing 1 changed file with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.test.component;

import static io.quarkus.commons.classloading.ClassloadHelper.fromClassNameToResourceName;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -12,7 +14,6 @@
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -1197,9 +1198,20 @@ private File getTestOutputDirectory(Class<?> testClass) {
if (outputDirectory != null) {
testOutputDirectory = new File(outputDirectory);
} else {
// Gets URL to the root test directory
URL testPath = testClass.getClassLoader().getResource(".");
testOutputDirectory = new File(URI.create(testPath.toString()));
// All below string transformations work with _URL encoded_ paths, where e.g.
// a space is replaced with %20. At the end, we feed this back to URI.create
// to make sure the encoding is dealt with properly, so we don't have to do this
// ourselves. Directly passing a URL-encoded string to the File() constructor
// does not work properly.

// org.acme.Foo -> org/acme/Foo.class
String testClassResourceName = fromClassNameToResourceName(testClass.getName());
// org/acme/Foo.class -> file:/some/path/to/project/target/test-classes/org/acme/Foo.class
String testPath = testClass.getClassLoader().getResource(testClassResourceName).toString();
// file:/some/path/to/project/target/test-classes/org/acme/Foo.class -> file:/some/path/to/project/target/test-classes
String testClassesRootPath = testPath.substring(0, testPath.length() - testClassResourceName.length() - 1);
// resolve back to File instance
testOutputDirectory = new File(URI.create(testClassesRootPath));
}
if (!testOutputDirectory.canWrite()) {
throw new IllegalStateException("Invalid test output directory: " + testOutputDirectory);
Expand Down

0 comments on commit 9c71d7a

Please sign in to comment.