Skip to content

Commit

Permalink
[SHRINKDESC-60] added removeAttribute method to Node, including testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
kenglxn authored and Andrew Lee Rubinger committed Jul 28, 2011
1 parent 79f4edb commit 032aab2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions spi/src/main/java/org/jboss/shrinkwrap/descriptor/spi/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,23 @@ public String getAttribute(final String name)
return this.attributes.get(name);
}

/**
* Remove a named attribute.<br/>
*
* @param name The attribute name
* @return The attribute value that was removed, or null if the attribute with the given name was not found.
* @throws IllegalArgumentException if the attribute with the given name does not exist on the node
*/
public String removeAttribute(final String name) throws IllegalArgumentException
{
String remove = this.attributes.remove(name);
if (remove == null)
{
throw new IllegalArgumentException("attribute with name '" + name + "' does not exist");
}
return remove;
}

/**
* Get all defined attributes for this Node in an
* immutable view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,31 @@ public void shouldBeAbleToReportAsRoot()
Assert.assertFalse("Child should not report as root", child.isRoot());
}

@Test
public void shouldRemoveAttributeByName() throws Exception
{
Node root = new Node(ROOT_NAME).attribute(ATTR_NAME, ATTR_VALUE);
Assert.assertEquals("attribute should exist", root.getAttribute(ATTR_NAME), ATTR_VALUE);

Assert.assertEquals("attribute value should be returned on removal", root.removeAttribute(ATTR_NAME), ATTR_VALUE);
Assert.assertNull("attribute should no longer exist", root.getAttribute(ATTR_NAME));
}

@Test
public void shouldThrowExceptionWhenAttemptingToRemoveAttributeThatDoesNotExist() throws Exception
{
String bogusAttribute = "SOME_NONEXISTANT_ATTR";

Node root = new Node(ROOT_NAME).attribute(ATTR_NAME, ATTR_VALUE);
Assert.assertEquals("attribute should exist", root.getAttribute(ATTR_NAME), ATTR_VALUE);

try
{
root.removeAttribute(bogusAttribute);
Assert.fail("should have thrown " + IllegalArgumentException.class.getSimpleName());
} catch (IllegalArgumentException e)
{
Assert.assertTrue("message should contain the attribute name", e.getMessage().contains(bogusAttribute));
}
}
}

0 comments on commit 032aab2

Please sign in to comment.