1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts.taglib.html;
22
23 import java.util.Locale;
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.framework.TestCase;
27 import org.apache.struts.mock.MockHttpServletRequest;
28 import org.apache.struts.mock.MockHttpServletResponse;
29 import org.apache.struts.mock.MockPageContext;
30 import org.apache.struts.mock.MockServletConfig;
31
32
33
34
35 public class TestHtmlTag extends TestCase {
36
37 private MockServletConfig config;
38 private MockHttpServletRequest request;
39 private MockHttpServletResponse response;
40 private MockPageContext pageContext;
41 private HtmlTag htmlTag;
42
43
44
45
46
47
48 public TestHtmlTag(String theName) {
49 super(theName);
50 }
51
52
53
54
55
56
57 public static void main(String[] theArgs) {
58 junit.awtui.TestRunner.main(new String[] { TestHtmlTag.class.getName() });
59 }
60
61
62
63
64
65 public static Test suite() {
66
67 return new TestSuite(TestHtmlTag.class);
68 }
69
70
71
72
73 public void setUp() {
74 config = new MockServletConfig();
75 request = new MockHttpServletRequest();
76 response = new MockHttpServletResponse();
77 pageContext = new MockPageContext(config, request, response);
78 htmlTag = new HtmlTag();
79 htmlTag.setPageContext(pageContext);
80 }
81
82
83
84
85 public void testValidLangTrue() {
86
87
88 htmlTag.setLang(true);
89
90
91 request.setLocale(Locale.US);
92 assertEquals("render en_US", "<html lang=\"en-US\">", htmlTag.renderHtmlStartElement());
93
94
95 request.setLocale(Locale.ENGLISH);
96 assertEquals("render en", "<html lang=\"en\">", htmlTag.renderHtmlStartElement());
97
98
99 request.setLocale(new Locale("abcd-efghijklmnopqrstuvwxyz", "ABCDEFGHIJKLM-NOPQRSTUVWXYZ", ""));
100 assertEquals("valid characters", "<html lang=\"abcd-efghijklmnopqrstuvwxyz-ABCDEFGHIJKLM-NOPQRSTUVWXYZ\">", htmlTag.renderHtmlStartElement());
101
102 }
103
104
105
106
107 public void testValidLangFalse() {
108
109
110 htmlTag.setLang(false);
111
112
113 request.setLocale(Locale.US);
114 assertEquals("ignore en_US", "<html>", htmlTag.renderHtmlStartElement());
115
116
117 request.setLocale(Locale.ENGLISH);
118 assertEquals("ignore en", "<html>", htmlTag.renderHtmlStartElement());
119
120 }
121
122
123
124
125 public void testInvalidLanguage() {
126
127
128 htmlTag.setLang(true);
129
130
131 request.setLocale(Locale.US);
132 assertEquals("check valid", "<html lang=\"en-US\">", htmlTag.renderHtmlStartElement());
133
134
135 request.setLocale(new Locale("/><script>alert()</script>", "", ""));
136 assertEquals("invalid <script>", "<html>", htmlTag.renderHtmlStartElement());
137
138
139 request.setLocale(new Locale("abc<def", "", ""));
140 assertEquals("invalid LT", "<html>", htmlTag.renderHtmlStartElement());
141
142
143 request.setLocale(new Locale("abc>def", "", ""));
144 assertEquals("invalid GT", "<html>", htmlTag.renderHtmlStartElement());
145
146
147 request.setLocale(new Locale("abc/def", "", ""));
148 assertEquals("invalid SLASH", "<html>", htmlTag.renderHtmlStartElement());
149
150
151 request.setLocale(new Locale("abc&def", "", ""));
152 assertEquals("invalid AMP", "<html>", htmlTag.renderHtmlStartElement());
153
154 }
155
156
157
158
159 public void testInvalidCountry() {
160
161
162 htmlTag.setLang(true);
163
164
165 request.setLocale(Locale.US);
166 assertEquals("check valid", "<html lang=\"en-US\">", htmlTag.renderHtmlStartElement());
167
168
169 request.setLocale(new Locale("en", "/><script>alert()</script>", ""));
170 assertEquals("invalid <script>", "<html lang=\"en\">", htmlTag.renderHtmlStartElement());
171
172
173 request.setLocale(new Locale("en", "abc<def", ""));
174 assertEquals("invalid LT", "<html lang=\"en\">", htmlTag.renderHtmlStartElement());
175
176
177 request.setLocale(new Locale("en", "abc>def", ""));
178 assertEquals("invalid GT", "<html lang=\"en\">", htmlTag.renderHtmlStartElement());
179
180
181 request.setLocale(new Locale("en", "abc/def", ""));
182 assertEquals("invalid SLASH", "<html lang=\"en\">", htmlTag.renderHtmlStartElement());
183
184
185 request.setLocale(new Locale("en", "abc&def", ""));
186 assertEquals("invalid AMP", "<html lang=\"en\">", htmlTag.renderHtmlStartElement());
187
188 }
189 }