MoveDirectory extends AbstractToOperation<MoveDirectory> { @Override protected TOExecutionResult execution(File transformedAppFolder, TransformationContext transformationContext) { File filesFrom = getAbsoluteFile(transformedAppFolder, transformationContext); File fileTo = getFileTo(transformedAppFolder, transformationContext); TOExecutionResult result = null; try { FileUtils.moveDirectory(filesFrom, fileTo); String details = String.format("Directory '%s' has been moved to '%s'", getRelativePath(transformedAppFolder, filesFrom), getRelativePath(transformedAppFolder, fileTo)); result = TOExecutionResult.success(this, details); } catch (IOException e) { result = TOExecutionResult.error(this, new TransformationOperationException("Directory could not be moved", e)); } return result; } MoveDirectory();   }
@Test public void test() { File fromDir = new File(transformedAppFolder, "blah"); File toDir = new File(transformedAppFolder, "/src/main/resources/blah"); assertTrue(fromDir.exists()); assertTrue(fromDir.isDirectory()); assertFalse(toDir.exists()); int fromDirChildrenCount = fromDir.listFiles().length; MoveDirectory moveDirectory = new MoveDirectory().relative("blah").setToRelative("src/main/resources/blah"); TOExecutionResult executionResult = moveDirectory.execution(transformedAppFolder, transformationContext); assertEquals(executionResult.getType(), TOExecutionResult.Type.SUCCESS); assertEquals(moveDirectory.getDescription(), "Move a directory from blah to src/main/resources/blah"); assertEquals(executionResult.getDetails(), "Directory '/blah' has been moved to '/src/main/resources/blah'"); assertFalse(fromDir.exists()); assertTrue(toDir.exists()); assertTrue(toDir.isDirectory()); assertEquals(fromDirChildrenCount, toDir.listFiles().length); }
@Test public void nonExistentFromFolderTest() { Mockito.when(transformationContext.get("ATT")).thenReturn(new File(transformedAppFolder, "/src/main/resources")); MoveDirectory moveDirectory = new MoveDirectory().relative("nonExistentFolder").setToAbsolute("ATT", "blah"); TOExecutionResult executionResult = moveDirectory.execution(transformedAppFolder, transformationContext); assertEquals(moveDirectory.getToAbsoluteAttribute(), "ATT"); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Directory could not be moved"); assertEquals(executionResult.getException().getCause().getMessage(), "Source '" + new File(transformedAppFolder, "nonExistentFolder").getAbsolutePath() + "' does not exist"); assertEquals(moveDirectory.getDescription(), "Move a directory from nonExistentFolder to the location defined by transformation context attribute ATT"); assertNull(executionResult.getDetails()); }
@Test public void existentToFolderTest() { MoveDirectory moveDirectory = new MoveDirectory().relative("blah").setToRelative("src/main/resources"); TOExecutionResult executionResult = moveDirectory.execution(transformedAppFolder, transformationContext); assertEquals(moveDirectory.getToRelative(), "src/main/resources"); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Directory could not be moved"); assertEquals(executionResult.getException().getCause().getMessage(), "Destination '" + new File(transformedAppFolder, "src/main/resources").getAbsolutePath() + "' already exists"); assertEquals(moveDirectory.getDescription(), "Move a directory from blah to src/main/resources"); assertNull(executionResult.getDetails()); }
@Test public void nonDirectoryTest() { MoveDirectory moveDirectory = new MoveDirectory().relative("pom.xml").setToRelative("src/main/resources"); TOExecutionResult executionResult = moveDirectory.execution(transformedAppFolder, transformationContext); assertEquals(moveDirectory.getToRelative(), "src/main/resources"); assertEquals(executionResult.getType(), TOExecutionResult.Type.ERROR); assertEquals(executionResult.getException().getClass(), TransformationOperationException.class); assertEquals(executionResult.getException().getMessage(), "Directory could not be moved"); assertEquals(executionResult.getException().getCause().getMessage(), "Source '" + new File(transformedAppFolder, "pom.xml").getAbsolutePath() + "' is not a directory"); assertEquals(moveDirectory.getDescription(), "Move a directory from pom.xml to src/main/resources"); assertNull(executionResult.getDetails()); }