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.Locale;
022:
023: import javax.xml.parsers.DocumentBuilder;
024: import javax.xml.parsers.DocumentBuilderFactory;
025: import javax.xml.transform.TransformerFactory;
026: import javax.xml.transform.dom.DOMSource;
027: import javax.xml.transform.stream.StreamResult;
028:
029: import org.apache.cocoon.core.container.ContainerTestCase;
030: import org.apache.cocoon.woody.Constants;
031: import org.apache.cocoon.xml.dom.DOMBuilder;
032: import org.apache.excalibur.source.impl.ResourceSource;
033: import org.custommonkey.xmlunit.Diff;
034: import org.w3c.dom.Document;
035: import org.w3c.dom.Element;
036:
037: /**
038: * Test case for Woody's DynamicSelectionList datatype.
039: * @version CVS $Id: DynamicSelectionListTestCase.java 433543 2006-08-22 06:22:54Z crossley $
040: */
041: public class DynamicSelectionListTestCase extends ContainerTestCase {
042:
043: protected DatatypeManager datatypeManager;
044: protected DocumentBuilder parser;
045:
046: /* (non-Javadoc)
047: * @see junit.framework.TestCase#setUp()
048: */
049: protected void setUp() throws Exception {
050: super .setUp();
051: datatypeManager = (DatatypeManager) this
052: .lookup(DatatypeManager.ROLE);
053: DocumentBuilderFactory factory = DocumentBuilderFactory
054: .newInstance();
055: factory.setNamespaceAware(true);
056: parser = factory.newDocumentBuilder();
057: }
058:
059: /* (non-Javadoc)
060: * @see junit.framework.TestCase#tearDown()
061: */
062: protected void tearDown() throws Exception {
063: if (datatypeManager != null) {
064: this .release(datatypeManager);
065: }
066: super .tearDown();
067: }
068:
069: /**
070: * Test the generateSaxFragment method.
071: * @throws MalformedURLException
072: * @throws ParserConfigurationException
073: */
074: public void testGenerateSaxFragment() throws Exception {
075: DOMBuilder dest = new DOMBuilder();
076: ResourceSource source = new ResourceSource(
077: "resource://org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.source.xml");
078: Document sourceDoc = parser.parse(source.getInputStream());
079: Element datatypeElement = (Element) sourceDoc
080: .getElementsByTagNameNS(Constants.WD_NS, "convertor")
081: .item(0);
082: Datatype datatype = datatypeManager.createDatatype(
083: datatypeElement, false);
084: DynamicSelectionList list = new DynamicSelectionList(datatype,
085: null, this .getManager());
086: list.generateSaxFragment(dest, Locale.ENGLISH, source);
087: ResourceSource expectedSource = new ResourceSource(
088: "resource://org/apache/cocoon/woody/datatype/DynamicSelectionListTestCase.dest.xml");
089: Document expected = parser.parse(expectedSource
090: .getInputStream());
091: assertEqual("Test if output is what is expected", expected,
092: dest.getDocument());
093: }
094:
095: /**
096: * Check is the source document is equal to the one produced by the method under test.
097: * @param message A message to print in case of failure.
098: * @param expected The expected (source) document.
099: * @param actual The actual (output) document.
100: */
101: private void assertEqual(String message, Document expected,
102: Document actual) {
103: expected.getDocumentElement().normalize();
104: actual.getDocumentElement().normalize();
105: // DIRTY HACK WARNING: we add the "xmlns:wi" attribute reported
106: // by DOM, as expected, but not generated by the method under test,
107: // otherwise the comparison would fail.
108: actual.getDocumentElement().setAttribute(Constants.WI_PREFIX,
109: Constants.WI_NS);
110: Diff diff = new Diff(expected, actual);
111: assertTrue(message + ", " + diff.toString(), diff.similar());
112: }
113:
114: /**
115: * Print a document to a writer for debugging purposes.
116: * @param document The document to print.
117: * @param out The writer to write to.
118: */
119: public final void print(Document document, Writer out) {
120: TransformerFactory factory = TransformerFactory.newInstance();
121: try {
122: javax.xml.transform.Transformer serializer = factory
123: .newTransformer();
124: serializer.transform(new DOMSource(document),
125: new StreamResult(out));
126: out.write('\n');
127: } catch (Exception e) {
128: e.printStackTrace();
129: }
130: }
131: }
|