Skip to content

Commit

Permalink
[SHRINKDESC-59] Bulk test coverage and fix bugs in Node SPI
Browse files Browse the repository at this point in the history
  • Loading branch information
tommysdk authored and Andrew Lee Rubinger committed Jul 21, 2011
1 parent 3933840 commit 22dfbf9
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 8 deletions.
13 changes: 12 additions & 1 deletion spi/src/main/java/org/jboss/shrinkwrap/descriptor/spi/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ public List<Node> get(String name)
return get(Queries.from(name));
}

/**
* Get all children matching the specified query.
*
* @param query The query to use for finding relevant child nodes
* @return All found children, or empty list if none found.
*/
public List<Node> get(Query query)
{
return new GetQuery(query).execute(this);
Expand Down Expand Up @@ -287,6 +293,10 @@ public List<Node> remove(Query query)
}

List<Node> found = get(query);
if(found == null)
{
return Collections.EMPTY_LIST;
}
for (Node child : found)
{
children.remove(child);
Expand Down Expand Up @@ -433,6 +443,7 @@ public String toString()
{
return this.getClass().getSimpleName() + "[" + name + "] " + "children["
+ (children != null ? children.size() : 0) + "] "
+ (attributes != null ? "attributes[" + attributes + "] " : "" + text != null ? "text[" + text + "] " : "");
+ (attributes != null ? "attributes[" + attributes + "] " : "")
+ (text != null ? "text[" + text + "] " : "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package org.jboss.shrinkwrap.descriptor.spi;

import java.util.List;
import java.util.Map;

import org.jboss.shrinkwrap.descriptor.spi.Node;
import org.jboss.shrinkwrap.descriptor.spi.query.NodeQuery;
import org.jboss.shrinkwrap.descriptor.spi.query.Query;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -41,6 +43,20 @@ public class NodeTestCase

private static final String BODY = "test_body";

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfNullNameParamInConstructor() throws Exception
{
Node parent = new Node(ROOT_NAME);
new Node(null, parent);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfSpaceInConstructorNameParam() throws Exception
{
Node parent = new Node(ROOT_NAME);
new Node("a name", parent);
}

@Test
public void shouldBeAbleToGetParentNode() throws Exception
{
Expand All @@ -64,8 +80,8 @@ public void shouldBeAbleToGetOrCreateExistingNode() throws Exception
1, root.children().size());

Assert.assertEquals(
"Verify the previous created node was returned",
child1, child1_ref);
"Verify the previous created node was returned",
child1, child1_ref);
}

@Test
Expand All @@ -80,8 +96,8 @@ public void shouldBeAbleToCreateMultipleEquallyNamedChildren() throws Exception
2, root.children().size());

Assert.assertNotSame(
"Verify the children are not the same object",
child1, child2);
"Verify the children are not the same object",
child1, child2);
}

@Test
Expand Down Expand Up @@ -116,8 +132,8 @@ public void shouldBeAbleToGetASingleNode() throws Exception
Node found = root.getSingle(CHILD_1_NAME);

Assert.assertEquals(
"Verify correct node was found",
child, found);
"Verify correct node was found",
child, found);
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -191,4 +207,231 @@ public void shouldBeAbleToReadAllChildTextBodyValues() throws Exception
Assert.assertTrue(textValues.contains(String.valueOf(i)));
}
}

@Test
public void shouldBeAbleToDetermineTextValue() throws Exception
{
String childName = "testval";
String childText = "textval";

Node root = new Node(ROOT_NAME);
Assert.assertNull(root.textValue(childName));

root.create(childName);
Assert.assertNull(root.textValue(childName));

root.children().get(0).text(childText);
Assert.assertNotNull(root.textValue(childName));
Assert.assertEquals(childText, root.textValue(childName));
}

