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 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  	// @Test
64  	// public void formValue() throws Exception {
65  	// Integer specifiedValue = -2;
66  	// Integer result = (Integer) TagUtils.formValue(new MockJspContext(),
67  	// new String[0], "testTagUtilsName", 2, specifiedValue);
68  	// assertEquals("result", specifiedValue, result);
69  	// }
70  	//
71  	// @Test
72  	// public void formValue1() throws Exception {
73  	// String[] strings = new String[3];
74  	// strings[0] = "testString";
75  	// String result = (String) TagUtils.formValue(new MockJspContext(),
76  	// strings, "testString", 0, null);
77  	// assertEquals("result", "testString", result);
78  	// }
79  	//
80  	// @Test
81  	// public void formValue2() throws Exception {
82  	// Integer specifiedValue = new Integer(0);
83  	// Integer result = (Integer) TagUtils.formValue(new MockJspContext(),
84  	// new String[0], "testTagUtilsName", 0, specifiedValue);
85  	// assertSame("result", specifiedValue, result);
86  	// }
87  	//
88  	// @Test
89  	// public void formValue3() throws Exception {
90  	// String result = (String) TagUtils.formValue(new MockJspContext(),
91  	// new String[0], "testTagUtilsName", 1, null);
92  	// assertEquals("result", "", result);
93  	// }
94  	//
95  	// @Test
96  	// public void formValue4() throws Exception {
97  	// String result = (String) TagUtils.formValue(new MockJspContext(),
98  	// new String[0], "testTagUtilsName", null, null);
99  	// assertEquals("result", "", result);
100 	// }
101 	//
102 	// @Test
103 	// public void formValue5() throws Exception {
104 	// Boolean specifiedValue = Boolean.FALSE;
105 	// Boolean result = (Boolean) TagUtils.formValue(new MockJspContext(),
106 	// new String[0], "testTagUtilsName", new Integer(-1),
107 	// specifiedValue);
108 	// assertSame("result", specifiedValue, result);
109 	// }
110 	//
111 	// @Test
112 	// public void formValue6() throws Exception {
113 	// String[] strings = new String[3];
114 	// Object result = TagUtils.formValue(new MockJspContext(), strings,
115 	// "testString", new Integer(0), null);
116 	// assertNull("result", result);
117 	// }
118 	//
119 	// @Test
120 	// public void formValueValidationFail1() throws Exception {
121 	// MockJspContext context = new MockJspContext();
122 	// context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
123 	// PageContext.REQUEST_SCOPE);
124 	// String result = (String) TagUtils.formValue(context, new String[0],
125 	// "testString", 0, null);
126 	// assertEquals("result", "", result);
127 	// }
128 	//
129 	// @Test
130 	// public void formValueValidationFail2() throws Exception {
131 	// MockJspContext context = new MockJspContext();
132 	// context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
133 	// PageContext.REQUEST_SCOPE);
134 	// String result = (String) TagUtils.formValue(context, new String[0],
135 	// "testString", 0, "aaa");
136 	// assertEquals("result", "aaa", result);
137 	// }
138 	//
139 	// @Test
140 	// public void formValueValidationFail3() throws Exception {
141 	// MockJspContext context = new MockJspContext();
142 	// context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
143 	// PageContext.REQUEST_SCOPE);
144 	// HashMap<String, Object[]> params = new HashMap<String, Object[]>();
145 	// params.put("testString", new String[] { "bbb" });
146 	// context.setAttribute(CubbyConstants.ATTR_PARAMS, params,
147 	// PageContext.REQUEST_SCOPE);
148 	// String result = (String) TagUtils.formValue(context, new String[0],
149 	// "testString", 0, "aaa");
150 	// assertEquals("result", "bbb", result);
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 	// @Test
268 	// public void multipleFormValues3() throws Exception {
269 	// Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
270 	// null, "testTagUtilsName", null);
271 	// assertEquals("result.length", 0, result.length);
272 	// }
273 	//
274 	// @Test
275 	// public void multipleFormValues4() throws Exception {
276 	// Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
277 	// null, "testTagUtilsName");
278 	// assertEquals("result.length", 0, result.length);
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 }