View Javadoc
1   /*
2    *    Copyright 2009-2021 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.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,
45          Arrays.asList(String.class, Integer.class), 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), Collections.singletonList("foo"));
56        Assertions.fail("Should have thrown ReflectionException");
57      } catch (Exception e) {
58        Assertions.assertTrue(e instanceof ReflectionException, "Should be ReflectionException");
59        Assertions.assertTrue(e.getMessage().contains("(String)"), "Should not have trailing commas in types list");
60        Assertions.assertTrue(e.getMessage().contains("(foo)"), "Should not have trailing commas in values list");
61      }
62    }
63  
64    @Test
65    void createHashMap() {
66       DefaultObjectFactory defaultObjectFactory=new DefaultObjectFactory();
67       Map  map= defaultObjectFactory.create(Map.class,null,null);
68       Assertions.assertTrue(map instanceof HashMap, "Should be HashMap");
69    }
70  
71    @Test
72    void createArrayList() {
73      DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
74      List list = defaultObjectFactory.create(List.class);
75      Assertions.assertTrue(list instanceof ArrayList, " list should be ArrayList");
76  
77      Collection collection = defaultObjectFactory.create(Collection.class);
78      Assertions.assertTrue(collection instanceof ArrayList, " collection should be ArrayList");
79  
80      Iterable iterable = defaultObjectFactory.create(Iterable.class);
81      Assertions.assertTrue(iterable instanceof ArrayList, " iterable should be ArrayList");
82    }
83  
84    @Test
85    void createTreeSet() {
86      DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
87      SortedSet sortedSet = defaultObjectFactory.create(SortedSet.class);
88      Assertions.assertTrue(sortedSet instanceof TreeSet, " sortedSet should be TreeSet");
89    }
90  
91    @Test
92    void createHashSet() {
93      DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
94      Set set = defaultObjectFactory.create(Set.class);
95      Assertions.assertTrue(set instanceof HashSet, " set should be HashSet");
96    }
97  }