Skip to content

Commit 8f7d601

Browse files
authored
Merge pull request #3041 from harawata/gh/3040-exception-in-vendordatabaseidprovider
`VendorDatabaseIdProvider` should throw exception if DB error occurs
2 parents 314bec1 + 18bebc3 commit 8f7d601

File tree

2 files changed

+103
-9
lines changed

2 files changed

+103
-9
lines changed

src/main/java/org/apache/ibatis/mapping/VendorDatabaseIdProvider.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
import javax.sql.DataSource;
2323

24-
import org.apache.ibatis.logging.Log;
25-
import org.apache.ibatis.logging.LogFactory;
24+
import org.apache.ibatis.builder.BuilderException;
2625

2726
/**
2827
* Vendor DatabaseId provider.
@@ -44,10 +43,9 @@ public String getDatabaseId(DataSource dataSource) {
4443
}
4544
try {
4645
return getDatabaseName(dataSource);
47-
} catch (Exception e) {
48-
LogHolder.log.error("Could not get a databaseId from dataSource", e);
46+
} catch (SQLException e) {
47+
throw new BuilderException("Error occurred when getting DB product name.", e);
4948
}
50-
return null;
5149
}
5250

5351
@Override
@@ -70,8 +68,4 @@ private String getDatabaseProductName(DataSource dataSource) throws SQLException
7068
}
7169
}
7270

73-
private static class LogHolder {
74-
private static final Log log = LogFactory.getLog(VendorDatabaseIdProvider.class);
75-
}
76-
7771
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

0 commit comments

Comments
 (0)