    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.LinkedHashMap;
    24  import java.util.Map;
    25
    26  import org.jdom.Element;
    27  import org.junit.Before;
    28  import org.junit.Test;
    29  import org.seasar.cubby.internal.util.StringUtils;
    30
    31  public class SelectTagMapTest extends SimpleTagTestCase {
    32
    33  	private SelectTag tag;
    34
    35  	private Map<Integer, String> items = new LinkedHashMap<Integer, String>();
    36
    37  	@Before
    38  	public void setUp() throws Exception {
    39  		tag = new SelectTag();
    40  		setupSimpleTag(tag);
    41  		setupErrors(context);
    42  		items.put(1, "name1");
    43  		items.put(2, "name2");
    44  		items.put(3, "name3");
    45  	}
    46
    47  	@Test
    48  	public void doTag1() throws Exception {
    49  		Map<String, String[]> map = new HashMap<String, String[]>();
    50  		map.put("stringField", new String[] { "1" });
    51  		tag.setParent(new MockFormTag(map));
    52  		tag.setName("stringField");
    53  		tag.setDynamicAttribute(null, "id", "stringField");
    54  		tag.setItems(items);
    55  		tag.doTag();
    56
    57  		Element element = getResultAsElementFromContext();
    58  		String message = "基本";
    59  		assertEquals(message, 2, element.getAttributes().size());
    60  		assertEquals(message, "stringField", element.getAttributeValue("name"));
    61  		assertEquals(message, "stringField", element.getAttributeValue("id"));
    62  		assertEquals(message, 4, element.getChildren().size());
    63  		for (Object o : element.getChildren("option")) {
    64  			Element child = (Element) o;
    65  			String value = child.getValue();
    66  			if (StringUtils.isEmpty(value)) {
    67  				assertEquals(message, 1, child.getAttributes().size());
    68  				assertTrue(message, StringUtils.isEmpty(child
    69  						.getAttributeValue("value")));
    70  			} else if ("name1".equals(value)) {
    71  				assertEquals(message, 2, child.getAttributes().size());
    72  				assertEquals(message, "1", child.getAttributeValue("value"));
    73  				assertEquals(message, "selected", child
    74  						.getAttributeValue("selected"));
    75  			} else if ("name2".equals(value)) {
    76  				assertEquals(message, 1, child.getAttributes().size());
    77  				assertEquals(message, "2", child.getAttributeValue("value"));
    78  			} else if ("name3".equals(value)) {
    79  				assertEquals(message, 1, child.getAttributes().size());
    80  				assertEquals(message, "3", child.getAttributeValue("value"));
    81  			} else {
    82  				fail(message);
    83  			}
    84  		}
    85  		// assertEquals("基本",
    86  		// "<select name=\"stringField\" id=\"stringField\" >\n" +
    87  		// "<option value=\"\"></option>\n" +
    88  		// "<option value=\"1\" selected=\"true\">name1</option>\n" +
    89  		// "<option value=\"2\" >name2</option>\n" +
    90  		// "<option value=\"3\" >name3</option>\n" +
    91  		// "</select>\n", context.getResult());
    92  	}
    93
    94  	@Test
    95  	public void doTag2() throws Exception {
    96  		Map<String, String[]> map = new HashMap<String, String[]>();
    97  		map.put("integerArrayField", new String[] { "1", "3" });
    98  		tag.setParent(new MockFormTag(map));
    99  		tag.setName("integerArrayField");
    100 		tag.setDynamicAttribute(null, "size", "5");
    101 		tag.setItems(items);
    102 		tag.doTag();
    103
    104 		Element element = getResultAsElementFromContext();
    105 		String message = "selectedの対象が2つ";
    106 		assertEquals(message, 2, element.getAttributes().size());
    107 		assertEquals(message, "integerArrayField", element
    108 				.getAttributeValue("name"));
    109 		assertEquals(message, "5", element.getAttributeValue("size"));
    110 		assertEquals(message, 4, element.getChildren().size());
    111 		for (Object o : element.getChildren("option")) {
    112 			Element child = (Element) o;
    113 			String value = child.getValue();
    114 			if (StringUtils.isEmpty(value)) {
    115 				assertEquals(message, 1, child.getAttributes().size());
    116 				assertTrue(message, StringUtils.isEmpty(child
    117 						.getAttributeValue("value")));
    118 			} else if ("name1".equals(value)) {
    119 				assertEquals(message, 2, child.getAttributes().size());
    120 				assertEquals(message, "1", child.getAttributeValue("value"));
    121 				assertEquals(message, "selected", child
    122 						.getAttributeValue("selected"));
    123 			} else if ("name2".equals(value)) {
    124 				assertEquals(message, 1, child.getAttributes().size());
    125 				assertEquals(message, "2", child.getAttributeValue("value"));
    126 			} else if ("name3".equals(value)) {
    127 				assertEquals(message, 2, child.getAttributes().size());
    128 				assertEquals(message, "3", child.getAttributeValue("value"));
    129 				assertEquals(message, "selected", child
    130 						.getAttributeValue("selected"));
    131 			} else {
    132 				fail(message);
    133 			}
    134 		}
    135 		// assertEquals("selectedの対象が2つ",
    136 		// "<select size=\"5\" name=\"integerArrayField\" >\n" +
    137 		// "<option value=\"\"></option>\n" +
    138 		// "<option value=\"1\" selected=\"true\">name1</option>\n" +
    139 		// "<option value=\"2\" >name2</option>\n" +
    140 		// "<option value=\"3\" selected=\"true\">name3</option>\n" +
    141 		// "</select>\n", context.getResult());
    142 	}
    143
    144 	@Test
    145 	public void doTag4() throws Exception {
    146 		Map<String, String[]> map = new HashMap<String, String[]>();
    147 		map.put("stringField", new String[] { "1" });
    148 		tag.setParent(new MockFormTag(map));
    149 		tag.setName("stringField");
    150 		tag.setItems(items);
    151 		tag.setEmptyOption(false);
    152 		tag.doTag();
    153
    154 		Element element = getResultAsElementFromContext();
    155 		String message = "emptyOption=false";
    156 		assertEquals(message, 1, element.getAttributes().size());
    157 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    158 		assertEquals(message, 3, element.getChildren().size());
    159 		for (Object o : element.getChildren("option")) {
    160 			Element child = (Element) o;
    161 			String value = child.getValue();
    162 			if ("name1".equals(value)) {
    163 				assertEquals(message, 2, child.getAttributes().size());
    164 				assertEquals(message, "1", child.getAttributeValue("value"));
    165 				assertEquals(message, "selected", child
    166 						.getAttributeValue("selected"));
    167 			} else if ("name2".equals(value)) {
    168 				assertEquals(message, 1, child.getAttributes().size());
    169 				assertEquals(message, "2", child.getAttributeValue("value"));
    170 			} else if ("name3".equals(value)) {
    171 				assertEquals(message, 1, child.getAttributes().size());
    172 				assertEquals(message, "3", child.getAttributeValue("value"));
    173 			} else {
    174 				fail(message);
    175 			}
    176 		}
    177 		// assertEquals("emptyOption=false",
    178 		// "<select name=\"stringField\" >\n" +
    179 		// "<option value=\"1\" selected=\"true\">name1</option>\n" +
    180 		// "<option value=\"2\" >name2</option>\n" +
    181 		// "<option value=\"3\" >name3</option>\n" +
    182 		// "</select>\n", context.getResult());
    183 	}
    184
    185 	@Test
    186 	public void doTag5() throws Exception {
    187 		Map<String, String[]> map = new HashMap<String, String[]>();
    188 		map.put("stringField", new String[] { "1" });
    189 		tag.setParent(new MockFormTag(map));
    190 		tag.setName("stringField");
    191 		tag.setItems(items);
    192 		tag.setEmptyOptionLabel("empty label");
    193 		tag.doTag();
    194
    195 		Element element = getResultAsElementFromContext();
    196 		String message = "emptyOption=true, emptyOptionLabel=empty label";
    197 		assertEquals(message, 1, element.getAttributes().size());
    198 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    199 		assertEquals(message, 4, element.getChildren().size());
    200 		for (Object o : element.getChildren("option")) {
    201 			Element child = (Element) o;
    202 			String value = child.getValue();
    203 			if ("empty label".equals(value)) {
    204 				assertEquals(message, 1, child.getAttributes().size());
    205 				assertTrue(message, StringUtils.isEmpty(child
    206 						.getAttributeValue("value")));
    207 			} else if ("name1".equals(value)) {
    208 				assertEquals(message, 2, child.getAttributes().size());
    209 				assertEquals(message, "1", child.getAttributeValue("value"));
    210 				assertEquals(message, "selected", child
    211 						.getAttributeValue("selected"));
    212 			} else if ("name2".equals(value)) {
    213 				assertEquals(message, 1, child.getAttributes().size());
    214 				assertEquals(message, "2", child.getAttributeValue("value"));
    215 			} else if ("name3".equals(value)) {
    216 				assertEquals(message, 1, child.getAttributes().size());
    217 				assertEquals(message, "3", child.getAttributeValue("value"));
    218 			} else {
    219 				fail(message);
    220 			}
    221 		}
    222 		// assertEquals("emptyOption=true, emptyOptionLabel=empty label",
    223 		// "<select name=\"stringField\" >\n" +
    224 		// "<option value=\"\">empty label</option>\n" +
    225 		// "<option value=\"1\" selected=\"true\">name1</option>\n" +
    226 		// "<option value=\"2\" >name2</option>\n" +
    227 		// "<option value=\"3\" >name3</option>\n" +
    228 		// "</select>\n", context.getResult());
    229 	}
    230
    231 	@Test
    232 	public void doTag11() throws Exception {
    233 		Map<String, String[]> map = new HashMap<String, String[]>();
    234 		map.put("stringField", new String[] { "1" });
    235 		tag.setParent(new MockFormTag(map));
    236 		tag.setName("stringField");
    237 		tag.setDynamicAttribute(null, "id", "stringField");
    238 		tag.setItems(items);
    239 		tag.setLabelProperty("name");
    240 		tag.doTag();
    241
    242 		Element element = getResultAsElementFromContext();
    243 		String message = "labelProperty設定";
    244 		assertEquals(message, 2, element.getAttributes().size());
    245 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    246 		assertEquals(message, "stringField", element.getAttributeValue("id"));
    247 		assertEquals(message, 4, element.getChildren().size());
    248 		for (Object o : element.getChildren("option")) {
    249 			Element child = (Element) o;
    250 			String value = child.getValue();
    251 			if (StringUtils.isEmpty(value)) {
    252 				assertEquals(message, 1, child.getAttributes().size());
    253 				assertTrue(message, StringUtils.isEmpty(child
    254 						.getAttributeValue("value")));
    255 			} else if ("name1".equals(value)) {
    256 				assertEquals(message, 2, child.getAttributes().size());
    257 				assertEquals(message, "1", child.getAttributeValue("value"));
    258 				assertEquals(message, "selected", child
    259 						.getAttributeValue("selected"));
    260 			} else if ("name2".equals(value)) {
    261 				assertEquals(message, 1, child.getAttributes().size());
    262 				assertEquals(message, "2", child.getAttributeValue("value"));
    263 			} else if ("name3".equals(value)) {
    264 				assertEquals(message, 1, child.getAttributes().size());
    265 				assertEquals(message, "3", child.getAttributeValue("value"));
    266 			} else {
    267 				fail(message);
    268 			}
    269 		}
    270 		// assertEquals("labelPropertyを設定",
    271 		// "<select name=\"stringField\" id=\"stringField\" >\n" +
    272 		// "<option value=\"\"></option>\n" +
    273 		// "<option value=\"1\" selected=\"true\">name1</option>\n" +
    274 		// "<option value=\"2\" >name2</option>\n" +
    275 		// "<option value=\"3\" >name3</option>\n" +
    276 		// "</select>\n", context.getResult());
    277 	}
    278
    279 	@Test
    280 	public void doTag12() throws Exception {
    281 		Map<String, String[]> map = new HashMap<String, String[]>();
    282 		map.put("stringField", new String[] { "1" });
    283 		tag.setParent(new MockFormTag(map));
    284 		tag.setName("stringField");
    285 		tag.setDynamicAttribute(null, "id", "stringField");
    286 		tag.setItems(items);
    287 		tag.setValueProperty("id");
    288 		tag.doTag();
    289
    290 		Element element = getResultAsElementFromContext();
    291 		String message = "valuePropertyを設定";
    292 		assertEquals(message, 2, element.getAttributes().size());
    293 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    294 		assertEquals(message, "stringField", element.getAttributeValue("id"));
    295 		assertEquals(message, 4, element.getChildren().size());
    296 		for (Object o : element.getChildren("option")) {
    297 			Element child = (Element) o;
    298 			String value = child.getValue();
    299 			if (StringUtils.isEmpty(value)) {
    300 				assertEquals(message, 1, child.getAttributes().size());
    301 				assertTrue(message, StringUtils.isEmpty(child
    302 						.getAttributeValue("value")));
    303 			} else if ("name1".equals(value)) {
    304 				assertEquals(message, 2, child.getAttributes().size());
    305 				assertEquals(message, "1", child.getAttributeValue("value"));
    306 				assertEquals(message, "selected", child
    307 						.getAttributeValue("selected"));
    308 			} else if ("name2".equals(value)) {
    309 				assertEquals(message, 1, child.getAttributes().size());
    310 				assertEquals(message, "2", child.getAttributeValue("value"));
    311 			} else if ("name3".equals(value)) {
    312 				assertEquals(message, 1, child.getAttributes().size());
    313 				assertEquals(message, "3", child.getAttributeValue("value"));
    314 			} else {
    315 				fail(message);
    316 			}
    317 		}
    318 		// assertEquals("valuePropertyを設定",
    319 		// "<select name=\"stringField\" id=\"stringField\" >\n" +
    320 		// "<option value=\"\"></option>\n" +
    321 		// "<option value=\"1\" selected=\"true\">name1</option>\n" +
    322 		// "<option value=\"2\" >name2</option>\n" +
    323 		// "<option value=\"3\" >name3</option>\n" +
    324 		// "</select>\n", context.getResult());
    325 	}
    326
    327 	@Test
    328 	public void doTag13() throws Exception {
    329 		Map<String, String[]> map = new HashMap<String, String[]>();
    330 		map.put("stringField", new String[] { "1" });
    331 		tag.setParent(new MockFormTag(map));
    332 		tag.setName("stringField");
    333 		tag.setDynamicAttribute(null, "id", "stringField");
    334 		tag.setItems(items);
    335 		tag.setValueProperty("id");
    336 		tag.setLabelProperty("name");
    337 		tag.doTag();
    338
    339 		Element element = getResultAsElementFromContext();
    340 		String message = "valuePropertyとlabelPropertyを設定";
    341 		assertEquals(message, 2, element.getAttributes().size());
    342 		assertEquals(message, "stringField", element.getAttributeValue("name"));
    343 		assertEquals(message, "stringField", element.getAttributeValue("id"));
    344 		assertEquals(message, 4, element.getChildren().size());
    345 		for (Object o : element.getChildren("option")) {
    346 			Element child = (Element) o;
    347 			String value = child.getValue();
    348 			if (StringUtils.isEmpty(value)) {
    349 				assertEquals(message, 1, child.getAttributes().size());
    350 				assertTrue(message, StringUtils.isEmpty(child
    351 						.getAttributeValue("value")));
    352 			} else if ("name1".equals(value)) {
    353 				assertEquals(message, 2, child.getAttributes().size());
    354 				assertEquals(message, "1", child.getAttributeValue("value"));
    355 				assertEquals(message, "selected", child
    356 						.getAttributeValue("selected"));
    357 			} else if ("name2".equals(value)) {
    358 				assertEquals(message, 1, child.getAttributes().size());
    359 				assertEquals(message, "2", child.getAttributeValue("value"));
    360 			} else if ("name3".equals(value)) {
    361 				assertEquals(message, 1, child.getAttributes().size());
    362 				assertEquals(message, "3", child.getAttributeValue("value"));
    363 			} else {
    364 				fail(message);
    365 			}
    366 		}
    367 		// assertEquals("valuePropertyとlabelPropertyを設定",
    368 		// "<select name=\"stringField\" id=\"stringField\" >\n" +
    369 		// "<option value=\"\"></option>\n" +
    370 		// "<option value=\"1\" selected=\"true\">name1</option>\n" +
    371 		// "<option value=\"2\" >name2</option>\n" +
    372 		// "<option value=\"3\" >name3</option>\n" +
    373 		// "</select>\n", context.getResult());
    374 	}
    375
    376 }