Skip to content

Commit

Permalink
[incubator-kie-issues#927] Overloading FileUtils methods to also cons…
Browse files Browse the repository at this point in the history
…ider 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]>
  • Loading branch information
gitgabrio and Gabriele-Cardosi authored Feb 14, 2024
1 parent d55ce2d commit 17d147d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
54 changes: 54 additions & 0 deletions drools-util/src/main/java/org/drools/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private 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
*/
Expand All @@ -58,6 +59,31 @@ public static File getFile(String fileName) {
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
Expand All @@ -69,6 +95,18 @@ public static FileInputStream getFileInputStream(String fileName) throws IOExcep
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
Expand All @@ -84,6 +122,22 @@ public static String getFileContent(String fileName) throws IOException {
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
Expand Down
31 changes: 28 additions & 3 deletions drools-util/src/test/java/org/drools/util/FileUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,21 +34,36 @@

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
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);
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,14 +38,13 @@

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
Expand All @@ -64,11 +64,11 @@ public void getResourcesFromDirectoryTest() {
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();
Expand Down Expand Up @@ -108,9 +108,9 @@ public void getClassPathElementsTest() {
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();
}
Expand All @@ -133,7 +133,7 @@ public void internalGetResourcesNotExisting() {

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));
}

Expand Down
1 change: 1 addition & 0 deletions drools-util/src/test/resources/subdir/TestFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file

0 comments on commit 17d147d

Please sign in to comment.