001 /*
002 * YamParseTreeTests.java
003 * Copyright (c) 1998-2008, The University of Sheffield.
004 *
005 * This code is from the GATE project (http://gate.ac.uk/) and is free
006 * software licenced under the GNU General Public License version 3. It is
007 * distributed without any warranty. For more details see COPYING.txt in the
008 * top level directory (or at http://gatewiki.sf.net/COPYING.txt).
009 *
010 * Hamish Cunningham, 12th May 2006
011 */
012
013 package gate.yam.parse;
014
015 import java.io.*;
016 import junit.framework.*;
017 import org.apache.log4j.Logger;
018 import gate.util.*;
019 import gate.yam.*;
020 import gate.yam.*;
021
022
023 /**
024 * Unit test for YAM parse trees.
025 */
026 public class YamParseTreeTests extends TestCase
027 {
028
029 /** Create the test case */
030 public YamParseTreeTests(String testName) {
031 super(testName);
032 }
033
034 /** Logger */
035 static Logger lgr = Logger.getLogger("gate.yam");
036
037 /** SInS (plain) logger */
038 static Logger slgr = Logger.getLogger("gate.sins");
039
040 /**
041 * Test setup and reset of trees.
042 */
043 public void testTreeStuff() throws Exception
044 {
045 // path, suffixes
046 String path = "/gate/yam/resources/yam-minimal.yam";
047
048 // report
049 lgr.info("testing tree setup and reset");
050
051 // a reader for the test file
052 InputStream testFileStream =
053 this.getClass().getResourceAsStream(path);
054 if(testFileStream == null)
055 fail("couldn't get resource " + path);
056 BufferedReader testReader =
057 new BufferedReader(new InputStreamReader(testFileStream, "UTF-8"));
058
059 // find the source directory
060 String testFilePath = this.getClass().getResource(path).getPath();
061 File testFileDir = new File(testFilePath).getParentFile();
062
063 // a writer for the output
064 StringWriter responseWriter = new StringWriter();
065
066 // run the translator and get the response
067 YamCommand yam = new YamCommand();
068 YamParseTree tree = yam.translate(
069 testReader, responseWriter, YamFile.FileType.HTML, testFileDir
070 );
071
072 // check that the tree is non-empty
073 SimpleNode n = tree.getRootNode();
074 if(treeEmpty(n)) fail("parse tree empty");
075
076 // check that there are the right number of links / includes
077 int links = tree.getLinks().size();
078 int includes = tree.getIncludes().size();
079 assertTrue("wrong number of links: " + links, links == 2);
080 assertTrue("wrong number of includes: " + includes, includes == 1);
081
082 // reset, and check that the tree is empty
083 tree.reset();
084 if(! treeEmpty(n)) fail("parse tree not empty");
085 } // testTreeStuff()
086
087
088 /**
089 * Test tree merging.
090 */
091 public void testTreeMerging() throws Exception
092 {
093 // path, suffixes
094 String path = "/gate/yam/resources/yam-minimal.yam";
095
096 // report
097 lgr.info("testing tree merging");
098
099 // readers for the test file
100 InputStream testFileStream1 =
101 this.getClass().getResourceAsStream(path);
102 if(testFileStream1 == null)
103 fail("couldn't get resource " + path);
104 BufferedReader testReader1 =
105 new BufferedReader(new InputStreamReader(testFileStream1, "UTF-8"));
106 InputStream testFileStream2 =
107 this.getClass().getResourceAsStream(path);
108 if(testFileStream2 == null)
109 fail("couldn't get resource " + path);
110 BufferedReader testReader2 =
111 new BufferedReader(new InputStreamReader(testFileStream2, "UTF-8"));
112
113 // find the source directory
114 String testFilePath = this.getClass().getResource(path).getPath();
115 File testFileDir = new File(testFilePath).getParentFile();
116
117 // writers for the output
118 StringWriter responseWriter1 = new StringWriter();
119 StringWriter responseWriter2 = new StringWriter();
120
121 // run two translators
122 YamCommand yam1 = new YamCommand();
123 YamParseTree tree1 = yam1.translate(
124 testReader1, responseWriter1, YamFile.FileType.HTML, testFileDir
125 );
126 YamCommand yam2 = new YamCommand();
127 YamParseTree tree2 = yam2.translate(
128 testReader2, responseWriter2, YamFile.FileType.HTML, testFileDir
129 );
130
131 // get stats for each tree and check they're the same
132 boolean gotTitle1 = (tree1.getTitleNode() != null);
133 boolean gotTitle2 = (tree2.getTitleNode() != null);
134 assertTrue("gotTitle flags disagree", gotTitle1 == gotTitle2);
135 int numHeaders1 = tree1.getHeadingNodes().size();
136 int numHeaders2 = tree2.getHeadingNodes().size();
137 assertTrue("num headers disagree", numHeaders1 == numHeaders2);
138 int numNodes1 = tree1.getNumNodes(tree1.getRootNode());
139 int numNodes2 = tree2.getNumNodes(tree2.getRootNode());
140 assertTrue("num nodes disagree", numNodes1 == numNodes2);
141 //lgr.info("headings = " + numHeaders1);
142 //lgr.info("nodes = " + numNodes1);
143
144 // merge the trees and check that their stats are combined
145 tree1.merge(tree2);
146 boolean gotTitleMerged = (tree1.getTitleNode() != null);
147 int numHeadersMerged = tree1.getHeadingNodes().size();
148 int numNodesMerged = tree1.getNumNodes(tree1.getRootNode());
149 //lgr.info("merged headings = " + numHeadersMerged);
150 //lgr.info("merged nodes = " + numNodesMerged);
151 assertTrue("seem to have mislaid merged title", gotTitleMerged);
152 assertTrue( // headings = double each
153 "wrong num merged headings", numHeadersMerged == (numHeaders1 * 2)
154 );
155 assertTrue( // double, minus one for the root
156 "wrong num merged nodes", numNodesMerged == ((numNodes1 * 2) - 1)
157 );
158
159 } // testTreeMerging()
160
161
162 /** Helper to check tree contents. */
163 public boolean treeEmpty(SimpleNode n) {
164 if(! (
165 n.getStart() == null &&
166 n.getEnd() == null &&
167 n.getBody() == null
168 )
169 ) {
170 //lgr.info("non-empty node: " + n.toString());
171 //lgr.info(
172 // "n.getStart/End/Body: " + n.getStart() + n.getEnd() + n.getBody()
173 //);
174 return false;
175 }
176
177 int i = n.jjtGetNumChildren();
178 if(i == 0) return true;
179 for(int j = 0; j < i; j++)
180 if(! treeEmpty((SimpleNode) n.jjtGetChild(j)))
181 return false;
182
183 return true;
184 } // treeEmpty(SimpleNode)
185
186 } // YamParseTreeTests
|