@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")); } }
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); } }
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); } } }
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(); }
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();  }
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();   }