CopyFile extends AbstractToOperation<CopyFile> { @Override protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) { File fileFrom = getAbsoluteFile(transformedAppFolder, transformationContext); File fileTo = getFileTo(transformedAppFolder, transformationContext); TOExecutionResult result; try { if (fileFrom.isDirectory()) { IOException ex = new IOException(getRelativePath(transformedAppFolder, fileFrom) + " (Is a directory)"); result = TOExecutionResult.error(this, new TransformationOperationException("File could not be copied", ex)); } else { String details = String.format("File '%s' has been copied to '%s'", getRelativePath(), getRelativePath(transformedAppFolder, fileTo)); FileUtils.copyFileToDirectory(fileFrom, fileTo); result = TOExecutionResult.success(this, details); } } catch (IOException e) { result = TOExecutionResult.error(this, new TransformationOperationException("File could not be copied", e)); } return result; } CopyFile();   }
@Test public void test() throws IOException { File fromFile = new File(transformedAppFolder, "foo.xml"); File toFile = new File(transformedAppFolder, "/src/main/resources/foo.xml"); assertTrue(fromFile.exists()); assertFalse(fromFile.isDirectory()); assertFalse(toFile.exists()); CopyFile copyFile = new CopyFile().relative("foo.xml").setToRelative("src/main/resources"); TOExecutionResult executionResult = copyFile.execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertEquals(copyFile.getDescription(), "Copy file foo.xml to src/main/resources"); assertEquals(executionResult.getDetails(), "File 'foo.xml' has been copied to '/src/main/resources'"); assertTrue(fromFile.exists()); assertTrue(toFile.exists()); assertTrue(FileUtils.contentEquals(fromFile, toFile)); }
@Test public void nonExistentFromFileTest() { CopyFile copyFile = new CopyFile().relative("nonExistentFile").setToRelative("src/main/resources"); TOExecutionResult executionResult = copyFile.execution(transformedAppFolder, transformationContext); assertEquals(copyFile.getToRelative(), "src/main/resources"); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "File could not be copied"); assertEquals(executionResult.getException().getCause().getMessage(), "File " + new File(transformedAppFolder, "nonExistentFile").getAbsolutePath() + " does not exist"); assertEquals(copyFile.getDescription(), "Copy file nonExistentFile to src/main/resources"); assertNull(executionResult.getDetails()); }
@Test public void nonExistentToFolderTest() throws IOException { File fromFile = new File(transformedAppFolder, "foo.xml"); File toFile = new File(transformedAppFolder, "fooFolder/foo.xml"); assertTrue(fromFile.exists()); assertFalse(fromFile.isDirectory()); assertFalse(toFile.exists()); assertFalse(toFile.getParentFile().exists()); CopyFile copyFile = new CopyFile().relative("foo.xml").setToRelative("fooFolder"); TOExecutionResult executionResult = copyFile.execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertEquals(copyFile.getDescription(), "Copy file foo.xml to fooFolder"); assertEquals(executionResult.getDetails(), "File 'foo.xml' has been copied to '/fooFolder'"); assertTrue(fromFile.exists()); assertTrue(toFile.exists()); assertTrue(FileUtils.contentEquals(fromFile, toFile)); }
@Test public void nonFileTest() { CopyFile copyFile = new CopyFile().relative("blah").setToRelative("src/main/resources"); TOExecutionResult executionResult = copyFile.execution(transformedAppFolder, transformationContext); assertEquals(copyFile.getToRelative(), "src/main/resources"); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "File could not be copied"); assertEquals(executionResult.getException().getCause().getClass(), IOException.class); assertEquals(executionResult.getException().getCause().getMessage(), "/blah (Is a directory)"); assertEquals(copyFile.getDescription(), "Copy file blah to src/main/resources"); assertNull(executionResult.getDetails()); }