KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > html > tokenizer > TagTokenizerTest


1 package com.opensymphony.module.sitemesh.html.tokenizer;
2
3 import com.opensymphony.module.sitemesh.html.Tag;
4 import com.opensymphony.module.sitemesh.html.Text;
5 import junit.framework.TestCase;
6
7 public class TagTokenizerTest extends TestCase {
8
9     private MockTokenHandler handler;
10
11     protected void setUp() throws Exception JavaDoc {
12         super.setUp();
13         handler = new MockTokenHandler();
14     }
15
16     public void testSplitsTagsFromText() {
17         // expectations
18
handler.expectTag(Tag.OPEN, "hello");
19         handler.expectText("cruel");
20         handler.expectTag(Tag.OPEN, "world");
21         handler.expectTag(Tag.OPEN, "and");
22         handler.expectText("some stuff");
23         // execute
24
TagTokenizer tokenizer = new TagTokenizer("<hello>cruel<world><and>some stuff");
25         tokenizer.start(handler);
26         // verify
27
handler.verify();
28     }
29
30     public void testDistinguishesBetweenOpenCloseAndEmptyTags() {
31         // expectations
32
handler.expectTag(Tag.OPEN, "open");
33         handler.expectTag(Tag.CLOSE, "close");
34         handler.expectTag(Tag.EMPTY, "empty");
35         // execute
36
TagTokenizer tokenizer = new TagTokenizer("<open></close><empty/>");
37         tokenizer.start(handler);
38         // verify
39
handler.verify();
40     }
41
42     public void testTreatsCommentsAsText() {
43         // expectations
44
handler.expectText("hello world ");
45         handler.expectText("<!-- how are<we> \n -doing? -->");
46         handler.expectText("good\n bye.");
47         handler.expectTag(Tag.OPEN, "br");
48         // execute
49
TagTokenizer tokenizer = new TagTokenizer("hello world <!-- how are<we> \n -doing? -->good\n bye.<br>");
50         tokenizer.start(handler);
51         // verify
52
handler.verify();
53     }
54
55     public void testExtractsUnquotedAttributesFromTag() {
56         // expectations
57
handler.expectTag(Tag.OPEN, "hello", new String JavaDoc[]{"name", "world", "foo", "boo"});
58         // execute
59
TagTokenizer tokenizer = new TagTokenizer("<hello name=world foo=boo>");
60         tokenizer.start(handler);
61         // verify
62
handler.verify();
63     }
64
65     public void testExtractsQuotedAttributesFromTag() {
66         // expectations
67
handler.expectTag(Tag.OPEN, "hello", new String JavaDoc[]{"name", "the world", "foo", "boo"});
68         // execute
69
TagTokenizer tokenizer = new TagTokenizer("<hello name=\"the world\" foo=\"boo\">");
70         tokenizer.start(handler);
71         // verify
72
handler.verify();
73     }
74
75     public void testHandlesMixedQuoteTypesInAttributes() {
76         // expectations
77
handler.expectTag(Tag.OPEN, "hello", new String JavaDoc[]{"name", "it's good", "foo", "say \"boo\""});
78         // execute
79
TagTokenizer tokenizer = new TagTokenizer("<hello name=\"it's good\" foo=\'say \"boo\"'>");
80         tokenizer.start(handler);
81         // verify
82
handler.verify();
83     }
84
85     public void testHandlesHtmlStyleEmptyAttributes() {
86         // expectations
87
handler.expectTag(Tag.OPEN, "hello", new String JavaDoc[]{"isgood", null, "and", null, "stuff", null});
88         // execute
89
TagTokenizer tokenizer = new TagTokenizer("<hello isgood and stuff>");
90         tokenizer.start(handler);
91         // verify
92
handler.verify();
93     }
94
95     public void testSupportsWhitespaceInElements() {
96         // expectations
97
handler.expectTag(Tag.OPEN, "hello", new String JavaDoc[]{"somestuff", "good", "foo", null, "x", "long\n string"});
98         handler.expectTag(Tag.EMPTY, "empty");
99         handler.expectTag(Tag.OPEN, "HTML", new String JavaDoc[]{"notonnewline", "yo", "newline", "hello", "anotherline", "bye"});
100         // execute
101
TagTokenizer tokenizer = new TagTokenizer(""
102                 + "<hello \n somestuff = \ngood \n foo \nx=\"long\n string\" >"
103                 + "<empty />"
104                 + "<HTML notonnewline=yo newline=\n"
105                 + "hello anotherline=\n"
106                 + "\"bye\">");
107         tokenizer.start(handler);
108         // verify
109
handler.verify();
110
111
112     }
113
114     public void testExposesOriginalTagToHandler() {
115         // Should really use a mock library for this expectation, but I'd rather not
116
// add a new dependency for the sake of a single test.
117
final String JavaDoc originalTag = "<hello \n somestuff = \ngood \n foo \nx=\"long\n string\" >";
118         TagTokenizer tokenizer = new TagTokenizer("some text" + originalTag + "more text");
119         final boolean[] called = {false}; // has to be final array so anonymous inner class can change the value.
120

121         tokenizer.start(new TokenHandler() {
122
123             public boolean shouldProcessTag(String JavaDoc name) {
124                 return true;
125             }
126
127             public void tag(Tag tag) {
128                 assertEquals(originalTag, tag.getContents());
129                 called[0] = true;
130             }
131
132             public void text(Text text) {
133                 // ignoring text for this test
134
}
135
136             public void warning(String JavaDoc message, int line, int column) {
137                 fail("Encountered error " + message);
138             }
139         });
140
141         assertTrue("tag() never called", called[0]);
142     }
143
144     public void testAllowsSlashInUnquotedAttribute() {
145         // expectations
146
handler.expectTag(Tag.OPEN, "something", new String JavaDoc[]{"type", "text/html"});
147         // execute
148
TagTokenizer tokenizer = new TagTokenizer("<something type=text/html>");
149         tokenizer.start(handler);
150         // verify
151
handler.verify();
152     }
153
154     public void testAllowsTrailingQuoteOnAttribute() {
155         // expectations
156
handler.expectTag(Tag.OPEN, "something", new String JavaDoc[]{"type", "bl'ah\""});
157         // execute
158
TagTokenizer tokenizer = new TagTokenizer("<something type=bl'ah\">");
159         tokenizer.start(handler);
160         // verify
161
handler.verify();
162     }
163
164     public void testAllowsAwkwardCharsInElementAndAttribute() {
165         // expectations
166
handler.expectTag(Tag.OPEN, "name:space", new String JavaDoc[]{"foo:bar", "x:y%"});
167         handler.expectTag(Tag.EMPTY, "a_b-c$d", new String JavaDoc[]{"b_b-c$d", "c_b=c$d"});
168         handler.expectTag(Tag.OPEN, "a", new String JavaDoc[]{"href", "/exec/obidos/flex-sign-in/ref=pd_nfy_gw_si/026-2634699-7306802?opt=a&page=misc/login/flex-sign-in-secure.html&response=tg/new-for-you/new-for-you/-/main"});
169         // execute
170
TagTokenizer tokenizer = new TagTokenizer(""
171                 + "<name:space foo:bar=x:y%>"
172                 + "<a_b-c$d b_b-c$d=c_b=c$d />"
173                 + "<a HREF=/exec/obidos/flex-sign-in/ref=pd_nfy_gw_si/026-2634699-7306802?opt=a&page=misc/login/flex-sign-in-secure.html&response=tg/new-for-you/new-for-you/-/main>");
174         tokenizer.start(handler);
175         // verify
176
handler.verify();
177
178     }
179
180     public void testTreatsXmpCdataScriptAndProcessingInstructionsAsText() {
181         // expectations
182
handler.expectText("<script language=jscript> if (a < b & > c)\n alert(); </script>");
183         handler.expectText("<xmp><evil \n<stuff<</xmp>");
184         handler.expectText("<?some stuff ?>");
185         handler.expectText("<![CDATA[ evil<>> <\n ]]>");
186         handler.expectText("<SCRIPT>stuff</SCRIPT>");
187         handler.expectText("<!DOCTYPE html PUBLIC \\\"-//W3C//DTD HTML 4.01 Transitional//EN\\\">");
188         // execute
189
TagTokenizer tokenizer = new TagTokenizer(""
190                 + "<script language=jscript> if (a < b & > c)\n alert(); </script>"
191                 + "<xmp><evil \n<stuff<</xmp>"
192                 + "<?some stuff ?>"
193                 + "<![CDATA[ evil<>> <\n ]]>"
194                 + "<SCRIPT>stuff</SCRIPT>"
195                 + "<!DOCTYPE html PUBLIC \\\"-//W3C//DTD HTML 4.01 Transitional//EN\\\">");
196         tokenizer.start(handler);
197         // verify
198
handler.verify();
199     }
200
201     /* TODO
202     public void testTreatsUnterminatedTagAtEofAsText() {
203         // expectations
204         handler.expectText("hello");
205         handler.expectText("<world");
206         // execute
207         HTMLTagTokenizer tokenizer = new HTMLTagTokenizer("hello<world");
208         tokenizer.start(handler);
209         // verify
210         handler.verify();
211     }
212
213     public void testTreatsUnterminatedAttributeNameAtEofAsText() {
214         // expectations
215         handler.expectText("hello");
216         handler.expectText("<world x");
217         // execute
218         HTMLTagTokenizer tokenizer = new HTMLTagTokenizer("hello<world x");
219         tokenizer.start(handler);
220         // verify
221         handler.verify();
222     }
223
224     public void testTreatsUnterminatedQuotedAttributeValueAtEofAsText() {
225         // expectations
226         handler.expectText("hello");
227         handler.expectText("<world x=\"fff");
228         // execute
229         HTMLTagTokenizer tokenizer = new HTMLTagTokenizer("hello<world x=\"fff");
230         tokenizer.start(handler);
231         // verify
232         handler.verify();
233     }
234
235     public void testTreatsUnterminatedUnquotedAttributeValueAtEofAsText() {
236         // expectations
237         handler.expectText("hello");
238         handler.expectText("<world x=fff");
239         // execute
240         HTMLTagTokenizer tokenizer = new HTMLTagTokenizer("hello<world x=fff");
241         tokenizer.start(handler);
242         // verify
243         handler.verify();
244     }
245     */

246
247     public void testIgnoresEvilMalformedPairOfAngleBrackets() {
248         // expectations
249
handler.expectTag(Tag.OPEN, "good");
250         // execute
251
TagTokenizer tokenizer = new TagTokenizer("<>< ><good><>");
252         tokenizer.start(handler);
253         // verify
254
handler.verify();
255     }
256
257     public void testDoesNotTryToParseTagsUnlessTheHandlerCares() {
258         // setup
259
handler = new MockTokenHandler() {
260             public boolean shouldProcessTag(String JavaDoc name) {
261                 return name.equals("good");
262             }
263         };
264         // expectations
265
handler.expectTag(Tag.OPEN, "good");
266         handler.expectText("<bad>");
267         handler.expectTag(Tag.CLOSE, "good");
268         handler.expectText("<![bad]-->");
269         // execute
270
TagTokenizer tokenizer = new TagTokenizer("<good><bad></good><![bad]-->");
271         tokenizer.start(handler);
272         // verify
273
handler.verify();
274     }
275
276     public void testParsesMagicCommentBlocks() {
277         // expectations
278
handler.expectTag(Tag.OPEN_MAGIC_COMMENT, "if", new String JavaDoc[] {"gte", null, "mso", null, "9", null});
279         handler.expectTag(Tag.OPEN, "stuff");
280         handler.expectTag(Tag.CLOSE_MAGIC_COMMENT, "endif");
281         // execute
282
TagTokenizer tokenizer = new TagTokenizer("<!--[if gte mso 9]><stuff><![endif]-->");
283         tokenizer.start(handler);
284         // verify
285
handler.verify();
286
287     }
288 }
289
290
Popular Tags