001: package com.opensymphony.module.sitemesh.parser;
002:
003: import com.opensymphony.module.sitemesh.HTMLPage;
004: import com.opensymphony.module.sitemesh.PageParser;
005:
006: import java.io.ByteArrayInputStream;
007: import java.io.File;
008: import java.io.FileReader;
009: import java.io.FilenameFilter;
010: import java.io.IOException;
011: import java.io.LineNumberReader;
012: import java.io.Reader;
013: import java.io.StringWriter;
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.List;
017: import java.util.Map;
018: import java.util.Properties;
019:
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.framework.Test;
023:
024: /**
025: * Test case for HTMLPageParser implementations. See parser-tests/readme.txt.
026: *
027: * @author Joe Walnes
028: */
029: public class HTMLPageParserTest extends TestCase {
030:
031: /**
032: * This test case builds a custom suite, containing a collection of smaller suites (one for each file in src/parser-tests).
033: */
034: public static Test suite() throws IOException {
035: TestSuite result = new TestSuite(HTMLPageParserTest.class
036: .getName());
037:
038: File[] files = listParserTests(new File("src/parser-tests"));
039: PageParser[] parsers = new PageParser[] { /*new FastPageParser(),*/new HTMLPageParser() };
040:
041: for (int i = 0; i < parsers.length; i++) {
042: PageParser parser = parsers[i];
043: String name = parser.getClass().getName();
044: TestSuite suiteForParser = new TestSuite(name);
045: for (int j = 0; j < files.length; j++) {
046: File file = files[j];
047: TestSuite suiteForFile = new TestSuite(file.getName()
048: .replace('.', '_'));
049: suiteForFile.addTest(new HTMLPageParserTest(parser,
050: file, "testTitle"));
051: suiteForFile.addTest(new HTMLPageParserTest(parser,
052: file, "testBody"));
053: suiteForFile.addTest(new HTMLPageParserTest(parser,
054: file, "testHead"));
055: suiteForFile.addTest(new HTMLPageParserTest(parser,
056: file, "testFullPage"));
057: suiteForFile.addTest(new HTMLPageParserTest(parser,
058: file, "testProperties"));
059: suiteForParser.addTest(suiteForFile);
060: }
061: result.addTest(suiteForParser);
062: }
063:
064: return result;
065: }
066:
067: private HTMLPage page;
068: private Map blocks;
069: private String encoding;
070: private final PageParser parser;
071: private File file;
072:
073: public HTMLPageParserTest(PageParser parser, File inputFile,
074: String test) {
075: super (test);
076: this .parser = parser;
077: file = inputFile;
078: encoding = "UTF8";
079: }
080:
081: protected void setUp() throws Exception {
082: super .setUp();
083: // read blocks from input file.
084: this .blocks = readBlocks(new FileReader(file));
085: // create PageParser and parse input block into HTMLPage object.
086: String input = (String) blocks.get("INPUT");
087: this .page = (HTMLPage) parser.parse(input.toCharArray());
088: }
089:
090: public void testTitle() throws Exception {
091: assertBlock("TITLE", page.getTitle());
092: }
093:
094: public void testBody() throws Exception {
095: StringWriter body = new StringWriter();
096: page.writeBody(body);
097: body.flush();
098: assertBlock("BODY", body.toString());
099: }
100:
101: public void testHead() throws Exception {
102: StringWriter head = new StringWriter();
103: page.writeHead(head);
104: head.flush();
105: assertBlock("HEAD", head.toString());
106: }
107:
108: public void testFullPage() throws Exception {
109: StringWriter fullPage = new StringWriter();
110: page.writePage(fullPage);
111: fullPage.flush();
112: assertBlock("INPUT", fullPage.toString());
113: }
114:
115: public void testProperties() throws Exception {
116: Properties props = new Properties();
117: String propsString = (String) blocks.get("PROPERTIES");
118: ByteArrayInputStream input = new ByteArrayInputStream(
119: propsString.trim().getBytes(encoding));
120: props.load(input);
121:
122: String[] pageKeys = page.getPropertyKeys();
123: assertEquals(file.getName()
124: + " : Unexpected number of page properties ["
125: + join(pageKeys) + "]", props.size(), pageKeys.length);
126:
127: for (int i = 0; i < pageKeys.length; i++) {
128: String pageKey = pageKeys[i];
129: String blockValue = props.getProperty(pageKey);
130: String pageValue = page.getProperty(pageKey);
131: assertEquals(file.getName(), blockValue == null ? null
132: : blockValue.trim(), pageValue == null ? null
133: : pageValue.trim());
134: }
135: }
136:
137: private String join(String[] values) {
138: StringBuffer result = new StringBuffer();
139: for (int i = 0; i < values.length; i++) {
140: if (i > 0) {
141: result.append(',');
142: }
143: result.append(values[i]);
144: }
145: return result.toString();
146: }
147:
148: //-------------------------------------------------
149:
150: private static File[] listParserTests(File dir) throws IOException {
151: // get list of files to ignore
152: LineNumberReader ignoreReader = new LineNumberReader(
153: new FileReader(new File(dir, "ignore.txt")));
154: final List ignoreFileNames = new ArrayList();
155: String line;
156: while ((line = ignoreReader.readLine()) != null) {
157: ignoreFileNames.add(line);
158: }
159: return dir.listFiles(new FilenameFilter() {
160: public boolean accept(File currentDir, String name) {
161: return name.startsWith("test")
162: && !ignoreFileNames.contains(name);
163: }
164: });
165: }
166:
167: private void assertBlock(String blockName, String result)
168: throws Exception {
169: String expected = (String) blocks.get(blockName);
170: assertEquals(file.getName() + " : Block did not match",
171: expected.trim(), result.trim());
172: }
173:
174: /**
175: * Read input to test and break down into blocks. See parser-tests/readme.txt
176: */
177: private Map readBlocks(Reader input) throws IOException {
178: Map blocks = new HashMap();
179: LineNumberReader reader = new LineNumberReader(input);
180: String line;
181: String blockName = null;
182: StringBuffer blockContents = null;
183: while ((line = reader.readLine()) != null) {
184: if (line.startsWith("~~~ ") && line.endsWith(" ~~~")) {
185: if (blockName != null) {
186: blocks.put(blockName, blockContents.toString());
187: }
188: blockName = line.substring(4, line.length() - 4);
189: blockContents = new StringBuffer();
190: } else {
191: if (blockName != null) {
192: blockContents.append(line);
193: blockContents.append('\n');
194: }
195: }
196: }
197:
198: if (blockName != null) {
199: blocks.put(blockName, blockContents.toString());
200: }
201:
202: return blocks;
203: }
204:
205: }
|