Skip to content

Commit

Permalink
Now handling NoSuchFileException
Browse files Browse the repository at this point in the history
  • Loading branch information
jansoren committed Mar 31, 2024
1 parent 2a917e1 commit 721886b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/main/java/org/filelize/file/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -27,14 +29,22 @@ public FileHandler(ObjectMapper objectMapper) {
}

public <T> T readFile(String fullPath, Class<T> valueType) throws IOException {
JsonNode json = objectMapper.readTree(Files.newBufferedReader(Paths.get(fullPath)));
return objectMapper.treeToValue(json, valueType);
try {
JsonNode json = objectMapper.readTree(getNewBufferedReader(fullPath));
return objectMapper.treeToValue(json, valueType);
} catch (NoSuchFileException e) {
return null;
}
}

public <T> Map<String, T> readFileMap(String fullPath, Class<T> valueType) throws IOException {
JsonNode json = objectMapper.readTree(Files.newBufferedReader(Paths.get(fullPath)));
JavaType mapType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class, valueType);
return objectMapper.readValue(json.traverse(), mapType);
try {
JsonNode json = objectMapper.readTree(getNewBufferedReader(fullPath));
JavaType mapType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class, valueType);
return objectMapper.readValue(json.traverse(), mapType);
} catch (NoSuchFileException e) {
return null;
}
}

public <T> Map<String, Object> readFiles(String folderPath, Class<T> valueType) throws IOException {
Expand All @@ -57,4 +67,8 @@ public void writeFile(String fullPath, Object object) throws IOException {
ensureFile(fullPath);
objectMapper.writerWithDefaultPrettyPrinter().writeValue(Files.newBufferedWriter(Paths.get(fullPath)), object);
}

private static BufferedReader getNewBufferedReader(String fullPath) throws IOException {
return Files.newBufferedReader(Paths.get(fullPath));
}
}
25 changes: 25 additions & 0 deletions src/test/java/org/filelize/file/FileHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.filelize.file;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.filelize.SomethingElse;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertNull;

public class FileHandlerTest {

private final FileHandler fileHandler;

public FileHandlerTest() {
this.fileHandler = new FileHandler(new ObjectMapper());
}

@Test
public void testReadFile_ShouldHandleNoSuchFileException() throws IOException {
var something = fileHandler.readFile("tmp/not_existing_folder.json", SomethingElse.class);
assertNull(something);
}

}

0 comments on commit 721886b

Please sign in to comment.