Skip to content

Commit

Permalink
[SHRINKWRAP-348] Fixed concerns regarding archive directory delete op…
Browse files Browse the repository at this point in the history
…erations
  • Loading branch information
tommysdk authored and ALRubinger committed Mar 17, 2012
1 parent ec8f953 commit 2e7e2a2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,42 @@ public boolean contains(final String path) throws IllegalArgumentException {
@Override
public Node delete(ArchivePath path) {
Validate.notNull(path, "No path was specified");
ArchivePath safePath = path;

final Node node = content.get(path);
NodeImpl node = content.get(safePath);
if (node == null) {
return null;
if (path.get().endsWith("/")) {
safePath = ArchivePaths.create(path.get().substring(0, path.get().length() - 1));
node = content.get(safePath);
}
if (node == null) {
return null;
}
}

return removeNodeRecursively(node, safePath);
}

/**
* Removes the specified node and its associated children from the contents
* of this archive.
*
* @param node the node to remove recursively
* @param path the path denoting the specified node
* @return the removed node itself
*/
private Node removeNodeRecursively(final NodeImpl node, final ArchivePath path) {
final NodeImpl parentNode = content.get(path.getParent());
if (parentNode != null) {
parentNode.removeChild(node);
}

// Recursively delete children if present
if (node.getChildren() != null) {
for (Node child : node.getChildren()) {
node.removeChild(child);
content.remove(child.getPath());
}
}
return content.remove(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,42 @@ public void addDuplicateResourceThrowsIllegalOverwriteException() {
// Fail us
TestCase.fail("Expected " + IllegalOverwriteException.class.getName() + " not received");
}

/**`
* Reproduces a bug within Archive.contains, discovered in SHRINKWRAP-348
*/
@Test
@ArchiveType(LibraryContainer.class)
public void containsShouldReturnFalseWhenParentNodeHasBeenDeleted() {
Archive<?> archive = createNewArchive();
final String archivePath = "WEB-INF/classes/org/drools/guvnor/gwtutil/";
final String file = archivePath + "file";

archive.add(EmptyAsset.INSTANCE, ArchivePaths.create(file));
Assert.assertTrue(archive.contains(file));

archive.delete(ArchivePaths.create("WEB-INF/classes/org/drools/guvnor/gwtutil"));
Assert.assertFalse(archive.contains(ArchivePaths.create(archivePath)));
Assert.assertFalse(archive.contains(ArchivePaths.create(file)));
}

/**
* Reproduces the behaviour described in SHRINKWRAP-348
*/
@Test
@ArchiveType(LibraryContainer.class)
public void shouldDeleteArchivePathWithTrailingSlash() {
Archive<?> archive = createNewArchive();
final String archivePath = "WEB-INF/classes/org/drools/guvnor/gwtutil/";
final String file = archivePath + "file";

archive.add(EmptyAsset.INSTANCE, ArchivePaths.create(file));
Assert.assertTrue(archive.contains(file));

archive.delete(ArchivePaths.create(archivePath));
Assert.assertFalse(archive.contains(ArchivePaths.create(archivePath)));
Assert.assertFalse(archive.contains(ArchivePaths.create(file)));
}

private void assertNotContainsClass(ArchivePath notExpectedPath) {
Assert.assertFalse("Located unexpected class at " + notExpectedPath.get(),
Expand Down

0 comments on commit 2e7e2a2

Please sign in to comment.