OutputWriterHelper { static void moveToDirectory(Map<String, File> files, File outputDirectory, ExistingOutputPolicy existingOutputPolicy, TaskExecutionContext executionContext) throws IOException { if (!outputDirectory.exists() && !outputDirectory.mkdirs()) { throw new IOException(String.format("Unable to make destination directory tree %s.", outputDirectory)); } if (!outputDirectory.isDirectory()) { throw new IOException(String.format("Wrong output destination %s, must be a directory.", outputDirectory)); } for (Entry<String, File> entry : files.entrySet()) { if (isBlank(entry.getKey())) { throw new IOException(String.format( "Unable to move %s to the output directory, no output name specified.", entry.getValue())); } moveFile(entry.getValue(), new File(outputDirectory, finalName(entry.getKey(), files.size())), existingOutputPolicy, executionContext); } } private  OutputWriterHelper();   }
@Test public void copyFailsDirectoryType() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("newName", folder.newFile()); try { OutputWriterHelper.moveToDirectory(files, folder.newFile(), ExistingOutputPolicy.OVERWRITE, context); fail("Exception expected"); } catch (IOException e) { assertTrue("Different exception expected.", e.getMessage().startsWith("Wrong output destination")); } }
@Test public void copyDirectorySkips() throws IOException { File dest = folder.newFolder(); File tempFile = folder.newFile(); Map<String, File> files = populateWithOneExisting(dest, tempFile); OutputWriterHelper.moveToDirectory(files, dest, ExistingOutputPolicy.SKIP, context); assertEquals(2, dest.list().length); assertEquals(1, context.notifiableTaskMetadata().taskOutput().size()); }
@Test public void copyDirectoryOverwrite() throws IOException { File dest = folder.newFolder(); File tempFile = folder.newFile(); Map<String, File> files = populateWithOneExisting(dest, tempFile); OutputWriterHelper.moveToDirectory(files, dest, ExistingOutputPolicy.OVERWRITE, context); assertEquals(2, dest.list().length); assertEquals(2, context.notifiableTaskMetadata().taskOutput().size()); }
@Test(expected = IOException.class) public void copyDirectoryFail() throws IOException { File dest = folder.newFolder(); File tempFile = folder.newFile(); Map<String, File> files = populateWithOneExisting(dest, tempFile); OutputWriterHelper.moveToDirectory(files, dest, ExistingOutputPolicy.FAIL, context); }
@Test public void updateFilname() throws IOException { Map<String, File> files = new LinkedHashMap<>(); File dest = folder.newFolder(); files.put("1_of_[TOTAL_FILESNUMBER].pdf", folder.newFile()); files.put("2_of_[TOTAL_FILESNUMBER].pdf", folder.newFile()); OutputWriterHelper.moveToDirectory(files, dest, ExistingOutputPolicy.SKIP, context); assertEquals(2, dest.list().length); assertEquals(2, context.notifiableTaskMetadata().taskOutput().size()); assertEquals("1_of_2.pdf", context.notifiableTaskMetadata().taskOutput().get(0).getName()); assertEquals("2_of_2.pdf", context.notifiableTaskMetadata().taskOutput().get(1).getName()); }
@Test public void namesAreUpdatedBeforeShorten() throws IOException { Map<String, File> files = new LinkedHashMap<>(); File dest = folder.newFolder(); files.put("[TOTAL_FILESNUMBER]" + repeat("a", 248) + "[TOTAL_FILESNUMBER].pdf", folder.newFile()); OutputWriterHelper.moveToDirectory(files, dest, ExistingOutputPolicy.SKIP, context); assertEquals(1, dest.list().length); assertEquals(1, context.notifiableTaskMetadata().taskOutput().size()); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName(), startsWith("1")); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName(), endsWith("1.pdf")); }
@Test public void namesAreShortened() throws IOException { Map<String, File> files = new LinkedHashMap<>(); File dest = folder.newFolder(); files.put("[TOTAL_FILESNUMBER]" + repeat("a", 400) + "[TOTAL_FILESNUMBER].pdf", folder.newFile()); OutputWriterHelper.moveToDirectory(files, dest, ExistingOutputPolicy.SKIP, context); assertEquals(1, dest.list().length); assertEquals(1, context.notifiableTaskMetadata().taskOutput().size()); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName(), startsWith("1")); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName(), endsWith("a.pdf")); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName().length(), is(lessThan(256))); }
@Test public void namesAreShortenedUnicode() throws IOException { Map<String, File> files = new LinkedHashMap<>(); File dest = folder.newFolder(); files.put("[TOTAL_FILESNUMBER]" + repeat("ว", 400) + "[TOTAL_FILESNUMBER].pdf", folder.newFile()); OutputWriterHelper.moveToDirectory(files, dest, ExistingOutputPolicy.SKIP, context); assertEquals(1, dest.list().length); assertEquals(1, context.notifiableTaskMetadata().taskOutput().size()); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName(), startsWith("1")); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName(), endsWith("ว.pdf")); assertThat(context.notifiableTaskMetadata().taskOutput().get(0).getName().length(), is(lessThan(256))); }
@Test public void copyFailsDirectoryMkdirs() throws IOException { File tempFile = folder.newFile(); Map<String, File> files = new HashMap<String, File>(); files.put("newName", tempFile); File outFile = mock(File.class); when(outFile.isDirectory()).thenReturn(Boolean.TRUE); when(outFile.exists()).thenReturn(Boolean.FALSE); when(outFile.mkdirs()).thenReturn(Boolean.FALSE); try { OutputWriterHelper.moveToDirectory(files, outFile, ExistingOutputPolicy.OVERWRITE, context); fail("Exception expected"); } catch (IOException e) { assertTrue("Different exception expected.", e.getMessage().startsWith("Unable to make destination")); } }
@Test public void moveCreatesDirectoryTree() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("file.pdf", folder.newFile()); Path out = Paths.get(outputFolder.newFolder().getAbsolutePath(), "this", "does", "not", "exist"); assertFalse(Files.isDirectory(out)); OutputWriterHelper.moveToDirectory(files, out.toFile(), ExistingOutputPolicy.OVERWRITE, context); assertTrue(Files.isDirectory(out)); }