01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.cocoon.forms.datatype;
19:
20: import java.io.Writer;
21:
22: import javax.xml.parsers.DocumentBuilder;
23: import javax.xml.parsers.DocumentBuilderFactory;
24: import javax.xml.transform.TransformerFactory;
25: import javax.xml.transform.dom.DOMSource;
26: import javax.xml.transform.stream.StreamResult;
27:
28: import org.apache.cocoon.core.container.ContainerTestCase;
29:
30: import org.custommonkey.xmlunit.Diff;
31:
32: import org.w3c.dom.Document;
33:
34: /**
35: * Abstract TestCase for CForms's SelectionList datatypes.
36: * @version $Id$
37: */
38: public abstract class AbstractSelectionListTestCase extends
39: ContainerTestCase {
40:
41: protected DatatypeManager datatypeManager;
42: protected DocumentBuilder parser;
43:
44: /* (non-Javadoc)
45: * @see junit.framework.TestCase#setUp()
46: */
47: protected void setUp() throws Exception {
48: super .setUp();
49: this .datatypeManager = (DatatypeManager) this
50: .lookup(DatatypeManager.ROLE);
51: DocumentBuilderFactory factory = DocumentBuilderFactory
52: .newInstance();
53: factory.setNamespaceAware(true);
54: this .parser = factory.newDocumentBuilder();
55: }
56:
57: /* (non-Javadoc)
58: * @see junit.framework.TestCase#tearDown()
59: */
60: protected void tearDown() throws Exception {
61: if (this .datatypeManager != null) {
62: this .release(this .datatypeManager);
63: }
64: super .tearDown();
65: }
66:
67: /**
68: * Check is the source document is equal to the one produced by the method under test.
69: * @param message A message to print in case of failure.
70: * @param expected The expected (source) document.
71: * @param actual The actual (output) document.
72: */
73: protected static void assertEqual(String message,
74: Document expected, Document actual) {
75: expected.getDocumentElement().normalize();
76: actual.getDocumentElement().normalize();
77:
78: Diff diff = new Diff(expected, actual);
79: assertTrue(message + ", " + diff.toString(), diff.similar());
80: }
81:
82: /**
83: * Print a document to a writer for debugging purposes.
84: * @param document The document to print.
85: * @param out The writer to write to.
86: */
87: protected static void print(Document document, Writer out) {
88: TransformerFactory factory = TransformerFactory.newInstance();
89: try {
90: javax.xml.transform.Transformer serializer = factory
91: .newTransformer();
92: serializer.transform(new DOMSource(document),
93: new StreamResult(out));
94: out.write('\n');
95: } catch (Exception e) {
96: e.printStackTrace();
97: }
98: }
99: }
|