KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > markup > TestMarkupWriter


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.markup;
16
17 import java.io.CharArrayWriter JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.hivemind.test.HiveMindTestCase;
22 import org.apache.tapestry.IMarkupWriter;
23
24 /**
25  * Tests for {@link org.apache.tapestry.markup.MarkupWriterImpl}.
26  *
27  * @author Howard M. Lewis Ship
28  * @since 4.0
29  */

30 public class TestMarkupWriter extends HiveMindTestCase
31 {
32     private static CharArrayWriter JavaDoc _writer;
33
34     private static final String JavaDoc NEWLINE = System.getProperty("line.separator");
35
36     private static class EchoMarkupFilter implements MarkupFilter
37     {
38         public void print(PrintWriter JavaDoc writer, char[] data, int offset, int length,
39                 boolean escapeQuotes)
40         {
41             writer.print('{');
42             writer.write(data, offset, length);
43             writer.print('}');
44         }
45     }
46
47     public static class PrintWriterFixture extends PrintWriter JavaDoc
48     {
49
50         public PrintWriterFixture()
51         {
52             super(_writer);
53         }
54     }
55
56     private MarkupFilter newFilter()
57     {
58         return (MarkupFilter) newMock(MarkupFilter.class);
59     }
60
61     private PrintWriter JavaDoc newPrintWriter()
62     {
63         _writer = new CharArrayWriter JavaDoc();
64
65         return new PrintWriter JavaDoc(_writer);
66     }
67
68     protected void tearDown() throws Exception JavaDoc
69     {
70         _writer = null;
71
72         super.tearDown();
73     }
74
75     private void assertOutput(String JavaDoc expected)
76     {
77         assertEquals(expected, _writer.toString());
78
79         _writer.reset();
80     }
81
82     public void testIntAttribute()
83     {
84         MarkupFilter filter = newFilter();
85         PrintWriter JavaDoc writer = newPrintWriter();
86
87         replayControls();
88
89         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
90
91         mw.begin("span");
92
93         assertOutput("<span");
94
95         mw.attribute("width", 5);
96
97         assertOutput(" width=\"5\"");
98
99         verifyControls();
100     }
101
102     public void testIntAttributeRequiresTag()
103     {
104         MarkupFilter filter = newFilter();
105         PrintWriter JavaDoc writer = newPrintWriter();
106
107         replayControls();
108
109         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
110
111         try
112         {
113             mw.attribute("width", 5);
114             unreachable();
115         }
116         catch (IllegalStateException JavaDoc ex)
117         {
118             // Expected.
119
}
120
121         verifyControls();
122     }
123
124     public void testBooleanAttribute()
125     {
126         MarkupFilter filter = newFilter();
127         PrintWriter JavaDoc writer = newPrintWriter();
128
129         replayControls();
130
131         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
132
133         mw.begin("div");
134
135         assertOutput("<div");
136
137         mw.attribute("true", true);
138
139         assertOutput(" true=\"true\"");
140
141         mw.attribute("false", false);
142
143         assertOutput(" false=\"false\"");
144
145         verifyControls();
146     }
147
148     public void testBooleanAttributeRequiresTag()
149     {
150         MarkupFilter filter = newFilter();
151         PrintWriter JavaDoc writer = newPrintWriter();
152
153         replayControls();
154
155         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
156
157         try
158         {
159             mw.attribute("true", true);
160             unreachable();
161         }
162         catch (IllegalStateException JavaDoc ex)
163         {
164             // Expected.
165
}
166
167         verifyControls();
168     }
169
170     public void testAttribute()
171     {
172         MarkupFilter filter = new EchoMarkupFilter();
173         PrintWriter JavaDoc writer = newPrintWriter();
174
175         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
176
177         mw.begin("span");
178         mw.attribute("width", "100%");
179
180         // Braces added by EchoMarkupFilter, to prove its there.
181

182         assertOutput("<span width=\"{100%}\"");
183     }
184
185     public void testAttributeNull()
186     {
187         MarkupFilter filter = newFilter();
188         PrintWriter JavaDoc writer = newPrintWriter();
189
190         replayControls();
191
192         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
193
194         mw.begin("span");
195         mw.attribute("width", null);
196
197         // Braces added by EchoMarkupFilter, to prove its there.
198

199         assertOutput("<span width=\"\"");
200
201         verifyControls();
202     }
203
204     public void testAttributeRequiresTag()
205     {
206         MarkupFilter filter = newFilter();
207         PrintWriter JavaDoc writer = newPrintWriter();
208
209         replayControls();
210
211         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
212
213         try
214         {
215             mw.attribute("attribute", "value");
216             unreachable();
217         }
218         catch (IllegalStateException JavaDoc ex)
219         {
220             // Expected.
221
}
222
223         verifyControls();
224     }
225
226     public void testEnd()
227     {
228         MarkupFilter filter = new EchoMarkupFilter();
229         PrintWriter JavaDoc writer = newPrintWriter();
230
231         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
232
233         mw.begin("div");
234         mw.attribute("width", "100%");
235         mw.end();
236
237         assertOutput("<div width=\"{100%}\"></div>");
238     }
239
240     public void testPrint()
241     {
242         MarkupFilter filter = new EchoMarkupFilter();
243         PrintWriter JavaDoc writer = newPrintWriter();
244
245         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
246
247         mw.print("Hello");
248
249         assertOutput("{Hello}");
250     }
251
252     public void testPrintWithRawFlag()
253     {
254         MarkupFilter filter = new EchoMarkupFilter();
255         PrintWriter JavaDoc writer = newPrintWriter();
256
257         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
258
259         mw.print("Filtered", false);
260
261         assertOutput("{Filtered}");
262
263         mw.print("Raw", true);
264
265         assertOutput("Raw");
266     }
267
268     public void testPrintClosesCurrentTag()
269     {
270         MarkupFilter filter = new EchoMarkupFilter();
271         PrintWriter JavaDoc writer = newPrintWriter();
272
273         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
274
275         mw.begin("div");
276         mw.print("Hello");
277         mw.end();
278
279         assertOutput("<div>{Hello}</div>");
280     }
281
282     public void testPrintNull()
283     {
284         MarkupFilter filter = newFilter();
285         PrintWriter JavaDoc writer = newPrintWriter();
286
287         replayControls();
288
289         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
290
291         mw.begin("span");
292         mw.print(null);
293         mw.end();
294
295         assertOutput("<span></span>");
296
297         verifyControls();
298     }
299
300     public void testCloseTag()
301     {
302         MarkupFilter filter = newFilter();
303         PrintWriter JavaDoc writer = newPrintWriter();
304
305         replayControls();
306
307         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
308
309         mw.begin("div");
310         mw.closeTag();
311
312         assertOutput("<div>");
313
314         mw.beginEmpty("img");
315         mw.closeTag();
316
317         assertOutput("<img/>");
318
319         verifyControls();
320     }
321
322     public void testNestedEnd()
323     {
324         MarkupFilter filter = newFilter();
325         PrintWriter JavaDoc writer = newPrintWriter();
326
327         replayControls();
328
329         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
330
331         mw.begin("h1");
332         mw.begin("b");
333         mw.beginEmpty("img");
334         mw.begin("span");
335         mw.closeTag();
336
337         assertOutput("<h1><b><img/><span>");
338
339         mw.end();
340
341         assertOutput("</span>");
342
343         mw.end();
344
345         // Note: skipping the empty <img> tag
346

347         assertOutput("</b>");
348
349         mw.end();
350
351         assertOutput("</h1>");
352
353         verifyControls();
354     }
355
356     public void testEndNamed()
357     {
358         MarkupFilter filter = newFilter();
359         PrintWriter JavaDoc writer = newPrintWriter();
360
361         replayControls();
362
363         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
364
365         mw.begin("h1");
366         mw.begin("b");
367         mw.beginEmpty("img");
368         mw.begin("span");
369         mw.closeTag();
370
371         assertOutput("<h1><b><img/><span>");
372
373         // Uses the stack to close elements.
374

375         mw.end("h1");
376
377         assertOutput("</span></b></h1>");
378
379         verifyControls();
380     }
381
382     public void testClose()
383     {
384         MarkupFilter filter = new EchoMarkupFilter();
385         PrintWriter JavaDoc writer = newPrintWriter();
386
387         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
388
389         mw.begin("span");
390         mw.begin("div");
391         mw.print("text");
392
393         assertOutput("<span><div>{text}");
394
395         mw.close();
396
397         assertOutput("</div></span>");
398
399         assertEquals(false, writer.checkError());
400
401         writer.println();
402
403         assertEquals(true, writer.checkError());
404     }
405
406     public void testComment()
407     {
408         MarkupFilter filter = newFilter();
409         PrintWriter JavaDoc writer = newPrintWriter();
410
411         replayControls();
412
413         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
414
415         mw.comment("Tapestry Rocks!");
416
417         // Note: not filtered
418

419         assertOutput("<!-- Tapestry Rocks! -->" + NEWLINE);
420
421         verifyControls();
422     }
423
424     public void testCommentClosesTag()
425     {
426         MarkupFilter filter = newFilter();
427         PrintWriter JavaDoc writer = newPrintWriter();
428
429         replayControls();
430
431         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
432
433         mw.begin("div");
434         mw.comment("Tapestry Rocks!");
435
436         // Note: not filtered
437

438         assertOutput("<div><!-- Tapestry Rocks! -->" + NEWLINE);
439
440         verifyControls();
441     }
442
443     /*
444      * Seems to cause problems with JDK 1.5
445      *
446      *
447      *
448     public void testFlush()
449     {
450         _writer = new CharArrayWriter();
451
452         MarkupFilter filter = newFilter();
453         PrintWriter writer = (PrintWriter) newMock(PrintWriterFixture.class);
454
455         writer.flush();
456
457         replayControls();
458
459         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
460
461         mw.flush();
462
463         verifyControls();
464     }
465     
466     */

467
468     public void testPrintCharArray()
469     {
470         MarkupFilter filter = new EchoMarkupFilter();
471         PrintWriter JavaDoc writer = newPrintWriter();
472
473         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
474
475         mw.print(new char[]
476         { 'A', 'j', 'a', 'x', 'i', 'a', 'n' }, 1, 3);
477
478         assertOutput("{jax}");
479     }
480
481     public void testPrintChar()
482     {
483         MarkupFilter filter = new EchoMarkupFilter();
484         PrintWriter JavaDoc writer = newPrintWriter();
485
486         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
487
488         mw.print('x');
489
490         assertOutput("{x}");
491     }
492
493     public void testPrintInt()
494     {
495         MarkupFilter filter = new EchoMarkupFilter();
496         PrintWriter JavaDoc writer = newPrintWriter();
497
498         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
499
500         mw.print(123);
501
502         assertOutput("123");
503     }
504
505     public void testPrintIntClosesTag()
506     {
507         MarkupFilter filter = new EchoMarkupFilter();
508         PrintWriter JavaDoc writer = newPrintWriter();
509
510         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
511
512         mw.begin("span");
513
514         assertOutput("<span");
515
516         mw.print(123);
517
518         assertOutput(">123");
519     }
520
521     public void testPrintLn()
522     {
523         MarkupFilter filter = newFilter();
524         PrintWriter JavaDoc writer = newPrintWriter();
525
526         replayControls();
527
528         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
529
530         mw.println();
531
532         assertOutput(NEWLINE);
533
534         verifyControls();
535     }
536
537     public void testPrintLnClosesTag()
538     {
539         MarkupFilter filter = newFilter();
540         PrintWriter JavaDoc writer = newPrintWriter();
541
542         replayControls();
543
544         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
545
546         mw.begin("p");
547
548         assertOutput("<p");
549
550         mw.println();
551
552         assertOutput(">" + NEWLINE);
553
554         verifyControls();
555     }
556
557     public void testPrintRawCharArray()
558     {
559         MarkupFilter filter = newFilter();
560         PrintWriter JavaDoc writer = newPrintWriter();
561
562         replayControls();
563
564         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
565
566         mw.begin("p");
567
568         assertOutput("<p");
569
570         mw.printRaw(new char[]
571         { 'a', 'b', 'c', 'd' }, 1, 2);
572
573         assertOutput(">bc");
574
575         verifyControls();
576     }
577
578     public void testPrintRawString()
579     {
580         MarkupFilter filter = newFilter();
581         PrintWriter JavaDoc writer = newPrintWriter();
582
583         replayControls();
584
585         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
586
587         mw.begin("p");
588
589         assertOutput("<p");
590
591         mw.print("Fred", true);
592
593         assertOutput(">Fred");
594
595         verifyControls();
596     }
597
598     public void testNestedWriter()
599     {
600         MarkupFilter filter = new EchoMarkupFilter();
601         PrintWriter JavaDoc writer = newPrintWriter();
602
603         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
604
605         mw.begin("div");
606
607         IMarkupWriter nested = mw.getNestedWriter();
608
609         assertEquals("text/html", nested.getContentType());
610
611         nested.begin("span");
612         nested.attribute("class", "inner");
613         nested.print("nested content");
614
615         mw.attribute("class", "outer");
616
617         assertOutput("<div class=\"{outer}\"");
618
619         nested.close();
620
621         // Close the <div>, then comes the inner/nested content.
622

623         assertOutput("><span class=\"{inner}\">{nested content}</span>");
624
625         mw.print("after content");
626
627         assertOutput("{after content}");
628
629         mw.end();
630
631         assertOutput("</div>");
632     }
633
634     public void testRepeatCloseOnNestedWriter()
635     {
636         MarkupFilter filter = new EchoMarkupFilter();
637         PrintWriter JavaDoc writer = newPrintWriter();
638
639         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
640
641         IMarkupWriter nested = mw.getNestedWriter();
642
643         nested.close();
644
645         try
646         {
647             nested.close();
648         }
649         catch (IllegalStateException JavaDoc ex)
650         {
651             // Expected.
652
}
653     }
654
655     public void testEndNamedNotOnStack()
656     {
657         MarkupFilter filter = new EchoMarkupFilter();
658         PrintWriter JavaDoc writer = newPrintWriter();
659
660         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
661
662         mw.begin("div");
663         mw.begin("span");
664         mw.begin("b");
665
666         try
667         {
668             mw.end("table");
669             unreachable();
670         }
671         catch (ApplicationRuntimeException ex)
672         {
673             assertEquals(
674                     "Can not close to element 'table', because no such element is on the active elements stack (div, span, b).",
675                     ex.getMessage());
676         }
677     }
678
679     public void testEndWithEmptyStack()
680     {
681         MarkupFilter filter = new EchoMarkupFilter();
682         PrintWriter JavaDoc writer = newPrintWriter();
683
684         IMarkupWriter mw = new MarkupWriterImpl("text/html", writer, filter);
685
686         try
687         {
688             mw.end();
689             unreachable();
690         }
691         catch (ApplicationRuntimeException ex)
692         {
693             assertEquals(
694                     "Can not end most recent element because the stack of active elements is empty.",
695                     ex.getMessage());
696         }
697     }
698 }
Popular Tags