View Javadoc
1   /*
2    *    Copyright 2009-2024 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.assertj.core.api.Assertions.assertThatThrownBy;
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNull;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.math.BigDecimal;
24  
25  import org.junit.jupiter.api.Test;
26  
27  class TypeAliasRegistryTest {
28  
29    @Test
30    void shouldRegisterAndResolveTypeAlias() {
31      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
32  
33      typeAliasRegistry.registerAlias("rich", "org.apache.ibatis.domain.misc.RichType");
34  
35      assertEquals("org.apache.ibatis.domain.misc.RichType", typeAliasRegistry.resolveAlias("rich").getName());
36    }
37  
38    @Test
39    void shouldFetchArrayType() {
40      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
41      assertEquals(Byte[].class, typeAliasRegistry.resolveAlias("byte[]"));
42    }
43  
44    @Test
45    void shouldBeAbleToRegisterSameAliasWithSameTypeAgain() {
46      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
47      typeAliasRegistry.registerAlias("String", String.class);
48      typeAliasRegistry.registerAlias("string", String.class);
49    }
50  
51    @Test
52    void shouldNotBeAbleToRegisterSameAliasWithDifferentType() {
53      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
54      assertThrows(TypeException.class, () -> typeAliasRegistry.registerAlias("string", BigDecimal.class));
55    }
56  
57    @Test
58    void shouldBeAbleToRegisterAliasWithNullType() {
59      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
60      typeAliasRegistry.registerAlias("foo", (Class<?>) null);
61      assertNull(typeAliasRegistry.resolveAlias("foo"));
62    }
63  
64    @Test
65    void shouldBeAbleToRegisterNewTypeIfRegisteredTypeIsNull() {
66      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
67      typeAliasRegistry.registerAlias("foo", (Class<?>) null);
68      typeAliasRegistry.registerAlias("foo", String.class);
69    }
70  
71    @Test
72    void shouldFetchCharType() {
73      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
74      assertEquals(Character.class, typeAliasRegistry.resolveAlias("char"));
75      assertEquals(Character[].class, typeAliasRegistry.resolveAlias("char[]"));
76      assertEquals(char[].class, typeAliasRegistry.resolveAlias("_char[]"));
77    }
78  
79    @Test
80    void shouldNotBeAbleToRegisterAliasWithEmptyString() {
81      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
82  
83      assertThatThrownBy(() -> typeAliasRegistry.registerAlias("foo", "")).isInstanceOf(TypeException.class)
84          .hasMessageContaining("Error registering type alias foo for");
85    }
86  
87    @Test
88    void shouldNotBeAbleToResolveNotExistsAlias() {
89      TypeAliasRegistry typeAliasRegistry = new TypeAliasRegistry();
90  
91      assertThatThrownBy(() -> typeAliasRegistry.resolveAlias("abc")).isInstanceOf(TypeException.class)
92          .hasMessageContaining(
93              "Could not resolve type alias 'abc'.  Cause: java.lang.ClassNotFoundException: Cannot find class: abc");
94    }
95  }