001 /*
002 * TestCreoleAnnotationHandler.java
003 *
004 * Copyright (c) 2008, The University of Sheffield.
005 *
006 * This file is part of GATE (see http://gate.ac.uk/), and is free
007 * software, licenced under the GNU Library General Public License,
008 * Version 2, June 1991 (in the distribution as file licence.html,
009 * and also available at http://gate.ac.uk/gate/licence.html).
010 *
011 * Ian Roberts, 27/Jul/2008
012 *
013 * $Id: TestCreoleAnnotationHandler.java 9845 2008-08-25 22:23:24Z ian_roberts $
014 */
015
016 package gate.creole;
017
018 import java.net.URL;
019 import gate.Gate;
020 import gate.util.GateException;
021
022 import junit.framework.*;
023 import org.custommonkey.xmlunit.*;
024 import org.jdom.input.SAXBuilder;
025 import org.jdom.output.DOMOutputter;
026 import org.jdom.output.XMLOutputter;
027 import org.jdom.output.Format;
028 import org.w3c.dom.Element;
029 import org.w3c.dom.Node;
030 import javax.xml.parsers.DocumentBuilder;
031 import javax.xml.parsers.DocumentBuilderFactory;
032
033 /**
034 * Test for the CreoleAnnotationHandler, compares the XML produced by the
035 * annotation handler to an expected result.
036 */
037 public class TestCreoleAnnotationHandler extends TestCase {
038
039 private SAXBuilder jdomBuilder = new SAXBuilder();
040
041 private DOMOutputter jdom2dom = new DOMOutputter();
042
043 private DocumentBuilderFactory jaxpFactory = DocumentBuilderFactory.newInstance();
044
045 /** Construction */
046 public TestCreoleAnnotationHandler(String name) throws GateException {
047 super(name);
048 }
049
050 public void setUp() throws Exception {
051 // Initialise the GATE library and creole register
052 Gate.init();
053
054 XMLUnit.setIgnoreComments(true);
055 XMLUnit.setIgnoreWhitespace(true);
056 XMLUnit.setIgnoreAttributeOrder(true);
057 }
058
059 /**
060 * Take a skeleton creole.xml file, process the annotations on the classes it
061 * mentions and compare the resulting XML to the expected result.
062 */
063 public void testCreoleAnnotationHandler() throws Exception {
064 URL originalUrl = Gate.getUrl("tests/creole-annotation-handler/initial-creole.xml");
065 org.jdom.Document creoleXml =
066 jdomBuilder.build(originalUrl.openStream());
067
068 CreoleAnnotationHandler processor = new CreoleAnnotationHandler(originalUrl);
069 processor.processAnnotations(creoleXml);
070
071 URL expectedURL = Gate.getUrl("tests/creole-annotation-handler/expected-creole.xml");
072
073 // XMLUnit requires the expected and actual results as W3C DOM rather than
074 // JDOM
075 DocumentBuilder docBuilder = jaxpFactory.newDocumentBuilder();
076 org.w3c.dom.Document targetXmlDOM =
077 docBuilder.parse(expectedURL.openStream());
078
079 org.w3c.dom.Document actualXmlDOM = jdom2dom.output(creoleXml);
080
081 Diff diff = XMLUnit.compareXML(targetXmlDOM, actualXmlDOM);
082
083 // compare parameter elements with the same NAME, resources with the same
084 // CLASS, and all other elements that have the same element name
085 diff.overrideElementQualifier(new ElementNameQualifier() {
086 public boolean qualifyForComparison(Element control, Element test) {
087 if("PARAMETER".equals(control.getTagName()) && "PARAMETER".equals(test.getTagName())) {
088 return control.getAttribute("NAME").equals(test.getAttribute("NAME"));
089 }
090 else if("RESOURCE".equals(control.getTagName()) && "RESOURCE".equals(test.getTagName())) {
091 String controlClass = findClass(control);
092 String testClass = findClass(test);
093 return (controlClass == null) ? (testClass == null)
094 : controlClass.equals(testClass);
095 }
096 else {
097 return super.qualifyForComparison(control, test);
098 }
099 }
100
101 private String findClass(Element resource) {
102 Node node = resource.getFirstChild();
103 while(node != null && !"CLASS".equals(node.getNodeName())) {
104 node = node.getNextSibling();
105 }
106
107 if(node != null) {
108 return node.getTextContent();
109 }
110 else {
111 return null;
112 }
113 }
114 });
115
116 // do the comparison!
117 boolean match = diff.similar();
118 if(!match) {
119 // if comparison failed, output the "actual" result document for
120 // debugging purposes
121 new XMLOutputter(Format.getPrettyFormat()).output(creoleXml, System.err);
122 }
123
124 assertTrue("XML produced by annotation handler does not match expected: "
125 + diff, match);
126 }
127
128 /** Test suite routine for the test runner */
129 public static Test suite() {
130 return new TestSuite(TestCreoleAnnotationHandler.class);
131 } // suite
132
133 }
|