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