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.scripting;
17  
18  import static com.googlecode.catchexception.apis.BDDCatchException.caughtException;
19  import static com.googlecode.catchexception.apis.BDDCatchException.when;
20  import static org.assertj.core.api.Assertions.assertThat;
21  import static org.assertj.core.api.BDDAssertions.then;
22  
23  import org.apache.ibatis.executor.parameter.ParameterHandler;
24  import org.apache.ibatis.mapping.BoundSql;
25  import org.apache.ibatis.mapping.MappedStatement;
26  import org.apache.ibatis.mapping.SqlSource;
27  import org.apache.ibatis.parsing.XNode;
28  import org.apache.ibatis.scripting.defaults.RawLanguageDriver;
29  import org.apache.ibatis.session.Configuration;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * @author Kazuki Shimizu
34   */
35  class LanguageDriverRegistryTest {
36  
37    private final LanguageDriverRegistry registry = new LanguageDriverRegistry();
38  
39    @Test
40    void registerByType() {
41      registry.register(RawLanguageDriver.class);
42      LanguageDriver driver = registry.getDriver(RawLanguageDriver.class);
43  
44      assertThat(driver).isInstanceOf(RawLanguageDriver.class);
45    }
46  
47    @Test
48    void registerByTypeSameType() {
49      registry.register(RawLanguageDriver.class);
50      LanguageDriver driver = registry.getDriver(RawLanguageDriver.class);
51  
52      registry.register(RawLanguageDriver.class);
53  
54      assertThat(driver).isSameAs(registry.getDriver(RawLanguageDriver.class));
55    }
56  
57    @Test
58    void registerByTypeNull() {
59      when(() -> registry.register((Class<? extends LanguageDriver>) null));
60      then(caughtException()).isInstanceOf(IllegalArgumentException.class)
61          .hasMessage("null is not a valid Language Driver");
62    }
63  
64    @Test
65    void registerByTypeDoesNotCreateNewInstance() {
66      when(() -> registry.register(PrivateLanguageDriver.class));
67      then(caughtException()).isInstanceOf(ScriptingException.class).hasMessage(
68          "Failed to load language driver for org.apache.ibatis.scripting.LanguageDriverRegistryTest$PrivateLanguageDriver");
69    }
70  
71    @Test
72    void registerByInstance() {
73      registry.register(new PrivateLanguageDriver());
74      LanguageDriver driver = registry.getDriver(PrivateLanguageDriver.class);
75  
76      assertThat(driver).isInstanceOf(PrivateLanguageDriver.class);
77    }
78  
79    @Test
80    void registerByInstanceSameType() {
81      registry.register(new PrivateLanguageDriver());
82      LanguageDriver driver = registry.getDriver(PrivateLanguageDriver.class);
83  
84      registry.register(new PrivateLanguageDriver());
85  
86      assertThat(driver).isSameAs(registry.getDriver(PrivateLanguageDriver.class));
87    }
88  
89    @Test
90    void registerByInstanceNull() {
91      when(() -> registry.register((LanguageDriver) null));
92      then(caughtException()).isInstanceOf(IllegalArgumentException.class)
93          .hasMessage("null is not a valid Language Driver");
94    }
95  
96    @Test
97    void setDefaultDriverClass() {
98      registry.setDefaultDriverClass(RawLanguageDriver.class);
99      assertThat(registry.getDefaultDriverClass() == RawLanguageDriver.class).isTrue();
100     assertThat(registry.getDefaultDriver()).isInstanceOf(RawLanguageDriver.class);
101   }
102 
103   static private class PrivateLanguageDriver implements LanguageDriver {
104 
105     @Override
106     public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject,
107         BoundSql boundSql) {
108       return null;
109     }
110 
111     @Override
112     public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
113       return null;
114     }
115 
116     @Override
117     public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
118       return null;
119     }
120   }
121 
122 }