1   /*
2    * Copyright 2004-2009 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
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  	// @Test
60  	// public void formValue() throws Exception {
61  	// Integer specifiedValue = -2;
62  	// Integer result = (Integer) TagUtils.formValue(new MockJspContext(),
63  	// new String[0], "testTagUtilsName", 2, specifiedValue);
64  	// assertEquals("result", specifiedValue, result);
65  	// }
66  	//
67  	// @Test
68  	// public void formValue1() throws Exception {
69  	// String[] strings = new String[3];
70  	// strings[0] = "testString";
71  	// String result = (String) TagUtils.formValue(new MockJspContext(),
72  	// strings, "testString", 0, null);
73  	// assertEquals("result", "testString", result);
74  	// }
75  	//
76  	// @Test
77  	// public void formValue2() throws Exception {
78  	// Integer specifiedValue = new Integer(0);
79  	// Integer result = (Integer) TagUtils.formValue(new MockJspContext(),
80  	// new String[0], "testTagUtilsName", 0, specifiedValue);
81  	// assertSame("result", specifiedValue, result);
82  	// }
83  	//
84  	// @Test
85  	// public void formValue3() throws Exception {
86  	// String result = (String) TagUtils.formValue(new MockJspContext(),
87  	// new String[0], "testTagUtilsName", 1, null);
88  	// assertEquals("result", "", result);
89  	// }
90  	//
91  	// @Test
92  	// public void formValue4() throws Exception {
93  	// String result = (String) TagUtils.formValue(new MockJspContext(),
94  	// new String[0], "testTagUtilsName", null, null);
95  	// assertEquals("result", "", result);
96  	// }
97  	//
98  	// @Test
99  	// public void formValue5() throws Exception {
100 	// Boolean specifiedValue = Boolean.FALSE;
101 	// Boolean result = (Boolean) TagUtils.formValue(new MockJspContext(),
102 	// new String[0], "testTagUtilsName", new Integer(-1),
103 	// specifiedValue);
104 	// assertSame("result", specifiedValue, result);
105 	// }
106 	//
107 	// @Test
108 	// public void formValue6() throws Exception {
109 	// String[] strings = new String[3];
110 	// Object result = TagUtils.formValue(new MockJspContext(), strings,
111 	// "testString", new Integer(0), null);
112 	// assertNull("result", result);
113 	// }
114 	//
115 	// @Test
116 	// public void formValueValidationFail1() throws Exception {
117 	// MockJspContext context = new MockJspContext();
118 	// context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
119 	// PageContext.REQUEST_SCOPE);
120 	// String result = (String) TagUtils.formValue(context, new String[0],
121 	// "testString", 0, null);
122 	// assertEquals("result", "", result);
123 	// }
124 	//
125 	// @Test
126 	// public void formValueValidationFail2() throws Exception {
127 	// MockJspContext context = new MockJspContext();
128 	// context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
129 	// PageContext.REQUEST_SCOPE);
130 	// String result = (String) TagUtils.formValue(context, new String[0],
131 	// "testString", 0, "aaa");
132 	// assertEquals("result", "aaa", result);
133 	// }
134 	//
135 	// @Test
136 	// public void formValueValidationFail3() throws Exception {
137 	// MockJspContext context = new MockJspContext();
138 	// context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
139 	// PageContext.REQUEST_SCOPE);
140 	// HashMap<String, Object[]> params = new HashMap<String, Object[]>();
141 	// params.put("testString", new String[] { "bbb" });
142 	// context.setAttribute(CubbyConstants.ATTR_PARAMS, params,
143 	// PageContext.REQUEST_SCOPE);
144 	// String result = (String) TagUtils.formValue(context, new String[0],
145 	// "testString", 0, "aaa");
146 	// assertEquals("result", "bbb", result);
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 	// @Test
264 	// public void multipleFormValues3() throws Exception {
265 	// Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
266 	// null, "testTagUtilsName", null);
267 	// assertEquals("result.length", 0, result.length);
268 	// }
269 	//
270 	// @Test
271 	// public void multipleFormValues4() throws Exception {
272 	// Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
273 	// null, "testTagUtilsName");
274 	// assertEquals("result.length", 0, result.length);
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 }