forked from apache/incubator-kie-drools
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[incubator-kie-issues#847] Include generated-resources directory as r…
…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
1 parent
5612909
commit 4921d86
Showing
3 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
drools-model/drools-codegen-common/src/test/java/org/drools/codegen/common/AppPathsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |