Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SHRINKWRAP-381] Move command for Nodes inside of a ShrinkWrap Archive #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 30 additions & 0 deletions api/src/main/java/org/jboss/shrinkwrap/api/Archive.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,36 @@ T add(Archive<?> archive, ArchivePath path, Class<? extends StreamExporter> expo
*/
T merge(Archive<?> source, String path, Filter<ArchivePath> filter) throws IllegalArgumentException;

/**
* Moves the asset under the source path to the target path.
*
* @param source
* The context under which to remove the assets
* @param target
* The context under which to add the moved assets
* @return the resulting archive with the moved assets
* @throws IllegalArgumentException
* If any of the paths is not specified
* @throws IllegalArchivePathException
* If the source path is invalid.
*/
T mv(ArchivePath source, ArchivePath target) throws IllegalArgumentException, IllegalArchivePathException;

/**
* Moves the asset under the source path to the target path.
*
* @param source
* The context under which to remove the assets
* @param target
* The context under which to add the moved assets
* @return the resulting archive with the moved assets
* @throws IllegalArgumentException
* If any of the paths is not specified
* @throws IllegalArchivePathException
* If the source path is invalid.
*/
T mv(String source, String target) throws IllegalArgumentException, IllegalArchivePathException;

/**
* Acts as a shorthand for {@link Archive#toString(Formatter)} where the {@link Formatters#SIMPLE} is leveraged.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jboss.shrinkwrap.api.Configuration;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.IllegalArchivePathException;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.ArchiveAsset;
Expand Down Expand Up @@ -521,6 +522,42 @@ public T merge(Archive<?> source, ArchivePath path, Filter<ArchivePath> filter)
return covariantReturn();
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#mv(org.jboss.shrinkwrap.api.ArchivePath, org.jboss.shrinkwrap.api.ArchivePath)
*/
@Override
public T mv(ArchivePath source, ArchivePath target) throws IllegalArgumentException, IllegalArchivePathException {
Validate.notNull(source, "The source path was not specified");
Validate.notNull(target, "The target path was not specified");

final Node nodeToMove = get(source);
if (null == nodeToMove) {
throw new IllegalArchivePathException(source.get() + " doesn't specify any node in the archive to move");
}
add(nodeToMove.getAsset(), target);
delete(source);

return covariantReturn();
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#mv(java.lang.String, java.lang.String)
*/
@Override
public T mv(String source, String target) throws IllegalArgumentException, IllegalArchivePathException {
Validate.notNullOrEmpty(source, "The source path was not specified");
Validate.notNullOrEmpty(target, "The target path was not specified");

final ArchivePath sourcePath = new BasicPath(source);
final ArchivePath targetPath = new BasicPath(target);

return mv(sourcePath, targetPath);
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.jboss.shrinkwrap.api.ClassLoaderSearchUtilDelegator;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.IllegalArchivePathException;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.ByteArrayAsset;
Expand Down Expand Up @@ -296,6 +297,28 @@ public T merge(final Archive<?> source, final String path) throws IllegalArgumen
return covarientReturn();
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#mv(org.jboss.shrinkwrap.api.ArchivePath, org.jboss.shrinkwrap.api.ArchivePath)
*/
@Override
public T mv(ArchivePath source, ArchivePath target) throws IllegalArgumentException, IllegalArchivePathException {
this.getArchive().mv(source, target);
return covarientReturn();
}

/**
* {@inheritDoc}
*
* @see org.jboss.shrinkwrap.api.Archive#mv(java.lang.String, java.lang.String)
*/
@Override
public T mv(String source, String target) throws IllegalArgumentException, IllegalArchivePathException {
this.getArchive().mv(source, target);
return covarientReturn();
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,26 @@ public void testNestedArchiveGet() throws Exception {
"Nested archive asset should be available through partent archive at " + expectedPath.get(),
nestedNode.getAsset());
}

@Test
public void shouldMoveAsset() {
final Archive<JavaArchive> archive = ShrinkWrap.create(JavaArchive.class, "archive.jar");
final String sourcePath = "path1";
final String targetPath = "path2";
archive.add(EmptyAsset.INSTANCE, sourcePath);
archive.mv(sourcePath, targetPath);

Assert.assertEquals("The archive should have only one asset", 1, numAssets(archive));
Assert.assertNotNull("The asset should be at the target path", archive.get(targetPath));
}

@Test(expected = IllegalArchivePathException.class)
public void shouldNotMoveAssetBecauseOfInexistentPath() {
final Archive<JavaArchive> archive = ShrinkWrap.create(JavaArchive.class, "archive.jar");
final String sourcePath = "non-existent-path1";
final String targetPath = "path2";
archive.mv(sourcePath, targetPath);
}

// -------------------------------------------------------------------------------------||
// Internal Helper Methods ------------------------------------------------------------||
Expand Down