1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.cubby.tags;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNull;
21 import static org.junit.Assert.assertSame;
22 import static org.junit.Assert.assertTrue;
23 import static org.junit.Assert.fail;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import javax.servlet.jsp.JspContext;
30 import javax.servlet.jsp.PageContext;
31
32 import org.junit.Test;
33 import org.seasar.cubby.CubbyConstants;
34 import org.seasar.cubby.action.ActionErrors;
35 import org.seasar.cubby.controller.FormWrapper;
36
37 public class TagUtilsTest {
38
39 @Test
40 public void addClassName() throws Exception {
41 Map<String, Object> dyn = new HashMap<String, Object>();
42 dyn.put("class", "testString");
43 TagUtils.addCSSClassName(dyn, "testTagUtilsClassName");
44 assertEquals("(HashMap) dyn.get(\"class\")",
45 "testString testTagUtilsClassName", dyn.get("class"));
46 }
47
48 @Test
49 public void addClassName1() throws Exception {
50 Map<String, Object> dyn = new HashMap<String, Object>();
51 TagUtils.addCSSClassName(dyn, "testTagUtilsClassName");
52 assertEquals("(HashMap) dyn.size()", 1, dyn.size());
53 assertEquals("(HashMap) dyn.get(\"class\")", "testTagUtilsClassName",
54 dyn.get("class"));
55 }
56
57 @Test
58 public void errors() throws Exception {
59 ActionErrors result = TagUtils.errors(new MockJspContext());
60 assertNull("result", result);
61 }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 @Test
154 public void isChecked() throws Exception {
155 Object[] values = new Object[1];
156 values[0] = "";
157 boolean result = TagUtils.contains(values, "testTagUtilsValue");
158 assertFalse("result", result);
159 }
160
161 @Test
162 public void isChecked1() throws Exception {
163 boolean result = TagUtils.contains(new ArrayList<Integer>(100),
164 "testTagUtilsValue");
165 assertFalse("result", result);
166 }
167
168 @Test
169 public void isChecked2() throws Exception {
170 Object[] values = new Object[2];
171 values[1] = "testString";
172 boolean result = TagUtils.contains(values, "testString");
173 assertTrue("result", result);
174 }
175
176 @Test
177 public void isChecked3() throws Exception {
178 Object[] values = new Object[0];
179 boolean result = TagUtils.contains(values, "testTagUtilsValue");
180 assertFalse("result", result);
181 }
182
183 @Test
184 public void isChecked4() throws Exception {
185 Object[] values = new Object[3];
186 values[0] = "";
187 boolean result = TagUtils.contains(values, "");
188 assertTrue("result", result);
189 }
190
191 @Test
192 public void isChecked5() throws Exception {
193 boolean result = TagUtils.contains("testString", "testString");
194 assertTrue("result", result);
195 }
196
197 @Test
198 public void isChecked6() throws Exception {
199 Object[] values = new Object[1];
200 boolean result = TagUtils.contains(values, "testTagUtilsValue");
201 assertFalse("result", result);
202 }
203
204 @Test
205 public void isChecked7() throws Exception {
206 Object[] values = new Object[3];
207 values[1] = Integer.valueOf(100);
208 boolean result = TagUtils.contains(values, "testTagUtilsValue");
209 assertFalse("result", result);
210 }
211
212 @Test
213 public void isChecked8() throws Exception {
214 boolean result = TagUtils.contains(Boolean.FALSE, "testTagUtilsValue");
215 assertFalse("result", result);
216 }
217
218 @Test
219 public void isChecked9() throws Exception {
220 Object[] values = new Object[4];
221 values[0] = "testString";
222 boolean result = TagUtils.contains(values, "testString");
223 assertTrue("result", result);
224 }
225
226 @Test
227 public void isChecked10() throws Exception {
228 Object[] values = new Object[2];
229 values[0] = Integer.valueOf(-2);
230 values[1] = "testString";
231 boolean result = TagUtils.contains(values, "testString");
232 assertTrue("result", result);
233 }
234
235 @Test
236 public void multipleFormValues() throws Exception {
237 String[] strings = new String[2];
238 Map<String, String[]> outputValues = new HashMap<String, String[]>();
239 outputValues.put("name", strings);
240 Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
241 new MockFormWrapper(outputValues), "testString", "name");
242 assertEquals(1, result.length);
243 assertNull("strings[0]", strings[0]);
244 }
245
246 @Test
247 public void multipleFormValues1() throws Exception {
248 Map<String, String[]> outputValues = new HashMap<String, String[]>();
249 Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
250 new MockFormWrapper(outputValues), "testTagUtilsName",
251 "checked");
252 assertEquals(1, result.length);
253 assertEquals(result[0], "checked");
254 }
255
256 @Test
257 public void multipleFormValues2() throws Exception {
258 Map<String, String[]> outputValues = new HashMap<String, String[]>();
259 outputValues.put("testTagUtilsCheckedValue", new String[0]);
260 Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
261 new MockFormWrapper(outputValues), "testTagUtilsName",
262 "testTagUtilsCheckedValue");
263 assertEquals("result.length", 1, result.length);
264 assertEquals("result[0]", "testTagUtilsCheckedValue", result[0]);
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281 @Test
282 public void multipleFormValues5() throws Exception {
283 String[] strings = new String[0];
284 Map<String, String[]> outputValues = new HashMap<String, String[]>();
285 outputValues.put("testString", strings);
286 String[] result = (String[]) TagUtils.multipleFormValues(
287 new MockJspContext(), new MockFormWrapper(outputValues),
288 "testString");
289 assertSame("result", strings, result);
290 }
291
292 @Test
293 public void multipleFormValues6() throws Exception {
294 Map<String, String[]> outputValues = new HashMap<String, String[]>();
295 outputValues.put("testTagUtilsName", new String[0]);
296 Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
297 new MockFormWrapper(outputValues), "testTagUtilsName");
298 assertEquals("result.length", 0, result.length);
299 }
300
301 @Test
302 public void multipleFormValues7() throws Exception {
303 String[] strings = new String[3];
304 Map<String, String[]> outputValues = new HashMap<String, String[]>();
305 outputValues.put("testString", strings);
306 String[] result = (String[]) TagUtils.multipleFormValues(
307 new MockJspContext(), new MockFormWrapper(outputValues),
308 "testString");
309 assertSame("result", strings, result);
310 assertNull("strings[0]", strings[0]);
311 }
312
313 @Test
314 public void toAttr() throws Exception {
315 String result = TagUtils.toAttr(new HashMap<String, Object>());
316 assertEquals("result", "", result);
317 }
318
319 @Test
320 public void toAttr1() throws Exception {
321 Map<String, Object> map = new HashMap<String, Object>();
322 map.put("testString", Integer.valueOf(-32));
323 String result = TagUtils.toAttr(map);
324 assertEquals("result", "testString=\"-32\" ", result);
325 }
326
327 @Test
328 public void addClassNameThrowsNullPointerException() throws Exception {
329 try {
330 TagUtils.addCSSClassName(null, "testTagUtilsClassName");
331 fail("Expected NullPointerException to be thrown");
332 } catch (NullPointerException ex) {
333 assertNull("ex.getMessage()", ex.getMessage());
334 }
335 }
336
337 @Test
338 public void errorsThrowsNullPointerException() throws Exception {
339 try {
340 TagUtils.errors(null);
341 fail("Expected NullPointerException to be thrown");
342 } catch (NullPointerException ex) {
343 assertNull("ex.getMessage()", ex.getMessage());
344 }
345 }
346
347 @Test
348 public void isCheckedThrowsClassCastException() throws Exception {
349 char[] values = new char[2];
350 try {
351 TagUtils.contains(values, "testTagUtilsValue");
352 fail("Expected ClassCastException to be thrown");
353 } catch (ClassCastException ex) {
354 assertEquals("ex.getClass()", ClassCastException.class, ex
355 .getClass());
356 }
357 }
358
359 @Test
360 public void isCheckedThrowsNullPointerException() throws Exception {
361 try {
362 TagUtils.contains(null, "testTagUtilsValue");
363 fail("Expected NullPointerException to be thrown");
364 } catch (NullPointerException ex) {
365 assertNull("ex.getMessage()", ex.getMessage());
366 }
367 }
368
369 @Test
370 public void toAttrThrowsNullPointerException() throws Exception {
371 try {
372 TagUtils.toAttr(null);
373 fail("Expected NullPointerException to be thrown");
374 } catch (NullPointerException ex) {
375 assertNull("ex.getMessage()", ex.getMessage());
376 }
377 }
378
379 static class MockFormWrapper implements FormWrapper {
380
381 private final Map<String, String[]> outputValues;
382
383 public MockFormWrapper(Map<String, String[]> outputValues) {
384 this.outputValues = outputValues;
385 }
386
387 public boolean hasValues(String name) {
388 return outputValues.containsKey(name);
389 }
390
391 public String[] getValues(String name) {
392 return outputValues.get(name);
393 }
394
395 }
396
397 @Test
398 public void testGetContextPath() {
399 JspContext jspContext = new MockJspContext();
400
401 jspContext.setAttribute(CubbyConstants.ATTR_CONTEXT_PATH, "",
402 PageContext.REQUEST_SCOPE);
403 assertEquals("", TagUtils.getContextPath(jspContext));
404
405 jspContext.setAttribute(CubbyConstants.ATTR_CONTEXT_PATH, "/",
406 PageContext.REQUEST_SCOPE);
407 assertEquals("", TagUtils.getContextPath(jspContext));
408
409 jspContext.setAttribute(CubbyConstants.ATTR_CONTEXT_PATH, "/a",
410 PageContext.REQUEST_SCOPE);
411 assertEquals("/a", TagUtils.getContextPath(jspContext));
412 }
413
414 }