Skip to content

Commit

Permalink
Merge pull request forge#197 from tremes/copy-plugin
Browse files Browse the repository at this point in the history
CopyPlugin update
  • Loading branch information
lincolnthree committed Aug 14, 2012
2 parents 540041a + e039962 commit c76edff
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 14 deletions.
Original file line number Diff line number Diff line change
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 @@ -38,6 +38,8 @@
public class CopyPlugin implements Plugin
{

@Inject @Current Resource<?> directory;

private final ResourceFactory resourceFactory;

@Inject
Expand All @@ -47,18 +49,17 @@ public CopyPlugin(final ResourceFactory resourceFactory)
}

@DefaultCommand
public void rename(
public void copy(
@Option(description = "source", required = true) final Resource<?> source,
@Option(description = "target", required = true) final String target)
{

if (isDirectory(source))
{
Resource<?> directory = source.getParent();
copyRecursively(source, directory, target);
}
else if (isFile(source))
{
Resource<?> directory = source.isFlagSet(ResourceFlag.Leaf) ? source.getParent() : source;
copy(source, directory, target);
}
else
Expand Down Expand Up @@ -108,36 +109,27 @@ private void copyRecursively(final Resource<?> source, Resource<?> directory, fi

if (!targetResource.exists())
{

newTargetDir = ((DirectoryResource) source.getParent()).getOrCreateChildDirectory(targetResource
newTargetDir = ((DirectoryResource) targetResource.getParent()).getOrCreateChildDirectory(targetResource
.getName());

}
else
{

newTargetDir = ((DirectoryResource) targetResource).getOrCreateChildDirectory(source.getName());

}

}
else if (isFile(source))
{

Resource<?> child = targetResource.getChild(source.getName());

if (child == null)
{

((DirectoryResource) targetResource).getOrCreateChildDirectory(source.getName()).setContents(
source.getResourceInputStream());

}
else
{

((FileResource<?>) child).setContents(source.getResourceInputStream());

}
newTargetDir = (DirectoryResource) targetResource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,53 @@ public void testCopyFileToNewFile() throws Exception
assertTrue(copy.exists());

}

@Test
public void testCopyFileWithRelativePathToNewFile() throws Exception
{

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

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

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

shell.execute("cp " + relativePath + " " + nonExisting);
Resource<?> copy = shell.getCurrentDirectory().getChild(nonExisting);
assertTrue(copy.exists());

}

@Test
public void testCopyFileWithRelativePathToExistingFolder() throws Exception
{

String testFolder = "testFolder";
String targetFolder = "targetFolder";
String file = "copyFile";

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

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

shell.execute("cp " + relativePath + " " + targetFolder);
Resource<?> copy = shell.getCurrentDirectory().getChild(targetFolder).getChild(file);
assertTrue(copy.exists());

}

@Test
public void testCopyFileWithRewrite() throws Exception
Expand Down Expand Up @@ -265,6 +312,71 @@ public void testCopyFolderToExistingFolder() throws Exception
DirectoryResource copySubFolder2 = ((DirectoryResource) copy).getChildDirectory(subFolderB);
assertTrue(copySubFolder2.exists());
}

@Test
public void testCopyFolderToNonExistingFolder() 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<?> dirToCopy = shell.getCurrentDirectory().getChildDirectory(testFolder);
assertTrue(dirToCopy.exists());
FileResource<?> file1 = (FileResource<?>) dirToCopy.getChild(fileA);
assertTrue(file1.exists());
Resource<?> subFolder1 = ((DirectoryResource) dirToCopy).getChildDirectory(subFolderA);
FileResource<?> file2 = (FileResource<?>) subFolder1.getChild(fileB);
assertTrue(file2.exists());

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

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

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

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

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

FileResource<?> copyfile1 = (FileResource<?>) copy.getChild(fileA);
assertTrue(copyfile1.exists());
assertTrue(compareFiles(copyfile1.getUnderlyingResourceObject(), file1.getUnderlyingResourceObject()));

DirectoryResource copySubFolder1 = ((DirectoryResource) copy).getChildDirectory(subFolderA);
assertTrue(copySubFolder1.exists());
FileResource<?> copyfile2 = (FileResource<?>) copySubFolder1.getChild(fileB);
assertTrue(copyfile2.exists());
assertTrue(compareFiles(copyfile2.getUnderlyingResourceObject(), file2.getUnderlyingResourceObject()));

DirectoryResource copySubFolder2 = ((DirectoryResource) copy).getChildDirectory(subFolderB);
assertTrue(copySubFolder2.exists());
}

private boolean compareFiles(File f1, File f2)
{
Expand Down

0 comments on commit c76edff

Please sign in to comment.