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