Skip to content

Commit

Permalink
[SHRINKDESC-90] Additional tests for Descriptors.
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmajsak authored and ALRubinger committed Sep 13, 2011
1 parent 0306698 commit 4fa7fa1
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public abstract class DescriptorImporterBase<T extends Descriptor> implements De
* Creates a new instance representing the specified backing model
* type
*
* @param The type of the backing object model for the descriptor
* @param endUserViewImplType The type of the backing object model for the descriptor
* @param descriptorName Thename of the descriptor
*
* @throws IllegalArgumentException If the model type is not specified
* @throws IllegalArgumentException If the descriptorName not specified
*/
Expand All @@ -70,7 +72,7 @@ public DescriptorImporterBase(final Class<T> endUserViewImplType, final String d
{
throw new IllegalArgumentException("End user view impl type must be specified");
}
if (descriptorName == null)
if (descriptorName == null || descriptorName.trim().length() == 0)
{
throw new IllegalArgumentException("Descriptor name must be specified");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.shrinkwrap.descriptor.spi.node;

public class DummyNodeDescriptor extends NodeDescriptorImplBase
{

private Node node;

public DummyNodeDescriptor(String descriptorName)
{
this(descriptorName, new Node("dummy"));
}

public DummyNodeDescriptor(String descriptorName, Node node)
{
super(descriptorName);
this.node = node;
}

@Override
public Node getRootNode()
{
return node;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
*/
package org.jboss.shrinkwrap.descriptor.spi.node.dom;

import static org.junit.Assert.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.jboss.shrinkwrap.descriptor.api.DescriptorExportException;
import org.jboss.shrinkwrap.descriptor.spi.node.DummyNodeDescriptor;
import org.jboss.shrinkwrap.descriptor.spi.node.Node;
import org.jboss.shrinkwrap.descriptor.test.util.TestTreeBuilder;
import org.jboss.shrinkwrap.descriptor.test.util.XmlAssert;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -38,7 +36,8 @@
public class XmlDomDescriptorExporterTestCase
{
private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
public static final String XML_WITH_COMMENT = "" +

private static final String XML_WITH_COMMENT = "" +
XML_HEADER +
"<root>" +
" <!-- comment -->" +
Expand Down Expand Up @@ -73,10 +72,9 @@ public void shouldBeAbleExportXMLWithComments() throws Exception
{
// given
Node root = load(XML_WITH_COMMENT);
XmlDomDescriptorExporterImpl xmlDomDescriptorExporter = new XmlDomDescriptorExporterImpl();

// when
String exportedXml = exportAsString(root, xmlDomDescriptorExporter);
String exportedXml = exportAsString(root);

// then
XmlAssert.assertIdentical(XML_WITH_COMMENT, exportedXml);
Expand All @@ -89,7 +87,7 @@ public void shouldBeAbleToExportSingleNodeTree() throws Exception
Node root = new Node("root");

// when
String exportedXml = exportAsString(root, new XmlDomDescriptorExporterImpl());
String exportedXml = exportAsString(root);

// then
XmlAssert.assertIdentical(XML_HEADER + "<root></root>", exportedXml);
Expand All @@ -103,7 +101,7 @@ public void shouldBeAbleToExportSingleNodeTreeWithAttributes() throws Exception
root.attribute("id", "1").attribute("name", "root");

// when
String exportedXml = exportAsString(root, new XmlDomDescriptorExporterImpl());
String exportedXml = exportAsString(root);

// then
XmlAssert.assertIdentical(XML_HEADER + "<root id=\"1\" name=\"root\"></root>", exportedXml);
Expand All @@ -120,21 +118,37 @@ public void shouldBeAbleToExportSingleNodeTreeWithAttributesAndText() throws Exc
.text("doovde");

// when
String exportedXml = exportAsString(root, new XmlDomDescriptorExporterImpl());
String exportedXml = exportAsString(root);

// then
XmlAssert.assertIdentical(XML_HEADER + "<root id=\"1\" name=\"root\">doovde</root>", exportedXml);

}

@Test
public void shouldCreateSingleNodeWithGivenNameWhenEmptyStringPassedToImport() throws Exception
{
// given
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, "test-descriptor");
Node expectedRoot = new Node("dummy");

// when
Node importedRoot = xmlDomNodeDescriptorImporter.from("").getRootNode();

// then
Assert.assertEquals(expectedRoot.getName(), importedRoot.getName());
Assert.assertTrue(importedRoot.getChildren().isEmpty());
}


@Test(expected = DescriptorExportException.class)
public void shouldThrowExceptionWhenTreeIsNull() throws Exception
{
// given
Node root = null;

// when
String exportedXml = exportAsString(root, new XmlDomDescriptorExporterImpl());
String exportedXml = exportAsString(root);

// then
// exception should be thrown
Expand All @@ -147,10 +161,10 @@ private Node load(String xml)
return XmlDomNodeImporter.INSTANCE.importAsNode(new ByteArrayInputStream(xml.getBytes()), true);
}

private String exportAsString(Node root, XmlDomDescriptorExporterImpl xmlDomExporter)
private String exportAsString(Node root)
{
final ByteArrayOutputStream output = new ByteArrayOutputStream();
xmlDomExporter.to(root, output);
new XmlDomDescriptorExporterImpl().to(root, output);
return new String(output.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package org.jboss.shrinkwrap.descriptor.spi.node.dom;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;

import org.jboss.shrinkwrap.descriptor.spi.node.DummyNodeDescriptor;
import org.jboss.shrinkwrap.descriptor.spi.node.Node;
import org.jboss.shrinkwrap.descriptor.test.util.NodeAssert;
import org.jboss.shrinkwrap.descriptor.spi.node.NodeDescriptor;
import org.jboss.shrinkwrap.descriptor.test.util.XmlAssert;
import org.junit.Assert;
import org.junit.Test;

Expand All @@ -33,12 +36,17 @@
*/
public class XmlDomNodeImporterTestCase
{

private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";

public static final String XML_WITH_COMMENT = "" +
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
XML_HEADER +
"<root>" +
" <!-- comment -->" +
" <child>test</child>" +
"</root>";
"</root>";

private static final String SINGLE_NODE = XML_HEADER + "<root></root>";

/*
* SHRINKDESC-31 - Comments in XML input Cause Export error
Expand All @@ -60,22 +68,108 @@ public void shouldBeAbleToImportXMLWithComments() throws Exception
public void shouldBeAbleToImportSingleNodeXmlWithAttributesAndText() throws Exception
{
// given
XmlDomNodeDescriptorImporterImpl<NodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<NodeDescriptor>(NodeDescriptor.class, "test-descriptor");
Node expectedRoot = new Node("root");
expectedRoot.attribute("id", "1")
.attribute("name", "root")
.text("doovde");

// when
Node root = XmlDomNodeImporter.INSTANCE.importAsNode(new FileInputStream("src/test/resources/single.xml"), true);;
Node root = xmlDomNodeDescriptorImporter.getNodeImporter().importAsNode(new FileInputStream("src/test/resources/single.xml"), true);

// then
Assert.assertEquals(expectedRoot.toString(true), root.toString(true));
}

@Test
public void shouldBeAbleToImportFromXmlString() throws Exception
{
// given
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, "test-descriptor");

// when
DummyNodeDescriptor nodeDescriptor = xmlDomNodeDescriptorImporter.from(SINGLE_NODE);

// then
XmlAssert.assertIdentical(SINGLE_NODE, nodeDescriptor.exportAsString());
}

@Test
public void shouldCreateSingleNodeWithGivenNameWhenEmptyStringPassedToImport() throws Exception
{
// given
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, "test-descriptor");
Node expectedRoot = new Node("dummy");

// when
Node importedRoot = xmlDomNodeDescriptorImporter.from("").getRootNode();

// then
Assert.assertEquals(expectedRoot.getName(), importedRoot.getName());
Assert.assertTrue(importedRoot.getChildren().isEmpty());
}

@Test
public void shouldBeAbleToImportXmlFileWithSingleNodeDescriptorWithAttributesAndText() throws Exception
{
// given
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, "test-descriptor");

Node expectedRoot = new Node("root");
expectedRoot.attribute("id", "1")
.attribute("name", "root")
.text("doovde");

// when
Node root = xmlDomNodeDescriptorImporter.from(new File("src/test/resources/single.xml")).getRootNode();

// then
Assert.assertEquals(expectedRoot.toString(true), root.toString(true));
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenTryingToImportFromNonExistingFile() throws Exception
{
// given
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, "test-descriptor");

// when
Node root = xmlDomNodeDescriptorImporter.from(new File("not-existing-file.xml")).getRootNode();

// then
// exception should be thrown
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenEndUserViewNotSpecified() throws Exception
{
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(null, "test-descriptor");
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenDescriptorNameNotSpecified() throws Exception
{
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, null);
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenDescriptorsNameIsEmpty() throws Exception
{
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, "");
}

@Test(expected = IllegalArgumentException.class)
public void shouldThrowExceptionWhenDescriptorsNameIsBlank() throws Exception
{
XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor> xmlDomNodeDescriptorImporter = new XmlDomNodeDescriptorImporterImpl<DummyNodeDescriptor>(DummyNodeDescriptor.class, " ");
}

// Utility methods

private Node load(String xml)
{
return XmlDomNodeImporter.INSTANCE.importAsNode(new ByteArrayInputStream(xml.getBytes()), true);
}


}

0 comments on commit 4fa7fa1

Please sign in to comment.