@Test(expected = IllegalArgumentException.class)
  public void cannotRemoveANullChildElement() {
    SpinXmlElement child = XML("<child/>");
    element.append(child);

    element.remove(child, null);
  }
 @Test
 public void canAppendChildElementAfterExistingElement() {
   SpinXmlElement child = XML("<child/>");
   SpinXmlElement date = element.childElement("date");
   element.appendAfter(child, date);
   SpinXmlElement insertedElement = element.childElements().get(1);
   assertThat(insertedElement.name()).isEqualTo("child");
 }
  @Test(expected = SpinXmlElementException.class)
  public void cannotRemoveNonChildElement() {
    SpinXmlElement child1 = XML("<child/>");
    SpinXmlElement child2 = XML("<child/>");

    element.append(child1);

    element.remove(child1, child2);
  }
 @Test
 public void canReplaceRootElement() {
   SpinXmlElement root = XML("<root/>");
   assertThat(element.name()).isEqualTo("customers");
   assertThat(element.childElements()).isNotEmpty();
   element = element.replace(root);
   assertThat(element.name()).isEqualTo("root");
   assertThat(element.childElements()).isEmpty();
 }
  @Test
  public void canAppendChildElement() {
    SpinXmlElement child = XML("<child/>");
    element = element.append(child);

    child.attr("id", "child");
    child = element.childElement(null, "child");

    assertThat(child).isNotNull();
    assertThat(child.attr("id").value()).isEqualTo("child");
  }
  @Test
  public void canAppendChildElementWithNamespace() {
    SpinXmlElement child = XML("<child xmlns=\"" + EXAMPLE_NAMESPACE + "\"/>");
    element = element.append(child);

    child.attr("id", "child");
    child = element.childElement(EXAMPLE_NAMESPACE, "child");

    assertThat(child).isNotNull();
    assertThat(child.attr("id").value()).isEqualTo("child");
  }
  @Test
  public void canAppendChildElementCollection() {
    Collection<SpinXmlElement> childElements = new ArrayList<SpinXmlElement>();
    childElements.add(XML("<child/>"));
    childElements.add(XML("<child/>"));
    childElements.add(XML("<child/>"));

    element = element.append(childElements);

    SpinList<SpinXmlElement> childs = element.childElements(null, "child");
    assertThat(childs).hasSize(3);
  }
  @Test
  public void canReplaceAElement() {
    SpinXmlElement child = XML("<child/>");
    SpinXmlElement date = element.childElement("date");
    assertThat(date).isNotNull();

    date.replace(child);

    assertThat(element.childElement(null, "child")).isNotNull();
    try {
      element.childElement("date");
    } catch (Exception e) {
      assertThat(e).isInstanceOf(SpinXmlElementException.class);
    }
  }
  @Test
  public void canRemoveAChildElement() {
    SpinXmlElement child = XML("<child/>");
    element.append(child);

    assertThat(element.childElement(null, "child")).isNotNull();

    element.remove(child);

    try {
      assertThat(element.childElement(null, "child"));
      fail("Child element should be removed");
    } catch (Exception e) {
      assertThat(e).isInstanceOf(SpinXmlElementException.class);
    }
  }
 @Test
 public void canWriteToWriter() {
   StringWriter writer = new StringWriter();
   element.writeToWriter(writer);
   String value = writer.toString();
   assertThat(value).isXmlEqualTo(EXAMPLE_XML);
 }
 @Test
 public void canGetAllAttributes() {
   SpinList<SpinXmlAttribute> attributes = element.attrs();
   assertThat(attributes).hasSize(4);
   for (SpinXmlAttribute attribute : attributes) {
     assertThat(attribute.name()).isIn("order", "dueUntil", "xmlns", "ex");
   }
 }
 @Test
 public void canGetAllAttributesByNullNamespace() {
   SpinList<SpinXmlAttribute> attributes = element.attrs(null);
   for (SpinXmlAttribute attribute : attributes) {
     assertThat(attribute.name()).isIn("order", "dueUntil");
     assertThat(attribute.value()).isIn("order1", "20150112");
     assertThat(attribute.namespace()).isNull();
   }
 }
 @Test
 public void canGetAllAttributesByNamespace() {
   SpinList<SpinXmlAttribute> attributes = element.attrs(EXAMPLE_NAMESPACE);
   for (SpinXmlAttribute attribute : attributes) {
     assertThat(attribute.name()).isIn("order", "dueUntil");
     assertThat(attribute.value()).isIn("order1", "20150112");
     assertThat(attribute.namespace()).isEqualTo(EXAMPLE_NAMESPACE);
   }
 }
 @Test
 public void canWriteAttributeByNamespaceAndNonExistingName() {
   String newValue =
       element
           .attrNs(EXAMPLE_NAMESPACE, NON_EXISTING, "newValue")
           .attrNs(EXAMPLE_NAMESPACE, NON_EXISTING)
           .value();
   assertThat(newValue).isEqualTo("newValue");
 }
  @Test
  public void canRemoveChildElementCollection() {
    SpinXmlElement child1 = XML("<child/>");
    SpinXmlElement child2 = XML("<child/>");
    SpinXmlElement child3 = XML("<child/>");
    element.append(child1, child2, child3);

    assertThat(element.childElements(null, "child")).hasSize(3);

    element.remove(element.childElements(null, "child"));

    try {
      assertThat(element.childElements(null, "child"));
      fail("Child element should be removed");
    } catch (Exception e) {
      assertThat(e).isInstanceOf(SpinXmlElementException.class);
    }
  }
 @Test
 public void canWriteAttributeByNamespaceAndName() {
   String newValue =
       element
           .attrNs(EXAMPLE_NAMESPACE, "order", "order2")
           .attrNs(EXAMPLE_NAMESPACE, "order")
           .value();
   assertThat(newValue).isEqualTo("order2");
 }
  @Test
  public void canAppendMultipleChildElements() {
    SpinXmlElement child1 = XML("<child/>");
    SpinXmlElement child2 = XML("<child/>");
    SpinXmlElement child3 = XML("<child/>");

    element = element.append(child1, child2, child3);

    child1.attr("id", "child");
    child2.attr("id", "child");
    child3.attr("id", "child");

    SpinList<SpinXmlElement> childs = element.childElements(null, "child");
    assertThat(childs).hasSize(3);

    for (SpinXmlElement childElement : childs) {
      assertThat(childElement).isNotNull();
      assertThat(childElement.attr("id").value()).isEqualTo("child");
    }
  }
 @Test
 public void canAppendChildElementAfterLastChildElement() {
   SpinXmlElement child = XML("<child/>");
   int childCount = element.childElements().size();
   SpinXmlElement lastChildElement = element.childElements().get(childCount - 1);
   element.appendAfter(child, lastChildElement);
   SpinXmlElement insertedElement = element.childElements().get(childCount);
   assertThat(insertedElement.name()).isEqualTo("child");
 }
 @Test
 public void canCheckAttributeByNonExistingNamespaceAndName() {
   boolean hasAttribute = element.hasAttrNs(NON_EXISTING, "order");
   assertThat(hasAttribute).isFalse();
 }
 @Test(expected = IllegalArgumentException.class)
 public void canCheckAttributeByNamespaceAndNullName() {
   element.hasAttrNs(EXAMPLE_NAMESPACE, null);
 }
 @Test
 public void canCheckAttributeByNamespaceAndNonExistingName() {
   boolean hasAttribute = element.hasAttrNs(EXAMPLE_NAMESPACE, NON_EXISTING);
   assertThat(hasAttribute).isFalse();
 }
 @Test
 public void canWriteToString() {
   assertThat(element.toString()).isXmlEqualTo(EXAMPLE_XML);
 }
 @Test(expected = IllegalArgumentException.class)
 public void cannotReplaceByNullElement() {
   element.replace(null);
 }
 @Test
 public void canCheckAttributeByNullNamespaceAndName() {
   boolean hasAttribute = element.hasAttrNs(null, "order");
   assertThat(hasAttribute).isTrue();
 }
 @Test(expected = SpinXmlElementException.class)
 public void cannotReplaceANonChildElement() {
   SpinXmlElement child = XML("<child/>");
   SpinXmlElement nonChild = XML("<child/>");
   element.replaceChild(nonChild, child);
 }
 @Test
 public void canCheckAttributeByNamespaceAndName() {
   boolean hasAttribute = element.hasAttrNs(EXAMPLE_NAMESPACE, "order");
   assertThat(hasAttribute).isFalse();
 }
 @Test(expected = IllegalArgumentException.class)
 public void cannotReplaceByANullChildElement() {
   SpinXmlElement date = element.childElement("date");
   element.replaceChild(date, null);
 }
 @Test(expected = IllegalArgumentException.class)
 public void cannotReplaceANullChildElement() {
   SpinXmlElement child = XML("<child/>");
   element.replaceChild(null, child);
 }
 @Test
 public void canReadAttributeByName() {
   SpinXmlAttribute attribute = element.attr("order");
   String value = attribute.value();
   assertThat(value).isEqualTo("order1");
 }
 @Test(expected = IllegalArgumentException.class)
 public void cannotGetAllChildElementsByNullNamespaceAndNullName() {
   element.childElements(null, null);
 }