KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > 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.woody.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.transformation.I18nTransformer;
30 import org.apache.cocoon.woody.Constants;
31 import org.apache.cocoon.woody.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 Woody's DynamicSelectionList datatype.
39  * @version CVS $Id: EnumSelectionListTestCase.java 55454 2004-10-24 18:02:39Z cziegeler $
40  */

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

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

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

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

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

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

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