Skip to content

Commit

Permalink
[SHRINKDESC-31] Fix the failing test case by adding comment structure…
Browse files Browse the repository at this point in the history
… to Node
  • Loading branch information
Andrew Lee Rubinger committed Jul 21, 2011
1 parent e79b4a8 commit 6b12649
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
34 changes: 34 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 @@ -73,6 +73,11 @@ public class Node
* CDATA
*/
private String text;

/**
* Denotes whether this is a comment (ie. <!-- X -->)
*/
private boolean comment;

// -------------------------------------------------------------------------------------||
// Constructor -------------------------------------------------------------------------||
Expand Down Expand Up @@ -173,6 +178,35 @@ public Map<String, String> getAttributes()
{
return Collections.unmodifiableMap(attributes);
}

/**
* Returns whether or not this {@link Node}
* represents a comment
* @return
*/
public boolean isComment()
{
return comment;
}

/**
* Marks this {@link Node} as a comment
* @param comment Whether or not this is a comment
* @return
* @throws IllegalArgumentException If this node has children
*/
public void setComment(final boolean comment) throws IllegalArgumentException
{
// Cannot have children
if (this.children.size() > 0)
{
throw new IllegalArgumentException("Cannot mark a " + Node.class.getSimpleName()
+ " with children as a comment");
}

// Set
this.comment = comment;
}

// -------------------------------------------------------------------------------------||
// Node creation / retrieval ----------------------------------------------------------||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@ private void writeRecursive(org.w3c.dom.Node target, Node source)
owned = (Document)target;
}
org.w3c.dom.Node targetChild = null;
if(source.text() != null)
// Comment node
if(source.isComment())
{
targetChild = owned.createComment(source.text());
}
else if(source.text() != null)
{
targetChild = owned.createElement(source.name());
targetChild.appendChild(owned.createTextNode(source.text()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*/
public class XmlDomImporter<T extends Descriptor> extends DescriptorImporterBase<T>
{

public XmlDomImporter(final Class<T> endUserViewImplType, String descriptorName)
{
super(endUserViewImplType, descriptorName);
Expand Down Expand Up @@ -77,12 +78,21 @@ private void readRecursive(Node target, org.w3c.dom.Node source)
{
for(int i = 0; i < sourceChildren.getLength(); i++)
{
org.w3c.dom.Node child = sourceChildren.item(i);
final org.w3c.dom.Node child = sourceChildren.item(i);
if(child.getNodeType() != org.w3c.dom.Node.TEXT_NODE)
{
Node newTarget = target.create(child.getNodeName());
// Create our representation of the Node
final Node newTarget = target.create(child.getNodeName());

if(onlyTextChildren(child))
{
// See if we're dealing with a comment and mark specifically
if (child.getNodeType() == org.w3c.dom.Node.COMMENT_NODE)
{
newTarget.setComment(true);
}

// Set text
newTarget.text(child.getTextContent());
readAttributes(newTarget, child);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,25 @@ public void shouldNotRemoveWithQueryParam() throws Exception
List<Node> removedNodes = root.remove(query);
Assert.assertTrue(removedNodes.isEmpty());
}

@Test
public void shouldNotBeCommentByDefault(){
final Node node = new Node(ROOT_NAME);
Assert.assertEquals("A Node should not be a comment by default",false, node.isComment());
}

@Test
public void shouldBeAbleToMarkAsComment(){
final Node node = new Node(ROOT_NAME);
node.setComment(true);
Assert.assertEquals("A Node set as comment should report as comment",true, node.isComment());
}

@Test(expected=IllegalArgumentException.class)
public void shouldNotBeAbleToSetNodeWitHChildrenAsComment(){
final Node node = new Node(ROOT_NAME);
node.create(CHILD_1_NAME);
node.setComment(true);
}

}

0 comments on commit 6b12649

Please sign in to comment.