001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.forms.formmodel;
019:
020: import java.util.Locale;
021:
022: import javax.xml.parsers.DocumentBuilder;
023: import javax.xml.parsers.DocumentBuilderFactory;
024:
025: import junit.framework.Assert;
026:
027: import org.apache.avalon.framework.service.ServiceManager;
028: import org.apache.cocoon.forms.FormsConstants;
029: import org.apache.cocoon.forms.FormManager;
030: import org.apache.cocoon.xml.AttributesImpl;
031: import org.apache.cocoon.xml.dom.DOMBuilder;
032: import org.apache.commons.jxpath.JXPathContext;
033: import org.apache.commons.jxpath.Pointer;
034: import org.w3c.dom.Document;
035: import org.xml.sax.SAXException;
036: import org.xml.sax.helpers.NamespaceSupport;
037:
038: /**
039: * Helper class to build Widget test cases.
040: *
041: * @version $Id: WidgetTestHelper.java 449149 2006-09-23 03:58:05Z crossley $
042: */
043: public class WidgetTestHelper {
044:
045: // Private constructor as we only have static methods
046: private WidgetTestHelper() {
047: }
048:
049: /**
050: * Get the result of a widget's generateSaxFragment() method as a Document.
051: * <p>
052: * The widget's fragment is encapsulated in a root <fi:fragment> element,
053: * since there's no guarantee that a widget outputs a single top-level element
054: * (there can be several elements, or even none if the widget is invisible)
055: *
056: * @param widget the widget of which we want the fragment
057: * @param locale the locale to be used to generate the fragment
058: * @return the document containing the fragment
059: */
060: public static Document getWidgetFragment(Widget widget,
061: Locale locale) throws SAXException {
062:
063: DOMBuilder domBuilder = new DOMBuilder();
064: // Start document and "fi:fragment" root element
065: domBuilder.startDocument();
066: domBuilder.startPrefixMapping(FormsConstants.INSTANCE_PREFIX,
067: FormsConstants.INSTANCE_NS);
068: // FIXME: why simply declaring the prefix isn't enough?
069: AttributesImpl attr = new AttributesImpl();
070: attr.addCDATAAttribute(NamespaceSupport.XMLNS, "fi:",
071: "xmlns:fi", FormsConstants.INSTANCE_NS);
072: domBuilder
073: .startElement(FormsConstants.INSTANCE_NS, "fragment",
074: FormsConstants.INSTANCE_PREFIX_COLON
075: + "fragment", attr);
076:
077: widget.generateSaxFragment(domBuilder, locale);
078:
079: // End "fi:fragment" element and document
080: domBuilder.endElement(FormsConstants.INSTANCE_NS, "fragment",
081: FormsConstants.INSTANCE_PREFIX_COLON + "fragment");
082: domBuilder.endPrefixMapping(FormsConstants.INSTANCE_PREFIX);
083: domBuilder.endDocument();
084:
085: // Return the document
086: return domBuilder.getDocument();
087: }
088:
089: public static void assertXPathEquals(String expected, String xpath,
090: Document doc) {
091: // use xpath as the message
092: assertXPathEquals(xpath, expected, xpath, doc);
093: }
094:
095: public static void assertXPathEquals(String message,
096: String expected, String xpath, Document doc) {
097: JXPathContext ctx = JXPathContext.newContext(doc);
098: ctx.setLenient(true);
099: Assert.assertEquals(message, expected, ctx.getValue(xpath));
100: }
101:
102: public static void assertXPathExists(String xpath, Document doc) {
103: // use xpath as message
104: assertXPathExists(xpath, xpath, doc);
105: }
106:
107: public static void assertXPathExists(String message, String xpath,
108: Document doc) {
109: JXPathContext ctx = JXPathContext.newContext(doc);
110: ctx.setLenient(true);
111: Pointer pointer = ctx.getPointer(xpath);
112: Assert.assertNotNull(message, pointer.getNode());
113: }
114:
115: public static void assertXPathNotExists(String xpath, Document doc) {
116: // use xpath as message
117: assertXPathNotExists(xpath, xpath, doc);
118: }
119:
120: public static void assertXPathNotExists(String message,
121: String xpath, Document doc) {
122: JXPathContext ctx = JXPathContext.newContext(doc);
123: ctx.setLenient(true);
124: Pointer pointer = ctx.getPointer(xpath);
125: Assert.assertNull(message, pointer.getNode());
126: }
127:
128: /**
129: * Load a Form whose definition relative to a given object (typically, the TestCase class).
130: *
131: * @param manager the ServiceManager that will be used to create the form
132: * @param obj the object relative to which the resource will be read
133: * @param resource the relative resource name for the form definition
134: * @return the Form
135: * @throws Exception
136: */
137: public static Form loadForm(ServiceManager manager, Object obj,
138: String resource) throws Exception {
139: // Load the document
140: DocumentBuilderFactory factory = DocumentBuilderFactory
141: .newInstance();
142: // Grmbl... why isn't this true by default?
143: factory.setNamespaceAware(true);
144: DocumentBuilder parser = factory.newDocumentBuilder();
145: Document doc = parser.parse(obj.getClass()
146: .getResource(resource).toExternalForm());
147:
148: // Create the form
149: FormManager formManager = (FormManager) manager
150: .lookup(FormManager.ROLE);
151: try {
152: return formManager.createForm(doc.getDocumentElement());
153: } finally {
154: manager.release(formManager);
155: }
156: }
157: }
|