diff --git a/pom.xml b/pom.xml index 3730562a6..0588b8dbf 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ + xmlns="http://maven.apache.org/POM/4.0.0" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 @@ -132,6 +132,12 @@ ${xmlunit.version} test + + com.google.jimfs + jimfs + 1.2 + test + @@ -213,7 +219,7 @@ org.codehaus.mojo - versions-maven-plugin + versions-maven-plugin 2.16.0 @@ -265,8 +271,10 @@ download-single - https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.min.js - ${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs + + https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.min.js + + ${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs true @@ -277,8 +285,10 @@ download-single - https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js - ${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs + + https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.js + + ${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs true @@ -291,7 +301,8 @@ https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfjs.version} cmaps - ${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs/cmaps + + ${project.resources[0].directory}/digital/slovensko/autogram/ui/gui/vendor/pdfjs/cmaps @@ -327,7 +338,8 @@ jdeps - + true ${project.build.directory}${file.separator}jdeps.out @@ -338,7 +350,8 @@ - + @@ -369,7 +382,8 @@ javafx.fxml javafx.graphics javafx.web - + jdk.unsupported jdk.httpserver @@ -415,13 +429,16 @@ 3.1.0 bash - ${project.build.scriptSourceDirectory}${file.separator}resources + + ${project.build.scriptSourceDirectory}${file.separator}resources ${project.build.scriptSourceDirectory}${file.separator}package.sh ${jlink.jdk.path}${file.separator}bin${file.separator}jpackage - ${project.build.directory}${file.separator}${project.artifactId}-${project.version}-${platform} + + ${project.build.directory}${file.separator}${project.artifactId}-${project.version}-${platform} ${project.build.directory}${file.separator}preparedJDK - ${project.basedir}${file.separator}src${file.separator}main${file.separator}resources${file.separator}digital${file.separator}slovensko${file.separator}autogram + + ${project.basedir}${file.separator}src${file.separator}main${file.separator}resources${file.separator}digital${file.separator}slovensko${file.separator}autogram ${platform} ${project.version} ${project.build.directory} @@ -498,4 +515,4 @@ - + \ No newline at end of file diff --git a/src/main/java/digital/slovensko/autogram/core/TargetPath.java b/src/main/java/digital/slovensko/autogram/core/TargetPath.java index 7e9365a48..22747424d 100644 --- a/src/main/java/digital/slovensko/autogram/core/TargetPath.java +++ b/src/main/java/digital/slovensko/autogram/core/TargetPath.java @@ -1,9 +1,9 @@ package digital.slovensko.autogram.core; -import java.io.File; -import java.nio.file.Paths; - -import com.google.common.io.Files; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; import digital.slovensko.autogram.core.errors.SourceAndTargetTypeMismatchException; import digital.slovensko.autogram.core.errors.TargetAlreadyExistsException; @@ -11,38 +11,43 @@ import digital.slovensko.autogram.core.errors.UnableToCreateDirectoryException; public class TargetPath { - private final File targetDirectory; + private final Path targetDirectory; private final String targetName; - private final File sourceFile; + private final Path sourceFile; private final boolean isForce; private final boolean isGenerated; private final boolean isForMultipleFiles; private final boolean isParents; - public TargetPath(String target, File source, boolean force, boolean parents) { + private final FileSystem fs; + + public TargetPath(String target, Path source, boolean force, boolean parents, FileSystem fileSystem) { + fs = fileSystem; sourceFile = source; isForce = force; isParents = parents; isGenerated = target == null; - isForMultipleFiles = source.isDirectory(); + isForMultipleFiles = Files.isDirectory(source); if (isGenerated) { if (isForMultipleFiles) { - targetDirectory = new File(generateUniqueName(source.getParent(), source.getName() + "_signed", "")); + targetDirectory = fs.getPath( + generateUniqueName(source.getParent().toString(), source.getFileName().toString() + "_signed", + "")); targetName = null; } else { - targetDirectory = source.getParentFile(); + targetDirectory = source.getParent(); targetName = null; } } else { - var targetFile = new File(target); + var targetFile = fs.getPath(target); if (!hasSourceAndTargetMatchingType(sourceFile, targetFile)) throw new SourceAndTargetTypeMismatchException(); - if (targetFile.exists() && !isForce) + if (Files.exists(targetFile) && !isForce) throw new TargetAlreadyExistsException(); if (isForMultipleFiles) { @@ -50,30 +55,34 @@ public TargetPath(String target, File source, boolean force, boolean parents) { targetName = null; } else { - targetDirectory = targetFile.getParentFile(); - targetName = targetFile.getName(); + targetDirectory = targetFile.getParent(); + targetName = targetFile.getFileName().toString(); } } } public static TargetPath fromParams(CliParameters params) { - return new TargetPath(params.getTarget(), params.getSource(), params.isForce(), params.shouldMakeParentDirectories()); + return new TargetPath(params.getTarget(), params.getSource().toPath(), params.isForce(), + params.shouldMakeParentDirectories(), FileSystems.getDefault()); } - public static TargetPath fromSource(File source) { - return new TargetPath(null, source, false, false); + public static TargetPath fromSource(Path source) { + return new TargetPath(null, source, false, false, FileSystems.getDefault()); } /** * Create directory when we want to fill it out */ public void mkdirIfDir() { - if (targetDirectory == null || targetDirectory.exists()) + if (targetDirectory == null || Files.exists(targetDirectory)) return; if (isParents) { - if (!targetDirectory.mkdirs()) + try { + Files.createDirectories(targetDirectory); + } catch (Exception e) { throw new UnableToCreateDirectoryException(); + } return; } @@ -81,16 +90,19 @@ public void mkdirIfDir() { if (!isForMultipleFiles) throw new TargetDirectoryDoesNotExistException(); - if (!targetDirectory.mkdir()) + try { + Files.createDirectory(targetDirectory); + } catch (Exception e) { throw new UnableToCreateDirectoryException(); + } } - private static boolean hasSourceAndTargetMatchingType(File source, File target) { - if (!target.exists()) + private static boolean hasSourceAndTargetMatchingType(Path source, Path target) { + if (!Files.exists(target)) return true; - var bothAreFiles = target.isFile() && source.isFile(); - var bothAreDirectories = target.isDirectory() && source.isDirectory(); + var bothAreFiles = Files.isRegularFile(target) && Files.isRegularFile(source); + var bothAreDirectories = Files.isDirectory(target) && Files.isDirectory(source); return (bothAreDirectories || bothAreFiles); } @@ -100,21 +112,21 @@ private static boolean hasSourceAndTargetMatchingType(File source, File target) * */ - public File getSaveFilePath(File singleSourceFile) { + public Path getSaveFilePath(Path singleSourceFile) { var file = _getSaveFilePath(singleSourceFile); - if (file.exists() && !isForce) + if (Files.exists(file) && !isForce) throw new TargetAlreadyExistsException(); return file; } - private File _getSaveFilePath(File singleSourceFile) { + private Path _getSaveFilePath(Path singleSourceFile) { var targetName = this.targetName == null ? generateTargetName(singleSourceFile) : this.targetName; - var targetDirectoryPath = targetDirectory == null ? "" : targetDirectory.getPath(); - File targetSingleFile = Paths.get(targetDirectoryPath, targetName).toFile(); + var targetDirectoryPath = targetDirectory == null ? "" : targetDirectory.toString(); + Path targetSingleFile = fs.getPath(targetDirectoryPath, targetName); - if (!targetSingleFile.exists()) + if (!Files.exists(targetSingleFile)) return targetSingleFile; if (isForce) @@ -122,9 +134,11 @@ private File _getSaveFilePath(File singleSourceFile) { if (isGenerated) { var parent = targetSingleFile.getParent(); - var baseName = Files.getNameWithoutExtension(targetSingleFile.getName()); - var extension = "." + Files.getFileExtension(targetSingleFile.getName()); - return new File(generateUniqueName(parent, baseName, extension)); + var baseName = com.google.common.io.Files + .getNameWithoutExtension(targetSingleFile.getFileName().toString()); + var extension = "." + + com.google.common.io.Files.getFileExtension(targetSingleFile.getFileName().toString()); + return fs.getPath(generateUniqueName(parent.toString(), baseName, extension)); } throw new TargetAlreadyExistsException(); @@ -136,8 +150,8 @@ private String generateUniqueName(String parent, String baseName, String extensi parent = parent == null ? "" : parent; while (true) { var newTargetFile = _generateUniqueNameGetNewTargetFile(parent, newBaseName, extension); - if (!newTargetFile.exists()) - return newTargetFile.getPath(); + if (!Files.exists(newTargetFile)) + return newTargetFile.toString(); if (count > 1000) throw new TargetAlreadyExistsException(); @@ -147,16 +161,19 @@ private String generateUniqueName(String parent, String baseName, String extensi } } - private File _generateUniqueNameGetNewTargetFile(String parent, String baseName, String extension) { - return Paths.get(parent, baseName + extension).toFile(); + private Path _generateUniqueNameGetNewTargetFile(String parent, String baseName, String extension) { + return fs.getPath(parent, baseName + extension); } - private String generateTargetName(File singleSourceFile) { - var extension = singleSourceFile.getName().endsWith(".pdf") ? ".pdf" : ".asice"; + private String generateTargetName(Path singleSourceFile) { + var extension = singleSourceFile.getFileName().toString().endsWith(".pdf") ? ".pdf" : ".asice"; if (isGenerated || isForMultipleFiles) - return Files.getNameWithoutExtension(singleSourceFile.getName()) + "_signed" + extension; + return com.google.common.io.Files.getNameWithoutExtension(singleSourceFile.getFileName().toString()) + + "_signed" + + extension; else - return Files.getNameWithoutExtension(targetDirectory.getName()) + extension; + return com.google.common.io.Files.getNameWithoutExtension(targetDirectory.getFileName().toString()) + + extension; } } diff --git a/src/main/java/digital/slovensko/autogram/ui/SaveFileResponder.java b/src/main/java/digital/slovensko/autogram/ui/SaveFileResponder.java index 37a0b1485..fe00721d0 100644 --- a/src/main/java/digital/slovensko/autogram/ui/SaveFileResponder.java +++ b/src/main/java/digital/slovensko/autogram/ui/SaveFileResponder.java @@ -15,7 +15,7 @@ public class SaveFileResponder extends Responder { private final TargetPath targetPathBuilder; public SaveFileResponder(File file, Autogram autogram) { - this(file, autogram, TargetPath.fromSource(file)); + this(file, autogram, TargetPath.fromSource(file.toPath())); } public SaveFileResponder(File file, Autogram autogram, TargetPath targetPathBuilder) { @@ -26,9 +26,9 @@ public SaveFileResponder(File file, Autogram autogram, TargetPath targetPathBuil public void onDocumentSigned(SignedDocument signedDocument) { try { - var targetFile = targetPathBuilder.getSaveFilePath(file); - signedDocument.getDocument().save(targetFile.getPath()); - autogram.onDocumentSaved(targetFile); + var targetFile = targetPathBuilder.getSaveFilePath(file.toPath()); + signedDocument.getDocument().save(targetFile.toString()); + autogram.onDocumentSaved(targetFile.toFile()); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/test/java/digital/slovensko/autogram/core/TargetPathTest.java b/src/test/java/digital/slovensko/autogram/core/TargetPathTest.java index f1b92ca06..20e433a6d 100644 --- a/src/test/java/digital/slovensko/autogram/core/TargetPathTest.java +++ b/src/test/java/digital/slovensko/autogram/core/TargetPathTest.java @@ -1,224 +1,221 @@ package digital.slovensko.autogram.core; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import java.io.File; -import java.lang.reflect.Field; +import java.io.IOException; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import org.junit.jupiter.api.Test; -import org.junit.platform.commons.util.ReflectionUtils; + +import com.google.common.jimfs.Jimfs; public class TargetPathTest { + // @Rule + // public MockitoRule rule = + // MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); /** * Used in GUI mode with single file * or used in CLI mode without target eg. `--cli -s /test/virtual/source.pdf` + * + * @throws IOException */ @Test - public void testSingleFileNoTarget() { - var sourceFile = mockSourceFile(); + public void testSingleFileNoTarget() throws IOException { + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceFile = fs.getPath("/test/virtual/source.pdf"); + Files.createDirectories(sourceFile.getParent()); + Files.createFile(sourceFile); - var targetPath = new TargetPath(null, sourceFile, false, false); + var targetPath = new TargetPath(null, sourceFile, false, false, fs); var target = targetPath.getSaveFilePath(sourceFile); - assertEqualPath("/test/virtual/source_signed.pdf", target.getPath()); + assertEqualPath(fs, "/test/virtual/source_signed.pdf", target); } /** - * Used in GUI mode with single file and no target when generated target file exits + * Used in GUI mode with single file and no target when generated target file + * exits * or used in CLI mode without target eg. `--cli -s /test/virtual/source.pdf` + * + * @throws IOException */ @Test - public void testSingleFileNoTargetFileExists() { - var sourceFile = mockSourceFile(); - - var targetPath = spy(new TargetPath(null, sourceFile, false, false)); - - - var method = ReflectionUtils.findMethod(TargetPath.class, "_generateUniqueNameGetNewTargetFile").orElseThrow(); - method.setAccessible(true); - + public void testSingleFileNoTargetFileExists() throws IOException { + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceFile = fs.getPath("/test/virtual/source.pdf"); + Files.createDirectories(sourceFile.getParent()); + Files.createFile(sourceFile); + Files.createFile(fs.getPath("/test/virtual/source_signed.pdf")); + + var targetPath = new TargetPath(null, sourceFile, false, false, fs); var target = targetPath.getSaveFilePath(sourceFile); - assertEqualPath("/test/virtual/source_signed (1).pdf", target.getPath()); + assertEqualPath(fs, "/test/virtual/source_signed (1).pdf", target); } /** * Used in GUI mode with single file * or used in CLI mode without target eg. `--cli -s /test/virtual/source.pdf` + * + * @throws IOException */ @Test - public void testSingleFileNoTargetAsice() { - var sourceFile = mockSourceFile(); - when(sourceFile.getPath()).thenReturn("/test/virtual/source.xml"); - when(sourceFile.getName()).thenReturn("source.xml"); + public void testSingleFileNoTargetAsice() throws IOException { + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceFile = fs.getPath("/test/virtual/source.xml"); + Files.createDirectories(sourceFile.getParent()); + Files.createFile(sourceFile); - var targetPath = new TargetPath(null, sourceFile, false, false); + var targetPath = new TargetPath(null, sourceFile, false, false, fs); var target = targetPath.getSaveFilePath(sourceFile); - assertEqualPath("/test/virtual/source_signed.asice", target.getPath()); + assertEqualPath(fs, "/test/virtual/source_signed.asice", target); } /** * Used in CLI mode eg. `--cli -s /test/virtual/source.pdf -t * /test/virtual/target.pdf` + * + * @throws IOException */ @Test - public void testSingleFileWithTarget() { - var sourceFile = mockSourceFile(); + public void testSingleFileWithTarget() throws IOException { + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceFile = fs.getPath("/test/virtual/source.pdf"); + Files.createDirectories(sourceFile.getParent()); + Files.createFile(sourceFile); - var targetPath = new TargetPath("/test/virtual/other/target.pdf", sourceFile, false, false); + var targetPath = new TargetPath("/test/virtual/other/target.pdf", sourceFile, false, false, fs); var target = targetPath.getSaveFilePath(sourceFile); - assertEqualPath("/test/virtual/other/target.pdf", target.getPath()); + assertEqualPath(fs, "/test/virtual/other/target.pdf", target); } @Test - public void testDirectoryWithTarget() { + public void testDirectoryWithTarget() throws IOException { - var sourceDirectory = mockSourceDirectory(); + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceDirectory = fs.getPath("/test/virtual/"); + Files.createDirectories(sourceDirectory); - var source1 = mockSourceFile("/test/virtual/source", "source1.pdf"); - var source2 = mockSourceFile("/test/virtual/source", "source2.pdf"); + var source1 = fs.getPath("/test/virtual/source", "source1.pdf"); + var source2 = fs.getPath("/test/virtual/source", "source2.pdf"); - var targetPath = new TargetPath("/test/virtual/target/", sourceDirectory, false, false); + var targetPath = new TargetPath("/test/virtual/target/", sourceDirectory, false, false, fs); var target1 = targetPath.getSaveFilePath(source1); var target2 = targetPath.getSaveFilePath(source2); - assertEqualPath("/test/virtual/target/source1_signed.pdf", target1.getPath()); - assertEqualPath("/test/virtual/target/source2_signed.pdf", target2.getPath()); + assertEqualPath(fs, "/test/virtual/target/source1_signed.pdf", target1); + assertEqualPath(fs, "/test/virtual/target/source2_signed.pdf", target2); } @Test - public void testDirectoryNoTarget() { - var sourceDirectory = mockSourceDirectory(); + public void testDirectoryNoTarget() throws IOException { + + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceDirectory = fs.getPath("/test/virtual/source/"); + Files.createDirectories(sourceDirectory); - var source1 = mockSourceFile("/test/virtual/source", "source1.pdf"); - var source2 = mockSourceFile("/test/virtual/source", "source2.pdf"); + var source1 = fs.getPath("/test/virtual/source", "source1.pdf"); + var source2 = fs.getPath("/test/virtual/source", "source2.pdf"); - var targetPath = new TargetPath(null, sourceDirectory, false, false); + var targetPath = new TargetPath(null, sourceDirectory, false, false, fs); var target1 = targetPath.getSaveFilePath(source1); var target2 = targetPath.getSaveFilePath(source2); - assertEqualPath("/test/virtual/source_signed/source1_signed.pdf", target1.getPath()); - assertEqualPath("/test/virtual/source_signed/source2_signed.pdf", target2.getPath()); + assertEqualPath(fs, "/test/virtual/source_signed/source1_signed.pdf", target1); + assertEqualPath(fs, "/test/virtual/source_signed/source2_signed.pdf", target2); } @Test - public void testMkdirIfDirNotExists() throws IllegalArgumentException, IllegalAccessException { - var sourceDirectory = mockSourceDirectory(); + public void testMkdirIfDirNotExists() throws IllegalArgumentException, + IllegalAccessException, IOException { - var targetDirectory = mockTargetDirectory(); - when(targetDirectory.exists()).thenReturn(false); - - var targetPath = new TargetPath(null, sourceDirectory, false, false); - - Field field = ReflectionUtils.findFields(TargetPath.class, f -> f.getName().equals("targetDirectory"), - ReflectionUtils.HierarchyTraversalMode.TOP_DOWN).get(0); - field.setAccessible(true); - field.set(targetPath, targetDirectory); + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceDirectory = fs.getPath("/test/virtual/source"); + Files.createDirectories(sourceDirectory); + var targetPath = new TargetPath(null, sourceDirectory, false, false, fs); targetPath.mkdirIfDir(); - verify(targetDirectory, times(1)).mkdir(); - verify(targetDirectory, times(0)).mkdirs(); + assertTrue(Files.exists(fs.getPath("/test/virtual/source_signed"))); } - @Test - public void testMkdirIfDirExists() throws IllegalArgumentException, IllegalAccessException { - var sourceDirectory = mockSourceDirectory(); + @Test() + public void testTargetDirExits() throws IllegalArgumentException, + IllegalAccessException, IOException { - var targetDirectory = mockTargetDirectory(); - when(targetDirectory.exists()).thenReturn(true); + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceDirectory = fs.getPath("/test/virtual/"); + Files.createDirectories(sourceDirectory); + Files.createDirectories(fs.getPath("/test/output")); - var targetPath = new TargetPath(null, sourceDirectory, false, false); + assertThrows(digital.slovensko.autogram.core.errors.TargetAlreadyExistsException.class, () -> { + var targetPath = new TargetPath("/test/output", sourceDirectory, false, false, fs); + }); + } - Field field = ReflectionUtils.findFields(TargetPath.class, f -> f.getName().equals("targetDirectory"), - ReflectionUtils.HierarchyTraversalMode.TOP_DOWN).get(0); - field.setAccessible(true); - field.set(targetPath, targetDirectory); + @Test() + public void testTargetFileExits() throws IllegalArgumentException, + IllegalAccessException, IOException { - targetPath.mkdirIfDir(); + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceFile = fs.getPath("/test/virtual/source.pdf"); + Files.createDirectories(sourceFile.getParent()); + Files.createFile(sourceFile); + Files.createFile(fs.getPath("/test/output.pdf")); - verify(targetDirectory, times(0)).mkdir(); - verify(targetDirectory, times(0)).mkdirs(); + assertThrows(digital.slovensko.autogram.core.errors.TargetAlreadyExistsException.class, () -> { + var targetPath = new TargetPath("/test/output.pdf", sourceFile, false, false, fs); + }); } - /* Mock helpers */ + @Test() + public void testMkdirIfDirExistsForce() throws IllegalArgumentException, + IllegalAccessException, IOException { - /** - * @return mock for `/test/virtual/source.pdf` - */ - private File mockSourceFile() { - return mockSourceFile("/test/virtual", "source.pdf"); - } + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceDirectory = fs.getPath("/test/virtual/"); + Files.createDirectories(sourceDirectory); + Files.createDirectories(fs.getPath("/test/output")); - private File mockSourceFile(String parentPath, String name) { - var sourceFileParent = mock(File.class); - when(sourceFileParent.getPath()).thenReturn("/test/virtual"); - when(sourceFileParent.getName()).thenReturn("virtual"); - when(sourceFileParent.getParent()).thenReturn("/test"); - when(sourceFileParent.isDirectory()).thenReturn(true); - when(sourceFileParent.isFile()).thenReturn(false); - - var sourceFile = mock(File.class); - when(sourceFile.getPath()).thenReturn(Paths.get(parentPath, name).toString()); - when(sourceFile.getName()).thenReturn(name); - when(sourceFile.getParent()).thenReturn(parentPath); - when(sourceFile.isDirectory()).thenReturn(false); - when(sourceFile.isFile()).thenReturn(true); - when(sourceFile.getParentFile()).thenReturn(sourceFileParent); - - return sourceFile; + var targetPath = new TargetPath("/test/output", sourceDirectory, true, false, fs); + targetPath.mkdirIfDir(); } - /** - * - * @return mock for `/test/virtual/source` - */ - private File mockSourceDirectory() { - var sourceDirectoryParent = mock(File.class); - when(sourceDirectoryParent.getPath()).thenReturn("/test/virtual/"); - when(sourceDirectoryParent.getName()).thenReturn("virtual"); - when(sourceDirectoryParent.getParent()).thenReturn("/test"); - when(sourceDirectoryParent.isDirectory()).thenReturn(true); - when(sourceDirectoryParent.isFile()).thenReturn(false); - - var sourceDirectory = mock(File.class); - when(sourceDirectory.getPath()).thenReturn("/test/virtual/source"); - when(sourceDirectory.getName()).thenReturn("source"); - when(sourceDirectory.getParent()).thenReturn("/test/virtual"); - when(sourceDirectory.isDirectory()).thenReturn(true); - when(sourceDirectory.isFile()).thenReturn(false); - when(sourceDirectory.getParentFile()).thenReturn(sourceDirectoryParent); - - return sourceDirectory; - } + @Test() + public void testMkdirIfDirExistsParents() throws IllegalArgumentException, + IllegalAccessException, IOException { + + FileSystem fs = Jimfs.newFileSystem(com.google.common.jimfs.Configuration.unix()); + var sourceDirectory = fs.getPath("/test/virtual/"); + Files.createDirectories(sourceDirectory); + var targetPath = new TargetPath("/test/output/parent/directories", sourceDirectory, false, true, fs); + targetPath.mkdirIfDir(); - /** - * - * @return mock for `/test/virtual/source_signed` - */ - private File mockTargetDirectory() { - var targetDirectory = mock(File.class); - when(targetDirectory.getPath()).thenReturn("/test/virtual/source_signed"); - when(targetDirectory.getName()).thenReturn("source_signed"); - when(targetDirectory.getParent()).thenReturn("/test/virtual"); - when(targetDirectory.isDirectory()).thenReturn(true); - when(targetDirectory.isFile()).thenReturn(false); - when(targetDirectory.exists()).thenReturn(false); - when(targetDirectory.mkdir()).thenReturn(true); - when(targetDirectory.mkdirs()).thenReturn(true); - - return targetDirectory; } /* Assert helpers */ - private void assertEqualPath(String expected, String actual) { - assertEquals(Paths.get(expected).normalize().toString(), Paths.get(actual).normalize().toString()); + private void assertEqualPath(FileSystem fs, String expected, String actual) { + assertEqualPath(fs.getPath(expected), fs.getPath(actual)); + } + + private void assertEqualPath(FileSystem fs, String expected, Path actual) { + assertEqualPath(fs.getPath(expected), actual); + } + + private void assertEqualPath(Path expected, Path actual) { + assertEquals(expected.normalize().toString(), actual.normalize().toString()); } }