1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.util.html;
23
24 import junit.framework.TestCase;
25
26
27
28
29
30
31
32 public class TestHTML extends TestCase {
33
34 private static final String[] m_strings = {
35 "The difference between me and you",
36 "is that I'm not on fire.",
37 "",
38 " \n \t",
39 };
40
41 public void testHTMLText() throws Exception {
42 for (int i = 0; i < m_strings.length; ++i) {
43 final HTMLText text = new HTMLText(m_strings[i]);
44 assertEquals(m_strings[i], text.toText());
45 assertEquals(m_strings[i], text.toHTML());
46 assertEquals(m_strings[i], text.toString());
47 }
48 }
49
50 public void testHTMLElement() throws Exception {
51 final HTMLElement parent = new HTMLElement("parent");
52 assertEquals("<parent/>", parent.toHTML());
53 assertEquals("", parent.toText());
54
55 final HTMLElement child = parent.addElement("child");
56 child.addElement("grandChild");
57 parent.addElement("child2");
58 child.addText("Some text");
59 assertEquals(
60 "<parent><child><grandChild/>Some text</child><child2/></parent>",
61 parent.toHTML());
62 assertEquals("Some text", parent.toText());
63
64 final HTMLElement nullElement = new HTMLElement();
65 assertEquals("", nullElement.toHTML());
66 nullElement.addElement("foo");
67 assertEquals("<foo/>", nullElement.toHTML());
68
69 final HTMLElement anotherElement = new HTMLElement("body");
70 anotherElement.addElement("P").addText("text");
71 anotherElement.addElement("code").addText("text2");
72 anotherElement.addElement("br");
73 anotherElement.addText("text3");
74
75 assertEquals("<body><P>text</P><code>text2</code><br/>text3</body>",
76 anotherElement.toHTML());
77 assertEquals("text\ntext2\ntext3", anotherElement.toText());
78 }
79
80 public void testHTMLDocument() throws Exception {
81 final HTMLDocument document = new HTMLDocument();
82 document.getHead().addElement("title").addText("Test");
83 document.getBody().addText("The body");
84
85 assertEquals("<html><head><title>Test</title></head>" +
86 "<body>The body</body></html>",
87 document.toHTML());
88 }
89 }