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 17d147dc7f [incubator-kie-issues#927] Overloading FileUtils methods to 
also consider parent directory (#5687)
17d147dc7f is described below

commit 17d147dc7fa67de258f21a9fc88f2bbc1a4998f3
Author: Gabriele Cardosi <[email protected]>
AuthorDate: Wed Feb 14 12:31:17 2024 +0100

    [incubator-kie-issues#927] Overloading FileUtils methods to also consider 
parent directory (#5687)
    
    * [incubator-kie-issues#927] Overloading FileUtils methods to also consider 
parent directory
    
    * [incubator-kie-issues#927] Fix ResourceHelperTest
    
    ---------
    
    Co-authored-by: Gabriele-Cardosi <[email protected]>
---
 .../src/main/java/org/drools/util/FileUtils.java   | 54 ++++++++++++++++++++++
 .../test/java/org/drools/util/FileUtilsTest.java   | 31 +++++++++++--
 .../java/org/drools/util/ResourceHelperTest.java   | 16 +++----
 drools-util/src/test/resources/subdir/TestFile.txt |  1 +
 4 files changed, 91 insertions(+), 11 deletions(-)

diff --git a/drools-util/src/main/java/org/drools/util/FileUtils.java 
b/drools-util/src/main/java/org/drools/util/FileUtils.java
index d62a9ef730..799d746498 100644
--- a/drools-util/src/main/java/org/drools/util/FileUtils.java
+++ b/drools-util/src/main/java/org/drools/util/FileUtils.java
@@ -42,6 +42,7 @@ public class FileUtils {
 
     /**
      * Retrieve the <code>File</code> of the given <b>file</b>
+     * This method does not guarantee the returned file if multiple files, 
with same name, are present in different directories
      * @param fileName
      * @return
      */
@@ -58,6 +59,31 @@ public class FileUtils {
         return toReturn;
     }
 
+    /**
+     * Retrieve the <code>File</code> of the given <b>file</b>
+     * @param fileName
+     * @param parentDir
+     * @return
+     */
+    public static File getFile(String fileName, String parentDir) {
+        String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
+        File parentDirectory = new File(parentDir);
+        if (!parentDirectory.exists() || !parentDirectory.canRead() || 
!parentDirectory.isDirectory()) {
+            throw new IllegalArgumentException("Failed to find parent 
directory " + parentDir);
+        }
+        File toReturn = ResourceHelper.getFileResourcesByExtension(extension)
+                .stream()
+                .filter(file -> file.getName().equals(fileName) &&
+                        file.getParentFile() != null &&
+                        
file.getParentFile().getAbsolutePath().equals(parentDirectory.getAbsolutePath()))
+                .findFirst()
+                .orElse(null);
+        if (toReturn == null) {
+            throw new IllegalArgumentException("Failed to find file " + 
fileName);
+        }
+        return toReturn;
+    }
+
     /**
      * Retrieve the <code>FileInputStream</code> of the given <b>file</b>
      * @param fileName
@@ -69,6 +95,18 @@ public class FileUtils {
         return new FileInputStream(sourceFile);
     }
 
+    /**
+     * Retrieve the <code>FileInputStream</code> of the given <b>file</b>
+     * @param fileName
+     * @param parentDir
+     * @return
+     * @throws IOException
+     */
+    public static FileInputStream getFileInputStream(String fileName, String 
parentDir) throws IOException {
+        File sourceFile = getFile(fileName, parentDir);
+        return new FileInputStream(sourceFile);
+    }
+
     /**
      * Retrieve the <b>content</b> of the given <b>file</b>
      * @param fileName
@@ -84,6 +122,22 @@ public class FileUtils {
         return toReturn;
     }
 
+    /**
+     * Retrieve the <b>content</b> of the given <b>file</b>
+     * @param fileName
+     * @param parentDir
+     * @return
+     * @throws IOException
+     */
+    public static String getFileContent(String fileName, String parentDir) 
throws IOException {
+        File file = getFile(fileName, parentDir);
+        Path path = file.toPath();
+        Stream<String> lines = Files.lines(path);
+        String toReturn = lines.collect(Collectors.joining("\n"));
+        lines.close();
+        return toReturn;
+    }
+
     /**
      * @param fileName
      * @param classLoader
diff --git a/drools-util/src/test/java/org/drools/util/FileUtilsTest.java 
b/drools-util/src/test/java/org/drools/util/FileUtilsTest.java
index c48d53f9e9..1a7e1064ac 100644
--- a/drools-util/src/test/java/org/drools/util/FileUtilsTest.java
+++ b/drools-util/src/test/java/org/drools/util/FileUtilsTest.java
@@ -22,6 +22,7 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Optional;
@@ -33,14 +34,24 @@ import static 
org.assertj.core.api.Assertions.assertThatExceptionOfType;
 
 public class FileUtilsTest {
 
-    private static final String TEST_FILE = "TestFile.txt";
+    public static final String TEST_FILE = "TestFile.txt";
     private static final String NOT_EXISTING_FILE = "NotExisting.txt";
 
+    private static final String EXISTING_DIRECTORY =  "subdir";
+
+    private static final String NOT_EXISTING_DIRECTORY = 
String.format(".%snotexisting", File.separator);
+
     @Test
     public void getFileExisting() {
         final File retrieved = FileUtils.getFile(TEST_FILE);
-        assertThat(retrieved).exists();
-        assertThat(retrieved.getName()).isEqualTo(TEST_FILE);
+        assertThat(retrieved).exists().hasName(TEST_FILE);
+    }
+
+    @Test
+    public void getFileExistingFromDirectory() {
+        final File retrieved = FileUtils.getFile(TEST_FILE, getSubdir());
+        assertThat(retrieved).exists().hasName(TEST_FILE);
+        
assertThat(retrieved.getParentFile()).exists().isDirectory().hasName(EXISTING_DIRECTORY);
     }
 
     @Test
@@ -48,6 +59,11 @@ public class FileUtilsTest {
         
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> 
FileUtils.getFile(NOT_EXISTING_FILE));
     }
 
+    @Test
+    public void getFileNotExistingDirectory() {
+        
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> 
FileUtils.getFile(TEST_FILE, NOT_EXISTING_DIRECTORY));
+    }
+
     @Test
     public void getFileInputStreamExisting() throws IOException {
         final FileInputStream retrieved = 
FileUtils.getFileInputStream(TEST_FILE);
@@ -80,4 +96,13 @@ public class FileUtilsTest {
         assertThat(Files.exists(tempDirectory)).isFalse();
         assertThat(Files.exists(tempFile)).isFalse();
     }
+
+    private static String getSubdir() {
+        URL subdirResource =  
FileUtilsTest.class.getClassLoader().getResource(EXISTING_DIRECTORY);
+        if (subdirResource == null) {
+            throw new RuntimeException("Failed to find subdir folder");
+        } else {
+            return subdirResource.getFile();
+        }
+    }
 }
\ No newline at end of file
diff --git a/drools-util/src/test/java/org/drools/util/ResourceHelperTest.java 
b/drools-util/src/test/java/org/drools/util/ResourceHelperTest.java
index e33653772c..f314dfc701 100644
--- a/drools-util/src/test/java/org/drools/util/ResourceHelperTest.java
+++ b/drools-util/src/test/java/org/drools/util/ResourceHelperTest.java
@@ -29,6 +29,7 @@ import java.util.stream.Collectors;
 import org.junit.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.drools.util.FileUtilsTest.TEST_FILE;
 import static org.drools.util.ResourceHelper.getFileResourcesByExtension;
 import static org.drools.util.ResourceHelper.getFileResourcesFromDirectory;
 import static org.drools.util.ResourceHelper.getResourcesByExtension;
@@ -37,14 +38,13 @@ import static 
org.drools.util.ResourceHelper.internalGetResources;
 
 public class ResourceHelperTest {
 
-    private static final String TEST_FILE = "TestFile.txt";
 
     @Test
     public void getResourcesByExtensionTest() {
         Collection<String> resources = getResourcesByExtension("txt");
         assertThat(resources)
-                .hasSize(1)
-                .anyMatch(elem -> elem.endsWith(TEST_FILE));
+                .hasSize(2)
+                .allMatch(elem -> elem.endsWith(TEST_FILE));
     }
 
     @Test
@@ -64,11 +64,11 @@ public class ResourceHelperTest {
         List<String> classPathElements = 
Arrays.asList(ResourceHelper.getClassPathElements());
         Optional<String> testFolder =
                 classPathElements.stream().filter(elem -> 
elem.contains("test-classes")).findFirst();
-        assertThat(testFolder.isPresent()).isTrue();
+        assertThat(testFolder).isPresent();
         File dir = new File(testFolder.get());
         String regex = ".*" + TEST_FILE;
         Collection<String> filesFound = getResourcesFromDirectory(dir, 
Pattern.compile(regex));
-        assertThat(filesFound).hasSize(1);
+        assertThat(filesFound).hasSize(2);
 
         assertThat(getResourcesFromDirectory(null, null)).isEmpty();
         assertThat(getResourcesFromDirectory(dir, 
Pattern.compile("noMatch"))).isEmpty();
@@ -108,9 +108,9 @@ public class ResourceHelperTest {
     public void internalGetResourcesTest() {
         List<String> classPathElements = 
Arrays.asList(ResourceHelper.getClassPathElements());
         Optional<String> testFolder = classPathElements.stream().filter(elem 
-> elem.contains("test-classes")).findFirst();
-        assertThat(testFolder.isPresent()).isTrue();
+        assertThat(testFolder).isPresent();
         Collection<String> filesFound = internalGetResources(testFolder.get(), 
Pattern.compile(".*\\.txt$"));
-        assertThat(filesFound.size()).isEqualTo(1);
+        assertThat(filesFound).hasSize(2);
 
         assertThat(internalGetResources(filesFound.iterator().next(), 
Pattern.compile(".*\\.txt$"))).isEmpty();
     }
@@ -133,7 +133,7 @@ public class ResourceHelperTest {
 
     private void commonVerifyCollectionWithExpectedFile(final Collection<File> 
toVerify, String expectedFile) {
         assertThat(toVerify).isNotNull();
-        assertThat(toVerify).hasSize(1)
+        assertThat(toVerify).hasSize(2)
                 .allMatch(file -> file.exists() && 
file.getName().equals(expectedFile));
     }
 
diff --git a/drools-util/src/test/resources/subdir/TestFile.txt 
b/drools-util/src/test/resources/subdir/TestFile.txt
new file mode 100644
index 0000000000..9e95ffa46c
--- /dev/null
+++ b/drools-util/src/test/resources/subdir/TestFile.txt
@@ -0,0 +1 @@
+// Empty file
\ No newline at end of file


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

Reply via email to