Skip to content

Commit c754ff2

Browse files
committed
Tests cover the current implementation
1 parent 314bec1 commit c754ff2

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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.*;
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.junit.jupiter.api.Test;
30+
31+
class VendorDatabaseIdProviderTest {
32+
33+
private static final String PRODUCT_NAME = "Chewbacca DB";
34+
35+
@Test
36+
void shouldNpeBeThrownIfDataSourceIsNull() {
37+
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
38+
try {
39+
provider.getDatabaseId(null);
40+
fail("Should NullPointerException be thrown.");
41+
} catch (NullPointerException e) {
42+
// pass
43+
}
44+
}
45+
46+
@Test
47+
void shouldProductNameBeReturnedIfPropertiesIsNull() throws Exception {
48+
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
49+
assertEquals(PRODUCT_NAME, provider.getDatabaseId(mockDataSource()));
50+
}
51+
52+
@Test
53+
void shouldProductNameBeTranslated() throws Exception {
54+
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
55+
Properties properties = new Properties();
56+
String partialProductName = "Chewbacca";
57+
String id = "chewie";
58+
properties.put(partialProductName, id);
59+
provider.setProperties(properties);
60+
assertEquals(id, provider.getDatabaseId(mockDataSource()));
61+
}
62+
63+
@Test
64+
void shouldNullBeReturnedIfNoMatch() throws Exception {
65+
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
66+
Properties properties = new Properties();
67+
properties.put("Ewok DB", "ewok");
68+
provider.setProperties(properties);
69+
assertNull(provider.getDatabaseId(mockDataSource()));
70+
}
71+
72+
@Test
73+
void shouldNullBeReturnedOnDbError() throws Exception {
74+
DataSource dataSource = mock(DataSource.class);
75+
when(dataSource.getConnection()).thenThrow(SQLException.class);
76+
77+
VendorDatabaseIdProvider provider = new VendorDatabaseIdProvider();
78+
Properties properties = new Properties();
79+
properties.put("Ewok DB", "ewok");
80+
assertNull(provider.getDatabaseId(dataSource));
81+
}
82+
83+
private DataSource mockDataSource() throws SQLException {
84+
DatabaseMetaData metaData = mock(DatabaseMetaData.class);
85+
when(metaData.getDatabaseProductName()).thenReturn(PRODUCT_NAME);
86+
Connection connection = mock(Connection.class);
87+
when(connection.getMetaData()).thenReturn(metaData);
88+
DataSource dataSource = mock(DataSource.class);
89+
when(dataSource.getConnection()).thenReturn(connection);
90+
return dataSource;
91+
}
92+
93+
}

0 commit comments

Comments
 (0)