1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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 }