    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.assertTrue;
    20  import static org.junit.Assert.fail;
    21
    22  import java.util.HashMap;
    23  import java.util.Map;
    24
    25  import javax.servlet.jsp.JspContext;
    26  import javax.servlet.jsp.JspException;
    27  import javax.servlet.jsp.PageContext;
    28
    29  import org.jdom.Element;
    30  import org.junit.Before;
    31  import org.junit.Test;
    32  import org.seasar.cubby.CubbyConstants;
    33  import org.seasar.cubby.internal.util.StringUtils;
    34
    35  public class InputTagTest extends SimpleTagTestCase {
    36
    37  	private InputTag tag;
    38
    39  	@Before
    40  	public void setup() throws Exception {
    41  		tag = new InputTag();
    42  		setupSimpleTag(tag);
    43  		setupErrors(context);
    44  		setUpParams(context);
    45  		Map<String, String[]> map = new HashMap<String, String[]>();
    46  		map.put("stringField", new String[] { "outputValue" });
    47  		tag.setParent(new MockFormTag(map));
    48  	}
    49
    50  	void setUpParams(JspContext context) {
    51  		Map<String, Object[]> map = new HashMap<String, Object[]>();
    52  		map.put("stringField", new String[] { "paramsValue" });
    53  		context.setAttribute(CubbyConstants.ATTR_PARAMS, map,
    54  				PageContext.REQUEST_SCOPE);
    55  	}
    56
    57  	void setValidationFail() {
    58  		context.setAttribute(CubbyConstants.ATTR_VALIDATION_FAIL, Boolean.TRUE,
    59  				PageContext.REQUEST_SCOPE);
    60  	}
    61
    62  	@Test
    63  	public void doTagCheckboxValueRequred() throws Exception {
    64  		tag.setName("stringField");
    65  		tag.setCheckedValue("value1");
    66  		tag.setType("checkbox");
    67  		try {
    68  			tag.doTag();
    69  			fail("checkboxではvalue属性は必須");
    70  		} catch (JspException ex) {
    71  			System.out.println(ex);
    72  			assertTrue(true);
    73  		}
    74  	}
    75
    76  	@Test
    77  	public void doTagCheckbox1() throws Exception {
    78  		tag.setType("checkbox");
    79  		tag.setName("stringField");
    80  		tag.setValue("outputValue");
    81  		tag.doTag();
    82
    83  		System.out.println(context.getResult());
    84
    85  		Element element = getResultAsElementFromContext();
    86  		String message = "valueが指定";
    87  		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    88  		assertEquals(message, 4, element.getAttributes().size());
    89  		assertEquals(message, "checkbox", element.getAttributeValue("type"));
    90  		assertEquals(message, "outputValue", element.getAttributeValue("value"));
    91  		assertEquals(message, "stringField", element.getAttributeValue("name"));
    92  		assertEquals(message, "checked", element.getAttributeValue("checked"));
    93  		// assertEquals("valueが指定",
    94  		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    95  		// checked=\"true\"/>\n", context.getResult());
    96  	}
    97
    98  	@Test
    99  	public void doTagCheckbox2() throws Exception {
    100 		tag.setType("checkbox");
    101 		tag.setName("stringField");
    102 		tag.setValue("value1");
    103 		tag.doTag();
    104
    105 		System.out.println(context.getResult());
    106
    107 		Element element = getResultAsElementFromContext();
    108 		String message = "valueが指定";
    109 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    110 		assertEquals(message, 3, element.getAttributes().size());
    111 		assertEquals(message, "checkbox", element.getAttributeValue("type"));
    112 		assertEquals(message, "value1", element.getAttributeValue("value"));
    113 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    114 		// assertEquals("valueが指定",
    115 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    116 		// />\n", context.getResult());
    117 	}
    118
    119 	@Test
    120 	public void doTagCheckboxWithCheckedValue1() throws Exception {
    121 		tag.setType("checkbox");
    122 		tag.setName("stringField");
    123 		tag.setValue("value1");
    124 		tag.setCheckedValue("checkedValue");
    125 		tag.doTag();
    126
    127 		System.out.println(context.getResult());
    128
    129 		Element element = getResultAsElementFromContext();
    130 		String message = "valueが指定";
    131 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    132 		assertEquals(message, 3, element.getAttributes().size());
    133 		assertEquals(message, "checkbox", element.getAttributeValue("type"));
    134 		assertEquals(message, "value1", element.getAttributeValue("value"));
    135 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    136 		// assertEquals("valueが指定",
    137 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    138 		// />\n", context.getResult());
    139 	}
    140
    141 	@Test
    142 	public void doTagCheckboxWithCheckedValue2() throws Exception {
    143 		tag.setType("checkbox");
    144 		tag.setName("stringField");
    145 		tag.setValue("value1");
    146 		tag.setCheckedValue("value1");
    147 		tag.doTag();
    148
    149 		System.out.println(context.getResult());
    150
    151 		Element element = getResultAsElementFromContext();
    152 		String message = "valueが指定";
    153 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    154 		assertEquals(message, 4, element.getAttributes().size());
    155 		assertEquals(message, "checkbox", element.getAttributeValue("type"));
    156 		assertEquals(message, "value1", element.getAttributeValue("value"));
    157 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    158 		assertEquals(message, "checked", element.getAttributeValue("checked"));
    159 		// assertEquals("valueが指定",
    160 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    161 		// />\n", context.getResult());
    162 	}
    163
    164 	@Test
    165 	public void doTagCheckboxWithCheckedValueError1() throws Exception {
    166 		setValidationFail();
    167
    168 		tag.setType("checkbox");
    169 		tag.setName("stringField");
    170 		tag.setValue("value1");
    171 		tag.setCheckedValue("value1");
    172 		tag.doTag();
    173 		System.out.println(context.getResult());
    174
    175 		Element element = getResultAsElementFromContext();
    176 		String message = "valueが指定";
    177 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    178 		assertEquals(message, 3, element.getAttributes().size());
    179 		assertEquals(message, "checkbox", element.getAttributeValue("type"));
    180 		assertEquals(message, "value1", element.getAttributeValue("value"));
    181 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    182 		// assertEquals("valueが指定",
    183 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    184 		// />\n", context.getResult());
    185 	}
    186
    187 	@Test
    188 	public void doTagCheckboxError1() throws Exception {
    189 		setValidationFail();
    190
    191 		tag.setType("checkbox");
    192 		tag.setName("stringField");
    193 		tag.setValue("paramsValue");
    194 		tag.doTag();
    195
    196 		System.out.println(context.getResult());
    197
    198 		Element element = getResultAsElementFromContext();
    199 		String message = "valueが指定";
    200 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    201 		assertEquals(message, 4, element.getAttributes().size());
    202 		assertEquals(message, "checkbox", element.getAttributeValue("type"));
    203 		assertEquals(message, "paramsValue", element.getAttributeValue("value"));
    204 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    205 		assertEquals(message, "checked", element.getAttributeValue("checked"));
    206 		// assertEquals("valueが指定",
    207 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    208 		// checked=\"true\"/>\n", context.getResult());
    209 	}
    210
    211 	@Test
    212 	public void doTagCheckboxError2() throws Exception {
    213 		setValidationFail();
    214
    215 		tag.setType("checkbox");
    216 		tag.setName("stringField");
    217 		tag.setValue("value1");
    218 		tag.doTag();
    219
    220 		System.out.println(context.getResult());
    221
    222 		Element element = getResultAsElementFromContext();
    223 		String message = "valueが指定";
    224 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    225 		assertEquals(message, 3, element.getAttributes().size());
    226 		assertEquals(message, "checkbox", element.getAttributeValue("type"));
    227 		assertEquals(message, "value1", element.getAttributeValue("value"));
    228 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    229 		// assertEquals("valueが指定",
    230 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    231 		// />\n", context.getResult());
    232 	}
    233
    234 	@Test
    235 	public void doTagRadioValueRequred() throws Exception {
    236 		tag.setName("stringField");
    237 		tag.setCheckedValue("value1");
    238 		tag.setType("radio");
    239 		try {
    240 			tag.doTag();
    241 			fail("radioではvalue属性は必須");
    242 		} catch (JspException ex) {
    243 			assertTrue(true);
    244 		}
    245 	}
    246
    247 	@Test
    248 	public void doTagRadio1() throws Exception {
    249 		tag.setType("radio");
    250 		tag.setName("stringField");
    251 		tag.setValue("outputValue");
    252 		tag.doTag();
    253
    254 		System.out.println(context.getResult());
    255
    256 		Element element = getResultAsElementFromContext();
    257 		String message = "valueが指定";
    258 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    259 		assertEquals(message, 4, element.getAttributes().size());
    260 		assertEquals(message, "radio", element.getAttributeValue("type"));
    261 		assertEquals(message, "outputValue", element.getAttributeValue("value"));
    262 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    263 		assertEquals(message, "checked", element.getAttributeValue("checked"));
    264 		// assertEquals("valueが指定",
    265 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    266 		// checked=\"true\"/>\n", context.getResult());
    267 	}
    268
    269 	@Test
    270 	public void doTagRadio2() throws Exception {
    271 		tag.setType("radio");
    272 		tag.setName("stringField");
    273 		tag.setValue("value1");
    274 		tag.doTag();
    275
    276 		System.out.println(context.getResult());
    277
    278 		Element element = getResultAsElementFromContext();
    279 		String message = "valueが指定";
    280 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    281 		assertEquals(message, 3, element.getAttributes().size());
    282 		assertEquals(message, "radio", element.getAttributeValue("type"));
    283 		assertEquals(message, "value1", element.getAttributeValue("value"));
    284 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    285 		// assertEquals("valueが指定",
    286 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    287 		// />\n", context.getResult());
    288 	}
    289
    290 	@Test
    291 	public void doTagRadioError1() throws Exception {
    292 		setValidationFail();
    293
    294 		tag.setType("radio");
    295 		tag.setName("stringField");
    296 		tag.setValue("paramsValue");
    297 		tag.doTag();
    298
    299 		System.out.println(context.getResult());
    300
    301 		Element element = getResultAsElementFromContext();
    302 		String message = "valueが指定";
    303 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    304 		assertEquals(message, 4, element.getAttributes().size());
    305 		assertEquals(message, "radio", element.getAttributeValue("type"));
    306 		assertEquals(message, "paramsValue", element.getAttributeValue("value"));
    307 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    308 		assertEquals(message, "checked", element.getAttributeValue("checked"));
    309 		// assertEquals("valueが指定",
    310 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    311 		// checked=\"true\"/>\n", context.getResult());
    312 	}
    313
    314 	@Test
    315 	public void doTagRadioError2() throws Exception {
    316 		setValidationFail();
    317
    318 		tag.setType("radio");
    319 		tag.setName("stringField");
    320 		tag.setValue("value1");
    321 		tag.doTag();
    322
    323 		System.out.println(context.getResult());
    324
    325 		Element element = getResultAsElementFromContext();
    326 		String message = "valueが指定";
    327 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    328 		assertEquals(message, 3, element.getAttributes().size());
    329 		assertEquals(message, "radio", element.getAttributeValue("type"));
    330 		assertEquals(message, "value1", element.getAttributeValue("value"));
    331 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    332 		// assertEquals("valueが指定",
    333 		// "<input type=\"checkbox\" value=\"value1\" name=\"stringField\"
    334 		// />\n", context.getResult());
    335 	}
    336
    337 	@Test
    338 	public void doTagText1() throws Exception {
    339 		tag.setType("text");
    340 		tag.setName("stringField");
    341 		tag.setValue("value1");
    342 		tag.doTag();
    343
    344 		System.out.println(context.getResult());
    345
    346 		Element element = getResultAsElementFromContext();
    347 		String message = "valueが指定";
    348 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    349 		assertEquals(message, 3, element.getAttributes().size());
    350 		assertEquals(message, "text", element.getAttributeValue("type"));
    351 		assertEquals(message, "value1", element.getAttributeValue("value"));
    352 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    353 		// assertEquals("valueが指定",
    354 		// "<input type=\"text\" value=\"value1\" name=\"stringField\" />\n",
    355 		// context.getResult());
    356 	}
    357
    358 	@Test
    359 	public void doTagText2() throws Exception {
    360 		tag.setType("text");
    361 		tag.setName("stringField");
    362 		// tag.setValue("value1");
    363 		tag.doTag();
    364
    365 		System.out.println(context.getResult());
    366
    367 		Element element = getResultAsElementFromContext();
    368 		String message = "valueが指定";
    369 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    370 		assertEquals(message, 3, element.getAttributes().size());
    371 		assertEquals(message, "text", element.getAttributeValue("type"));
    372 		assertEquals(message, "outputValue", element.getAttributeValue("value"));
    373 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    374 		// assertEquals("valueが指定",
    375 		// "<input type=\"text\" value=\"value1\" name=\"stringField\" />\n",
    376 		// context.getResult());
    377 	}
    378
    379 	@Test
    380 	public void doTagTextError1() throws Exception {
    381 		setValidationFail();
    382
    383 		tag.setType("text");
    384 		tag.setName("stringField");
    385 		tag.setValue("value1");
    386 		tag.doTag();
    387
    388 		System.out.println(context.getResult());
    389
    390 		Element element = getResultAsElementFromContext();
    391 		String message = "valueが指定";
    392 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    393 		assertEquals(message, 3, element.getAttributes().size());
    394 		assertEquals(message, "text", element.getAttributeValue("type"));
    395 		assertEquals(message, "paramsValue", element.getAttributeValue("value"));
    396 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    397 		// assertEquals("valueが指定",
    398 		// "<input type=\"text\" value=\"value1\" name=\"stringField\" />\n",
    399 		// context.getResult());
    400 	}
    401
    402 	@Test
    403 	public void doTagTextError2() throws Exception {
    404 		setValidationFail();
    405
    406 		tag.setType("text");
    407 		tag.setName("stringField");
    408 		// tag.setValue("value1");
    409 		tag.doTag();
    410
    411 		System.out.println(context.getResult());
    412
    413 		Element element = getResultAsElementFromContext();
    414 		String message = "valueが指定";
    415 		assertTrue(message, StringUtils.isEmpty(element.getValue()));
    416 		assertEquals(message, 3, element.getAttributes().size());
    417 		assertEquals(message, "text", element.getAttributeValue("type"));
    418 		assertEquals(message, "paramsValue", element.getAttributeValue("value"));
    419 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    420 		// assertEquals("valueが指定",
    421 		// "<input type=\"text\" value=\"value1\" name=\"stringField\" />\n",
    422 		// context.getResult());
    423 	}
    424
    425 }