KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > woody > datatype > FlowJXPathSelectionListTestCase


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.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.xml.parsers.DocumentBuilder JavaDoc;
27 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
28 import javax.xml.transform.TransformerFactory JavaDoc;
29 import javax.xml.transform.dom.DOMSource JavaDoc;
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31
32 import org.apache.avalon.framework.context.Context;
33 import org.apache.avalon.framework.context.DefaultContext;
34 import org.apache.cocoon.components.ContextHelper;
35 import org.apache.cocoon.components.flow.FlowHelper;
36 import org.apache.cocoon.core.container.ContainerTestCase;
37 import org.apache.cocoon.environment.ObjectModelHelper;
38 import org.apache.cocoon.environment.Request;
39 import org.apache.cocoon.environment.mock.MockRequest;
40 import org.apache.cocoon.woody.Constants;
41 import org.apache.cocoon.xml.dom.DOMBuilder;
42 import org.apache.excalibur.source.Source;
43 import org.apache.excalibur.source.impl.ResourceSource;
44 import org.custommonkey.xmlunit.Diff;
45 import org.w3c.dom.Document JavaDoc;
46 import org.w3c.dom.Element JavaDoc;
47
48 /**
49  * Test case for Woody's FlowModelSelectionList datatype.
50  * @version CVS $Id: FlowJXPathSelectionListTestCase.java 55454 2004-10-24 18:02:39Z cziegeler $
51  */

52 public class FlowJXPathSelectionListTestCase extends ContainerTestCase {
53
54     protected DatatypeManager datatypeManager;
55     protected DocumentBuilder JavaDoc parser;
56
57     /* (non-Javadoc)
58      * @see junit.framework.TestCase#setUp()
59      */

60     protected void setUp() throws Exception JavaDoc {
61         super.setUp();
62         datatypeManager = (DatatypeManager) this.lookup(DatatypeManager.ROLE);
63         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
64         factory.setNamespaceAware(true);
65         parser = factory.newDocumentBuilder();
66     }
67     
68     /* (non-Javadoc)
69      * @see junit.framework.TestCase#tearDown()
70      */

71     protected void tearDown() throws Exception JavaDoc {
72         if (datatypeManager != null) {
73             this.release(datatypeManager);
74         }
75         super.tearDown();
76     }
77     
78     /**
79      * Test the generateSaxFragment method.
80      */

81     public void testGenerateSaxFragment() throws Exception JavaDoc {
82         List JavaDoc beans = new ArrayList JavaDoc(2);
83         beans.add(new TestBean("1", "One"));
84         beans.add(new TestBean("2", "Two"));
85         Map JavaDoc flowContextObject = new HashMap JavaDoc();
86         flowContextObject.put("beans", beans);
87         Request request = new MockRequest();
88         Map JavaDoc objectModel = new HashMap JavaDoc();
89         FlowHelper.setContextObject(objectModel, flowContextObject);
90         objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
91         Map JavaDoc contextObjectModel = new HashMap JavaDoc();
92         contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectModel);
93         Context context = new DefaultContext(contextObjectModel);
94         Source JavaDoc sampleSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.source.xml");
95         Document JavaDoc sample = parser.parse(sampleSource.getInputStream());
96         Element JavaDoc datatypeElement = (Element JavaDoc) sample.getElementsByTagNameNS(Constants.WD_NS, "datatype").item(0);
97         Datatype datatype = datatypeManager.createDatatype(datatypeElement, false);
98         FlowJXPathSelectionList list = new FlowJXPathSelectionList
99             (context, "beans", "key", "value", datatype);
100         DOMBuilder dest = new DOMBuilder();
101         list.generateSaxFragment(dest, Locale.ENGLISH);
102         Source JavaDoc expectedSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.dest.xml");
103         Document JavaDoc expected = parser.parse(expectedSource.getInputStream());
104         assertEqual("Test if generated list matches expected",
105             expected, dest.getDocument());
106     }
107     
108     /**
109      * Test the generateSaxFragment method with a list containing a null value.
110      */

111     public void testGenerateSaxFragmentWithNull() throws Exception JavaDoc {
112         List JavaDoc beans = new ArrayList JavaDoc(2);
113         beans.add(null);
114         beans.add(new TestBean("1", "One"));
115         beans.add(new TestBean("2", "Two"));
116         Map JavaDoc flowContextObject = new HashMap JavaDoc();
117         flowContextObject.put("beans", beans);
118         Request request = new MockRequest();
119         Map JavaDoc objectModel = new HashMap JavaDoc();
120         FlowHelper.setContextObject(objectModel, flowContextObject);
121         objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
122         Map JavaDoc contextObjectModel = new HashMap JavaDoc();
123         contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL, objectModel);
124         Context context = new DefaultContext(contextObjectModel);
125         Source JavaDoc sampleSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.source.xml");
126         Document JavaDoc sample = parser.parse(sampleSource.getInputStream());
127         Element JavaDoc datatypeElement = (Element JavaDoc) sample.getElementsByTagNameNS(Constants.WD_NS, "datatype").item(0);
128         Datatype datatype = datatypeManager.createDatatype(datatypeElement, false);
129         FlowJXPathSelectionList list = new FlowJXPathSelectionList
130             (context, "beans", "key", "value", datatype);
131         DOMBuilder dest = new DOMBuilder();
132         list.generateSaxFragment(dest, Locale.ENGLISH);
133         Source JavaDoc expectedSource = new ResourceSource("resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCaseWithNull.dest.xml");
134         Document JavaDoc expected = parser.parse(expectedSource.getInputStream());
135         assertEqual("Test if generated list matches expected",
136                 expected, dest.getDocument());
137     }
138     
139     /**
140      * Check is the source document is equal to the one produced by the method under test.
141      * @param message A message to print in case of failure.
142      * @param expected The expected (source) document.
143      * @param actual The actual (output) document.
144      */

145     private void assertEqual(String JavaDoc message, Document JavaDoc expected, Document JavaDoc actual) {
146         expected.getDocumentElement().normalize();
147         actual.getDocumentElement().normalize();
148         // DIRTY HACK WARNING: we add the "xmlns:wi" attribute reported
149
// by DOM, as expected, but not generated by the method under test,
150
// otherwise the comparison would fail.
151
actual.getDocumentElement().setAttribute(Constants.WI_PREFIX,
152                 Constants.WI_NS);
153         Diff diff = new Diff(expected, actual);
154         assertTrue(message + ", " + diff.toString(), diff.similar());
155     }
156
157     /**
158      * Print a document to a writer for debugging purposes.
159      * @param document The document to print.
160      * @param out The writer to write to.
161      */

162     public final void print(Document JavaDoc document, Writer JavaDoc out) {
163         TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
164         try {
165             javax.xml.transform.Transformer JavaDoc serializer =
166                 factory.newTransformer();
167             serializer.transform(
168                 new DOMSource JavaDoc(document),
169                 new StreamResult JavaDoc(out));
170             out.write('\n');
171         } catch (Exception JavaDoc e) {
172             e.printStackTrace();
173         }
174     }
175     
176     public static class TestBean {
177         private String JavaDoc key;
178         private String JavaDoc value;
179
180         public TestBean(String JavaDoc key, String JavaDoc value) {
181             this.key = key;
182             this.value = value;
183         }
184         
185         public String JavaDoc getKey() {
186             return key;
187         }
188         
189         public String JavaDoc getValue() {
190             return value;
191         }
192         
193         public String JavaDoc toString() {
194             return "{ " + key + " : " + value + " }";
195         }
196     }
197 }
198
Popular Tags