diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-12-31 16:00:54 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-12-31 16:00:54 +0000 |
| commit | d91ddfe924176707062ce447722993d9d9aac415 (patch) | |
| tree | 54c24d64b35ec1fa3df5bb39a7cb84eea927881b /src | |
| parent | 53b659ef61e8a67f5a11ca3dd726a3215fbe673d (diff) | |
Remove equals and hashCode from XML nodes
Diffstat (limited to 'src')
3 files changed, 66 insertions, 91 deletions
diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java index 659be2d..f138917 100644 --- a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java +++ b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java @@ -28,7 +28,15 @@ public class XmlElement implements XmlNode { public String getName() { return name; } - + + public Map<String, String> getAttributes() { + return attributes; + } + + public List<XmlNode> getChildren() { + return children; + } + @Override public <T> T accept(XmlNodeVisitor<T> visitor) { return visitor.visit(this); @@ -43,43 +51,4 @@ public class XmlElement implements XmlNode { return "XmlElement(name=" + name + ", attributes=" + attributes + ", children=" + children + ")"; } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result - + ((attributes == null) ? 0 : attributes.hashCode()); - result = prime * result - + ((children == null) ? 0 : children.hashCode()); - result = prime * result + ((name == null) ? 0 : name.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - XmlElement other = (XmlElement) obj; - if (attributes == null) { - if (other.attributes != null) - return false; - } else if (!attributes.equals(other.attributes)) - return false; - if (children == null) { - if (other.children != null) - return false; - } else if (!children.equals(other.children)) - return false; - if (name == null) { - if (other.name != null) - return false; - } else if (!name.equals(other.name)) - return false; - return true; - } } diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java b/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java index 82e5993..9b349e3 100644 --- a/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java +++ b/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java @@ -20,29 +20,4 @@ public class XmlTextNode implements XmlNode { public String toString() { return "XmlTextNode(value=" + value + ")"; } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((value == null) ? 0 : value.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - XmlTextNode other = (XmlTextNode) obj; - if (value == null) { - if (other.value != null) - return false; - } else if (!value.equals(other.value)) - return false; - return true; - } } diff --git a/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java b/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java index 00f8fb2..b93838e 100644 --- a/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/xml/XmlParserTests.java @@ -1,18 +1,23 @@ package org.zwobble.mammoth.tests.xml; -import java.io.ByteArrayInputStream; - +import com.google.common.collect.ImmutableBiMap; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.hamcrest.Matcher; +import org.hamcrest.collection.IsEmptyIterable; +import org.hamcrest.collection.IsIterableContainingInOrder; import org.junit.Test; import org.zwobble.mammoth.xml.XmlElement; +import org.zwobble.mammoth.xml.XmlNode; import org.zwobble.mammoth.xml.XmlParser; import org.zwobble.mammoth.xml.XmlTextNode; -import com.google.common.collect.ImmutableBiMap; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; +import java.io.ByteArrayInputStream; +import java.util.List; +import java.util.Map; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; public class XmlParserTests { private final XmlParser parser = new XmlParser(ImmutableBiMap.of()); @@ -21,59 +26,56 @@ public class XmlParserTests { public void canParseSelfClosingElement() { assertThat( parser.parseString("<body/>"), - is(new XmlElement("body"))); + isElement("body")); assertThat( parser.parseString("<values/>"), - is(new XmlElement("values"))); + isElement("values")); } @Test public void canParseEmptyElementWithSeparateClosingTag() { assertThat( parser.parseString("<body></body>"), - is(new XmlElement("body"))); + isElement("body")); } @Test public void canParseAttributesOfTag() { assertThat( parser.parseString("<body name='bob'></body>"), - is(new XmlElement("body", ImmutableMap.of("name", "bob")))); + isElement("body", ImmutableMap.of("name", "bob"))); } @Test public void canParseElementWithDescendantElements() { assertThat( parser.parseString("<body><a><b/></a><a/></body>"), - is(new XmlElement("body", ImmutableMap.of(), ImmutableList.of( - new XmlElement("a", ImmutableMap.of(), ImmutableList.of( - new XmlElement("b") - )), - new XmlElement("a") - )))); + isElement("body", ImmutableMap.of(), ImmutableList.of( + isElement("a", ImmutableMap.of(), ImmutableList.of( + isElement("b"))), + isElement("a")))); } - + @Test public void canParseTextNodes() { assertThat( parser.parseString("<body>Hello!</body>"), - is(new XmlElement("body", ImmutableMap.of(), ImmutableList.of( - new XmlTextNode("Hello!") - )))); + isElement("body", ImmutableMap.of(), ImmutableList.of( + isTextNode("Hello!")))); } @Test public void unmappedNamespaceUrisInElementNamesAreIncludedInBracesAsPrefix() { assertThat( parser.parseString("<w:body xmlns:w='word'/>"), - is(new XmlElement("{word}body"))); + isElement("{word}body")); } @Test public void unmappedNamespaceUrisInAttributeNamesAreIncludedInBracesAsPrefix() { assertThat( parser.parseString("<body xmlns:w='word' w:name='bob'></body>"), - is(new XmlElement("body", ImmutableMap.of("{word}name", "bob")))); + isElement("body", ImmutableMap.of("{word}name", "bob"))); } @Test @@ -81,7 +83,7 @@ public class XmlParserTests { XmlParser parser = new XmlParser(ImmutableBiMap.of("x", "word")); assertThat( parser.parseString("<w:body xmlns:w='word'/>"), - is(new XmlElement("x:body"))); + isElement("x:body")); } @Test @@ -89,13 +91,42 @@ public class XmlParserTests { XmlParser parser = new XmlParser(ImmutableBiMap.of("x", "word")); assertThat( parser.parseString("<body xmlns:w='word' w:name='bob'/>"), - is(new XmlElement("body", ImmutableMap.of("x:name", "bob")))); + isElement("body", ImmutableMap.of("x:name", "bob"))); } @Test public void canParseInputStream() { assertThat( parser.parseStream(new ByteArrayInputStream("<body/>".getBytes())), - is(new XmlElement("body"))); + isElement("body")); + } + + private Matcher<XmlElement> isElement(String name) { + return isElement(name, ImmutableMap.of()); + } + + private Matcher<XmlElement> isElement(String name, Map<String, String> attributes) { + return isElement(name, attributes, ImmutableList.of()); + } + + private Matcher<XmlElement> isElement(String name, Map<String, String> attributes, List<Matcher<? extends XmlNode>> children) { + return allOf( + hasProperty("name", equalTo(name)), + hasProperty("attributes", equalTo(attributes)), + hasProperty("children", isNodes(children))); + } + + private Matcher<Iterable<XmlNode>> isNodes(List<Matcher<? extends XmlNode>> children) { + if (children.isEmpty()) { + return (Matcher)new IsEmptyIterable<>(); + } else { + return new IsIterableContainingInOrder<>((List) children); + } + } + + private Matcher<XmlNode> isTextNode(String value) { + return allOf( + instanceOf(XmlTextNode.class), + hasProperty("value", equalTo(value))); } } |
