1 16 17 package org.apache.cocoon.forms.formmodel; 18 19 import java.util.Locale ; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 24 import junit.framework.Assert; 25 26 import org.apache.avalon.framework.service.ServiceManager; 27 import org.apache.cocoon.forms.FormsConstants; 28 import org.apache.cocoon.forms.FormManager; 29 import org.apache.cocoon.xml.AttributesImpl; 30 import org.apache.cocoon.xml.dom.DOMBuilder; 31 import org.apache.commons.jxpath.JXPathContext; 32 import org.apache.commons.jxpath.Pointer; 33 import org.w3c.dom.Document ; 34 import org.xml.sax.SAXException ; 35 import org.xml.sax.helpers.NamespaceSupport ; 36 37 42 public class WidgetTestHelper { 43 44 private WidgetTestHelper() {} 46 47 58 public static Document getWidgetFragment(Widget widget, Locale locale) throws SAXException { 59 60 DOMBuilder domBuilder = new DOMBuilder(); 61 domBuilder.startDocument(); 63 domBuilder.startPrefixMapping(FormsConstants.INSTANCE_PREFIX, FormsConstants.INSTANCE_NS); 64 AttributesImpl attr = new AttributesImpl(); 66 attr.addCDATAAttribute(NamespaceSupport.XMLNS, "fi:", "xmlns:fi", FormsConstants.INSTANCE_NS); 67 domBuilder.startElement(FormsConstants.INSTANCE_NS, "fragment", FormsConstants.INSTANCE_PREFIX_COLON + "fragment", attr); 68 69 widget.generateSaxFragment(domBuilder, locale); 70 71 domBuilder.endElement(FormsConstants.INSTANCE_NS, "fragment", FormsConstants.INSTANCE_PREFIX_COLON + "fragment"); 73 domBuilder.endPrefixMapping(FormsConstants.INSTANCE_PREFIX); 74 domBuilder.endDocument(); 75 76 return domBuilder.getDocument(); 78 } 79 80 public static void assertXPathEquals(String expected, String xpath, Document doc) { 81 assertXPathEquals(xpath, expected, xpath, doc); 83 } 84 85 public static void assertXPathEquals(String message, String expected, String xpath, Document doc) { 86 JXPathContext ctx = JXPathContext.newContext(doc); 87 ctx.setLenient(true); 88 Assert.assertEquals(message, expected, ctx.getValue(xpath)); 89 } 90 91 public static void assertXPathExists(String xpath, Document doc) { 92 assertXPathExists(xpath, xpath, doc); 94 } 95 96 public static void assertXPathExists(String message, String xpath, Document doc) { 97 JXPathContext ctx = JXPathContext.newContext(doc); 98 ctx.setLenient(true); 99 Pointer pointer = ctx.getPointer(xpath); 100 Assert.assertNotNull(message, pointer.getNode()); 101 } 102 103 public static void assertXPathNotExists(String xpath, Document doc) { 104 assertXPathNotExists(xpath, xpath, doc); 106 } 107 108 public static void assertXPathNotExists(String message, String xpath, Document doc) { 109 JXPathContext ctx = JXPathContext.newContext(doc); 110 ctx.setLenient(true); 111 Pointer pointer = ctx.getPointer(xpath); 112 Assert.assertNull(message, pointer.getNode()); 113 } 114 115 124 public static Form loadForm(ServiceManager manager, Object obj, String resource) throws Exception { 125 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 127 factory.setNamespaceAware(true); 129 DocumentBuilder parser = factory.newDocumentBuilder(); 130 Document doc = parser.parse(obj.getClass().getResource(resource).toExternalForm()); 131 132 FormManager formManager = (FormManager)manager.lookup(FormManager.ROLE); 134 try { 135 return formManager.createForm(doc.getDocumentElement()); 136 } finally { 137 manager.release(formManager); 138 } 139 } 140 } 141 | Popular Tags |