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.assertNull;
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.FormatPattern;
32  import org.seasar.cubby.controller.impl.DefaultFormatPattern;
33  import org.seasar.cubby.converter.ConversionHelper;
34  import org.seasar.cubby.converter.Converter;
35  import org.seasar.cubby.internal.controller.FormWrapper;
36  import org.seasar.cubby.internal.controller.FormWrapperFactory;
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 		assertNull(specifiedName);
161 
162 		String[] foo = formWrapper.getValues("foo");
163 		assertArrayEquals(new String[] { "def" }, foo);
164 
165 		String[] specifiedConverter = formWrapper
166 				.getValues("specifiedConverter");
167 		assertArrayEquals(new String[] { "ghi" }, specifiedConverter);
168 
169 		String[] specifiedNameAndConverter = formWrapper
170 				.getValues("specifiedNameAndConverter");
171 		assertNull(specifiedNameAndConverter);
172 
173 		String[] bar = formWrapper.getValues("bar");
174 		assertArrayEquals(new String[] { "jkl" }, bar);
175 	}
176 
177 	public static class TestBean {
178 
179 		Date date;
180 
181 		Integer num1;
182 
183 		Integer[] num2;
184 
185 		List<String> num3;
186 
187 		public Date getDate() {
188 			if (date == null) {
189 				return null;
190 			}
191 			return new Date(date.getTime());
192 		}
193 
194 		public void setDate(Date date) {
195 			if (date == null) {
196 				this.date = null;
197 			} else {
198 				this.date = new Date(date.getTime());
199 			}
200 		}
201 
202 		public Integer getNum1() {
203 			return num1;
204 		}
205 
206 		public void setNum1(Integer num1) {
207 			this.num1 = num1;
208 		}
209 
210 		public Integer[] getNum2() {
211 			return num2 == null ? null : num2.clone();
212 		}
213 
214 		public void setNum2(Integer[] num2) {
215 			this.num2 = num2 == null ? null : num2.clone();
216 		}
217 
218 		public List<String> getNum3() {
219 			return num3;
220 		}
221 
222 		public void setNum3(List<String> num3) {
223 			this.num3 = num3;
224 		}
225 
226 	}
227 
228 	public static class AnnotatedBean {
229 
230 		private String normal;
231 
232 		private String specifiedName;
233 
234 		private String specifiedConverter;
235 
236 		private String specifiedNameAndConverter;
237 
238 		public String getNormal() {
239 			return normal;
240 		}
241 
242 		@RequestParameter
243 		public void setNormal(String normal) {
244 			this.normal = normal;
245 		}
246 
247 		public String getSpecifiedName() {
248 			return specifiedName;
249 		}
250 
251 		@RequestParameter(name = "foo")
252 		public void setSpecifiedName(String specifiedName) {
253 			this.specifiedName = specifiedName;
254 		}
255 
256 		public String getSpecifiedConverter() {
257 			return specifiedConverter;
258 		}
259 
260 		@RequestParameter(converter = BraceConverter.class)
261 		public void setSpecifiedConverter(String specifiedConverter) {
262 			this.specifiedConverter = specifiedConverter;
263 		}
264 
265 		public String getSpecifiedNameAndConverter() {
266 			return specifiedNameAndConverter;
267 		}
268 
269 		@RequestParameter(name = "bar", converter = BraceConverter.class)
270 		public void setSpecifiedNameAndConverter(
271 				String specifiedNameAndConverter) {
272 			this.specifiedNameAndConverter = specifiedNameAndConverter;
273 		}
274 
275 	}
276 
277 	public static class BraceConverter implements Converter {
278 
279 		public Object convertToObject(Object value, Class<?> objectType,
280 				ConversionHelper helper) {
281 			if (value == null) {
282 				return null;
283 			}
284 			return "{" + value + "}";
285 		}
286 
287 		public String convertToString(Object value, ConversionHelper helper) {
288 			if (value == null) {
289 				return null;
290 			}
291 			return value.toString().substring(1, value.toString().length() - 1);
292 		}
293 
294 		public Class<?> getObjectType() {
295 			return String.class;
296 		}
297 
298 		public boolean canConvert(Class<?> parameterType, Class<?> objectType) {
299 			return false;
300 		}
301 
302 	}
303 
304 }