@Test
public void shouldReturnEmptyListForMissingTextValues() throws Exception
{
Node root = new Node(ROOT_NAME);
root.create("child1");
root.create("child2");
root.create("child3").text(null);
List<String> textValues = root.textValues("textValue");
Assert.assertNotNull(textValues);
Assert.assertTrue(textValues.isEmpty());
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionIfMultipleChildrenWithSameNameOnTextValue() throws Exception
{
String childName = "child";
Node root = new Node(ROOT_NAME);
Assert.assertNull(root.textValue(childName));

root.create(childName);
root.create(childName);
root.children().get(0).text("text");
root.children().get(1).text("text");

root.textValue(childName);
}

@Test
public void shouldFindAllPropertiesInToString() throws Exception
{
Node root = new Node(ROOT_NAME);
Assert.assertTrue(root.toString().contains(root.getClass().getSimpleName()));
Assert.assertTrue(root.toString().contains("children"));
Assert.assertTrue(root.toString().contains("attributes"));
Assert.assertFalse(root.toString().contains("text"));

root.text("arbitrary cdata");
Assert.assertTrue(root.toString().contains("Node"));
Assert.assertTrue(root.toString().contains("children"));
Assert.assertTrue(root.toString().contains("attributes"));
Assert.assertTrue(root.toString().contains("text"));
Assert.assertTrue(root.toString().contains(root.text()));
}

@Test
public void assertToStringFormat() throws Exception {
Node root = new Node(ROOT_NAME);
String r = root.toString();
Assert.assertTrue(r.startsWith(root.getClass().getSimpleName()));
Assert.assertTrue(r.indexOf("text") < r.indexOf("Node"));
Assert.assertTrue(r.indexOf("Node") < r.indexOf("children"));
Assert.assertTrue(r.indexOf("children") < r.indexOf("attributes"));

root.text("arbitrary cdata");
r = root.toString();
Assert.assertTrue(r.indexOf("Node") < r.indexOf("text"));
Assert.assertTrue(r.indexOf("Node") < r.indexOf("children"));
Assert.assertTrue(r.indexOf("children") < r.indexOf("attributes"));
Assert.assertTrue(r.indexOf("attributes") < r.indexOf("text"));

Assert.assertTrue("Unexpected content? " + root.toString(), root.toString().contains("children[0]"));
root.create("testchild1");
root.create("testchild2");
Assert.assertTrue("Unexpected content? " + root.toString(), root.toString().contains("children[2]"));

Assert.assertTrue("Unexpected content? " + root.toString(), root.toString().contains("attributes[{}]"));
root.attribute("name", "value");
Assert.assertTrue("Unexpected content? " + root.toString(), root.toString().contains("attributes[{name=value}]"));

Assert.assertTrue("Unexpected content? " + root.toString(), root.toString().contains("text[arbitrary cdata]"));
}

@Test(expected = UnsupportedOperationException.class)
public void shouldHaveImmutableAttributeMap() throws Exception
{
Node root = new Node(ROOT_NAME);
root.attribute("attribute1", "value");
root.attribute("attribute2", "value");
Map<String, String> attributes = root.getAttributes();
attributes.clear();
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForNullStringParameter() throws Exception
{
Node root = new Node(ROOT_NAME);
root.remove((String) null);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForEmptyStringParameter() throws Exception
{
Node root = new Node(ROOT_NAME);
root.remove("");
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionForNullQueryParameter() throws Exception
{
Node root = new Node(ROOT_NAME);
root.remove((Query) null);
}

@Test
public void shouldRemoveNodeByString() throws Exception
{
String name = "child";
Node root = new Node(ROOT_NAME);

Assert.assertTrue(root.children().isEmpty());
root.create(name);
Assert.assertFalse(root.children().isEmpty());
Assert.assertEquals(1, root.children().size());

root.remove(name);
Assert.assertTrue(root.children().isEmpty());
}

@Test
public void shouldRemoveSingleChildNodeWithNodeParam()
{
Node root = new Node(ROOT_NAME);
Node child = root.create("child_node");
Assert.assertTrue(root.removeSingle(child));
}

@Test
public void shouldNotRemoveSingleChildNodeWithNodeParam()
{
Node root = new Node(ROOT_NAME);
Node child = new Node("another_node");
Assert.assertFalse(root.removeSingle((Node) null));
Assert.assertFalse(root.removeSingle(child));
root.create("a_proper_child_node");
Assert.assertFalse(root.removeSingle(child));
}

@Test
public void shouldRemoveSingleChildNodeWithStringParam()
{
Node root = new Node(ROOT_NAME);
String childNodeName = "another_node";
Assert.assertNull(root.removeSingle(childNodeName));
root.create(childNodeName);
Node removedChild = root.removeSingle(childNodeName);
Assert.assertNotNull(removedChild);
Assert.assertEquals(childNodeName, removedChild.name());
}

@Test
public void shouldNotRemoveSingleChildNodeWithStringParam()
{
Node root = new Node(ROOT_NAME);
Assert.assertNull(root.removeSingle("node_that_doesn't_exist"));
root.create("a_node");
Assert.assertNull(root.removeSingle("nonexisting_node"));
}

@Test
public void shouldRemoveWithQueryParam() throws Exception
{
Node root = new Node(ROOT_NAME);
Node child = root.create("child_node");

Assert.assertFalse(root.children().isEmpty());
Assert.assertEquals(child, root.children().get(0));

NodeQuery nodeQuery = new NodeQuery(child.name());
Query query = new Query(false);
query.addDefinition(nodeQuery);
List<Node> removedNodes = root.remove(query);
Assert.assertNotNull(removedNodes);
Assert.assertFalse(removedNodes.isEmpty());
Assert.assertEquals(1, removedNodes.size());
}

@Test
public void shouldRemoveWithAbsoluteQueryParam() throws Exception
{
Node root = new Node(ROOT_NAME);
Node child = root.create("child_node");

Assert.assertFalse(root.children().isEmpty());
Assert.assertEquals(child, root.children().get(0));

NodeQuery nodeQuery = new NodeQuery(child.name());
Query query = new Query(true);
query.addDefinition(nodeQuery);
List<Node> removedNodes = root.remove(query);
Assert.assertNotNull(removedNodes);
Assert.assertTrue(removedNodes.isEmpty());
}

@Test
public void shouldNotRemoveWithQueryParam() throws Exception
{
Node root = new Node(ROOT_NAME);
Node child = root.create("child_node");

Assert.assertFalse(root.children().isEmpty());
Assert.assertEquals(child, root.children().get(0));

NodeQuery nodeQuery = new NodeQuery("some_other_name");
Query query = new Query(false);
query.addDefinition(nodeQuery);
List<Node> removedNodes = root.remove(query);
Assert.assertTrue(removedNodes.isEmpty());
}

}

0 comments on commit 22dfbf9

Please sign in to comment.