GenericConversionServiceTests.java 8.9 KB
Newer Older
K
Keith Donald 已提交
1
/*
2
 * Copyright 2002-2009 the original author or authors.
K
Keith Donald 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
16

17
package org.springframework.core.convert.support;
K
Keith Donald 已提交
18

K
polish  
Keith Donald 已提交
19 20 21
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;
K
Keith Donald 已提交
22 23
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
K
polish  
Keith Donald 已提交
24

K
Keith Donald 已提交
25 26
import java.util.AbstractList;
import java.util.ArrayList;
K
polish  
Keith Donald 已提交
27
import java.util.HashMap;
28
import java.util.LinkedHashSet;
K
Keith Donald 已提交
29 30
import java.util.LinkedList;
import java.util.List;
K
polish  
Keith Donald 已提交
31
import java.util.Map;
32
import java.util.Set;
K
Keith Donald 已提交
33

34
import org.junit.Ignore;
K
Keith Donald 已提交
35
import org.junit.Test;
K
polish  
Keith Donald 已提交
36
import org.springframework.core.convert.ConversionFailedException;
37
import org.springframework.core.convert.ConverterNotFoundException;
K
Keith Donald 已提交
38
import org.springframework.core.convert.TypeDescriptor;
K
Keith Donald 已提交
39 40
import org.springframework.core.convert.converter.Converter;

41 42 43 44
/**
 * @author Keith Donald
 */
public class GenericConversionServiceTests {
K
Keith Donald 已提交
45

46
	private GenericConversionService converter = new GenericConversionService();
K
Keith Donald 已提交
47

K
polish  
Keith Donald 已提交
48 49
	@Test
	public void executeConversion() {
50
		converter.addConverterFactory(new StringToNumberConverterFactory());
K
polish  
Keith Donald 已提交
51
		assertEquals(new Integer(3), converter.convert("3", Integer.class));
K
Keith Donald 已提交
52
	}
K
polish  
Keith Donald 已提交
53 54 55

	@Test
	public void executeConversionNullSource() {
K
polish  
Keith Donald 已提交
56
		assertEquals(null, converter.convert(null, Integer.class));
K
polish  
Keith Donald 已提交
57
	}
K
Keith Donald 已提交
58

K
Keith Donald 已提交
59 60
	@Test
	public void executeCompatibleSource() {
K
polish  
Keith Donald 已提交
61
		assertEquals(Boolean.FALSE, converter.convert(false, boolean.class));
K
Keith Donald 已提交
62 63
	}

K
polish  
Keith Donald 已提交
64
	@Test
K
polish  
Keith Donald 已提交
65
	public void converterNotFound() {
K
Keith Donald 已提交
66
		try {
K
polish  
Keith Donald 已提交
67
			converter.convert("3", Integer.class);
K
Keith Donald 已提交
68
			fail("Should have thrown an exception");
69
		} catch (ConverterNotFoundException e) {
K
Keith Donald 已提交
70 71 72
		}
	}

K
polish  
Keith Donald 已提交
73 74
	@Test
	public void addConverterNoSourceTargetClassInfoAvailable() {
K
polish  
Keith Donald 已提交
75
		try {
76
			converter.addConverter(new Converter() {
77
				public Object convert(Object source) {
K
polish  
Keith Donald 已提交
78 79 80 81 82 83 84 85 86
					return source;
				}
			});
			fail("Should have failed");
		} catch (IllegalArgumentException e) {

		}
	}

K
polish  
Keith Donald 已提交
87 88
	@Test
	public void convertNull() {
K
polish  
Keith Donald 已提交
89
		assertNull(converter.convert(null, Integer.class));
K
Keith Donald 已提交
90
	}
K
Keith Donald 已提交
91 92

