KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > WidgetTestHelper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.cocoon.forms.formmodel;
18
19 import java.util.Locale JavaDoc;
20
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
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 JavaDoc;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.helpers.NamespaceSupport JavaDoc;
36
37 /**
38  * Helper class to build Widget test cases.
39  *
40  * @version $Id: WidgetTestHelper.java 326838 2005-10-20 06:26:53Z sylvain $
41  */

42 public class WidgetTestHelper {
43     
44     // Private constructor as we only have static methods
45
private WidgetTestHelper() {}
46
47     /**
48      * Get the result of a widget's generateSaxFragment() method as a Document.
49      * <p>
50      * The widget's fragment is encapsulated in a root &lt;fi:fragment&gt; element,
51      * since there's no guarantee that a widget outputs a single top-level element
52      * (there can be several elements, or even none if the widget is invisible)
53      *
54      * @param widget the widget of which we want the fragment
55      * @param locale the locale to be used to generate the fragment
56      * @return the document containing the fragment
57      */

58     public static Document JavaDoc getWidgetFragment(Widget widget, Locale JavaDoc locale) throws SAXException JavaDoc {
59         
60         DOMBuilder domBuilder = new DOMBuilder();
61         // Start document and "fi:fragment" root element
62
domBuilder.startDocument();
63         domBuilder.startPrefixMapping(FormsConstants.INSTANCE_PREFIX, FormsConstants.INSTANCE_NS);
64         // FIXME: why simply declaring the prefix isn't enough?
65
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         // End "fi:fragment" element and document
72
domBuilder.endElement(FormsConstants.INSTANCE_NS, "fragment", FormsConstants.INSTANCE_PREFIX_COLON + "fragment");
73         domBuilder.endPrefixMapping(FormsConstants.INSTANCE_PREFIX);
74         domBuilder.endDocument();
75         
76         // Return the document
77
return domBuilder.getDocument();
78     }
79     
80     public static void assertXPathEquals(String JavaDoc expected, String JavaDoc xpath, Document JavaDoc doc) {
81         // use xpath as the message
82
assertXPathEquals(xpath, expected, xpath, doc);
83     }
84     
85     public static void assertXPathEquals(String JavaDoc message, String JavaDoc expected, String JavaDoc xpath, Document JavaDoc 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 JavaDoc xpath, Document JavaDoc doc) {
92         // use xpath as message
93
assertXPathExists(xpath, xpath, doc);
94     }
95     
96     public static void assertXPathExists(String JavaDoc message, String JavaDoc xpath, Document JavaDoc 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 JavaDoc xpath, Document JavaDoc doc) {
104         // use xpath as message
105
assertXPathNotExists(xpath, xpath, doc);
106     }
107     
108     public static void assertXPathNotExists(String JavaDoc message, String JavaDoc xpath, Document JavaDoc 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     /**
116      * Load a Form whose definition relative to a given object (typically, the TestCase class).
117      *
118      * @param manager the ServiceManager that will be used to create the form
119      * @param obj the object relative to which the resource will be read
120      * @param resource the relative resource name for the form definition
121      * @return the Form
122      * @throws Exception
123      */

124     public static Form loadForm(ServiceManager manager, Object JavaDoc obj, String JavaDoc resource) throws Exception JavaDoc {
125         // Load the document
126
DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
127         // Grmbl... why isn't this true by default?
128
factory.setNamespaceAware(true);
129         DocumentBuilder JavaDoc parser = factory.newDocumentBuilder();
130         Document JavaDoc doc = parser.parse(obj.getClass().getResource(resource).toExternalForm());
131
132         // Create the form
133
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