From 4921d867a4ec44465e03c84c9d92f25dbd0df450 Mon Sep 17 00:00:00 2001 From: Gabriele Cardosi Date: Fri, 8 Mar 2024 15:57:45 +0100 Subject: [PATCH] [incubator-kie-issues#847] Include generated-resources directory as resource (#5765) * [incubator-kie-issues#847] WIP * [incubator-kie-issues#847] Add unit tests for AppPaths * [incubator-kie-issues#847] Fixed AppPaths.getResourceFiles. Updated tests --------- Co-authored-by: Gabriele-Cardosi --- drools-model/drools-codegen-common/pom.xml | 16 ++- .../org/drools/codegen/common/AppPaths.java | 23 ++- .../drools/codegen/common/AppPathsTest.java | 134 ++++++++++++++++++ 3 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java diff --git a/drools-model/drools-codegen-common/pom.xml b/drools-model/drools-codegen-common/pom.xml index 52a5bea8ddf..a0888da9179 100644 --- a/drools-model/drools-codegen-common/pom.xml +++ b/drools-model/drools-codegen-common/pom.xml @@ -30,7 +30,6 @@ 999-SNAPSHOT - org.drools drools-codegen-common Drools :: Codegen :: Common @@ -44,5 +43,20 @@ com.github.javaparser javaparser-core + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.jupiter + junit-jupiter-params + test + \ No newline at end of file diff --git a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java index 37479b76a1c..2e2bab4d361 100644 --- a/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java +++ b/drools-model/drools-codegen-common/src/main/java/org/drools/codegen/common/AppPaths.java @@ -47,6 +47,7 @@ public static AppPaths.BuildTool findBuildTool() { private final boolean isJar; private final BuildTool bt; private final Path resourcesPath; + private final Path generatedResourcesPath; private final Path outputTarget; public static AppPaths fromProjectDir(Path projectDir, Path outputTarget) { @@ -79,8 +80,10 @@ protected AppPaths(Set projectPaths, Collection classesPaths, boolea this.outputTarget = outputTarget; if (bt == BuildTool.GRADLE) { resourcesPath = Paths.get(""); // no prefix required + generatedResourcesPath = null; } else { resourcesPath = Paths.get("src", resourcesBasePath, "resources"); + generatedResourcesPath = Paths.get(TARGET_DIR, "generated-resources"); } } @@ -106,11 +109,27 @@ private Path[] getJarPaths() { } public File[] getResourceFiles() { - return projectPaths.stream().map(p -> p.resolve(resourcesPath).toFile()).toArray(File[]::new); + File[] toReturn = projectPaths.stream().map(p -> p.resolve(resourcesPath).toFile()).toArray(File[]::new); + if (generatedResourcesPath != null) { + File[] generatedResourcesFiles = projectPaths.stream().map(p -> p.resolve(generatedResourcesPath).toFile()).toArray(File[]::new); + File[] newToReturn = new File[toReturn.length + generatedResourcesFiles.length]; + System.arraycopy( toReturn, 0, newToReturn, 0, toReturn.length ); + System.arraycopy( generatedResourcesFiles, 0, newToReturn, toReturn.length, generatedResourcesFiles.length ); + toReturn = newToReturn; + } + return toReturn; } public Path[] getResourcePaths() { - return transformPaths(projectPaths, p -> p.resolve(resourcesPath)); + Path[] toReturn = transformPaths(projectPaths, p -> p.resolve(resourcesPath)); + if (generatedResourcesPath != null) { + Path[] generatedResourcesPaths = transformPaths(projectPaths, p -> p.resolve(generatedResourcesPath)); + Path[] newToReturn = new Path[toReturn.length + generatedResourcesPaths.length]; + System.arraycopy( toReturn, 0, newToReturn, 0, toReturn.length ); + System.arraycopy( generatedResourcesPaths, 0, newToReturn, toReturn.length, generatedResourcesPaths.length ); + toReturn = newToReturn; + } + return toReturn; } public Path[] getSourcePaths() { diff --git a/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java new file mode 100644 index 00000000000..a13c03a86ce --- /dev/null +++ b/drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java @@ -0,0 +1,134 @@ +package org.drools.codegen.common; + +import java.io.File; +import java.nio.file.Path; +import java.util.Collection; + +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.drools.codegen.common.AppPaths.TARGET_DIR; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@Execution(ExecutionMode.SAME_THREAD) +public class AppPathsTest { + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void fromProjectDir(boolean withGradle) { + String projectDirPath = "projectDir"; + String outputTargetPath = "outputTarget"; + Path projectDir = Path.of(projectDirPath); + Path outputTarget = Path.of(outputTargetPath); + if (withGradle) { + System.setProperty("org.gradle.appname", "gradle-impl"); + } else { + System.clearProperty("org.gradle.appname"); + } + AppPaths retrieved = AppPaths.fromProjectDir(projectDir, outputTarget); + getPathsTest(retrieved, projectDirPath, withGradle, false); + getFirstProjectPathTest(retrieved, projectDirPath, outputTargetPath, withGradle); + getResourceFilesTest(retrieved, projectDirPath, withGradle, false); + getResourcePathsTest(retrieved, projectDirPath, withGradle, false); + getSourcePathsTest(retrieved, projectDirPath); + getClassesPathsTest(retrieved); + getOutputTargetTest(retrieved, outputTargetPath); + } + + @ParameterizedTest + @ValueSource(booleans = {true, false}) + public void fromTestDir(boolean withGradle) { + String projectDirPath = "projectDir"; + String outputTargetPath = String.format("%s/%s", projectDirPath, TARGET_DIR).replace("/", File.separator); + Path projectDir = Path.of(projectDirPath); + if (withGradle) { + System.setProperty("org.gradle.appname", "gradle-impl"); + } else { + System.clearProperty("org.gradle.appname"); + } + AppPaths retrieved = AppPaths.fromTestDir(projectDir); + getPathsTest(retrieved, projectDirPath, withGradle, true); + getFirstProjectPathTest(retrieved, projectDirPath, outputTargetPath, withGradle); + getResourceFilesTest(retrieved, projectDirPath, withGradle, true); + getResourcePathsTest(retrieved, projectDirPath, withGradle, true); + getSourcePathsTest(retrieved, projectDirPath); + getClassesPathsTest(retrieved); + getOutputTargetTest(retrieved, outputTargetPath); + } + + private void getPathsTest(AppPaths toCheck, String projectDirPath, boolean isGradle, boolean isTestDir) { + Path[] retrieved = toCheck.getPaths(); + commonCheckResourcePaths(retrieved, projectDirPath, isGradle, isTestDir,"getPathsTest"); + } + + private void getFirstProjectPathTest(AppPaths toCheck, String projectPath, String outputPath, boolean isGradle) { + Path retrieved = toCheck.getFirstProjectPath(); + String expectedPath; + if (isGradle) { + expectedPath = outputPath; + } else { + expectedPath = projectPath; + } + assertEquals(Path.of(expectedPath), retrieved, "AppPathsTest.getFirstProjectPathTest"); + } + + private void getResourceFilesTest(AppPaths toCheck, String projectDirPath, boolean isGradle, boolean isTestDir) { + File[] retrieved = toCheck.getResourceFiles(); + int expected = isGradle ? 1 : 2; + assertEquals(expected, retrieved.length, "AppPathsTest.getResourceFilesTest"); + String expectedPath; + String sourceDir = isTestDir ? "test" : "main"; + if (isGradle) { + expectedPath = projectDirPath; + } else { + expectedPath = String.format("%s/src/%s/resources", projectDirPath, sourceDir).replace("/", File.separator); + } + assertEquals(new File(expectedPath), retrieved[0], "AppPathsTest.getResourceFilesTest"); + if (!isGradle) { + expectedPath = String.format("%s/target/generated-resources", projectDirPath).replace("/", File.separator); + assertEquals(new File(expectedPath), retrieved[1], "AppPathsTest.getResourceFilesTest"); + } + } + + private void getResourcePathsTest(AppPaths toCheck, String projectDirPath, boolean isGradle, boolean isTestDir) { + Path[] retrieved = toCheck.getResourcePaths(); + commonCheckResourcePaths(retrieved, projectDirPath, isGradle, isTestDir, "getResourcePathsTest"); + } + + private void getSourcePathsTest(AppPaths toCheck, String projectPath) { + Path[] retrieved = toCheck.getSourcePaths(); + assertEquals(1, retrieved.length, "AppPathsTest.getSourcePathsTest"); + String expectedPath = String.format("%s/src", projectPath).replace("/", File.separator); + assertEquals(Path.of(expectedPath), retrieved[0], "AppPathsTest.getSourcePathsTest"); + } + + private void getClassesPathsTest(AppPaths toCheck) { + Collection retrieved = toCheck.getClassesPaths(); + assertTrue(retrieved.isEmpty(), "AppPathsTest.getClassesPathsTest"); + } + + private void getOutputTargetTest(AppPaths toCheck, String outputPath) { + Path retrieved = toCheck.getOutputTarget(); + assertEquals(Path.of(outputPath), retrieved, "AppPathsTest.getOutputTargetTest"); + } + + private void commonCheckResourcePaths(Path[] toCheck, String projectDirPath, boolean isGradle, boolean isTestDir, String methodName) { + int expected = isGradle ? 1 : 2; + assertEquals(expected, toCheck.length, String.format("AppPathsTest.%s", methodName)); + String expectedPath; + String sourceDir = isTestDir ? "test" : "main"; + if (isGradle) { + expectedPath = projectDirPath; + } else { + expectedPath = String.format("%s/src/%s/resources", projectDirPath, sourceDir).replace("/", File.separator); + } + assertEquals(Path.of(expectedPath), toCheck[0], String.format("AppPathsTest.%s", methodName)); + if (!isGradle) { + expectedPath = String.format("%s/target/generated-resources", projectDirPath).replace("/", File.separator); + assertEquals(Path.of(expectedPath), toCheck[1], String.format("AppPathsTest.%s", methodName)); + } + } +} \ No newline at end of file