1 package org.apache.maven.doxia.module.xhtml;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileFilter;
24 import java.util.Iterator;
25 import java.util.regex.Pattern;
26
27 import org.apache.maven.doxia.parser.AbstractParserTest;
28 import org.apache.maven.doxia.parser.Parser;
29 import org.apache.maven.doxia.sink.impl.SinkEventElement;
30 import org.apache.maven.doxia.sink.impl.SinkEventTestingSink;
31
32
33
34
35 public class XhtmlParserTest
36 extends AbstractParserTest
37 {
38 private XhtmlParser parser;
39
40
41 protected void setUp()
42 throws Exception
43 {
44 super.setUp();
45
46 parser = (XhtmlParser) lookup( Parser.class, "xhtml" );
47
48
49
50 String tmpDir = System.getProperty( "java.io.tmpdir" );
51
52
53 File[] tmpFiles = new File( tmpDir ).listFiles( new FileFilter()
54 {
55 Pattern xsdPatterns = Pattern.compile( "(xhtml-lat1.ent|xhtml1-transitional.dtd|xhtml-special.ent|xhtml-symbol.ent)" );
56
57 @Override
58 public boolean accept( File pathname )
59 {
60 return xsdPatterns.matcher( pathname.getName() ).matches();
61 }
62 } );
63
64 for ( File tmpFile : tmpFiles )
65 {
66 tmpFile.delete();
67 }
68
69 }
70
71
72 protected Parser createParser()
73 {
74 return parser;
75 }
76
77
78 protected String outputExtension()
79 {
80 return "xhtml";
81 }
82
83
84 public void testDocumentBodyEventsList()
85 throws Exception
86 {
87 String text = "<html><body></body></html>";
88
89 SinkEventTestingSink sink = new SinkEventTestingSink();
90
91 ( (XhtmlParser) createParser() ).parse( text, sink );
92
93 Iterator<SinkEventElement> it = sink.getEventList().iterator();
94
95 assertEquals( "body", it.next().getName() );
96 assertEquals( "body_", it.next().getName() );
97 assertFalse( it.hasNext() );
98 }
99
100
101 public void testHeadEventsList()
102 throws Exception
103 {
104 String text = "<head><title>Title</title><meta name=\"author\" content=\"Author\" />"
105 + "<meta name=\"date\" content=\"Date\" /><meta name=\"security\" content=\"low\"/></head>";
106
107 SinkEventTestingSink sink = new SinkEventTestingSink();
108
109 ( (XhtmlParser) createParser() ).parse( text, sink );
110
111 Iterator<SinkEventElement> it = sink.getEventList().iterator();
112
113 assertEquals( "head", it.next().getName() );
114 assertEquals( "title", it.next().getName() );
115 assertEquals( "text", it.next().getName() );
116 assertEquals( "title_", it.next().getName() );
117 assertEquals( "author", it.next().getName() );
118 assertEquals( "text", it.next().getName() );
119 assertEquals( "author_", it.next().getName() );
120 assertEquals( "date", it.next().getName() );
121 assertEquals( "text", it.next().getName() );
122 assertEquals( "date_", it.next().getName() );
123 assertEquals( "unknown", it.next().getName() );
124 assertEquals( "head_", it.next().getName() );
125 assertFalse( it.hasNext() );
126 }
127
128
129 public void testPreEventsList()
130 throws Exception
131 {
132 String text = "<pre></pre>";
133
134 SinkEventTestingSink sink = new SinkEventTestingSink();
135
136 ( (XhtmlParser) createParser() ).parse( text, sink );
137
138 Iterator<SinkEventElement> it = sink.getEventList().iterator();
139
140 assertEquals( "verbatim", it.next().getName() );
141 assertEquals( "verbatim_", it.next().getName() );
142 assertFalse( it.hasNext() );
143 }
144
145
146
147
148
149
150 public void testUnknown()
151 throws Exception
152 {
153 String text = "<applet><param name=\"name\" value=\"value\"/><unknown/></applet>";
154
155 SinkEventTestingSink sink = new SinkEventTestingSink();
156
157 ( (XhtmlParser) createParser() ).parse( text, sink );
158
159 Iterator<SinkEventElement> it = sink.getEventList().iterator();
160 assertEquals( "unknown", it.next().getName() );
161 assertEquals( "unknown", it.next().getName() );
162 assertEquals( "unknown", it.next().getName() );
163 assertEquals( "unknown", it.next().getName() );
164 assertFalse( it.hasNext() );
165 }
166
167
168 public void testTocMacro()
169 throws Exception
170 {
171 String text = "<html><body>" +
172 "<!-- MACRO{toc|fromDepth=1|toDepth=2} -->" +
173 "<h1>heading 1</h1><h2>heading 2</h2><h3>heading 3</h3>" +
174 "</body></html>";
175
176 SinkEventTestingSink sink = new SinkEventTestingSink();
177
178 ( (XhtmlParser) createParser() ).parse( text, sink );
179
180 Iterator<SinkEventElement> it = sink.getEventList().iterator();
181
182 assertEquals( "body", it.next().getName() );
183 assertEquals( "list", it.next().getName() );
184 assertEquals( "listItem", it.next().getName() );
185 assertEquals( "link", it.next().getName() );
186 assertEquals( "text", it.next().getName() );
187 assertEquals( "link_", it.next().getName() );
188 assertEquals( "list", it.next().getName() );
189 assertEquals( "listItem", it.next().getName() );
190 assertEquals( "link", it.next().getName() );
191 assertEquals( "text", it.next().getName() );
192 assertEquals( "link_", it.next().getName() );
193 assertEquals( "listItem_", it.next().getName() );
194 assertEquals( "list_", it.next().getName() );
195 assertEquals( "listItem_", it.next().getName() );
196 assertEquals( "list_", it.next().getName() );
197 }
198 }