diff --git a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java index b719ad95ec6a1..2b97510ead239 100644 --- a/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java +++ b/extensions/smallrye-openapi/deployment/src/main/java/io/quarkus/smallrye/openapi/deployment/SmallRyeOpenApiProcessor.java @@ -166,14 +166,12 @@ void configFiles(BuildProducer watchedFiles, SmallRyeOpenApiConfig openApiConfig, LaunchModeBuildItem launchMode, OutputTargetBuildItem outputTargetBuildItem) throws IOException { - // Add any aditional directories if configured if (launchMode.getLaunchMode().isDevOrTest() && openApiConfig.additionalDocsDirectory.isPresent()) { List additionalStaticDocuments = openApiConfig.additionalDocsDirectory.get(); for (Path path : additionalStaticDocuments) { // Scan all yaml and json files - List filesInDir = getResourceFiles(ClassPathUtils.toResourceName(path), - outputTargetBuildItem.getOutputDirectory()); + List filesInDir = getResourceFiles(path, outputTargetBuildItem.getOutputDirectory()); for (String possibleFile : filesInDir) { watchedFiles.produce(new HotDeploymentWatchedFileBuildItem(possibleFile)); } @@ -795,7 +793,7 @@ private List findStaticModels(SmallRyeOpenApiConfig openApiConfig, Path for (Path path : additionalStaticDocuments) { // Scan all yaml and json files try { - List filesInDir = getResourceFiles(ClassPathUtils.toResourceName(path), target); + List filesInDir = getResourceFiles(path, target); for (String possibleModelFile : filesInDir) { addStaticModelIfExist(results, possibleModelFile); } @@ -829,32 +827,31 @@ private void addStaticModelIfExist(List results, Format format, String p } } - private List getResourceFiles(String pathName, Path target) throws IOException { + private List getResourceFiles(Path resourcePath, Path target) throws IOException { + final String resourceName = ClassPathUtils.toResourceName(resourcePath); List filenames = new ArrayList<>(); - if (target == null) { + // Here we are resolving the resource dir relative to the classes dir and if it does not exist, we fallback to locating the resource dir on the classpath. + // Although the classes dir should already be on the classpath. + // In a QuarkusUnitTest the module's classes dir and the test application root could be different directories, is this code here for that reason? + final Path targetResourceDir = target == null ? null : target.resolve("classes").resolve(resourcePath); + if (targetResourceDir != null && Files.exists(targetResourceDir)) { + try (Stream paths = Files.list(targetResourceDir)) { + return paths.map((t) -> { + return resourceName + "/" + t.getFileName().toString(); + }).collect(Collectors.toList()); + } + } else { ClassLoader cl = Thread.currentThread().getContextClassLoader(); - try (InputStream inputStream = cl.getResourceAsStream(pathName)) { + try (InputStream inputStream = cl.getResourceAsStream(resourceName)) { if (inputStream != null) { try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) { String resource; while ((resource = br.readLine()) != null) { - filenames.add(pathName + "/" + resource); + filenames.add(resourceName + "/" + resource); } } } } - } else { - Path classes = target.resolve("classes"); - if (classes != null) { - Path path = classes.resolve(pathName); - if (Files.exists(path)) { - try (Stream paths = Files.list(path)) { - return paths.map((t) -> { - return pathName + "/" + t.getFileName().toString(); - }).collect(Collectors.toList()); - } - } - } } return filenames; } diff --git a/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java b/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java index 0ada89168455a..1e0a51c11ed52 100644 --- a/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java +++ b/test-framework/common/src/main/java/io/quarkus/test/common/PathTestHelper.java @@ -258,4 +258,24 @@ private static boolean isInTestDir(URL resource) { private static Path toPath(URL resource) { return ClassPathUtils.toLocalPath(resource); } + + /** + * Returns the build directory of the project given its base dir and the test classes dir. + * + * @param projectRoot project dir + * @param testClassLocation test dir + * + * @return project build dir + */ + public static Path getProjectBuildDir(Path projectRoot, Path testClassLocation) { + Path outputDir; + try { + // this should work for both maven and gradle + outputDir = projectRoot.resolve(projectRoot.relativize(testClassLocation).getName(0)); + } catch (Exception e) { + // this shouldn't happen since testClassLocation is usually found under the project dir + outputDir = projectRoot; + } + return outputDir; + } } diff --git a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java index 860f6aa74c2f8..3e72ea04d2fa0 100644 --- a/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java +++ b/test-framework/junit5-internal/src/main/java/io/quarkus/test/QuarkusUnitTest.java @@ -12,6 +12,7 @@ import java.lang.reflect.Modifier; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -540,6 +541,8 @@ public boolean test(String s) { .setMode(QuarkusBootstrap.Mode.TEST) .addExcludedPath(testLocation) .setProjectRoot(testLocation) + .setTargetDirectory( + PathTestHelper.getProjectBuildDir(Paths.get("").normalize().toAbsolutePath(), testLocation)) .setFlatClassPath(flatClassPath) .setForcedDependencies(forcedDependencies); for (JavaArchive dependency : additionalDependencies) { diff --git a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java index 89756decef6d5..970242d209c98 100644 --- a/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java +++ b/test-framework/junit5/src/main/java/io/quarkus/test/junit/AbstractJvmQuarkusTestExtension.java @@ -104,14 +104,7 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class