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.mapping;
17  
18  import static org.junit.jupiter.api.Assertions.*;
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.when;
21  
22  import java.sql.Connection;
23  import java.sql.DatabaseMetaData;
24  import java.sql.SQLException;
25  import java.util.Properties;
26  
27  import javax.sql.DataSource;
28  
29  import org.apache.ibatis.builder.BuilderException;
30  import org.junit.jupiter.api.Test;
31  
32  class VendorDatabaseIdProviderTest {
33  
34    private static final String PRODUCT_NAME = "Chewbacca DB";
35  
36    @Test
37    void shouldNpeBeThrownIfDataSourceIsNull() {
38      VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
39      try {
40        provider.getDatabaseId(null);
41        fail("Should NullPointerException be thrown.");
42      } catch (NullPointerException e) {
43        // pass
44      }
45    }
46  
47    @Test
48    void shouldProductNameBeReturnedIfPropertiesIsNull() throws Exception {
49      VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
50      assertEquals(PRODUCT_NAME, provider.getDatabaseId(mockDataSource()));
51    }
52  
53    @Test
54    void shouldProductNameBeReturnedIfPropertiesIsEmpty() throws Exception {
55      VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
56      provider.setProperties(new Properties());
57      assertEquals(PRODUCT_NAME, provider.getDatabaseId(mockDataSource()));
58    }
59  
60    @Test
61    void shouldProductNameBeTranslated() throws Exception {
62      VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
63      Properties properties = new Properties();
64      String partialProductName = "Chewbacca";
65      String id = "chewie";
66      properties.put(partialProductName, id);
67      provider.setProperties(properties);
68      assertEquals(id, provider.getDatabaseId(mockDataSource()));
69    }
70  
71    @Test
72    void shouldNullBeReturnedIfNoMatch() throws Exception {
73      VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
74      Properties properties = new Properties();
75      properties.put("Ewok DB", "ewok");
76      provider.setProperties(properties);
77      assertNull(provider.getDatabaseId(mockDataSource()));
78    }
79  
80    @Test
81    void shouldNullBeReturnedOnDbError() throws Exception {
82      DataSource dataSource = mock(DataSource.class);
83      when(dataSource.getConnection()).thenThrow(SQLException.class);
84  
85      VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
86      Properties properties = new Properties();
87      properties.put("Ewok DB", "ewok");
88      try {
89        provider.getDatabaseId(dataSource);
90        fail("Should BuilderException be thrown.");
91      } catch (BuilderException e) {
92        // pass
93      }
94    }
95  
96    private DataSource mockDataSource() throws SQLException {
97      DatabaseMetaData metaData = mock(DatabaseMetaData.class);
98      when(metaData.getDatabaseProductName()).thenReturn(PRODUCT_NAME);
99      Connection connection = mock(Connection.class);
100     when(connection.getMetaData()).thenReturn(metaData);
101     DataSource dataSource = mock(DataSource.class);
102     when(dataSource.getConnection()).thenReturn(connection);
103     return dataSource;
104   }
105 
106 }