001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.cocoon.woody.datatype;
019:
020: import java.io.Writer;
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.List;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import javax.xml.parsers.DocumentBuilder;
028: import javax.xml.parsers.DocumentBuilderFactory;
029: import javax.xml.transform.TransformerFactory;
030: import javax.xml.transform.dom.DOMSource;
031: import javax.xml.transform.stream.StreamResult;
032:
033: import org.apache.avalon.framework.context.Context;
034: import org.apache.avalon.framework.context.DefaultContext;
035: import org.apache.cocoon.components.ContextHelper;
036: import org.apache.cocoon.components.flow.FlowHelper;
037: import org.apache.cocoon.core.container.ContainerTestCase;
038: import org.apache.cocoon.environment.ObjectModelHelper;
039: import org.apache.cocoon.environment.Request;
040: import org.apache.cocoon.environment.mock.MockRequest;
041: import org.apache.cocoon.woody.Constants;
042: import org.apache.cocoon.xml.dom.DOMBuilder;
043: import org.apache.excalibur.source.Source;
044: import org.apache.excalibur.source.impl.ResourceSource;
045: import org.custommonkey.xmlunit.Diff;
046: import org.w3c.dom.Document;
047: import org.w3c.dom.Element;
048:
049: /**
050: * Test case for Woody's FlowModelSelectionList datatype.
051: * @version CVS $Id: FlowJXPathSelectionListTestCase.java 433543 2006-08-22 06:22:54Z crossley $
052: */
053: public class FlowJXPathSelectionListTestCase extends ContainerTestCase {
054:
055: protected DatatypeManager datatypeManager;
056: protected DocumentBuilder parser;
057:
058: /* (non-Javadoc)
059: * @see junit.framework.TestCase#setUp()
060: */
061: protected void setUp() throws Exception {
062: super .setUp();
063: datatypeManager = (DatatypeManager) this
064: .lookup(DatatypeManager.ROLE);
065: DocumentBuilderFactory factory = DocumentBuilderFactory
066: .newInstance();
067: factory.setNamespaceAware(true);
068: parser = factory.newDocumentBuilder();
069: }
070:
071: /* (non-Javadoc)
072: * @see junit.framework.TestCase#tearDown()
073: */
074: protected void tearDown() throws Exception {
075: if (datatypeManager != null) {
076: this .release(datatypeManager);
077: }
078: super .tearDown();
079: }
080:
081: /**
082: * Test the generateSaxFragment method.
083: */
084: public void testGenerateSaxFragment() throws Exception {
085: List beans = new ArrayList(2);
086: beans.add(new TestBean("1", "One"));
087: beans.add(new TestBean("2", "Two"));
088: Map flowContextObject = new HashMap();
089: flowContextObject.put("beans", beans);
090: Request request = new MockRequest();
091: Map objectModel = new HashMap();
092: FlowHelper.setContextObject(objectModel, flowContextObject);
093: objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
094: Map contextObjectModel = new HashMap();
095: contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL,
096: objectModel);
097: Context context = new DefaultContext(contextObjectModel);
098: Source sampleSource = new ResourceSource(
099: "resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.source.xml");
100: Document sample = parser.parse(sampleSource.getInputStream());
101: Element datatypeElement = (Element) sample
102: .getElementsByTagNameNS(Constants.WD_NS, "datatype")
103: .item(0);
104: Datatype datatype = datatypeManager.createDatatype(
105: datatypeElement, false);
106: FlowJXPathSelectionList list = new FlowJXPathSelectionList(
107: context, "beans", "key", "value", datatype);
108: DOMBuilder dest = new DOMBuilder();
109: list.generateSaxFragment(dest, Locale.ENGLISH);
110: Source expectedSource = new ResourceSource(
111: "resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.dest.xml");
112: Document expected = parser.parse(expectedSource
113: .getInputStream());
114: assertEqual("Test if generated list matches expected",
115: expected, dest.getDocument());
116: }
117:
118: /**
119: * Test the generateSaxFragment method with a list containing a null value.
120: */
121: public void testGenerateSaxFragmentWithNull() throws Exception {
122: List beans = new ArrayList(2);
123: beans.add(null);
124: beans.add(new TestBean("1", "One"));
125: beans.add(new TestBean("2", "Two"));
126: Map flowContextObject = new HashMap();
127: flowContextObject.put("beans", beans);
128: Request request = new MockRequest();
129: Map objectModel = new HashMap();
130: FlowHelper.setContextObject(objectModel, flowContextObject);
131: objectModel.put(ObjectModelHelper.REQUEST_OBJECT, request);
132: Map contextObjectModel = new HashMap();
133: contextObjectModel.put(ContextHelper.CONTEXT_OBJECT_MODEL,
134: objectModel);
135: Context context = new DefaultContext(contextObjectModel);
136: Source sampleSource = new ResourceSource(
137: "resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCase.source.xml");
138: Document sample = parser.parse(sampleSource.getInputStream());
139: Element datatypeElement = (Element) sample
140: .getElementsByTagNameNS(Constants.WD_NS, "datatype")
141: .item(0);
142: Datatype datatype = datatypeManager.createDatatype(
143: datatypeElement, false);
144: FlowJXPathSelectionList list = new FlowJXPathSelectionList(
145: context, "beans", "key", "value", datatype);
146: DOMBuilder dest = new DOMBuilder();
147: list.generateSaxFragment(dest, Locale.ENGLISH);
148: Source expectedSource = new ResourceSource(
149: "resource://org/apache/cocoon/woody/datatype/FlowJXPathSelectionListTestCaseWithNull.dest.xml");
150: Document expected = parser.parse(expectedSource
151: .getInputStream());
152: assertEqual("Test if generated list matches expected",
153: expected, dest.getDocument());
154: }
155:
156: /**
157: * Check is the source document is equal to the one produced by the method under test.
158: * @param message A message to print in case of failure.
159: * @param expected The expected (source) document.
160: * @param actual The actual (output) document.
161: */
162: private void assertEqual(String message, Document expected,
163: Document actual) {
164: expected.getDocumentElement().normalize();
165: actual.getDocumentElement().normalize();
166: // DIRTY HACK WARNING: we add the "xmlns:wi" attribute reported
167: // by DOM, as expected, but not generated by the method under test,
168: // otherwise the comparison would fail.
169: actual.getDocumentElement().setAttribute(Constants.WI_PREFIX,
170: Constants.WI_NS);
171: Diff diff = new Diff(expected, actual);
172: assertTrue(message + ", " + diff.toString(), diff.similar());
173: }
174:
175: /**
176: * Print a document to a writer for debugging purposes.
177: * @param document The document to print.
178: * @param out The writer to write to.
179: */
180: public final void print(Document document, Writer out) {
181: TransformerFactory factory = TransformerFactory.newInstance();
182: try {
183: javax.xml.transform.Transformer serializer = factory
184: .newTransformer();
185: serializer.transform(new DOMSource(document),
186: new StreamResult(out));
187: out.write('\n');
188: } catch (Exception e) {
189: e.printStackTrace();
190: }
191: }
192:
193: public static class TestBean {
194: private String key;
195: private String value;
196:
197: public TestBean(String key, String value) {
198: this .key = key;
199: this .value = value;
200: }
201:
202: public String getKey() {
203: return key;
204: }
205:
206: public String getValue() {
207: return value;
208: }
209:
210: public String toString() {
211: return "{ " + key + " : " + value + " }";
212: }
213: }
214: }
|