KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > datatype > DynamicSelectionListTestCase


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.datatype;
18
19 import java.io.Writer JavaDoc;
20 import java.util.Locale JavaDoc;
21
22 import javax.xml.parsers.DocumentBuilder JavaDoc;
23 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
24 import javax.xml.transform.TransformerFactory JavaDoc;
25 import javax.xml.transform.dom.DOMSource JavaDoc;
26 import javax.xml.transform.stream.StreamResult JavaDoc;
27
28 import org.apache.cocoon.core.container.ContainerTestCase;
29 import org.apache.cocoon.forms.FormsConstants;
30 import org.apache.cocoon.xml.dom.DOMBuilder;
31 import org.apache.excalibur.source.impl.ResourceSource;
32 import org.custommonkey.xmlunit.Diff;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35
36 /**
37  * Test case for CForms's DynamicSelectionList datatype.
38  * @version $Id: DynamicSelectionListTestCase.java 326838 2005-10-20 06:26:53Z sylvain $
39  */

40 public class DynamicSelectionListTestCase extends ContainerTestCase {
41
42     protected DatatypeManager datatypeManager;
43     protected DocumentBuilder JavaDoc parser;
44
45     /* (non-Javadoc)
46      * @see junit.framework.TestCase#setUp()
47      */

48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50         datatypeManager = (DatatypeManager) this.lookup(DatatypeManager.ROLE);
51         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
52         factory.setNamespaceAware(true);
53         parser = factory.newDocumentBuilder();
54     }
55     
56     /* (non-Javadoc)
57      * @see junit.framework.TestCase#tearDown()
58      */

59     protected void tearDown() throws Exception JavaDoc {
60         if (datatypeManager != null) {
61             this.release(datatypeManager);
62         }
63         super.tearDown();
64     }
65     
66     /**
67      * Test the generateSaxFragment method.
68      * @throws MalformedURLException
69      * @throws ParserConfigurationException
70      */

71     public void testGenerateSaxFragment() throws Exception JavaDoc {
72         DOMBuilder dest = new DOMBuilder();
73         ResourceSource source =
74             new ResourceSource("resource://org/apache/cocoon/forms/datatype/DynamicSelectionListTestCase.source.xml");
75         Document JavaDoc sourceDoc = parser.parse(source.getInputStream());
76         Element JavaDoc datatypeElement = (Element JavaDoc) sourceDoc.getElementsByTagNameNS(FormsConstants.DEFINITION_NS, "convertor").item(0);
77         Datatype datatype = datatypeManager.createDatatype(datatypeElement, false);
78         DynamicSelectionList list =
79             new DynamicSelectionList(datatype, null, this.getManager());
80         list.generateSaxFragment(dest, Locale.ENGLISH, source);
81         ResourceSource expectedSource =
82             new ResourceSource("resource://org/apache/cocoon/forms/datatype/DynamicSelectionListTestCase.dest.xml");
83         Document JavaDoc expected = parser.parse(expectedSource.getInputStream());
84         assertEqual("Test if output is what is expected",
85                 expected, dest.getDocument());
86     }
87
88     /**
89      * Check is the source document is equal to the one produced by the method under test.
90      * @param message A message to print in case of failure.
91      * @param expected The expected (source) document.
92      * @param actual The actual (output) document.
93      */

94     private void assertEqual(String JavaDoc message, Document JavaDoc expected, Document JavaDoc actual) {
95         expected.getDocumentElement().normalize();
96         actual.getDocumentElement().normalize();
97         // DIRTY HACK WARNING: we add the "xmlns:wi" attribute reported
98
// by DOM, as expected, but not generated by the method under test,
99
// otherwise the comparison would fail.
100
actual.getDocumentElement().setAttribute(FormsConstants.INSTANCE_PREFIX,
101                 FormsConstants.INSTANCE_NS);
102         Diff diff = new Diff(expected, actual);
103         assertTrue(message + ", " + diff.toString(), diff.similar());
104     }
105
106     /**
107      * Print a document to a writer for debugging purposes.
108      * @param document The document to print.
109      * @param out The writer to write to.
110      */

111     public final void print(Document JavaDoc document, Writer JavaDoc out) {
112         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
113         try {
114             javax.xml.transform.Transformer JavaDoc serializer =
115                 factory.newTransformer();
116             serializer.transform(
117                 new DOMSource JavaDoc(document),
118                 new StreamResult JavaDoc(out));
119             out.write('\n');
120         } catch (Exception JavaDoc e) {
121             e.printStackTrace();
122         }
123     }
124 }
125
Popular Tags