PageRenderTime 29ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/springframework-3.0.5/projects/org.springframework.core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 457 lines | 366 code | 72 blank | 19 comment | 11 complexity | c3ae6e2c6befd90895b8a28a7f17962e MD5 | raw file
  1. /*
  2. * Copyright 2002-2010 the original author or authors.
  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, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.core.convert.support;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import java.util.HashMap;
  20. import java.util.LinkedHashMap;
  21. import java.util.LinkedList;
  22. import java.util.List;
  23. import java.util.Map;
  24. import static org.junit.Assert.*;
  25. import org.junit.Test;
  26. import org.springframework.core.convert.ConversionFailedException;
  27. import org.springframework.core.convert.ConverterNotFoundException;
  28. import org.springframework.core.convert.TypeDescriptor;
  29. import org.springframework.core.convert.converter.Converter;
  30. import org.springframework.core.io.Resource;
  31. import org.springframework.core.io.DescriptiveResource;
  32. import org.springframework.util.StopWatch;
  33. import org.springframework.util.StringUtils;
  34. /**
  35. * @author Keith Donald
  36. * @author Juergen Hoeller
  37. */
  38. public class GenericConversionServiceTests {
  39. private GenericConversionService conversionService = new GenericConversionService();
  40. @Test
  41. public void convert() {
  42. conversionService.addConverterFactory(new StringToNumberConverterFactory());
  43. assertEquals(new Integer(3), conversionService.convert("3", Integer.class));
  44. }
  45. @Test
  46. public void convertNullSource() {
  47. assertEquals(null, conversionService.convert(null, Integer.class));
  48. }
  49. @Test
  50. public void convertAssignableSource() {
  51. assertEquals(Boolean.FALSE, conversionService.convert(false, boolean.class));
  52. assertEquals(Boolean.FALSE, conversionService.convert(false, Boolean.class));
  53. }
  54. @Test
  55. public void converterNotFound() {
  56. try {
  57. conversionService.convert("3", Integer.class);
  58. fail("Should have thrown an exception");
  59. }
  60. catch (ConverterNotFoundException e) {
  61. }
  62. }
  63. @Test
  64. public void addConverterNoSourceTargetClassInfoAvailable() {
  65. try {
  66. conversionService.addConverter(new Converter() {
  67. public Object convert(Object source) {
  68. return source;
  69. }
  70. });
  71. fail("Should have failed");
  72. } catch (IllegalArgumentException e) {
  73. }
  74. }
  75. @Test
  76. public void sourceTypeIsVoid() {
  77. GenericConversionService conversionService = new GenericConversionService();
  78. assertFalse(conversionService.canConvert(void.class, String.class));
  79. }
  80. @Test
  81. public void targetTypeIsVoid() {
  82. GenericConversionService conversionService = new GenericConversionService();
  83. assertFalse(conversionService.canConvert(String.class, void.class));
  84. }
  85. @Test
  86. public void convertNull() {
  87. assertNull(conversionService.convert(null, Integer.class));
  88. }
  89. public void convertNullTargetClass() {
  90. assertNull(conversionService.convert("3", null));
  91. }
  92. @Test
  93. public void convertNullTypeDescriptor() {
  94. assertNull(conversionService.convert("3", TypeDescriptor.valueOf(String.class), TypeDescriptor.NULL));
  95. }
  96. @Test
  97. public void convertWrongTypeArgument() {
  98. conversionService.addConverterFactory(new StringToNumberConverterFactory());
  99. try {
  100. conversionService.convert("BOGUS", Integer.class);
  101. fail("Should have failed");
  102. }
  103. catch (ConversionFailedException e) {
  104. }
  105. }
  106. @Test
  107. public void convertSuperSourceType() {
  108. conversionService.addConverter(new Converter<CharSequence, Integer>() {
  109. public Integer convert(CharSequence source) {
  110. return Integer.valueOf(source.toString());
  111. }
  112. });
  113. Integer result = conversionService.convert("3", Integer.class);
  114. assertEquals(new Integer(3), result);
  115. }
  116. @Test
  117. public void convertObjectToPrimitive() {
  118. assertFalse(conversionService.canConvert(String.class, boolean.class));
  119. conversionService.addConverter(new StringToBooleanConverter());
  120. assertTrue(conversionService.canConvert(String.class, boolean.class));
  121. Boolean b = conversionService.convert("true", boolean.class);
  122. assertEquals(Boolean.TRUE, b);
  123. assertTrue(conversionService.canConvert(TypeDescriptor.valueOf(String.class), TypeDescriptor
  124. .valueOf(boolean.class)));
  125. b = (Boolean) conversionService.convert("true", TypeDescriptor.valueOf(String.class), TypeDescriptor
  126. .valueOf(boolean.class));
  127. assertEquals(Boolean.TRUE, b);
  128. }
  129. @Test
  130. public void convertObjectToPrimitiveViaConverterFactory() {
  131. assertFalse(conversionService.canConvert(String.class, int.class));
  132. conversionService.addConverterFactory(new StringToNumberConverterFactory());
  133. assertTrue(conversionService.canConvert(String.class, int.class));
  134. Integer three = conversionService.convert("3", int.class);
  135. assertEquals(3, three.intValue());
  136. }
  137. @Test
  138. public void genericConverterDelegatingBackToConversionServiceConverterNotFound() {
  139. conversionService.addConverter(new ObjectToArrayConverter(conversionService));
  140. assertFalse(conversionService.canConvert(String.class, Integer[].class));
  141. }
  142. @Test
  143. public void testListToIterableConversion() {
  144. GenericConversionService conversionService = new GenericConversionService();
  145. List<Object> raw = new ArrayList<Object>();
  146. raw.add("one");
  147. raw.add("two");
  148. Object converted = conversionService.convert(raw, Iterable.class);
  149. assertSame(raw, converted);
  150. }
  151. @Test
  152. public void testListToObjectConversion() {
  153. GenericConversionService conversionService = new GenericConversionService();
  154. List<Object> raw = new ArrayList<Object>();
  155. raw.add("one");
  156. raw.add("two");
  157. Object converted = conversionService.convert(raw, Object.class);
  158. assertSame(raw, converted);
  159. }
  160. @Test
  161. public void testMapToObjectConversion() {
  162. GenericConversionService conversionService = new GenericConversionService();
  163. Map<Object, Object> raw = new HashMap<Object, Object>();
  164. raw.put("key", "value");
  165. Object converted = conversionService.convert(raw, Object.class);
  166. assertSame(raw, converted);
  167. }
  168. @Test
  169. public void testInterfaceToString() {
  170. GenericConversionService conversionService = new GenericConversionService();
  171. conversionService.addConverter(new MyBaseInterfaceConverter());
  172. conversionService.addConverter(new ObjectToStringConverter());
  173. Object converted = conversionService.convert(new MyInterfaceImplementer(), String.class);
  174. assertEquals("RESULT", converted);
  175. }
  176. @Test
  177. public void testInterfaceArrayToStringArray() {
  178. GenericConversionService conversionService = new GenericConversionService();
  179. conversionService.addConverter(new MyBaseInterfaceConverter());
  180. conversionService.addConverter(new ArrayToArrayConverter(conversionService));
  181. String[] converted = conversionService.convert(new MyInterface[] {new MyInterfaceImplementer()}, String[].class);
  182. assertEquals("RESULT", converted[0]);
  183. }
  184. @Test
  185. public void testObjectArrayToStringArray() {
  186. GenericConversionService conversionService = new GenericConversionService();
  187. conversionService.addConverter(new MyBaseInterfaceConverter());
  188. conversionService.addConverter(new ArrayToArrayConverter(conversionService));
  189. String[] converted = conversionService.convert(new MyInterfaceImplementer[] {new MyInterfaceImplementer()}, String[].class);
  190. assertEquals("RESULT", converted[0]);
  191. }
  192. @Test
  193. public void testStringArrayToResourceArray() {
  194. GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
  195. conversionService.addConverter(new MyStringArrayToResourceArrayConverter());
  196. Resource[] converted = conversionService.convert(new String[] {"x1", "z3"}, Resource[].class);
  197. assertEquals(2, converted.length);
  198. assertEquals("1", converted[0].getDescription());
  199. assertEquals("3", converted[1].getDescription());
  200. }
  201. @Test
  202. public void testStringArrayToIntegerArray() {
  203. GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
  204. conversionService.addConverter(new MyStringArrayToIntegerArrayConverter());
  205. Integer[] converted = conversionService.convert(new String[] {"x1", "z3"}, Integer[].class);
  206. assertEquals(2, converted.length);
  207. assertEquals(1, converted[0].intValue());
  208. assertEquals(3, converted[1].intValue());
  209. }
  210. @Test
  211. public void testStringToIntegerArray() {
  212. GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
  213. conversionService.addConverter(new MyStringToIntegerArrayConverter());
  214. Integer[] converted = conversionService.convert("x1,z3", Integer[].class);
  215. assertEquals(2, converted.length);
  216. assertEquals(1, converted[0].intValue());
  217. assertEquals(3, converted[1].intValue());
  218. }
  219. @Test
  220. public void testWildcardMap() throws Exception {
  221. GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
  222. Map<String, String> input = new LinkedHashMap<String, String>();
  223. input.put("key", "value");
  224. Object converted = conversionService.convert(input, TypeDescriptor.forObject(input),
  225. new TypeDescriptor(getClass().getField("wildcardMap")));
  226. assertEquals(input, converted);
  227. }
  228. @Test
  229. public void testListOfList() {
  230. GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
  231. List<List<String>> list = Collections.singletonList(Collections.singletonList("Foo"));
  232. assertNotNull(service.convert(list, String.class));
  233. }
  234. @Test
  235. public void testEmptyList() {
  236. GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
  237. List list = Collections.emptyList();
  238. List result = service.convert(list, List.class);
  239. assertSame(list, result);
  240. result = service.convert(list, list.getClass());
  241. assertSame(list, result);
  242. }
  243. @Test
  244. public void testEmptyMap() {
  245. GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
  246. Map map = Collections.emptyMap();
  247. Map result = service.convert(map, Map.class);
  248. assertSame(map, result);
  249. result = service.convert(map, map.getClass());
  250. assertSame(map, result);
  251. }
  252. @Test
  253. public void testStringToString() {
  254. GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
  255. String value = "myValue";
  256. String result = service.convert(value, String.class);
  257. assertSame(value, result);
  258. }
  259. @Test
  260. public void testStringToObject() {
  261. GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
  262. String value = "myValue";
  263. Object result = service.convert(value, Object.class);
  264. assertSame(value, result);
  265. }
  266. @Test
  267. public void testIgnoreCopyConstructor() {
  268. GenericConversionService service = ConversionServiceFactory.createDefaultConversionService();
  269. WithCopyConstructor value = new WithCopyConstructor();
  270. Object result = service.convert(value, WithCopyConstructor.class);
  271. assertSame(value, result);
  272. }
  273. @Test
  274. public void testPerformance1() {
  275. GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
  276. StopWatch watch = new StopWatch("integer->string conversionPerformance");
  277. watch.start("convert 4,000,000 with conversion service");
  278. for (int i = 0; i < 4000000; i++) {
  279. conversionService.convert(3, String.class);
  280. }
  281. watch.stop();
  282. watch.start("convert 4,000,000 manually");
  283. for (int i = 0; i < 4000000; i++) {
  284. new Integer(3).toString();
  285. }
  286. watch.stop();
  287. System.out.println(watch.prettyPrint());
  288. }
  289. @Test
  290. public void testPerformance2() throws Exception {
  291. GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
  292. StopWatch watch = new StopWatch("list<string> -> list<integer> conversionPerformance");
  293. watch.start("convert 4,000,000 with conversion service");
  294. List<String> source = new LinkedList<String>();
  295. source.add("1");
  296. source.add("2");
  297. source.add("3");
  298. TypeDescriptor td = new TypeDescriptor(getClass().getField("list"));
  299. for (int i = 0; i < 1000000; i++) {
  300. conversionService.convert(source, TypeDescriptor.forObject(source), td);
  301. }
  302. watch.stop();
  303. watch.start("convert 4,000,000 manually");
  304. for (int i = 0; i < 4000000; i++) {
  305. List<Integer> target = new ArrayList<Integer>(source.size());
  306. for (String element : source) {
  307. target.add(Integer.valueOf(element));
  308. }
  309. }
  310. watch.stop();
  311. System.out.println(watch.prettyPrint());
  312. }
  313. public static List<Integer> list;
  314. @Test
  315. public void testPerformance3() throws Exception {
  316. GenericConversionService conversionService = ConversionServiceFactory.createDefaultConversionService();
  317. StopWatch watch = new StopWatch("map<string, string> -> map<string, integer> conversionPerformance");
  318. watch.start("convert 4,000,000 with conversion service");
  319. Map<String, String> source = new HashMap<String, String>();
  320. source.put("1", "1");
  321. source.put("2", "2");
  322. source.put("3", "3");
  323. TypeDescriptor td = new TypeDescriptor(getClass().getField("map"));
  324. for (int i = 0; i < 1000000; i++) {
  325. conversionService.convert(source, TypeDescriptor.forObject(source), td);
  326. }
  327. watch.stop();
  328. watch.start("convert 4,000,000 manually");
  329. for (int i = 0; i < 4000000; i++) {
  330. Map<String, Integer> target = new HashMap<String, Integer>(source.size());
  331. for (Map.Entry<String, String> entry : source.entrySet()) {
  332. target.put(entry.getKey(), Integer.valueOf(entry.getValue()));
  333. }
  334. }
  335. watch.stop();
  336. System.out.println(watch.prettyPrint());
  337. }
  338. public static Map<String, Integer> map;
  339. private interface MyBaseInterface {
  340. }
  341. private interface MyInterface extends MyBaseInterface {
  342. }
  343. private static class MyInterfaceImplementer implements MyInterface {
  344. }
  345. private static class MyBaseInterfaceConverter implements Converter<MyBaseInterface, String> {
  346. public String convert(MyBaseInterface source) {
  347. return "RESULT";
  348. }
  349. }
  350. private static class MyStringArrayToResourceArrayConverter implements Converter<String[], Resource[]> {
  351. public Resource[] convert(String[] source) {
  352. Resource[] result = new Resource[source.length];
  353. for (int i = 0; i < source.length; i++) {
  354. result[i] = new DescriptiveResource(source[i].substring(1));
  355. }
  356. return result;
  357. }
  358. }
  359. private static class MyStringArrayToIntegerArrayConverter implements Converter<String[], Integer[]> {
  360. public Integer[] convert(String[] source) {
  361. Integer[] result = new Integer[source.length];
  362. for (int i = 0; i < source.length; i++) {
  363. result[i] = Integer.parseInt(source[i].substring(1));
  364. }
  365. return result;
  366. }
  367. }
  368. private static class MyStringToIntegerArrayConverter implements Converter<String, Integer[]> {
  369. public Integer[] convert(String source) {
  370. String[] srcArray = StringUtils.commaDelimitedListToStringArray(source);
  371. Integer[] result = new Integer[srcArray.length];
  372. for (int i = 0; i < srcArray.length; i++) {
  373. result[i] = Integer.parseInt(srcArray[i].substring(1));
  374. }
  375. return result;
  376. }
  377. }
  378. public static class WithCopyConstructor {
  379. public WithCopyConstructor() {
  380. }
  381. public WithCopyConstructor(WithCopyConstructor value) {
  382. }
  383. }
  384. public static Map<String, ?> wildcardMap;
  385. }