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.assertNull;
20  
21  import java.util.Arrays;
22  import java.util.List;
23  
24  import javax.servlet.jsp.PageContext;
25  import javax.servlet.jsp.tagext.JspTag;
26  
27  import org.jdom.Element;
28  import org.junit.Before;
29  import org.junit.Test;
30  import org.seasar.cubby.CubbyConstants;
31  import org.seasar.cubby.action.RequestMethod;
32  import org.seasar.cubby.controller.FormatPattern;
33  import org.seasar.cubby.controller.impl.DefaultFormatPattern;
34  import org.seasar.cubby.mock.MockContainerProvider;
35  import org.seasar.cubby.mock.MockConverterProvider;
36  import org.seasar.cubby.mock.MockPathResolverProvider;
37  import org.seasar.cubby.plugin.PluginRegistry;
38  import org.seasar.cubby.plugins.BinderPlugin;
39  import org.seasar.cubby.routing.PathResolver;
40  import org.seasar.cubby.routing.impl.PathResolverImpl;
41  import org.seasar.cubby.routing.impl.PathTemplateParserImpl;
42  import org.seasar.cubby.spi.ContainerProvider;
43  import org.seasar.cubby.spi.ConverterProvider;
44  import org.seasar.cubby.spi.PathResolverProvider;
45  import org.seasar.cubby.spi.container.Container;
46  import org.seasar.cubby.spi.container.LookupException;
47  
48  public class FormTagTest extends AbstractStandardTagTestCase {
49  
50  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
51  
52  	private FormTag tag;
53  
54  	@Before
55  	public void setupProvider() {
56  		final BinderPlugin binderPlugin = new BinderPlugin();
57  		final PathResolver pathResolver = new PathResolverImpl(
58  				new PathTemplateParserImpl());
59  		pathResolver.add("/mockFormTagTest/foo", MockFormTagTestAction.class,
60  				"foo", RequestMethod.GET, null, 0);
61  		pathResolver.add("/mockFormTagTest/bar/{id}",
62  				MockFormTagTestAction.class, "bar", RequestMethod.GET, null, 0);
63  		binderPlugin.bind(PathResolverProvider.class).toInstance(
64  				new MockPathResolverProvider(pathResolver));
65  		final FormatPattern formatPattern = new DefaultFormatPattern();
66  		binderPlugin.bind(ContainerProvider.class).toInstance(
67  				new MockContainerProvider(new Container() {
68  
69  					public <T> T lookup(Class<T> type) {
70  						if (FormatPattern.class.equals(type)) {
71  							return type.cast(formatPattern);
72  						}
73  						throw new LookupException(type.getName());
74  					}
75  
76  				}));
77  		binderPlugin.bind(ConverterProvider.class).toInstance(
78  				new MockConverterProvider());
79  		pluginRegistry.register(binderPlugin);
80  	}
81  
82  	@Before
83  	public void setUp() throws Exception {
84  		tag = new FormTag();
85  		setupBodyTag(tag);
86  		setupErrors(context);
87  		context.setAttribute(CubbyConstants.ATTR_CONTEXT_PATH, "/brabra",
88  				PageContext.REQUEST_SCOPE);
89  	}
90  
91  	@Test
92  	public void doTagNoChild() throws Exception {
93  		FormDto form = new FormDto();
94  		form.setStringField("value1");
95  
96  		tag.setValue(form);
97  		tag.setDynamicAttribute(null, "action", "/todo/save");
98  		doLifecycle(tag);
99  
100 		System.out.println(context.getResult());
101 		// "<form action=\"/todo/save\" >\n</form>\n"
102 
103 		Element element = getResultAsElementFromContext();
104 		String message = "フォームオブジェクトが指定";
105 		assertEquals(message, 1, element.getAttributes().size());
106 		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
107 		assertNull("フォームオブジェクトは除去されていること", context.findAttribute("__form"));
108 	}
109 
110 	@Test
111 	public void doTagEmptyBody() throws Exception {
112 		FormDto form = new FormDto();
113 		form.setStringField("value1");
114 
115 		tag.setValue(form);
116 		tag.setDynamicAttribute(null, "action", "/todo/save");
117 
118 		doLifecycle(tag);
119 
120 		System.out.println(context.getResult());
121 		// "<form action=\"/todo/save\" >\n</form>\n"
122 
123 		Element element = getResultAsElementFromContext();
124 		String message = "Bodyが空の場合";
125 		assertEquals(message, 1, element.getAttributes().size());
126 		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
127 	}
128 
129 	@Test
130 	public void doTagWithTextAreaTag() throws Exception {
131 		FormDto form = new FormDto();
132 		form.setStringField("value1");
133 
134 		tag.setValue(form);
135 		tag.setDynamicAttribute(null, "action", "/todo/save");
136 		doLifecycle(tag, new ChildrenFactory() {
137 
138 			public List<JspTag> create() {
139 				TextareaTag textareaTag = new TextareaTag();
140 				textareaTag.setName("stringField");
141 				return Arrays.asList(new JspTag[] { textareaTag });
142 			}
143 
144 		});
145 
146 		System.out.println(context.getResult());
147 		// "<form action=\"/todo/save\" >\n" +
148 		// "<textarea name=\"stringField\" >value1</textarea>\n" +
149 		// "</form>\n"
150 
151 		Element element = getResultAsElementFromContext();
152 		String message = "フォームオブジェクトが指定、子要素がある場合";
153 		assertEquals(message, 1, element.getAttributes().size());
154 		assertEquals(message, "/todo/save", element.getAttributeValue("action"));
155 		assertEquals(message, 1, element.getChildren().size());
156 		Element child = element.getChild("textarea");
157 		assertEquals(message, 1, child.getAttributes().size());
158 		assertEquals(message, "stringField", child.getAttributeValue("name"));
159 		assertEquals(message, "value1", child.getValue());
160 	}
161 
162 	@Test
163 	public void doTagWithSpecifiedAction() throws Exception {
164 		FormDto form = new FormDto();
165 		form.setStringField("value1");
166 
167 		tag.setValue(form);
168 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
169 		tag.setActionMethod("foo");
170 		doLifecycle(tag);
171 
172 		System.out.println(context.getResult());
173 		// "<form action=\"/brabra/mockFormTagTest/bar/123?token=abc\" >\n" +
174 		// "</form>\n", context.getResult());
175 
176 		Element element = getResultAsElementFromContext();
177 		String message = "アクションクラス、メソッド指定";
178 		assertEquals(message, 1, element.getAttributes().size());
179 		assertEquals(message, "/brabra/mockFormTagTest/foo", element
180 				.getAttributeValue("action"));
181 		assertEquals(message, 0, element.getChildren().size());
182 	}
183 
184 	@Test
185 	public void doTagWithSpecifiedActionAndParam() throws Exception {
186 		FormDto form = new FormDto();
187 		form.setStringField("value1");
188 
189 		tag.setValue(form);
190 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
191 		tag.setActionMethod("bar");
192 		doLifecycle(tag, new ChildrenFactory() {
193 
194 			public List<JspTag> create() {
195 				ParamTag paramTag1 = new ParamTag();
196 				paramTag1.setName("id");
197 				paramTag1.setValue("123");
198 				ParamTag paramTag2 = new ParamTag();
199 				paramTag2.setName("token");
200 				paramTag2.setValue("abc");
201 				return Arrays.asList(new JspTag[] { paramTag1, paramTag2 });
202 			}
203 
204 		});
205 
206 		System.out.println(context.getResult());
207 		// "<form action=\"/brabra/mockFormTagTest/bar/123?token=abc\" >\n" +
208 		// "</form>\n", context.getResult());
209 
210 		Element element = getResultAsElementFromContext();
211 		String message = "アクションクラス、メソッド指定、paramタグあり";
212 		assertEquals(message, 1, element.getAttributes().size());
213 		assertEquals(message, "/brabra/mockFormTagTest/bar/123?token=abc",
214 				element.getAttributeValue("action"));
215 		assertEquals(message, 0, element.getChildren().size());
216 	}
217 
218 	@Test
219 	public void doTagWithTextAreaAndSpecifiedActionAndParam() throws Exception {
220 		FormDto form = new FormDto();
221 		form.setStringField("value1");
222 
223 		tag.setValue(form);
224 		tag.setActionClass(MockFormTagTestAction.class.getCanonicalName());
225 		tag.setActionMethod("bar");
226 		doLifecycle(tag, new ChildrenFactory() {
227 
228 			public List<JspTag> create() {
229 				ParamTag paramTag1 = new ParamTag();
230 				paramTag1.setName("id");
231 				paramTag1.setValue("123");
232 				ParamTag paramTag2 = new ParamTag();
233 				paramTag2.setName("token");
234 				paramTag2.setValue("abc");
235 				InputTag inputTag = new InputTag();
236 				inputTag.setType("text");
237 				inputTag.setName("stringField");
238 				return Arrays.asList(new JspTag[] { paramTag1, paramTag2,
239 						inputTag });
240 			}
241 
242 		});
243 
244 		System.out.println(context.getResult());
245 		// "<form action=\"/brabra/mockFormTagTest/bar/123?token=abc\" >\n" +
246 		// "</form>\n", context.getResult());
247 
248 		Element element = getResultAsElementFromContext();
249 		String message = "アクションクラス、メソッド指定、paramタグあり";
250 		assertEquals(message, 1, element.getAttributes().size());
251 		assertEquals(message, "/brabra/mockFormTagTest/bar/123?token=abc",
252 				element.getAttributeValue("action"));
253 		assertEquals(message, 1, element.getChildren().size());
254 		Element child = element.getChild("input");
255 		assertEquals(message, 3, child.getAttributes().size());
256 		assertEquals(message, "text", child.getAttributeValue("type"));
257 		assertEquals(message, "stringField", child.getAttributeValue("name"));
258 		assertEquals(message, "value1", child.getAttributeValue("value"));
259 		assertEquals(message, "", child.getValue());
260 	}
261 
262 	public void testDoTagProtocol() throws Exception {
263 		FormDto form = new FormDto();
264 		form.setStringField("value1");
265 
266 		tag.setValue(form);
267 		tag.setDynamicAttribute(null, "action", "/todo/save");
268 		tag.setProtocol("https");
269 		doLifecycle(tag);
270 
271 		System.out.println(context.getResult());
272 		// "<form action=\"/todo/save\" >\n</form>\n"
273 
274 		Element element = getResultAsElementFromContext();
275 		String message = "フォームオブジェクトが指定";
276 		assertEquals(message, 1, element.getAttributes().size());
277 		assertEquals(message, "https://localhost/todo/save", element
278 				.getAttributeValue("action"));
279 		assertNull("フォームオブジェクトは除去されていること", context.findAttribute("__form"));
280 	}
281 
282 	public void testDoTagPort() throws Exception {
283 		FormDto form = new FormDto();
284 		form.setStringField("value1");
285 
286 		tag.setValue(form);
287 		tag.setDynamicAttribute(null, "action", "/todo/save");
288 		tag.setPort(8080);
289 		doLifecycle(tag);
290 
291 		System.out.println(context.getResult());
292 		// "<form action=\"/todo/save\" >\n</form>\n"
293 
294 		Element element = getResultAsElementFromContext();
295 		String message = "フォームオブジェクトが指定";
296 		assertEquals(message, 1, element.getAttributes().size());
297 		assertEquals(message, "http://localhost:8080/todo/save", element
298 				.getAttributeValue("action"));
299 		assertNull("フォームオブジェクトは除去されていること", context.findAttribute("__form"));
300 	}
301 
302 	public void testDoTagProtocolAndPort() throws Exception {
303 		FormDto form = new FormDto();
304 		form.setStringField("value1");
305 
306 		tag.setValue(form);
307 		tag.setDynamicAttribute(null, "action", "/todo/save");
308 		tag.setProtocol("https");
309 		tag.setPort(8080);
310 		doLifecycle(tag);
311 
312 		System.out.println(context.getResult());
313 		// "<form action=\"/todo/save\" >\n</form>\n"
314 
315 		Element element = getResultAsElementFromContext();
316 		String message = "フォームオブジェクトが指定";
317 		assertEquals(message, 1, element.getAttributes().size());
318 		assertEquals(message, "https://localhost:8080/todo/save", element
319 				.getAttributeValue("action"));
320 		assertNull("フォームオブジェクトは除去されていること", context.findAttribute("__form"));
321 	}
322 }