OutputWriterHelper { static void moveToFile(Map<String, File> files, File outputFile, ExistingOutputPolicy existingOutputPolicy, TaskExecutionContext executionContext) throws IOException { if (outputFile.exists() && !outputFile.isFile()) { throw new IOException(String.format("Wrong output destination %s, must be a file.", outputFile)); } if (files.size() != 1) { throw new IOException( String.format("Wrong files map size %d, must be 1 to copy to the selected destination %s", files.size(), outputFile)); } for (Entry<String, File> entry : files.entrySet()) { moveFile(entry.getValue(), outputFile, of(existingOutputPolicy).filter(p -> p != SKIP).orElseGet(() -> { LOG.debug("Cannot use {} output policy for single output, replaced with {}", SKIP, FAIL); return FAIL; }), executionContext); } } private  OutputWriterHelper();   }
@Test public void copyFailsMapSize() { Map<String, File> files = new HashMap<String, File>(); File outFile = mock(File.class); when(outFile.isFile()).thenReturn(Boolean.TRUE); try { OutputWriterHelper.moveToFile(files, outFile, ExistingOutputPolicy.OVERWRITE, context); fail("Exception expected"); } catch (IOException e) { assertTrue("Different exception expected.", e.getMessage().startsWith("Wrong files map size")); } }
@Test public void copyFailsFileType() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("newName", folder.newFile()); File outFile = mock(File.class); when(outFile.isFile()).thenReturn(Boolean.FALSE); when(outFile.exists()).thenReturn(Boolean.TRUE); try { OutputWriterHelper.moveToFile(files, outFile, ExistingOutputPolicy.OVERWRITE, context); fail("Exception expected"); } catch (IOException e) { assertTrue("Different exception expected.", e.getMessage().endsWith("must be a file.")); } }
@Test public void copyFailsOverwrite() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("newName", folder.newFile()); File outFile = mock(File.class); when(outFile.isFile()).thenReturn(Boolean.TRUE); when(outFile.exists()).thenReturn(Boolean.TRUE); try { OutputWriterHelper.moveToFile(files, outFile, ExistingOutputPolicy.FAIL, context); fail("Exception expected"); } catch (IOException e) { assertTrue("Different exception expected.", e.getMessage().startsWith("Unable to write")); } }
@Test public void copySingleFileSkipFallbacksToFail() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("newName", folder.newFile()); File outFile = mock(File.class); when(outFile.isFile()).thenReturn(Boolean.TRUE); when(outFile.exists()).thenReturn(Boolean.TRUE); try { OutputWriterHelper.moveToFile(files, outFile, ExistingOutputPolicy.SKIP, context); fail("Exception expected"); } catch (IOException e) { assertTrue("Different exception expected.", e.getMessage().startsWith("Unable to write")); } }
@Test public void existingOutputPolicyRENAME_conflict() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("existing.pdf", folder.newFile()); File outFile = outputFolder.newFile("existing.pdf"); outputFolder.newFile("existing(1).pdf"); outputFolder.newFile("existing(2).pdf"); OutputWriterHelper.moveToFile(files, outFile, ExistingOutputPolicy.RENAME, context); assertThat(Arrays.asList(outputFolder.getRoot().list()), hasItem("existing(3).pdf")); }
@Test public void existingOutputPolicyRENAME_noConflict() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("ok.pdf", folder.newFile()); File outFile = new File(outputFolder.getRoot(), "ok.pdf"); OutputWriterHelper.moveToFile(files, outFile, ExistingOutputPolicy.RENAME, context); assertThat(Arrays.asList(outputFolder.getRoot().list()), hasItem("ok.pdf")); }
@Test public void existingOutputPolicyRENAME_exception() throws IOException { Map<String, File> files = new HashMap<String, File>(); files.put("existing.pdf", folder.newFile()); File outFile = outputFolder.newFile("existing.pdf"); for (int i = 1; i <= 100; i++) { outputFolder.newFile(String.format("existing(%d).pdf", i)); } try { OutputWriterHelper.moveToFile(files, outFile, ExistingOutputPolicy.RENAME, context); fail("Exception expected, about the fact that a new filename that doesn't exist could not be generated"); } catch (IOException e) { assertTrue("Different exception expected, got: " + e.getMessage(), e.getMessage().startsWith("Unable to generate a new filename that does not exist")); } }