Skip to content

Commit

Permalink
Merge pull request #200 from tremes/plugin-updates
Browse files Browse the repository at this point in the history
FORGE-620 fix
  • Loading branch information
gastaldi committed Aug 17, 2012
2 parents ffc7a37 + 8555d42 commit a8d6c1b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 14 deletions.
Expand Up @@ -14,8 +14,8 @@
import org.jboss.forge.resources.DirectoryResource;
import org.jboss.forge.resources.FileResource;
import org.jboss.forge.resources.Resource;
import org.jboss.forge.resources.ResourceFlag;
import org.jboss.forge.shell.plugins.Alias;
import org.jboss.forge.shell.plugins.Current;
import org.jboss.forge.shell.plugins.DefaultCommand;
import org.jboss.forge.shell.plugins.Help;
import org.jboss.forge.shell.plugins.Option;
Expand All @@ -36,6 +36,10 @@
@Help("Renames a file or directory")
public class MovePlugin implements Plugin
{

@Inject @Current
Resource<?> directory;

private final ResourceFactory resourceFactory;

@Inject
Expand All @@ -51,20 +55,8 @@ public void rename(
@Option(name = "force", shortName = "f", description = "force operation", flagOnly = true) final boolean force,
final PipeOut out)
{
if (isDirectory(source))
{
Resource<?> directory = source.getParent();
rename(source, directory, target, force, out);
}
else if (isFile(source))
{
Resource<?> directory = source.isFlagSet(ResourceFlag.Leaf) ? source.getParent() : source;
Resource<?> directory = this.directory;
rename(source, directory, target, force, out);
}
else
{
throw new RuntimeException("cannot rename resource type: " + source.getClass().getSimpleName());
}
}

private void rename(final Resource<?> source, Resource<?> directory, final String target, final boolean force,
Expand Down
Expand Up @@ -7,6 +7,7 @@
package org.jboss.forge.shell.test.plugins.builtin;

import org.jboss.forge.resources.DirectoryResource;
import org.jboss.forge.resources.FileResource;
import org.jboss.forge.resources.Resource;
import org.jboss.forge.shell.Shell;
import org.jboss.forge.test.AbstractShellTest;
Expand Down Expand Up @@ -187,5 +188,93 @@ public void testRenameDirectory() throws Exception
assertFalse(directoryToRename.exists());
assertTrue(renamedDirectory.exists());
}

@Test
public void testMoveFileWithRelativePath() throws Exception
{

String testFolder = "testFolder";
String file = "moveFile";
String nonExisting = "newNoneExisting";

initializeJavaProject();
Shell shell = getShell();
shell.execute("mkdir " + testFolder);
String relativePath = testFolder.concat("/").concat(file);
shell.execute("touch " + relativePath);

Resource<?> fileToMove = shell.getCurrentDirectory().getChild(testFolder).getChild(file);
assertTrue(fileToMove.exists());

shell.execute("mv " + relativePath + " " + nonExisting);
Resource<?> move = shell.getCurrentDirectory().getChild(nonExisting);
assertTrue(move.exists());
assertFalse(fileToMove.exists());

}

@Test
public void testMoveFolderToNonExistingFolder() throws Exception
{

String testFolder = "testFolder";
String newFolder = "newFolder";
String nonExisting = "nonExisting";
String subFolderA = "subFolder1";
String subFolderB = "subFolder2";
String fileA = "file1";
String fileB = "file2";
String relativePath = newFolder.concat("/").concat(nonExisting);

initializeJavaProject();
Shell shell = getShell();
shell.execute("mkdir " + testFolder);
shell.execute("mkdir " + newFolder);
shell.execute("cd " + testFolder);
shell.execute("mkdir " + subFolderA);
shell.execute("mkdir " + subFolderB);
shell.execute("touch " + fileA);
shell.execute("cd " + subFolderA);
shell.execute("touch " + fileB);
shell.execute("cd ..");
shell.execute("cd ..");

Resource<?> dirToMove = shell.getCurrentDirectory().getChildDirectory(testFolder);
assertTrue(dirToMove.exists());
FileResource<?> file1 = (FileResource<?>) dirToMove.getChild(fileA);
assertTrue(file1.exists());
Resource<?> subFolder1 = ((DirectoryResource) dirToMove).getChildDirectory(subFolderA);
FileResource<?> file2 = (FileResource<?>) subFolder1.getChild(fileB);
assertTrue(file2.exists());

assertTrue(((DirectoryResource) subFolder1).isDirectory());
assertTrue(((DirectoryResource) subFolder1).exists());

Resource<?> subFolder2 = ((DirectoryResource) dirToMove).getChildDirectory(subFolderB);
assertTrue(((DirectoryResource) subFolder2).isDirectory());
assertTrue(((DirectoryResource) subFolder2).exists());

shell.execute("mv " + testFolder + " " + relativePath);

Resource<?> targetParent = shell.getCurrentDirectory().getChildDirectory(newFolder);
assertTrue(targetParent.exists());
assertTrue(((DirectoryResource) targetParent).isDirectory());

Resource<?> move = ((DirectoryResource) targetParent).getChildDirectory(nonExisting);
assertTrue(move.exists());
assertTrue(((DirectoryResource) move).isDirectory());

FileResource<?> movedfile1 = (FileResource<?>) move.getChild(fileA);
assertTrue(movedfile1.exists());

DirectoryResource movedSubFolder1 = ((DirectoryResource) move).getChildDirectory(subFolderA);
assertTrue(movedSubFolder1.exists());
FileResource<?> movedfile2 = (FileResource<?>) movedSubFolder1.getChild(fileB);
assertTrue(movedfile2.exists());

DirectoryResource movedSubFolder2 = ((DirectoryResource) move).getChildDirectory(subFolderB);
assertTrue(movedSubFolder2.exists());
assertFalse(dirToMove.exists());
}

}

0 comments on commit a8d6c1b

Please sign in to comment.