KickJava   Java API By Example, From Geeks To Geeks.

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


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.transformation.I18nTransformer;
29 import org.apache.cocoon.core.container.ContainerTestCase;
30 import org.apache.cocoon.forms.FormsConstants;
31 import org.apache.cocoon.forms.datatype.typeimpl.EnumType;
32 import org.apache.cocoon.xml.dom.DOMBuilder;
33 import org.apache.excalibur.source.impl.ResourceSource;
34 import org.custommonkey.xmlunit.Diff;
35 import org.w3c.dom.Document JavaDoc;
36
37 /**
38  * Test case for CForms's DynamicSelectionList datatype.
39  *
40  * @version $Id: EnumSelectionListTestCase.java 326838 2005-10-20 06:26:53Z sylvain $
41  */

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

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

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

73     public void testGenerateSaxFragment() throws Exception JavaDoc {
74         DOMBuilder dest = new DOMBuilder();
75         EnumSelectionList list =
76             new EnumSelectionList(Sex.class.getName(), new EnumType(), false);
77         list.generateSaxFragment(dest, Locale.ENGLISH);
78         ResourceSource expectedSource =
79             new ResourceSource("resource://org/apache/cocoon/forms/datatype/EnumSelectionListTestCase.dest-no-null.xml");
80         Document JavaDoc expected = parser.parse(expectedSource.getInputStream());
81         assertEqual("Test if output is what is expected",
82                 expected, dest.getDocument());
83     }
84     
85     /**
86      * Test the generateSaxFragment method with a nullable selection list
87      * @throws MalformedURLException
88      * @throws ParserConfigurationException
89      */

90     public void testGenerateSaxFragmentNullable() throws Exception JavaDoc {
91         DOMBuilder dest = new DOMBuilder();
92         EnumSelectionList list =
93             new EnumSelectionList(Sex.class.getName(), new EnumType(), true);
94         list.generateSaxFragment(dest, Locale.ENGLISH);
95         ResourceSource expectedSource =
96             new ResourceSource("resource://org/apache/cocoon/forms/datatype/EnumSelectionListTestCase.dest.xml");
97         Document JavaDoc expected = parser.parse(expectedSource.getInputStream());
98         assertEqual("Test if output is what is expected",
99                 expected, dest.getDocument());
100     }
101     
102     /**
103      * Check is the source document is equal to the one produced by the method under test.
104      * @param message A message to print in case of failure.
105      * @param expected The expected (source) document.
106      * @param actual The actual (output) document.
107      */

108     private void assertEqual(String JavaDoc message, Document JavaDoc expected, Document JavaDoc actual) {
109         expected.getDocumentElement().normalize();
110         actual.getDocumentElement().normalize();
111         // DIRTY HACK WARNING: we add the "xmlns:*" attributes reported
112
// by DOM, as expected, but not generated by the method under test,
113
// otherwise the comparison would fail.
114
actual.getDocumentElement().setAttribute(FormsConstants.INSTANCE_PREFIX,
115                 FormsConstants.INSTANCE_NS);
116         actual.getDocumentElement().setAttribute("i18n",
117                 I18nTransformer.I18N_NAMESPACE_URI);
118         Diff diff = new Diff(expected, actual);
119         assertTrue(message + ", " + diff.toString(), diff.similar());
120     }
121
122     /**
123      * Print a document to a writer for debugging purposes.
124      * @param document The document to print.
125      * @param out The writer to write to.
126      */

127     public final void print(Document JavaDoc document, Writer JavaDoc out) {
128         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
129         try {
130             javax.xml.transform.Transformer JavaDoc serializer =
131                 factory.newTransformer();
132             serializer.transform(
133                 new DOMSource JavaDoc(document),
134                 new StreamResult JavaDoc(out));
135             out.write('\n');
136         } catch (Exception JavaDoc e) {
137             e.printStackTrace();
138         }
139     }
140 }
141
Popular Tags