1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.reflection.factory;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.SortedSet;
27 import java.util.TreeSet;
28
29 import org.apache.ibatis.reflection.ReflectionException;
30 import org.junit.jupiter.api.Assertions;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36
37
38 class DefaultObjectFactoryTest {
39
40 @Test
41 void createClass() {
42 DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
43 TestClass testClass = defaultObjectFactory.create(TestClass.class, Arrays.asList(String.class, Integer.class),
44 Arrays.asList("foo", 0));
45
46 Assertions.assertEquals((Integer) 0, testClass.myInteger, "myInteger didn't match expected");
47 Assertions.assertEquals("foo", testClass.myString, "myString didn't match expected");
48 }
49
50 @Test
51 void createClassThrowsProperErrorMsg() {
52 DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
53 try {
54 defaultObjectFactory.create(TestClass.class, List.of(String.class), List.of("foo"));
55 Assertions.fail("Should have thrown ReflectionException");
56 } catch (Exception e) {
57 Assertions.assertTrue(e instanceof ReflectionException, "Should be ReflectionException");
58 Assertions.assertTrue(e.getMessage().contains("(String)"), "Should not have trailing commas in types list");
59 Assertions.assertTrue(e.getMessage().contains("(foo)"), "Should not have trailing commas in values list");
60 }
61 }
62
63 @Test
64 void createHashMap() {
65 DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
66 Map map = defaultObjectFactory.create(Map.class, null, null);
67 Assertions.assertTrue(map instanceof HashMap, "Should be HashMap");
68 }
69
70 @Test
71 void createArrayList() {
72 DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
73 List list = defaultObjectFactory.create(List.class);
74 Assertions.assertTrue(list instanceof ArrayList, " list should be ArrayList");
75
76 Collection collection = defaultObjectFactory.create(Collection.class);
77 Assertions.assertTrue(collection instanceof ArrayList, " collection should be ArrayList");
78
79 Iterable iterable = defaultObjectFactory.create(Iterable.class);
80 Assertions.assertTrue(iterable instanceof ArrayList, " iterable should be ArrayList");
81 }
82
83 @Test
84 void createTreeSet() {
85 DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
86 SortedSet sortedSet = defaultObjectFactory.create(SortedSet.class);
87 Assertions.assertTrue(sortedSet instanceof TreeSet, " sortedSet should be TreeSet");
88 }
89
90 @Test
91 void createHashSet() {
92 DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
93 Set set = defaultObjectFactory.create(Set.class);
94 Assertions.assertTrue(set instanceof HashSet, " set should be HashSet");
95 }
96 }