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