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