Source Code Cross Referenced for HTMLPageParserTest.java in  » Web-Framework » SiteMesh » com » opensymphony » module » sitemesh » parser » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » SiteMesh » com.opensymphony.module.sitemesh.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.