View Javadoc
1   /*
2    *    Copyright 2009-2022 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.type;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  import static org.junit.jupiter.api.Assertions.assertNull;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.math.BigDecimal;
23  
24  import org.junit.jupiter.api.Test;
25  
26  class TypeAliasRegistryTest {
27  
28    @Test
29    void shouldRegisterAndResolveTypeAlias() {
30      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
31  
32      typeAliasRegistry.registerAlias("rich", "org.apache.ibatis.domain.misc.RichType");
33  
34      assertEquals("org.apache.ibatis.domain.misc.RichType", typeAliasRegistry.resolveAlias("rich").getName());
35    }
36  
37    @Test
38    void shouldFetchArrayType() {
39      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
40      assertEquals(Byte[].class, typeAliasRegistry.resolveAlias("byte[]"));
41    }
42  
43    @Test
44    void shouldBeAbleToRegisterSameAliasWithSameTypeAgain() {
45      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
46      typeAliasRegistry.registerAlias("String", String.class);
47      typeAliasRegistry.registerAlias("string", String.class);
48    }
49  
50    @Test
51    void shouldNotBeAbleToRegisterSameAliasWithDifferentType() {
52      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
53      assertThrows(TypeException.class, () -> typeAliasRegistry.registerAlias("string", BigDecimal.class));
54    }
55  
56    @Test
57    void shouldBeAbleToRegisterAliasWithNullType() {
58      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
59      typeAliasRegistry.registerAlias("foo", (Class<?>) null);
60      assertNull(typeAliasRegistry.resolveAlias("foo"));
61    }
62  
63    @Test
64    void shouldBeAbleToRegisterNewTypeIfRegisteredTypeIsNull() {
65      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
66      typeAliasRegistry.registerAlias("foo", (Class<?>) null);
67      typeAliasRegistry.registerAlias("foo", String.class);
68    }
69  
70    @Test
71    void shouldFetchCharType() {
72      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
73      assertEquals(Character.class, typeAliasRegistry.resolveAlias("char"));
74      assertEquals(Character[].class, typeAliasRegistry.resolveAlias("char[]"));
75      assertEquals(char[].class, typeAliasRegistry.resolveAlias("_char[]"));
76    }
77  
78  }