This page was generated using Webiyo. See its source code and unit tests.
|
org/webiyo/util/test/ElementChecker.javapackage org.webiyo.util.test; import junit.framework.Assert; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.webiyo.web.HtmlWriter; import org.webiyo.web.Renderable; import org.webiyo.xml.Xml; import org.webiyo.xml.XmlException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import static org.webiyo.xml.dtds.Html.NBSP; import static org.webiyo.xml.dtds.Html.DIV; public class ElementChecker { protected final Element element; protected final String currentXPath; public ElementChecker(final Renderable renderable) throws IOException { String xml = render(new Renderable() { public void render(HtmlWriter out) throws IOException { out.declareEntities(DIV, NBSP); out.startTag(DIV); renderable.render(out); out.endTag(DIV); } }); this.element = Xml.parse(xml).getDocumentElement(); this.currentXPath = "/"; } public ElementChecker(Element element, String elementXPath) { this.element = element; this.currentXPath = elementXPath; } public void checkText(String expectedText, String xpath) throws XmlException { Assert.assertEquals(fullXPath(xpath) + " contains unexpected text", expectedText, text(xpath)); } public void checkLink(String expectedHRef, String expectedText, String xpath) throws XmlException { ElementChecker link = navigateTo(xpath); Assert.assertEquals("a", link.element.getNodeName()); Assert.assertEquals("unexpected href at " + link.currentXPath, expectedHRef, link.element.getAttribute("href")); checkText(expectedText, xpath); } public void checkNonBreakingLink(String expectedHRef, String expectedText, String xpath) throws XmlException { checkLink(expectedHRef, nonBreakingText(expectedText), xpath); } public String text(String xpath) throws XmlException { return selectOne(xpath).getTextContent(); } public RowChecker navigateToRow(String xpath) throws XmlException { return new RowChecker(Xml.selectOne(element, xpath, Element.class), fullXPath(xpath)); } public ElementChecker navigateTo(String xpath) throws XmlException { return new ElementChecker(Xml.selectOne(element, xpath, Element.class), fullXPath(xpath)); } public List<ElementChecker> select(String xpath) throws XmlException { List<Element> elements = Xml.select(element, xpath, Element.class); List<ElementChecker> result = new ArrayList<ElementChecker>(); int count = 1; for (Element elt : elements) { result.add(new ElementChecker(elt, "(" + fullXPath(xpath) + ")[" + count + "]")); count++; } return result; } public List<Node> children() { return Xml.toList(element.getChildNodes(), Node.class); } public void checkName(String expectedElementName) { Assert.assertEquals(expectedElementName, element.getNodeName()); } // end of public methods private Node selectOne(String xpath) throws XmlException { return Xml.selectOne(element, xpath, Node.class); } private String nonBreakingText(String text) { return text.replace(' ', NBSP.getCodePoint()); } private String fullXPath(String childXPath) { String parentXPath = currentXPath; if (childXPath.startsWith("/")) { return childXPath; } else if (childXPath.startsWith("./")) { if (!parentXPath.endsWith("/")) parentXPath += "/"; return parentXPath + childXPath.substring(2); } else if (childXPath.equals(".")) { return parentXPath; } else { if (!parentXPath.endsWith("/")) parentXPath += "/"; return parentXPath + childXPath; } } protected static String render(Renderable renderable) throws IOException { StringBuilder builder = new StringBuilder(); HtmlWriter out = new HtmlWriter(builder); renderable.render(out); return builder.toString(); } } |