DefaultMultipleOutputWriter implements MultipleOutputWriter { @Override public void addOutput(PopulatedFileOutput fileOutput) { if (nonNull(multipleFiles.putIfAbsent(fileOutput.getName(), fileOutput.getFile()))) { int count = 1; String basename = getBaseName(fileOutput.getName()); String extension = getExtension(fileOutput.getName()); while (nonNull( multipleFiles.putIfAbsent(format("%s(%d).%s", basename, count, extension), fileOutput.getFile())) && count < 100) { count++; } } } DefaultMultipleOutputWriter(ExistingOutputPolicy existingOutputPolicy, TaskExecutionContext executionContext); @Override void dispatch(FileTaskOutput output); @Override void dispatch(DirectoryTaskOutput output); @Override void dispatch(FileOrDirectoryTaskOutput output); @Override void addOutput(PopulatedFileOutput fileOutput);  }
@Test public void moveToDir() throws TaskOutputVisitException, IOException { File out = folder.newFolder(); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.OVERWRITE, context); victim.addOutput(FileOutput.file(folder.newFile()).name("out.pdf")); new DirectoryTaskOutput(out).accept(victim); assertEquals(1, out.list().length); }
@Test public void moveToFileOrDir() throws TaskOutputVisitException, IOException { File out = folder.newFolder(); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.OVERWRITE, context); victim.addOutput(FileOutput.file(folder.newFile()).name("out.pdf")); new FileOrDirectoryTaskOutput(out).accept(victim); assertEquals(1, out.list().length); }
@Test(expected = TaskOutputVisitException.class) public void moveToDirExsisting() throws TaskOutputVisitException, IOException { File outFile = folder.newFile(); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.FAIL, context); victim.addOutput(FileOutput.file(outFile).name(outFile.getName())); new DirectoryTaskOutput(outFile.getParentFile()).accept(victim); }
@Test public void moveToDirExsistingOverwrite() throws TaskOutputVisitException, IOException { File existing = folder.newFile(); assertEquals(0, existing.length()); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.OVERWRITE, context); Path outFile = Files.createTempFile("sejda", ".tmp"); Files.write(outFile, new byte[] { 0, 1, 1, 1 }); victim.addOutput(FileOutput.file(outFile.toFile()).name(existing.getName())); new DirectoryTaskOutput(existing.getParentFile()).accept(victim); assertEquals(1, existing.getParentFile().list().length); assertEquals(4, existing.getParentFile().listFiles()[0].length()); Files.deleteIfExists(outFile); }
@Test public void moveToDirExsistingSkip() throws TaskOutputVisitException, IOException { File existing = folder.newFile(); assertEquals(0, existing.length()); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.SKIP, context); Path outFile = Files.createTempFile("sejda", ".tmp"); Files.write(outFile, new byte[] { 0, 1, 1, 1 }); victim.addOutput(FileOutput.file(outFile.toFile()).name(existing.getName())); new DirectoryTaskOutput(existing.getParentFile()).accept(victim); assertEquals(1, existing.getParentFile().list().length); assertEquals(0, existing.getParentFile().listFiles()[0].length()); Files.delete(outFile); }
@Test(expected = TaskOutputVisitException.class) public void moveToFileDirExsisting() throws TaskOutputVisitException, IOException { File outFile = folder.newFile(); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.FAIL, context); victim.addOutput(FileOutput.file(outFile).name(outFile.getName())); new FileOrDirectoryTaskOutput(outFile.getParentFile()).accept(victim); }
@Test public void moveToDirExsistingRenamed() throws TaskOutputVisitException, IOException { File outFile = folder.newFile(); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.RENAME, context); victim.addOutput(FileOutput.file(folder.newFile()).name(outFile.getName())); new DirectoryTaskOutput(outFile.getParentFile()).accept(victim); assertEquals(2, outFile.getParentFile().list().length); }
@Test public void addFilesRenamesExisting() throws TaskOutputVisitException, IOException { DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.FAIL, context); victim.addOutput(FileOutput.file(folder.newFile()).name("myName.pdf")); victim.addOutput(FileOutput.file(folder.newFile()).name("myName.pdf")); victim.addOutput(FileOutput.file(folder.newFile()).name("myName.pdf")); File outFolder = folder.newFolder(); new DirectoryTaskOutput(outFolder).accept(victim); assertEquals(3, outFolder.list().length); }
@Test public void moveToFileOrDirExsistingRenamed() throws TaskOutputVisitException, IOException { File outFile = folder.newFile(); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.RENAME, context); victim.addOutput(FileOutput.file(folder.newFile()).name(outFile.getName())); new FileOrDirectoryTaskOutput(outFile.getParentFile()).accept(victim); assertEquals(2, outFile.getParentFile().list().length); }
@Test(expected = TaskOutputVisitException.class) public void moveToFileOrDirInvalidOut() throws TaskOutputVisitException, IOException { DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.RENAME, context); victim.addOutput(FileOutput.file(folder.newFile()).name("a")); victim.addOutput(FileOutput.file(folder.newFile()).name("b")); new FileOrDirectoryTaskOutput(folder.newFile()).accept(victim); }
@Test public void moveToFileOrDirSingleFile() throws TaskOutputVisitException, IOException { File out = folder.newFile(); assertEquals(0, out.length()); Path outFile = Files.createTempFile("sejda", ".tmp"); Files.write(outFile, new byte[] { 0, 1, 1, 1 }); DefaultMultipleOutputWriter victim = new DefaultMultipleOutputWriter(ExistingOutputPolicy.OVERWRITE, context); victim.addOutput(FileOutput.file(outFile.toFile()).name("out.pdf")); new FileOrDirectoryTaskOutput(out).accept(victim); assertEquals(4, out.length()); Files.deleteIfExists(outFile); }