Skip to content

Commit

Permalink
[incubator-kie-issues#847] Include generated-resources directory as r…
Browse files Browse the repository at this point in the history
…esource (apache#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]>
  • Loading branch information
2 people authored and rgdoliveira committed Mar 11, 2024
1 parent 5612909 commit 4921d86
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 3 deletions.
16 changes: 15 additions & 1 deletion drools-model/drools-codegen-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<version>999-SNAPSHOT</version>
</parent>

<groupId>org.drools</groupId>
<artifactId>drools-codegen-common</artifactId>

<name>Drools :: Codegen :: Common</name>
Expand All @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -79,8 +80,10 @@ protected AppPaths(Set<Path> projectPaths, Collection<Path> 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");
}
}

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 4921d86

Please sign in to comment.