Kyero_3 extends XmlConverter<KyeroDocument, KyeroVersion> { protected void downgradeNewBuildElements(Document doc) throws JaxenException { List nodes = XmlUtils.newXPath( "/io:root/io:property/io:new_build", doc).selectNodes(doc); for (Object item : nodes) { Element node = (Element) item; Element parentNode = (Element) node.getParentNode(); String value = StringUtils.trimToNull(node.getTextContent()); if ("1".equals(value)) { Element priceFreqNode = (Element) XmlUtils.newXPath("io:price_freq", doc) .selectSingleNode(parentNode); if (priceFreqNode == null) { priceFreqNode = doc.createElementNS(KyeroUtils.NAMESPACE, "price_freq"); priceFreqNode.setTextContent("new_build"); parentNode.appendChild(priceFreqNode); } else if ("sale".equalsIgnoreCase(priceFreqNode.getTextContent())) { priceFreqNode.setTextContent("new_build"); } } parentNode.removeChild(node); } }  @Override KyeroVersion getVersion(); @Override void downgradeToPreviousVersion(KyeroDocument doc); @Override void upgradeFromPreviousVersion(KyeroDocument doc);  }
@Test public void testDowngradeNewBuildElements() { try { Document doc = buildDocumentForDowngrade(); int count; count = XmlUtils.countNodes( "/io:root/io:property/io:new_build", doc); Assert.assertEquals( "3 <new_build> elements available before conversion.", 3, count); count = XmlUtils.countNodes( "/io:root/io:property/io:price_freq[text()='sale']", doc); Assert.assertEquals( "2 <price_freq>sale</price_freq> elements available before conversion.", 2, count); count = XmlUtils.countNodes( "/io:root/io:property/io:price_freq[text()='new_build']", doc); Assert.assertEquals( "0 <price_freq>new_build</price_freq> elements available before conversion.", 0, count); new Kyero_3().downgradeNewBuildElements(doc); count = XmlUtils.countNodes( "/io:root/io:property/io:new_build", doc); Assert.assertEquals( "0 <new_build> elements available after conversion.", 0, count); count = XmlUtils.countNodes( "/io:root/io:property/io:price_freq[text()='sale']", doc); Assert.assertEquals( "1 <price_freq>sale</price_freq> element available after conversion.", 1, count); count = XmlUtils.countNodes( "/io:root/io:property/io:price_freq[text()='new_build']", doc); Assert.assertEquals( "1 <price_freq>new_build</price_freq> element available after conversion.", 1, count); } catch (Exception ex) { LOGGER.error("Test of Kyero_3.downgradeNewBuildElements failed!"); LOGGER.error("> " + ex.getLocalizedMessage(), ex); Assert.fail("Test of Kyero_3.downgradeNewBuildElements failed!"); } }