1   /*
2    * Copyright 2004-2008 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 java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.servlet.jsp.PageContext;
23  
24  import junit.framework.TestCase;
25  
26  import org.seasar.cubby.CubbyConstants;
27  import org.seasar.cubby.action.ActionErrors;
28  
29  public class TagUtilsTest extends TestCase {
30  
31  	public void testConstructor() throws Throwable {
32  		new TagUtils();
33  		assertTrue("Test call resulted in expected outcome", true);
34  	}
35  
36  	@SuppressWarnings("unchecked")
37  	public void testAddClassName() throws Throwable {
38  		Map dyn = new HashMap();
39  		dyn.put("class", "testString");
40  		TagUtils.addClassName(dyn, "testTagUtilsClassName");
41  		assertEquals("(HashMap) dyn.get(\"class\")",
42  				"testString testTagUtilsClassName", dyn.get("class"));
43  	}
44  
45  	@SuppressWarnings("unchecked")
46  	public void testAddClassName1() throws Throwable {
47  		Map dyn = new HashMap();
48  		TagUtils.addClassName(dyn, "testTagUtilsClassName");
49  		assertEquals("(HashMap) dyn.size()", 1, dyn.size());
50  		assertEquals("(HashMap) dyn.get(\"class\")", "testTagUtilsClassName",
51  				dyn.get("class"));
52  	}
53  
54  	public void testErrors() throws Throwable {
55  		ActionErrors result = TagUtils.errors(new MockJspContext());
56  		assertNull("result", result);
57  	}
58  
59  	public void testFormValue() throws Throwable {
60  		Integer specifiedValue = -2;
61  		Integer result = (Integer) TagUtils.formValue(new MockJspContext(),
62  				new String[0], "testTagUtilsName", 2, specifiedValue);
63  		assertEquals("result", specifiedValue, result);
64  	}
65  
66  	public void testFormValue1() throws Throwable {
67  		String[] strings = new String[3];
68  		strings[0] = "testString";
69  		String result = (String) TagUtils.formValue(new MockJspContext(),
70  				strings, "testString", 0, null);
71  		assertEquals("result", "testString", result);
72  	}
73  
74  	public void testFormValue2() throws Throwable {
75  		Integer specifiedValue = new Integer(0);
76  		Integer result = (Integer) TagUtils.formValue(new MockJspContext(),
77  				new String[0], "testTagUtilsName", 0, specifiedValue);
78  		assertSame("result", specifiedValue, result);
79  	}
80  
81  	public void testFormValue3() throws Throwable {
82  		String result = (String) TagUtils.formValue(new MockJspContext(),
83  				new String[0], "testTagUtilsName", 1, null);
84  		assertEquals("result", "", result);
85  	}
86  
87  	public void testFormValue4() throws Throwable {
88  		String result = (String) TagUtils.formValue(new MockJspContext(),
89  				new String[0], "testTagUtilsName", null, null);
90  		assertEquals("result", "", result);
91  	}
92  
93  	public void testFormValue5() throws Throwable {
94  		Boolean specifiedValue = Boolean.FALSE;
95  		Boolean result = (Boolean) TagUtils.formValue(new MockJspContext(),
96  				new String[0], "testTagUtilsName", new Integer(-1),
97  				specifiedValue);
98  		assertSame("result", specifiedValue, result);
99  	}
100 
101 	@SuppressWarnings("unchecked")
102 	public void testFormValue6() throws Throwable {
103 		String[] strings = new String[3];
104 		Object result = TagUtils.formValue(new MockJspContext(), strings,
105 				"testString", new Integer(0), null);
106 		assertNull("result", result);
107 	}
108 
109 	public void testFormValueValidationFail1() throws Throwable {
110 		MockJspContext context = new MockJspContext();
111 		context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
112 				PageContext.REQUEST_SCOPE);
113 		String result = (String) TagUtils.formValue(context, new String[0],
114 				"testString", 0, null);
115 		assertEquals("result", "", result);
116 	}
117 
118 	public void testFormValueValidationFail2() throws Throwable {
119 		MockJspContext context = new MockJspContext();
120 		context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
121 				PageContext.REQUEST_SCOPE);
122 		String result = (String) TagUtils.formValue(context, new String[0],
123 				"testString", 0, "aaa");
124 		assertEquals("result", "aaa", result);
125 	}
126 
127 	public void testFormValueValidationFail3() throws Throwable {
128 		MockJspContext context = new MockJspContext();
129 		context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, true,
130 				PageContext.REQUEST_SCOPE);
131 		HashMap<String, Object[]> params = new HashMap<String, Object[]>();
132 		params.put("testString", new String[] { "bbb" });
133 		context.setAttribute(CubbyConstants.ATTR_PARAMS, params,
134 				PageContext.REQUEST_SCOPE);
135 		String result = (String) TagUtils.formValue(context, new String[0],
136 				"testString", 0, "aaa");
137 		assertEquals("result", "bbb", result);
138 	}
139 
140 	public void testIsChecked() throws Throwable {
141 		Object[] values = new Object[1];
142 		values[0] = "";
143 		boolean result = TagUtils.contains(values, "testTagUtilsValue");
144 		assertFalse("result", result);
145 	}
146 
147 	@SuppressWarnings("unchecked")
148 	public void testIsChecked1() throws Throwable {
149 		boolean result = TagUtils.contains(new ArrayList(100),
150 				"testTagUtilsValue");
151 		assertFalse("result", result);
152 	}
153 
154 	public void testIsChecked2() throws Throwable {
155 		Object[] values = new Object[2];
156 		values[1] = "testString";
157 		boolean result = TagUtils.contains(values, "testString");
158 		assertTrue("result", result);
159 	}
160 
161 	public void testIsChecked3() throws Throwable {
162 		Object[] values = new Object[0];
163 		boolean result = TagUtils.contains(values, "testTagUtilsValue");
164 		assertFalse("result", result);
165 	}
166 
167 	public void testIsChecked4() throws Throwable {
168 		Object[] values = new Object[3];
169 		values[0] = "";
170 		boolean result = TagUtils.contains(values, "");
171 		assertTrue("result", result);
172 	}
173 
174 	public void testIsChecked5() throws Throwable {
175 		boolean result = TagUtils.contains("testString", "testString");
176 		assertTrue("result", result);
177 	}
178 
179 	public void testIsChecked6() throws Throwable {
180 		Object[] values = new Object[1];
181 		boolean result = TagUtils.contains(values, "testTagUtilsValue");
182 		assertFalse("result", result);
183 	}
184 
185 	public void testIsChecked7() throws Throwable {
186 		Object[] values = new Object[3];
187 		values[1] = new Integer(100);
188 		boolean result = TagUtils.contains(values, "testTagUtilsValue");
189 		assertFalse("result", result);
190 	}
191 
192 	public void testIsChecked8() throws Throwable {
193 		boolean result = TagUtils.contains(Boolean.FALSE, "testTagUtilsValue");
194 		assertFalse("result", result);
195 	}
196 
197 	public void testIsChecked9() throws Throwable {
198 		Object[] values = new Object[4];
199 		values[0] = "testString";
200 		boolean result = TagUtils.contains(values, "testString");
201 		assertTrue("result", result);
202 	}
203 
204 	public void testIsChecked10() throws Throwable {
205 		Object[] values = new Object[2];
206 		values[0] = new Integer(-2);
207 		values[1] = "testString";
208 		boolean result = TagUtils.contains(values, "testString");
209 		assertTrue("result", result);
210 	}
211 
212 	public void testMultipleFormValues() throws Throwable {
213 		String[] strings = new String[2];
214 		String[] result = (String[]) TagUtils.multipleFormValues(
215 				new MockJspContext(), strings, "testString", null);
216 		assertSame("result", strings, result);
217 		assertNull("strings[0]", strings[0]);
218 	}
219 
220 	public void testMultipleFormValues1() throws Throwable {
221 		Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
222 				new String[0], "testTagUtilsName", null);
223 		assertEquals("result.length", 0, result.length);
224 	}
225 
226 	public void testMultipleFormValues2() throws Throwable {
227 		Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
228 				new String[0], "testTagUtilsName", "testTagUtilsCheckedValue");
229 		assertEquals("result.length", 1, result.length);
230 		assertEquals("result[0]", "testTagUtilsCheckedValue", result[0]);
231 	}
232 
233 	public void testMultipleFormValues3() throws Throwable {
234 		Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
235 				null, "testTagUtilsName", null);
236 		assertEquals("result.length", 0, result.length);
237 	}
238 
239 	public void testMultipleFormValues4() throws Throwable {
240 		Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
241 				null, "testTagUtilsName");
242 		assertEquals("result.length", 0, result.length);
243 	}
244 
245 	@SuppressWarnings("unchecked")
246 	public void testMultipleFormValues5() throws Throwable {
247 		String[] strings = new String[0];
248 		String[] result = (String[]) TagUtils.multipleFormValues(
249 				new MockJspContext(), strings, "testString");
250 		assertSame("result", strings, result);
251 	}
252 
253 	@SuppressWarnings("unchecked")
254 	public void testMultipleFormValues6() throws Throwable {
255 		Object[] result = TagUtils.multipleFormValues(new MockJspContext(),
256 				new String[0], "testTagUtilsName");
257 		assertEquals("result.length", 0, result.length);
258 	}
259 
260 	@SuppressWarnings("unchecked")
261 	public void testMultipleFormValues7() throws Throwable {
262 		String[] strings = new String[3];
263 		String[] result = (String[]) TagUtils.multipleFormValues(
264 				new MockJspContext(), strings, "testString");
265 		assertSame("result", strings, result);
266 		assertNull("strings[0]", strings[0]);
267 	}
268 
269 	@SuppressWarnings("unchecked")
270 	public void testToAttr() throws Throwable {
271 		String result = TagUtils.toAttr(new HashMap());
272 		assertEquals("result", "", result);
273 	}
274 
275 	@SuppressWarnings("unchecked")
276 	public void testToAttr1() throws Throwable {
277 		Map map = new HashMap();
278 		map.put("testString", new Integer(-32));
279 		String result = TagUtils.toAttr(map);
280 		assertEquals("result", "testString=\"-32\" ", result);
281 	}
282 
283 	public void testAddClassNameThrowsNullPointerException() throws Throwable {
284 		try {
285 			TagUtils.addClassName(null, "testTagUtilsClassName");
286 			fail("Expected NullPointerException to be thrown");
287 		} catch (NullPointerException ex) {
288 			assertNull("ex.getMessage()", ex.getMessage());
289 		}
290 	}
291 
292 	public void testErrorsThrowsNullPointerException() throws Throwable {
293 		try {
294 			TagUtils.errors(null);
295 			fail("Expected NullPointerException to be thrown");
296 		} catch (NullPointerException ex) {
297 			assertNull("ex.getMessage()", ex.getMessage());
298 		}
299 	}
300 
301 	public void testIsCheckedThrowsClassCastException() throws Throwable {
302 		char[] values = new char[2];
303 		try {
304 			TagUtils.contains(values, "testTagUtilsValue");
305 			fail("Expected ClassCastException to be thrown");
306 		} catch (ClassCastException ex) {
307 			assertEquals("ex.getClass()", ClassCastException.class, ex
308 					.getClass());
309 		}
310 	}
311 
312 	public void testIsCheckedThrowsNullPointerException() throws Throwable {
313 		try {
314 			TagUtils.contains(null, "testTagUtilsValue");
315 			fail("Expected NullPointerException to be thrown");
316 		} catch (NullPointerException ex) {
317 			assertNull("ex.getMessage()", ex.getMessage());
318 		}
319 	}
320 
321 	public void testToAttrThrowsNullPointerException() throws Throwable {
322 		try {
323 			TagUtils.toAttr(null);
324 			fail("Expected NullPointerException to be thrown");
325 		} catch (NullPointerException ex) {
326 			assertNull("ex.getMessage()", ex.getMessage());
327 		}
328 	}
329 }