Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the File Tool for common file handling
- Loading branch information
1 parent
16faaba
commit 9f6804d
Showing
7 changed files
with
265 additions
and
64 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
core/src/main/java/io/github/vocabhunter/analysis/core/FileTool.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Open Source Software published under the Apache Licence, Version 2.0. | ||
*/ | ||
|
||
package io.github.vocabhunter.analysis.core; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public final class FileTool { | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
||
private static final List<String> MINIMAL_JSON = Collections.singletonList("{}"); | ||
|
||
private FileTool() { | ||
// Prevent instantiation - all methods are static | ||
} | ||
|
||
public static void ensureDirectoryExists(final Path file, final String errorTemplate) { | ||
Path parent = file.getParent(); | ||
|
||
try { | ||
if (parent != null) { | ||
Files.createDirectories(parent); | ||
} | ||
} catch (final IOException e) { | ||
throw buildError(parent, errorTemplate, e); | ||
} | ||
} | ||
|
||
public static void writeMinimalJson(final Path file, final String errorTemplate) { | ||
try { | ||
Files.write(file, MINIMAL_JSON); | ||
} catch (final IOException e) { | ||
throw buildError(file, errorTemplate, e); | ||
} | ||
} | ||
|
||
public static void writeAsJson(final Path file, final Object v, final String errorTemplate) { | ||
try { | ||
MAPPER.writeValue(file.toFile(), v); | ||
} catch (final IOException e) { | ||
throw buildError(file, errorTemplate, e); | ||
} | ||
} | ||
|
||
public static <T> T readFromJson(final Class<T> beanClass, final Path file, final String errorTemplate) { | ||
try { | ||
return MAPPER.readValue(file.toFile(), beanClass); | ||
} catch (final IOException e) { | ||
throw buildError(file, errorTemplate, e); | ||
} | ||
} | ||
|
||
private static VocabHunterException buildError(final Path file, final String errorTemplate, final Throwable e) { | ||
return new VocabHunterException(String.format(errorTemplate, file), e); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
core/src/test/java/io/github/vocabhunter/analysis/core/FileToolTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Open Source Software published under the Apache Licence, Version 2.0. | ||
*/ | ||
|
||
package io.github.vocabhunter.analysis.core; | ||
|
||
import io.github.vocabhunter.test.utils.TestFileManager; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class FileToolTest { | ||
private static final String ERROR_TEMPLATE = "Error %s"; | ||
private TestFileManager manager; | ||
|
||
private Path base; | ||
|
||
private Path existingFile; | ||
|
||
private Path newFile; | ||
|
||
private Path subDirectory; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
manager = new TestFileManager(getClass()); | ||
base = manager.getDirectory(); | ||
existingFile = manager.addFile("existingFile"); | ||
Files.createFile(existingFile); | ||
newFile = manager.addFile("newFile"); | ||
subDirectory = manager.addFile("directory"); | ||
Files.createDirectories(subDirectory); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
manager.cleanup(); | ||
} | ||
|
||
@Test | ||
public void testEnsureDirectoryExistsWithDirectory() { | ||
FileTool.ensureDirectoryExists(existingFile, ERROR_TEMPLATE); | ||
|
||
assertTrue(Files.isDirectory(base)); | ||
} | ||
|
||
@Test(expected = VocabHunterException.class) | ||
public void testEnsureDirectoryExistsWithError() { | ||
Path impossible = existingFile.resolve("file"); | ||
|
||
FileTool.ensureDirectoryExists(impossible, ERROR_TEMPLATE); | ||
} | ||
|
||
@Test | ||
public void testEnsureDirectoryExistsWithoutBase() { | ||
Path file = Paths.get("file"); | ||
FileTool.ensureDirectoryExists(file, ERROR_TEMPLATE); | ||
} | ||
|
||
@Test | ||
public void testWriteMinimalJsonSuccess() throws Exception { | ||
FileTool.writeMinimalJson(newFile, ERROR_TEMPLATE); | ||
|
||
List<String> result = Files.readAllLines(newFile); | ||
List<String> expected = Collections.singletonList("{}"); | ||
|
||
assertEquals(expected, result); | ||
} | ||
|
||
@Test(expected = VocabHunterException.class) | ||
public void testWriteMinimalJsonFailure() throws Exception { | ||
FileTool.writeMinimalJson(subDirectory, ERROR_TEMPLATE); | ||
} | ||
|
||
@Test | ||
public void testReadThenWriteJson() { | ||
FileToolTestBean bean = new FileToolTestBean("left", "right"); | ||
|
||
FileTool.writeAsJson(newFile, bean, ERROR_TEMPLATE); | ||
FileToolTestBean result = FileTool.readFromJson(FileToolTestBean.class, newFile, ERROR_TEMPLATE); | ||
|
||
assertEquals(bean, result); | ||
} | ||
|
||
@Test(expected = VocabHunterException.class) | ||
public void testWriteAsJsonWithError() { | ||
FileTool.writeAsJson(subDirectory, new FileToolTestBean(), ERROR_TEMPLATE); | ||
} | ||
|
||
@Test(expected = VocabHunterException.class) | ||
public void testReadFromJsonWithError() { | ||
FileTool.readFromJson(FileToolTestBean.class, subDirectory, ERROR_TEMPLATE); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
core/src/test/java/io/github/vocabhunter/analysis/core/FileToolTestBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Open Source Software published under the Apache Licence, Version 2.0. | ||
*/ | ||
|
||
package io.github.vocabhunter.analysis.core; | ||
|
||
import org.apache.commons.lang3.builder.EqualsBuilder; | ||
import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
|
||
public class FileToolTestBean { | ||
private String left; | ||
|
||
private String right; | ||
|
||
public FileToolTestBean() { | ||
// No argument constructor to allow use as standard Java Bean | ||
} | ||
|
||
public FileToolTestBean(final String left, final String right) { | ||
this.left = left; | ||
this.right = right; | ||
} | ||
|
||
public String getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(final String left) { | ||
this.left = left; | ||
} | ||
|
||
public String getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(final String right) { | ||
this.right = right; | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
|
||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
|
||
FileToolTestBean that = (FileToolTestBean) o; | ||
|
||
return new EqualsBuilder() | ||
.append(left, that.left) | ||
.append(right, that.right) | ||
.isEquals(); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return new HashCodeBuilder() | ||
.append(left) | ||
.append(right) | ||
.toHashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this) | ||
.append("left", left) | ||
.append("right", right) | ||
.toString(); | ||
} | ||
} |
Oops, something went wrong.