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