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