KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > TestHtmlUtil


1 package jodd.servlet;
2
3 import junit.framework.TestCase;
4
5 public class TestHtmlUtil extends TestCase {
6
7     public void testGetName() {
8         String tag = "<select name=\"Marie\" >";
9         String s = HtmlUtil.getAttribute(tag, "name");
10         assertEquals("Marie", s);
11
12         tag = "<select NAME='Marie' >";
13         s = HtmlUtil.getAttribute(tag, "name");
14         assertEquals("Marie", s);
15
16         tag = "<select NaME=\"Marie\">";
17         s = HtmlUtil.getAttribute(tag, "name");
18         assertEquals("Marie", s);
19
20         tag = "<select nake='Marie' >";
21         s = HtmlUtil.getAttribute(tag, "name");
22         assertNull(s);
23         tag = "<select nake=\"Marie >";
24         s = HtmlUtil.getAttribute(tag, "name");
25         assertNull(s);
26
27         tag = "<select name=' Marie \\\"the Hot\\\" >";
28         s = HtmlUtil.getAttribute(tag, "name");
29         assertNull(s); // attribute not closed
30
tag += "'>";
31         s = HtmlUtil.getAttribute(tag, "name");
32         assertEquals(" Marie \\\"the Hot\\\" >", s);
33     }
34
35     public void testTagName() {
36         String body = "<html>< TaG atr='val'>";
37         String tag = HtmlUtil.getTagName(body);
38         assertEquals("html", tag);
39
40         tag = HtmlUtil.getTagName(body, 1);
41         assertNull(tag);
42
43         tag = HtmlUtil.getTagName(body, 6);
44         assertEquals("TaG", tag);
45
46         body = "</html></ tag>";
47         tag = HtmlUtil.getTagName(body);
48         assertEquals("/html", tag);
49
50         tag = HtmlUtil.getTagName(body, 7);
51         assertEquals("/tag", tag);
52     }
53
54
55
56     public void testAddValue() {
57         String tag = "<input name=\"xxx\">";
58         String s = HtmlUtil.addAttribute(tag, "value", "qwerty");
59         assertEquals("<input name=\"xxx\" value=\"qwerty\">" , s);
60
61         s = HtmlUtil.addAttribute(tag, "value", "123\"");
62         assertEquals("<input name=\"xxx\" value=\"123&quot;\">", s);
63
64         s = HtmlUtil.addAttribute(tag, "value");
65         assertEquals("<input name=\"xxx\" value>", s);
66
67
68         tag = "<before><input name=\"xxx\">";
69         s = HtmlUtil.addAttribute(tag, "value", "qwerty", 8);
70         assertEquals("<input name=\"xxx\" value=\"qwerty\">" , s);
71
72         s = HtmlUtil.addAttribute(tag, "value", "123\"", 8);
73         assertEquals("<input name=\"xxx\" value=\"123&quot;\">", s);
74
75         s = HtmlUtil.addAttribute(tag, "value", 8);
76         assertEquals("<input name=\"xxx\" value>", s);
77
78     }
79
80 }
81
Popular Tags