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 6661a18cc2 [incubator-kie-issues#1003] Refactored AppPaths - enforced 
immutability (#5777)
6661a18cc2 is described below

commit 6661a18cc2383f90607f7d7ed37f8111f4a25400
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Tue Mar 12 11:41:24 2024 +0100

    [incubator-kie-issues#1003] Refactored AppPaths - enforced immutability 
(#5777)
    
    * [incubator-kie-issues#1003] Refactored AppPaths - enforced immutability
    
    * [incubator-kie-issues#1003] Minor fix - removed unused public method 
(previously private)
    
    ---------
    
    Co-authored-by: Gabriele-Cardosi <[email protected]>
---
 .../java/org/drools/codegen/common/AppPaths.java   | 126 ++++++++++++---------
 .../org/drools/codegen/common/AppPathsTest.java    |  17 ++-
 2 files changed, 86 insertions(+), 57 deletions(-)

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 2e2bab4d36..e12fd0c550 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
@@ -22,6 +22,7 @@ import java.io.File;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashSet;
@@ -41,17 +42,32 @@ public class AppPaths {
 
     public static final String TARGET_DIR = "target";
 
+    public static final String SRC_DIR = "src";
+
+    public static final String RESOURCES_DIR = "resources";
+
+    public static final String GENERATED_RESOURCES_DIR = "generated-resources";
+
+    public static final String MAIN_DIR = "main";
+    public static final String TEST_DIR = "test";
+
     private final Set<Path> projectPaths = new LinkedHashSet<>();
     private final Collection<Path> classesPaths = new ArrayList<>();
 
     private final boolean isJar;
-    private final BuildTool bt;
-    private final Path resourcesPath;
-    private final Path generatedResourcesPath;
     private final Path outputTarget;
 
+    private final Path[] paths;
+
+    private final Path firstProjectPath;
+
+    private final Path[] resourcePaths;
+    private final File[] resourceFiles;
+
+    private final Path[] sourcePaths;
+
     public static AppPaths fromProjectDir(Path projectDir, Path outputTarget) {
-        return new AppPaths(Collections.singleton(projectDir), 
Collections.emptyList(), false, BuildTool.findBuildTool(), "main", 
outputTarget);
+        return new AppPaths(Collections.singleton(projectDir), 
Collections.emptyList(), false, BuildTool.findBuildTool(), MAIN_DIR, 
outputTarget);
     }
 
     /**
@@ -61,7 +77,7 @@ public class AppPaths {
      * @return
      */
     public static AppPaths fromTestDir(Path projectDir) {
-        return new AppPaths(Collections.singleton(projectDir), 
Collections.emptyList(), false, BuildTool.findBuildTool(), "test", 
Paths.get(projectDir.toString(), TARGET_DIR));
+        return new AppPaths(Collections.singleton(projectDir), 
Collections.emptyList(), false, BuildTool.findBuildTool(), TEST_DIR, 
Paths.get(projectDir.toString(), TARGET_DIR));
     }
 
     /**
@@ -74,66 +90,34 @@ public class AppPaths {
     protected AppPaths(Set<Path> projectPaths, Collection<Path> classesPaths, 
boolean isJar, BuildTool bt,
             String resourcesBasePath, Path outputTarget) {
         this.isJar = isJar;
-        this.bt = bt;
         this.projectPaths.addAll(projectPaths);
         this.classesPaths.addAll(classesPaths);
         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");
-        }
+        firstProjectPath = getFirstProjectPath(this.projectPaths, 
outputTarget, bt);
+        resourcePaths = getResourcePaths(this.projectPaths, resourcesBasePath, 
bt);
+        paths = isJar ? getJarPaths(isJar, this.classesPaths) : resourcePaths;
+        resourceFiles = getResourceFiles(resourcePaths);
+        sourcePaths = getSourcePaths(this.projectPaths);
     }
 
     public Path[] getPaths() {
-        if (isJar) {
-            return getJarPaths();
-        } else {
-            return getResourcePaths();
-        }
+        return paths;
     }
 
     public Path getFirstProjectPath() {
-        return bt == BuildTool.MAVEN
-               ? projectPaths.iterator().next()
-               : outputTarget;
-    }
-
-    private Path[] getJarPaths() {
-        if (!isJar) {
-            throw new IllegalStateException("Not a jar");
-        }
-        return classesPaths.toArray(new Path[classesPaths.size()]);
+        return firstProjectPath;
     }
 
     public File[] getResourceFiles() {
-        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;
+        return resourceFiles;
     }
 
     public Path[] getResourcePaths() {
-        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;
+        return resourcePaths;
     }
 
     public Path[] getSourcePaths() {
-        return transformPaths(projectPaths, p -> p.resolve("src"));
+        return sourcePaths;
     }
 
     public Collection<Path> getClassesPaths() {
@@ -144,10 +128,6 @@ public class AppPaths {
         return outputTarget;
     }
 
-    private Path[] transformPaths(Collection<Path> paths, UnaryOperator<Path> 
f) {
-        return paths.stream().map(f).toArray(Path[]::new);
-    }
-
     @Override
     public String toString() {
         return "AppPaths{" +
@@ -156,4 +136,48 @@ public class AppPaths {
                 ", isJar=" + isJar +
                 '}';
     }
+
+    static Path getFirstProjectPath(Set<Path> innerProjectPaths, Path 
innerOutputTarget, BuildTool innerBt) {
+        return innerBt == BuildTool.MAVEN
+                ? innerProjectPaths.iterator().next()
+                : innerOutputTarget;
+    }
+
+    static Path[] getJarPaths(boolean isInnerJar, Collection<Path> 
innerClassesPaths) {
+        if (!isInnerJar) {
+            throw new IllegalStateException("Not a jar");
+        } else {
+            return innerClassesPaths.toArray(new Path[0]);
+        }
+    }
+
+    static Path[] getResourcePaths(Set<Path> innerProjectPaths, String 
resourcesBasePath, BuildTool innerBt) {
+        Path[] toReturn;
+        if (innerBt == BuildTool.GRADLE) {
+            toReturn = transformPaths(innerProjectPaths, p -> 
p.resolve(Paths.get("")));
+        } else {
+            toReturn = transformPaths(innerProjectPaths, p -> 
p.resolve(Paths.get(SRC_DIR, resourcesBasePath,
+                                                                               
   RESOURCES_DIR)));
+            Path[] generatedResourcesPaths = transformPaths(innerProjectPaths, 
p -> p.resolve(Paths.get(TARGET_DIR,
+                                                                               
                         GENERATED_RESOURCES_DIR)));
+            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;
+    }
+
+    static File[] getResourceFiles(Path[] innerResourcePaths) {
+        return 
Arrays.stream(innerResourcePaths).map(Path::toFile).toArray(File[]::new);
+    }
+
+    static Path[] getSourcePaths(Set<Path> innerProjectPaths) {
+        return transformPaths(innerProjectPaths, p -> p.resolve(SRC_DIR));
+    }
+
+    static Path[] transformPaths(Collection<Path> paths, UnaryOperator<Path> 
f) {
+        return paths.stream().map(f).toArray(Path[]::new);
+    }
+
 }
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
index a13c03a86c..ed7824518b 100644
--- 
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
@@ -9,16 +9,21 @@ 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.GENERATED_RESOURCES_DIR;
+import static org.drools.codegen.common.AppPaths.MAIN_DIR;
+import static org.drools.codegen.common.AppPaths.RESOURCES_DIR;
+import static org.drools.codegen.common.AppPaths.SRC_DIR;
 import static org.drools.codegen.common.AppPaths.TARGET_DIR;
+import static org.drools.codegen.common.AppPaths.TEST_DIR;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 @Execution(ExecutionMode.SAME_THREAD)
-public class AppPathsTest {
+class AppPathsTest {
 
     @ParameterizedTest
     @ValueSource(booleans = {true, false})
-    public void fromProjectDir(boolean withGradle) {
+    void fromProjectDir(boolean withGradle) {
         String projectDirPath = "projectDir";
         String outputTargetPath = "outputTarget";
         Path projectDir = Path.of(projectDirPath);
@@ -40,7 +45,7 @@ public class AppPathsTest {
 
     @ParameterizedTest
     @ValueSource(booleans = {true, false})
-    public void fromTestDir(boolean withGradle) {
+    void fromTestDir(boolean withGradle) {
         String projectDirPath = "projectDir";
         String outputTargetPath = String.format("%s/%s", projectDirPath, 
TARGET_DIR).replace("/", File.separator);
         Path projectDir = Path.of(projectDirPath);
@@ -80,15 +85,15 @@ public class AppPathsTest {
         int expected = isGradle ? 1 : 2;
         assertEquals(expected, retrieved.length, 
"AppPathsTest.getResourceFilesTest");
         String expectedPath;
-        String sourceDir =  isTestDir ? "test" : "main";
+        String sourceDir =  isTestDir ? TEST_DIR : MAIN_DIR;
         if (isGradle) {
             expectedPath = projectDirPath;
         } else {
-            expectedPath = String.format("%s/src/%s/resources", 
projectDirPath, sourceDir).replace("/", File.separator);
+            expectedPath = String.format("%s/%s/%s/%s", projectDirPath, 
SRC_DIR, sourceDir, RESOURCES_DIR).replace("/", File.separator);
         }
         assertEquals(new File(expectedPath), retrieved[0], 
"AppPathsTest.getResourceFilesTest");
         if (!isGradle) {
-            expectedPath = String.format("%s/target/generated-resources", 
projectDirPath).replace("/", File.separator);
+            expectedPath = String.format("%s/%s/%s", projectDirPath, 
TARGET_DIR, GENERATED_RESOURCES_DIR).replace("/", File.separator);
             assertEquals(new File(expectedPath), retrieved[1], 
"AppPathsTest.getResourceFilesTest");
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to