    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.internal.controller.impl;
    17
    18  import static org.junit.Assert.assertArrayEquals;
    19  import static org.junit.Assert.assertEquals;
    20  import static org.junit.Assert.*;
    21
    22  import java.util.Arrays;
    23  import java.util.Calendar;
    24  import java.util.Date;
    25  import java.util.List;
    26
    27  import org.junit.After;
    28  import org.junit.Before;
    29  import org.junit.Test;
    30  import org.seasar.cubby.action.RequestParameter;
    31  import org.seasar.cubby.controller.FormWrapper;
    32  import org.seasar.cubby.controller.FormWrapperFactory;
    33  import org.seasar.cubby.controller.FormatPattern;
    34  import org.seasar.cubby.controller.impl.DefaultFormatPattern;
    35  import org.seasar.cubby.converter.ConversionHelper;
    36  import org.seasar.cubby.converter.Converter;
    37  import org.seasar.cubby.mock.MockContainerProvider;
    38  import org.seasar.cubby.mock.MockConverterProvider;
    39  import org.seasar.cubby.plugin.PluginRegistry;
    40  import org.seasar.cubby.plugins.BinderPlugin;
    41  import org.seasar.cubby.spi.BeanDescProvider;
    42  import org.seasar.cubby.spi.ContainerProvider;
    43  import org.seasar.cubby.spi.ConverterProvider;
    44  import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
    45  import org.seasar.cubby.spi.container.Container;
    46  import org.seasar.cubby.spi.container.LookupException;
    47
    48  public class FormWrapperFactoryImplTest {
    49
    50  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
    51
    52  	private FormWrapperFactory formWrapperFactory;
    53
    54  	@Before
    55  	public void setup() {
    56  		final FormatPattern formatPattern = new DefaultFormatPattern();
    57  		final BinderPlugin binderPlugin = new BinderPlugin();
    58  		binderPlugin.bind(ContainerProvider.class).toInstance(
    59  				new MockContainerProvider(new Container() {
    60
    61  					public <T> T lookup(Class<T> type) {
    62  						if (FormatPattern.class.equals(type)) {
    63  							return type.cast(formatPattern);
    64  						}
    65  						throw new LookupException(type.getName());
    66  					}
    67  				}));
    68  		binderPlugin.bind(BeanDescProvider.class).toInstance(
    69  				new DefaultBeanDescProvider());
    70  		binderPlugin.bind(ConverterProvider.class).toInstance(
    71  				new MockConverterProvider(new BraceConverter()));
    72
    73  		pluginRegistry.register(binderPlugin);
    74
    75  		formWrapperFactory = new FormWrapperFactoryImpl();
    76  	}
    77
    78  	@After
    79  	public void teadown() {
    80  		pluginRegistry.clear();
    81  	}
    82
    83  	@Test
    84  	public void beanToMap() {
    85  		Calendar cal = Calendar.getInstance();
    86  		cal.set(2006, 0, 1);
    87
    88  		TestBean bean = new TestBean();
    89  		bean.setDate(cal.getTime());
    90  		bean.setNum1(5);
    91  		bean.setNum2(new Integer[] { 2, 3, 4 });
    92  		bean.setNum3(Arrays.asList(new String[] { "abc", "def" }));
    93
    94  		FormWrapper formWrapper = formWrapperFactory.create(bean);
    95  		String[] date = formWrapper.getValues("date");
    96  		assertEquals(1, date.length);
    97  		assertEquals("2006-01-01", date[0]);
    98
    99  		String[] num1 = formWrapper.getValues("num1");
    100 		assertEquals(1, num1.length);
    101 		assertEquals("5", num1[0]);
    102
    103 		String[] num2 = formWrapper.getValues("num2");
    104 		assertEquals(3, num2.length);
    105 		assertEquals("2", num2[0]);
    106 		assertEquals("3", num2[1]);
    107 		assertEquals("4", num2[2]);
    108
    109 		String[] num3 = formWrapper.getValues("num3");
    110 		assertEquals(2, num3.length);
    111 		assertEquals("abc", num3[0]);
    112 		assertEquals("def", num3[1]);
    113
    114 		String[] noprop = formWrapper.getValues("noprop");
    115 		assertNull(noprop);
    116 	}
    117
    118 	@Test
    119 	public void beanToMap2() {
    120 		TestBean bean = new TestBean();
    121 		bean.setNum2(new Integer[] { null, null, null });
    122 		bean.setNum3(Arrays.asList(new String[] { null, null }));
    123
    124 		FormWrapper formWrapper = formWrapperFactory.create(bean);
    125 		String[] date = formWrapper.getValues("date");
    126 		assertNull(date);
    127
    128 		String[] num1 = formWrapper.getValues("num1");
    129 		assertNull(num1);
    130
    131 		String[] num2 = formWrapper.getValues("num2");
    132 		assertEquals(3, num2.length);
    133 		assertNull(num2[0]);
    134 		assertNull(num2[1]);
    135 		assertNull(num2[2]);
    136
    137 		String[] num3 = formWrapper.getValues("num3");
    138 		assertEquals(2, num3.length);
    139 		assertNull(num3[0]);
    140 		assertNull(num3[1]);
    141
    142 		String[] noprop = formWrapper.getValues("noprop");
    143 		assertNull(noprop);
    144 	}
    145
    146 	@Test
    147 	public void beanToMap3() {
    148 		AnnotatedBean bean = new AnnotatedBean();
    149 		bean.setNormal("abc");
    150 		bean.setSpecifiedName("def");
    151 		bean.setSpecifiedConverter("{ghi}");
    152 		bean.setSpecifiedNameAndConverter("{jkl}");
    153
    154 		FormWrapper formWrapper = formWrapperFactory.create(bean);
    155
    156 		String[] normal = formWrapper.getValues("normal");
    157 		assertArrayEquals(new String[] { "abc" }, normal);
    158
    159 		String[] specifiedName = formWrapper.getValues("specifiedName");
    160 		assertNotNull(specifiedName);
    161
    162 		String[] specifiedName2 = formWrapper.getValues("specifiedName2");
    163 		assertNull(specifiedName2);
    164
    165 		String[] foo = formWrapper.getValues("foo");
    166 		assertArrayEquals(new String[] { "def" }, foo);
    167
    168 		String[] specifiedConverter = formWrapper
    169 				.getValues("specifiedConverter");
    170 		assertArrayEquals(new String[] { "ghi" }, specifiedConverter);
    171
    172 		String[] specifiedNameAndConverter = formWrapper
    173 				.getValues("specifiedNameAndConverter");
    174 		assertNotNull(specifiedNameAndConverter);
    175
    176 		String[] specifiedNameAndConverter2 = formWrapper
    177 				.getValues("specifiedNameAndConverter2");
    178 		assertNull(specifiedNameAndConverter2);
    179
    180 		String[] bar = formWrapper.getValues("bar");
    181 		assertArrayEquals(new String[] { "jkl" }, bar);
    182 	}
    183
    184 	public static class TestBean {
    185
    186 		Date date;
    187
    188 		Integer num1;
    189
    190 		Integer[] num2;
    191
    192 		List<String> num3;
    193
    194 		public Date getDate() {
    195 			if (date == null) {
    196 				return null;
    197 			}
    198 			return new Date(date.getTime());
    199 		}
    200
    201 		public void setDate(Date date) {
    202 			if (date == null) {
    203 				this.date = null;
    204 			} else {
    205 				this.date = new Date(date.getTime());
    206 			}
    207 		}
    208
    209 		public Integer getNum1() {
    210 			return num1;
    211 		}
    212
    213 		public void setNum1(Integer num1) {
    214 			this.num1 = num1;
    215 		}
    216
    217 		public Integer[] getNum2() {
    218 			return num2 == null ? null : num2.clone();
    219 		}
    220
    221 		public void setNum2(Integer[] num2) {
    222 			this.num2 = num2 == null ? null : num2.clone();
    223 		}
    224
    225 		public List<String> getNum3() {
    226 			return num3;
    227 		}
    228
    229 		public void setNum3(List<String> num3) {
    230 			this.num3 = num3;
    231 		}
    232
    233 	}
    234
    235 	public static class AnnotatedBean {
    236
    237 		private String normal;
    238
    239 		private String specifiedName;
    240
    241 		private String specifiedConverter;
    242
    243 		private String specifiedNameAndConverter;
    244
    245 		public String getNormal() {
    246 			return normal;
    247 		}
    248
    249 		@RequestParameter
    250 		public void setNormal(String normal) {
    251 			this.normal = normal;
    252 		}
    253
    254 		public String getSpecifiedName() {
    255 			return specifiedName;
    256 		}
    257
    258 		@RequestParameter(name = "foo")
    259 		public void setSpecifiedName(String specifiedName) {
    260 			this.specifiedName = specifiedName;
    261 		}
    262
    263 		public String getSpecifiedConverter() {
    264 			return specifiedConverter;
    265 		}
    266
    267 		@RequestParameter(converter = BraceConverter.class)
    268 		public void setSpecifiedConverter(String specifiedConverter) {
    269 			this.specifiedConverter = specifiedConverter;
    270 		}
    271
    272 		public String getSpecifiedNameAndConverter() {
    273 			return specifiedNameAndConverter;
    274 		}
    275
    276 		@RequestParameter(name = "bar", converter = BraceConverter.class)
    277 		public void setSpecifiedNameAndConverter(
    278 				String specifiedNameAndConverter) {
    279 			this.specifiedNameAndConverter = specifiedNameAndConverter;
    280 		}
    281
    282 	}
    283
    284 	public static class BraceConverter implements Converter {
    285
    286 		public Object convertToObject(Object value, Class<?> objectType,
    287 				ConversionHelper helper) {
    288 			if (value == null) {
    289 				return null;
    290 			}
    291 			return "{" + value + "}";
    292 		}
    293
    294 		public String convertToString(Object value, ConversionHelper helper) {
    295 			if (value == null) {
    296 				return null;
    297 			}
    298 			return value.toString().substring(1, value.toString().length() - 1);
    299 		}
    300
    301 		public Class<?> getObjectType() {
    302 			return String.class;
    303 		}
    304
    305 		public boolean canConvert(Class<?> parameterType, Class<?> objectType) {
    306 			return false;
    307 		}
    308
    309 	}
    310
    311 }