This is an automated email from the ASF dual-hosted git repository.
gitgabrio pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-kie-drools.git
The following commit(s) were added to refs/heads/main by this push:
new 58a3bc7286 [incubator-kie-issues#847] Include generated-resources
directory as resource (#5765)
58a3bc7286 is described below
commit 58a3bc728696952de3676ff659da5df989257f22
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Fri Mar 8 15:57:45 2024 +0100
[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 <[email protected]>
---
drools-model/drools-codegen-common/pom.xml | 16 ++-
.../java/org/drools/codegen/common/AppPaths.java | 23 +++-
.../org/drools/codegen/common/AppPathsTest.java | 134 +++++++++++++++++++++
3 files changed, 170 insertions(+), 3 deletions(-)
diff --git a/drools-model/drools-codegen-common/pom.xml
b/drools-model/drools-codegen-common/pom.xml
index 52a5bea8dd..a0888da917 100644
--- a/drools-model/drools-codegen-common/pom.xml
+++ b/drools-model/drools-codegen-common/pom.xml
@@ -30,7 +30,6 @@
<version>999-SNAPSHOT</version>
</parent>
- <groupId>org.drools</groupId>
<artifactId>drools-codegen-common</artifactId>
<name>Drools :: Codegen :: Common</name>
@@ -44,5 +43,20 @@
<groupId>com.github.javaparser</groupId>
<artifactId>javaparser-core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
\ 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 37479b76a1..2e2bab4d36 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 class AppPaths {
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 @@ public class AppPaths {
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 @@ public class AppPaths {
}
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 0000000000..a13c03a86c
--- /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<Path> 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
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]