View Javadoc
1 /* 2 * Angkor Web Framework 3 * 4 * Distributable under LGPL license. 5 * See terms of license at gnu.org. 6 */ 7 8 package com.tirsen.angkor.test.unit; 9 10 import com.tirsen.angkor.Component; 11 import com.tirsen.angkor.test.unit.mock.MockRenderContext; 12 import junit.framework.Assert; 13 import org.w3c.dom.Document; 14 import org.w3c.dom.Element; 15 import org.w3c.dom.NodeList; 16 import org.jaxen.jdom.JDOMXPath; 17 import org.jaxen.JaxenException; 18 19 import java.io.IOException; 20 21 /*** 22 * TODO document TestUtility. 23 * 24 * @author $Author: tirsen $ 25 * @version $Revision: 1.7 $ 26 * <BR> 27 * $Id: TestUtility.java,v 1.7 2002/10/13 19:59:23 tirsen Exp $ 28 */ 29 public class TestUtility extends Assert 30 { 31 public static org.jdom.Element selectElement(org.jdom.Document document, String xpath) 32 { 33 org.jdom.Element element = null; 34 try 35 { 36 JDOMXPath jdomxPath = new JDOMXPath(xpath); 37 element = (org.jdom.Element) jdomxPath.selectSingleNode(document); 38 } 39 catch (JaxenException e) 40 { 41 throw new RuntimeException(e); 42 } 43 return element; 44 } 45 46 public static Element findElementById(Document doc, String id) 47 { 48 String idAttr = "id"; 49 return findElementById(doc.getDocumentElement(), id, idAttr); 50 } 51 52 public static Element findElementById(Element element, String id, String idAttr) 53 { 54 if (id.equals(element.getAttribute(idAttr))) return element; 55 NodeList children = element.getChildNodes(); 56 for (int i = 0; i < children.getLength(); i++) 57 { 58 if (children.item(i) instanceof Element) 59 { 60 Element result = findElementById((Element) children.item(i), id, idAttr); 61 if (result != null) return result; 62 } 63 } 64 return null; 65 } 66 67 public static Element testComponent(MockRenderContext context, String componentName) throws IOException 68 { 69 Component component = (Component) context.getApplication().getComponent(componentName).getView(); 70 component.render(context); 71 Document doc = context.parseRenderedHTML(); 72 System.out.println("component = " + component); 73 Element element = findElementById(doc, component.getId()); 74 assertNotNull("component " + componentName + " was not rendered", element); 75 assertTrue("id not rendered correctly", component.getId().equals(element.getAttribute("id"))); 76 77 return element; 78 } 79 80 public static Element testComponent(MockRenderContext context, Component component) throws IOException 81 { 82 component.render(context); 83 Document doc = context.parseRenderedHTML(); 84 System.out.println("component = " + component); 85 return testComponent(doc, component); 86 } 87 88 public static Element testComponent(Document doc, Component component) 89 { 90 Element element = findElementById(doc, component.getId()); 91 assertNotNull("component " + component + " was not rendered", element); 92 assertTrue("id not rendered correctly", component.getId().equals(element.getAttribute("id"))); 93 return element; 94 } 95 }

This page was automatically generated by Maven