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.assertTrue;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.servlet.jsp.PageContext;
26  
27  import org.jdom.Element;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.seasar.cubby.internal.util.StringUtils;
31  
32  public class TextareaTagTest extends SimpleTagTestCase {
33  
34  	private TextareaTag tag;
35  
36  	@Before
37  	public void setup() throws Exception {
38  		tag = new TextareaTag();
39  		setupSimpleTag(tag);
40  		setupErrors(context);
41  		jspBody.setBody("Dummy Body Text");
42  	}
43  
44  	@Test
45  	public void doTag1() throws Exception {
46  		tag.setParent(new MockFormTag(new HashMap<String, String[]>()));
47  		tag.setName("content");
48  		tag.setValue("value1");
49  		// tag.setDynamicAttribute(null, "name", "content");
50  		// tag.setDynamicAttribute(null, "value", "value1");
51  		tag.setDynamicAttribute(null, "id", "content");
52  		tag.doTag();
53  		Element element = getResultAsElementFromContext();
54  		String message = "フォームオブジェクトが空でvalueが指定されている場合";
55  		assertEquals(message, "value1", element.getValue());
56  		assertEquals(message, 2, element.getAttributes().size());
57  		assertEquals(message, "content", element.getAttributeValue("id"));
58  		assertEquals(message, "content", element.getAttributeValue("name"));
59  	}
60  
61  	@Test
62  	public void doTag2() throws Exception {
63  		FormDto form = new FormDto();
64  		form.setStringField("value1");
65  		Map<String, String[]> map = new HashMap<String, String[]>();
66  		map.put("stringField", new String[] { "value1" });
67  		tag.setParent(new MockFormTag(map));
68  		context.setAttribute("__form", form, PageContext.REQUEST_SCOPE);
69  		tag.setName("stringField");
70  		// tag.setDynamicAttribute(null, "name", "stringField");
71  		tag.doTag();
72  		Element element = getResultAsElementFromContext();
73  		String message = "フォームオブジェクトとname指定の場合";
74  		assertEquals(message, "value1", element.getValue());
75  		assertEquals(message, 1, element.getAttributes().size());
76  		assertEquals(message, "stringField", element.getAttributeValue("name"));
77  	}
78  
79  	@Test
80  	public void doTag3() throws Exception {
81  		FormDto form = new FormDto();
82  		form.setStringField("value1");
83  		Map<String, String[]> map = new HashMap<String, String[]>();
84  		tag.setParent(new MockFormTag(map));
85  		tag.setName("stringField");
86  		// tag.setDynamicAttribute(null, "name", "stringField");
87  		tag.doTag();
88  		Element element = getResultAsElementFromContext();
89  		String message = "フォームオブジェクトが空でnameが指定されている場合";
90  		assertTrue(message, StringUtils.isEmpty(element.getValue()));
91  		assertEquals(message, 1, element.getAttributes().size());
92  		assertEquals(message, "stringField", element.getAttributeValue("name"));
93  	}
94  
95  }