@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 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 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 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 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 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(expected = IllegalArgumentException.class) public void cannotGetAllChildElementsByNullNamespaceAndNullName() { element.childElements(null, null); }
@Test(expected = SpinXmlElementException.class) public void cannotGetAllChildElementsByNonExistingNamespaceAndNonExistingName() { element.childElements(NON_EXISTING, NON_EXISTING); }
@Test public void canGetAllChildElementsByNullNamespaceAndName() { SpinList<SpinXmlElement> childElements = element.childElements(null, "info"); assertThat(childElements).hasSize(2); }
@Test public void canGetAllChildElementsByNamespaceAndName() { SpinList<SpinXmlElement> childElements = element.childElements(EXAMPLE_NAMESPACE, "customer"); assertThat(childElements).hasSize(3); }
@Test public void canGetAllChildElementsByName() { SpinList<SpinXmlElement> childElements = element.childElements("customer"); assertThat(childElements).hasSize(3); }
@Test public void canGetAllChildElements() { SpinList<SpinXmlElement> childElements = element.childElements(); assertThat(childElements).hasSize(7); }