Skip to content

Commit 5a7d431

Browse files
author
wei.zw
committed
change the method of instantiateClass from default to private and add test cases
1 parent c6a7878 commit 5a7d431

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/main/java/org/apache/ibatis/reflection/factory/DefaultObjectFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void setProperties(Properties properties) {
5555
// no props for default
5656
}
5757

58-
<T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
58+
private <T> T instantiateClass(Class<T> type, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) {
5959
try {
6060
Constructor<T> constructor;
6161
if (constructorArgTypes == null || constructorArgs == null) {

src/test/java/org/apache/ibatis/reflection/factory/DefaultObjectFactoryTest.java

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,18 @@
1515
*/
1616
package org.apache.ibatis.reflection.factory;
1717

18+
import java.util.ArrayList;
1819
import java.util.Arrays;
20+
import java.util.Collection;
1921
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.HashSet;
24+
import java.util.Iterator;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Set;
28+
import java.util.SortedSet;
29+
import java.util.TreeSet;
2030

2131
import org.apache.ibatis.reflection.ReflectionException;
2232
import org.junit.Assert;
@@ -30,20 +40,20 @@
3040
public class DefaultObjectFactoryTest {
3141

3242
@Test
33-
public void instantiateClass() throws Exception {
43+
public void createClass() throws Exception {
3444
DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
35-
TestClass testClass = defaultObjectFactory.instantiateClass(TestClass.class,
45+
TestClass testClass = defaultObjectFactory.create(TestClass.class,
3646
Arrays.<Class<?>>asList(String.class, Integer.class), Arrays.<Object>asList("foo", 0));
3747

3848
Assert.assertEquals("myInteger didn't match expected", (Integer) 0, testClass.myInteger);
3949
Assert.assertEquals("myString didn't match expected", "foo", testClass.myString);
4050
}
4151

4252
@Test
43-
public void instantiateClassThrowsProperErrorMsg() {
53+
public void createClassThrowsProperErrorMsg() {
4454
DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
4555
try {
46-
defaultObjectFactory.instantiateClass(TestClass.class, Collections.<Class<?>>singletonList(String.class), Collections.<Object>singletonList("foo"));
56+
defaultObjectFactory.create(TestClass.class, Collections.<Class<?>>singletonList(String.class), Collections.<Object>singletonList("foo"));
4757
Assert.fail("Should have thrown ReflectionException");
4858
} catch (Exception e) {
4959
Assert.assertTrue("Should be ReflectionException", e instanceof ReflectionException);
@@ -52,4 +62,39 @@ public void instantiateClassThrowsProperErrorMsg() {
5262
}
5363
}
5464

65+
@Test
66+
public void creatHashMap() throws Exception{
67+
DefaultObjectFactory defaultObjectFactory=new DefaultObjectFactory();
68+
Map map= defaultObjectFactory.create(Map.class,null,null);
69+
Assert.assertTrue("Should be HashMap",map instanceof HashMap);
70+
}
71+
72+
@Test
73+
public void createArrayList() throws Exception {
74+
DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
75+
List list = defaultObjectFactory.create(List.class);
76+
Assert.assertTrue(" list should be ArrayList", list instanceof ArrayList);
77+
78+
Collection collection = defaultObjectFactory.create(Collection.class);
79+
Assert.assertTrue(" collection should be ArrayList", collection instanceof ArrayList);
80+
81+
Iterable iterable = defaultObjectFactory.create(Iterable.class);
82+
Assert.assertTrue(" iterable should be ArrayList", iterable instanceof ArrayList);
83+
}
84+
85+
86+
@Test
87+
public void createTreeSet() throws Exception {
88+
DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
89+
SortedSet sortedSet = defaultObjectFactory.create(SortedSet.class);
90+
Assert.assertTrue(" sortedSet should be TreeSet", sortedSet instanceof TreeSet);
91+
}
92+
93+
94+
@Test
95+
public void createHashSet() throws Exception {
96+
DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
97+
Set set = defaultObjectFactory.create(Set.class);
98+
Assert.assertTrue(" set should be HashSet", set instanceof HashSet);
99+
}
55100
}

0 commit comments

Comments
 (0)