From 17d147dc7fa67de258f21a9fc88f2bbc1a4998f3 Mon Sep 17 00:00:00 2001 From: Gabriele Cardosi Date: Wed, 14 Feb 2024 12:31:17 +0100 Subject: [PATCH] [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 --- .../main/java/org/drools/util/FileUtils.java | 54 +++++++++++++++++++ .../java/org/drools/util/FileUtilsTest.java | 31 +++++++++-- .../org/drools/util/ResourceHelperTest.java | 16 +++--- .../src/test/resources/subdir/TestFile.txt | 1 + 4 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 drools-util/src/test/resources/subdir/TestFile.txt 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 d62a9ef730b..799d7464980 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 @@ private FileUtils() { /** * Retrieve the File of the given file + * 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 static File getFile(String fileName) { return toReturn; } + /** + * Retrieve the File of the given file + * @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 FileInputStream of the given file * @param fileName @@ -69,6 +95,18 @@ public static FileInputStream getFileInputStream(String fileName) throws IOExcep return new FileInputStream(sourceFile); } + /** + * Retrieve the FileInputStream of the given file + * @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 content of the given file * @param fileName @@ -84,6 +122,22 @@ public static String getFileContent(String fileName) throws IOException { return toReturn; } + /** + * Retrieve the content of the given file + * @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 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 c48d53f9e92..1a7e1064ac5 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.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 @@ 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 void getFileNotExisting() { 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 void deleteDirectory() throws IOException { 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 e33653772c4..f314dfc7017 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 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 @@ public class ResourceHelperTest { - private static final String TEST_FILE = "TestFile.txt"; @Test public void getResourcesByExtensionTest() { Collection 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 void getResourcesFromDirectoryTest() { List classPathElements = Arrays.asList(ResourceHelper.getClassPathElements()); Optional 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 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 void getClassPathElementsTest() { public void internalGetResourcesTest() { List classPathElements = Arrays.asList(ResourceHelper.getClassPathElements()); Optional testFolder = classPathElements.stream().filter(elem -> elem.contains("test-classes")).findFirst(); - assertThat(testFolder.isPresent()).isTrue(); + assertThat(testFolder).isPresent(); Collection 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 void internalGetResourcesNotExisting() { private void commonVerifyCollectionWithExpectedFile(final Collection 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 00000000000..9e95ffa46c8 --- /dev/null +++ b/drools-util/src/test/resources/subdir/TestFile.txt @@ -0,0 +1 @@ +// Empty file \ No newline at end of file