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