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