1   /*
2    * Copyright 2004-2008 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 java.util.Arrays;
19  import java.util.List;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.jsp.PageContext;
23  import javax.servlet.jsp.tagext.JspTag;
24  
25  import org.jdom.Element;
26  import org.seasar.cubby.CubbyConstants;
27  
28  public class FormTagTest extends AbstractStandardTagTestCase {
29  
30  	public HttpServletRequest request;
31  
32  	public FormTag tag;
33  
34  	@Override
35  	protected void setUp() throws Exception {
36  		include(getClass().getName().replace('.', '/') + ".dicon");
37  		super.setUp();
38  		tag = new FormTag();
39  		setupBodyTag(tag);
40  		setupErrors(context);
41  		context.setAttribute(CubbyConstants.ATTR_CONTEXT_PATH, "/brabra",
42  				PageContext.REQUEST_SCOPE);
43  	}
44  
45  	public void testDoTagNoChild() throws Exception {
46  		FormDto form = new FormDto();
47  		form.setStringField("value1");
48  
49  		tag.setValue(form);
50  		tag.setDynamicAttribute(null, "action", "/todo/save");
51  		doLifecycle(tag);
52  
53  		System.out.println(context.getResult());
54  		// "<form action=\"/todo/save\" >\n</form>\n"
55  
56  		Element element = getResultAsElementFromContext();
57  		String message = "フォームオブジェクトが指定";
58  		assertEquals(message, 1, element.getAttributes().size());
59  		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
60  		assertNull("フォームオブジェクトは除去されていること", context.findAttribute("__form"));
61  	}
62  
63  	public void testDoTagEmptyBody() throws Exception {
64  		FormDto form = new FormDto();
65  		form.setStringField("value1");
66  
67  		tag.setValue(form);
68  		tag.setDynamicAttribute(null, "action", "/todo/save");
69  
70  		doLifecycle(tag);
71  
72  		System.out.println(context.getResult());
73  		// "<form action=\"/todo/save\" >\n</form>\n"
74  
75  		Element element = getResultAsElementFromContext();
76  		String message = "Bodyが空の場合";
77  		assertEquals(message, 1, element.getAttributes().size());
78  		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
79  	}
80  
81  	public void testDoTagWithTextAreaTag() throws Exception {
82  		FormDto form = new FormDto();
83  		form.setStringField("value1");
84  
85  		tag.setValue(form);
86  		tag.setDynamicAttribute(null, "action", "/todo/save");
87  		doLifecycle(tag, new ChildrenFactory() {
88  
89  			public List<JspTag> create() {
90  				TextareaTag textareaTag = new TextareaTag();
91  				textareaTag.setName("stringField");
92  				return Arrays.asList(new JspTag[] { textareaTag });
93  			}
94  
95  		});
96  
97  		System.out.println(context.getResult());
98  		// "<form action=\"/todo/save\" >\n" +
99  		// "<textarea name=\"stringField\" >value1</textarea>\n" +
100 		// "</form>\n"
101 
102 		Element element = getResultAsElementFromContext();
103 		String message = "フォームオブジェクトが指定、子要素がある場合";
104 		assertEquals(message, 1, element.getAttributes().size());
105 		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
106 		assertEquals(message, 1, element.getChildren().size());
107 		Element child = element.getChild("textarea");
108 		assertEquals(message, 1, child.getAttributes().size());
109 		assertEquals(message, "stringField", child.getAttributeValue("name"));
110 		assertEquals(message, "value1", child.getValue());
111 	}
112 
113 	public void testDoTagWithSpecifiedAction() throws Exception {
114 		FormDto form = new FormDto();
115 		form.setStringField("value1");
116 
117 		tag.setValue(form);
118 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
119 		tag.setActionMethod("foo");
120 		doLifecycle(tag);
121 
122 		System.out.println(context.getResult());
123 		// "<form action=\"/brabra/mockFormTagTest/bar/123?token=abc\" >\n" +
124 		// "</form>\n", context.getResult());
125 
126 		Element element = getResultAsElementFromContext();
127 		String message = "アクションクラス、メソッド指定";
128 		assertEquals(message, 1, element.getAttributes().size());
129 		assertEquals(message, "/brabra/mockFormTagTest/foo", element
130 				.getAttributeValue("action"));
131 		assertEquals(message, 0, element.getChildren().size());
132 	}
133 
134 	public void testDoTagWithSpecifiedActionAndParam() throws Exception {
135 		FormDto form = new FormDto();
136 		form.setStringField("value1");
137 
138 		tag.setValue(form);
139 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
140 		tag.setActionMethod("bar");
141 		doLifecycle(tag, new ChildrenFactory() {
142 
143 			public List<JspTag> create() {
144 				ParamTag paramTag1 = new ParamTag();
145 				paramTag1.setName("id");
146 				paramTag1.setValue("123");
147 				ParamTag paramTag2 = new ParamTag();
148 				paramTag2.setName("token");
149 				paramTag2.setValue("abc");
150 				return Arrays.asList(new JspTag[] { paramTag1, paramTag2 });
151 			}
152 
153 		});
154 
155 		System.out.println(context.getResult());
156 		// "<form action=\"/brabra/mockFormTagTest/bar/123?token=abc\" >\n" +
157 		// "</form>\n", context.getResult());
158 
159 		Element element = getResultAsElementFromContext();
160 		String message = "アクションクラス、メソッド指定、paramタグあり";
161 		assertEquals(message, 1, element.getAttributes().size());
162 		assertEquals(message, "/brabra/mockFormTagTest/bar/123?token=abc",
163 				element.getAttributeValue("action"));
164 		assertEquals(message, 0, element.getChildren().size());
165 	}
166 
167 	public void testDoTagWithTextAreaAndSpecifiedActionAndParam()
168 			throws Exception {
169 		FormDto form = new FormDto();
170 		form.setStringField("value1");
171 
172 		tag.setValue(form);
173 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
174 		tag.setActionMethod("bar");
175 		doLifecycle(tag, new ChildrenFactory() {
176 
177 			public List<JspTag> create() {
178 				ParamTag paramTag1 = new ParamTag();
179 				paramTag1.setName("id");
180 				paramTag1.setValue("123");
181 				ParamTag paramTag2 = new ParamTag();
182 				paramTag2.setName("token");
183 				paramTag2.setValue("abc");
184 				InputTag inputTag = new InputTag();
185 				inputTag.setType("text");
186 				inputTag.setName("stringField");
187 				return Arrays.asList(new JspTag[] { paramTag1, paramTag2,
188 						inputTag });
189 			}
190 
191 		});
192 
193 		System.out.println(context.getResult());
194 		// "<form action=\"/brabra/mockFormTagTest/bar/123?token=abc\" >\n" +
195 		// "</form>\n", context.getResult());
196 
197 		Element element = getResultAsElementFromContext();
198 		String message = "アクションクラス、メソッド指定、paramタグあり";
199 		assertEquals(message, 1, element.getAttributes().size());
200 		assertEquals(message, "/brabra/mockFormTagTest/bar/123?token=abc",
201 				element.getAttributeValue("action"));
202 		assertEquals(message, 1, element.getChildren().size());
203 		Element child = element.getChild("input");
204 		assertEquals(message, 3, child.getAttributes().size());
205 		assertEquals(message, "text", child.getAttributeValue("type"));
206 		assertEquals(message, "stringField", child.getAttributeValue("name"));
207 		assertEquals(message, "value1", child.getAttributeValue("value"));
208 		assertEquals(message, "", child.getValue());
209 	}
210 
211 }