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