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