	@Test(expected = IllegalArgumentException.class)
93
	public void convertNullTargetClass() {
K
Keith Donald 已提交
94
		assertEquals("3", converter.convert("3", (Class<?>) null));
95
	}
K
Keith Donald 已提交
96

97 98
	@Test
	public void convertNullConversionPointType() {
K
Keith Donald 已提交
99
		assertEquals(null, converter.convert(3, TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
100 101
	}

K
polish  
Keith Donald 已提交
102 103
	@Test
	public void convertWrongTypeArgument() {
104
		converter.addConverterFactory(new StringToNumberConverterFactory());
K
Keith Donald 已提交
105
		try {
K
polish  
Keith Donald 已提交
106
			converter.convert("BOGUS", Integer.class);
K
Keith Donald 已提交
107
			fail("Should have failed");
K
polish  
Keith Donald 已提交
108
		} catch (ConversionFailedException e) {
K
Keith Donald 已提交
109 110 111 112

		}
	}

K
polish  
Keith Donald 已提交
113 114
	@Test
	public void convertSuperSourceType() {
115
		converter.addConverter(new Converter<CharSequence, Integer>() {
116
			public Integer convert(CharSequence source) {
K
Keith Donald 已提交
117 118 119
				return Integer.valueOf(source.toString());
			}
		});
K
polish  
Keith Donald 已提交
120
		Integer result = converter.convert("3", Integer.class);
K
Keith Donald 已提交
121 122 123
		assertEquals(new Integer(3), result);
	}

K
polish  
Keith Donald 已提交
124 125
	@Test
	public void convertObjectToPrimitive() {
126
		converter.addConverterFactory(new StringToNumberConverterFactory());
K
polish  
Keith Donald 已提交
127
		Integer three = converter.convert("3", int.class);
K
Keith Donald 已提交
128 129 130
		assertEquals(3, three.intValue());
	}

K
polish  
Keith Donald 已提交
131 132
	@Test
	public void convertArrayToArray() {
133
		converter.addConverterFactory(new StringToNumberConverterFactory());
K
polish  
Keith Donald 已提交
134
		Integer[] result = converter.convert(new String[] { "1", "2", "3" }, Integer[].class);
K
Keith Donald 已提交
135 136 137 138 139
		assertEquals(new Integer(1), result[0]);
		assertEquals(new Integer(2), result[1]);
		assertEquals(new Integer(3), result[2]);
	}

K
polish  
Keith Donald 已提交
140 141
	@Test
	public void convertArrayToPrimitiveArray() {
142
		converter.addConverterFactory(new StringToNumberConverterFactory());
J
Juergen Hoeller 已提交
143
		int[] result = converter.convert(new String[] { "1", "2", "3" }, int[].class);
K
Keith Donald 已提交
144 145 146 147
		assertEquals(1, result[0]);
		assertEquals(2, result[1]);
		assertEquals(3, result[2]);
	}
K
Keith Donald 已提交
148 149 150 151 152 153 154 155
	
	@Test
	public void convertArrayToArrayAssignable() {
		int[] result = converter.convert(new int[] { 1, 2, 3 }, int[].class);
		assertEquals(1, result[0]);
		assertEquals(2, result[1]);
		assertEquals(3, result[2]);
	}
K
Keith Donald 已提交
156

K
polish  
Keith Donald 已提交
157 158
	@Test
	public void convertArrayToListInterface() {
K
polish  
Keith Donald 已提交
159
		List<?> result = converter.convert(new String[] { "1", "2", "3" }, List.class);
K
Keith Donald 已提交
160 161 162 163 164
		assertEquals("1", result.get(0));
		assertEquals("2", result.get(1));
		assertEquals("3", result.get(2));
	}

K
polish  
Keith Donald 已提交
165
	public List<Integer> genericList = new ArrayList<Integer>();
K
polish  
Keith Donald 已提交
166 167 168

	@Test
	public void convertArrayToListGenericTypeConversion() throws Exception {
169
		converter.addConverterFactory(new StringToNumberConverterFactory());
K
Keith Donald 已提交
170 171
		List<Integer> result = (List<Integer>) converter.convert(new String[] { "1", "2", "3" }, TypeDescriptor
				.valueOf(String[].class), new TypeDescriptor(getClass().getDeclaredField("genericList")));
K
Keith Donald 已提交
172 173 174 175
		assertEquals(new Integer("1"), result.get(0));
		assertEquals(new Integer("2"), result.get(1));
		assertEquals(new Integer("3"), result.get(2));
	}
K
polish  
Keith Donald 已提交
176 177 178

	@Test
	public void convertArrayToListImpl() {
K
polish  
Keith Donald 已提交
179
		LinkedList<?> result = converter.convert(new String[] { "1", "2", "3" }, LinkedList.class);
K
Keith Donald 已提交
180 181 182 183 184
		assertEquals("1", result.get(0));
		assertEquals("2", result.get(1));
		assertEquals("3", result.get(2));
	}

K
Keith Donald 已提交
185
	@Test(expected=ConversionFailedException.class)
K
polish  
Keith Donald 已提交
186
	public void convertArrayToAbstractList() {
K
Keith Donald 已提交
187
		converter.convert(new String[] { "1", "2", "3" }, AbstractList.class);
K
Keith Donald 已提交
188 189
	}

K
polish  
Keith Donald 已提交
190 191
	@Test
	public void convertListToArray() {
K
polish  
Keith Donald 已提交
192
		List<String> list = new ArrayList<String>();
K
Keith Donald 已提交
193 194 195
		list.add("1");
		list.add("2");
		list.add("3");
J
Juergen Hoeller 已提交
196
		String[] result = converter.convert(list, String[].class);
K
Keith Donald 已提交
197 198 199 200 201
		assertEquals("1", result[0]);
		assertEquals("2", result[1]);
		assertEquals("3", result[2]);
	}

K
polish  
Keith Donald 已提交
202 203
	@Test
	public void convertListToArrayWithComponentConversion() {
204
		converter.addConverterFactory(new StringToNumberConverterFactory());
K
polish  
Keith Donald 已提交
205
		List<String> list = new ArrayList<String>();
K
Keith Donald 已提交
206 207 208
		list.add("1");
		list.add("2");
		list.add("3");
K
polish  
Keith Donald 已提交
209
		Integer[] result = converter.convert(list, Integer[].class);
K
Keith Donald 已提交
210 211 212 213 214
		assertEquals(new Integer(1), result[0]);
		assertEquals(new Integer(2), result[1]);
		assertEquals(new Integer(3), result[2]);
	}

215 216 217 218 219 220 221
	@Test
	public void convertCollectionToCollection() throws Exception {
		converter.addConverterFactory(new StringToNumberConverterFactory());
		Set<String> foo = new LinkedHashSet<String>();
		foo.add("1");
		foo.add("2");
		foo.add("3");
K
Keith Donald 已提交
222 223
		List<Integer> bar = (List<Integer>) converter.convert(foo, TypeDescriptor.valueOf(List.class),
				new TypeDescriptor(getClass().getField("genericList")));
224 225 226 227
		assertEquals(new Integer(1), bar.get(0));
		assertEquals(new Integer(2), bar.get(1));
		assertEquals(new Integer(3), bar.get(2));
	}
K
Keith Donald 已提交
228

K
polish  
Keith Donald 已提交
229 230 231 232 233 234 235
	public Map<Integer, FooEnum> genericMap = new HashMap<Integer, FooEnum>();

	@Test
	public void convertMapToMap() throws Exception {
		Map<String, String> foo = new HashMap<String, String>();
		foo.put("1", "BAR");
		foo.put("2", "BAZ");
236 237
		converter.addConverterFactory(new StringToNumberConverterFactory());
		converter.addConverterFactory(new StringToEnumConverterFactory());
K
Keith Donald 已提交
238 239
		Map<String, FooEnum> map = (Map<String, FooEnum>) converter.convert(foo, TypeDescriptor.valueOf(Map.class),
				new TypeDescriptor(getClass().getField("genericMap")));
K
polish  
Keith Donald 已提交
240 241
		assertEquals(map.get(1), FooEnum.BAR);
		assertEquals(map.get(2), FooEnum.BAZ);
K
polish  
Keith Donald 已提交
242 243
	}

K
Keith Donald 已提交
244
	@Test
K
Keith Donald 已提交
245 246 247 248 249 250 251 252 253 254 255 256
	public void convertObjectToArray() {
		String[] result = converter.convert("test", String[].class);
		assertEquals(1, result.length);
		assertEquals("test", result[0]);
	}

	@Test
	public void convertObjectToArrayWithElementConversion() {
		converter.addConverterFactory(new StringToNumberConverterFactory());
		Integer[] result = converter.convert("1", Integer[].class);
		assertEquals(1, result.length);
		assertEquals(new Integer(1), result[0]);
K
Keith Donald 已提交
257
	}
K
Keith Donald 已提交
258

K
polish  
Keith Donald 已提交
259
	@Test
260
	@Ignore
K
polish  
Keith Donald 已提交
261
	public void convertStringToArrayWithElementConversion() {
K
Keith Donald 已提交
262
		converter.addConverterFactory(new StringToNumberConverterFactory());
J
Juergen Hoeller 已提交
263
		Integer[] result = converter.convert("1,2,3", Integer[].class);
K
polish  
Keith Donald 已提交
264 265 266 267
		assertEquals(3, result.length);
		assertEquals(new Integer(1), result[0]);
		assertEquals(new Integer(2), result[1]);
		assertEquals(new Integer(3), result[2]);
K
Keith Donald 已提交
268 269
	}

K
Keith Donald 已提交
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
	@Test
	public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
		try {
			converter.convert("1", Integer[].class);
		} catch (ConversionFailedException e) {
			assertTrue(e.getCause() instanceof ConverterNotFoundException);
		}
	}
	
	@Test
	public void parent() {
		GenericConversionService parent = new GenericConversionService();
		converter.setParent(parent);
		assertFalse(converter.canConvert(String.class, Integer.class));
		try {
			converter.convert("3", Integer.class);
		} catch (ConverterNotFoundException e) {
			
		}
	}

K
Keith Donald 已提交
291
	public static enum FooEnum {
K
polish  
Keith Donald 已提交
292
		BAR, BAZ
K
Keith Donald 已提交
293 294
	}

J
Juergen Hoeller 已提交
295
}