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