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