| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to You under the Apache License, Version 2.0 |
| * (the "License"); you may not use this file except in compliance with |
| * the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package org.apache.cocoon.forms.datatype; |
| |
| import java.io.Writer; |
| |
| import javax.xml.parsers.DocumentBuilder; |
| import javax.xml.parsers.DocumentBuilderFactory; |
| import javax.xml.transform.TransformerFactory; |
| import javax.xml.transform.dom.DOMSource; |
| import javax.xml.transform.stream.StreamResult; |
| |
| import org.apache.cocoon.core.container.ContainerTestCase; |
| |
| import org.custommonkey.xmlunit.Diff; |
| |
| import org.w3c.dom.Document; |
| |
| /** |
| * Abstract TestCase for CForms's SelectionList datatypes. |
| * @version $Id$ |
| */ |
| public abstract class AbstractSelectionListTestCase extends ContainerTestCase { |
| |
| protected DatatypeManager datatypeManager; |
| protected DocumentBuilder parser; |
| |
| /* (non-Javadoc) |
| * @see junit.framework.TestCase#setUp() |
| */ |
| protected void setUp() throws Exception { |
| super.setUp(); |
| this.datatypeManager = (DatatypeManager) this.lookup(DatatypeManager.ROLE); |
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| factory.setNamespaceAware(true); |
| this.parser = factory.newDocumentBuilder(); |
| } |
| |
| /* (non-Javadoc) |
| * @see junit.framework.TestCase#tearDown() |
| */ |
| protected void tearDown() throws Exception { |
| if (this.datatypeManager != null) { |
| this.release(this.datatypeManager); |
| } |
| super.tearDown(); |
| } |
| |
| /** |
| * Check is the source document is equal to the one produced by the method under test. |
| * @param message A message to print in case of failure. |
| * @param expected The expected (source) document. |
| * @param actual The actual (output) document. |
| */ |
| protected static void assertEqual(String message, Document expected, Document actual) { |
| expected.getDocumentElement().normalize(); |
| actual.getDocumentElement().normalize(); |
| |
| Diff diff = new Diff(expected, actual); |
| assertTrue(message + ", " + diff.toString(), diff.similar()); |
| } |
| |
| /** |
| * Print a document to a writer for debugging purposes. |
| * @param document The document to print. |
| * @param out The writer to write to. |
| */ |
| protected static void print(Document document, Writer out) { |
| TransformerFactory factory = TransformerFactory.newInstance(); |
| try { |
| javax.xml.transform.Transformer serializer = |
| factory.newTransformer(); |
| serializer.transform( |
| new DOMSource(document), |
| new StreamResult(out)); |
| out.write('\n'); |
| } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| } |
| } |