Browse Source

Remove code that's no longer used

Closes gh-23993
pull/23999/head
Stephane Nicoll 5 years ago
parent
commit
62f26ee36f
  1. 87
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookup.java
  2. 111
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookupTests.java

87
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookup.java

@ -1,87 +0,0 @@ @@ -1,87 +0,0 @@
/*
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.orm.jpa;
import java.sql.DatabaseMetaData;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.orm.jpa.vendor.Database;
/**
* Utility to lookup well known {@link Database Databases} from a {@link DataSource}.
*
* @author Eddú Meléndez
* @author Phillip Webb
*/
final class DatabaseLookup {
private static final Log logger = LogFactory.getLog(DatabaseLookup.class);
private static final Map<DatabaseDriver, Database> LOOKUP;
static {
Map<DatabaseDriver, Database> map = new EnumMap<>(DatabaseDriver.class);
map.put(DatabaseDriver.DERBY, Database.DERBY);
map.put(DatabaseDriver.H2, Database.H2);
map.put(DatabaseDriver.HSQLDB, Database.HSQL);
map.put(DatabaseDriver.MYSQL, Database.MYSQL);
map.put(DatabaseDriver.ORACLE, Database.ORACLE);
map.put(DatabaseDriver.POSTGRESQL, Database.POSTGRESQL);
map.put(DatabaseDriver.SQLSERVER, Database.SQL_SERVER);
map.put(DatabaseDriver.DB2, Database.DB2);
map.put(DatabaseDriver.INFORMIX, Database.INFORMIX);
map.put(DatabaseDriver.HANA, Database.HANA);
LOOKUP = Collections.unmodifiableMap(map);
}
private DatabaseLookup() {
}
/**
* Return the most suitable {@link Database} for the given {@link DataSource}.
* @param dataSource the source {@link DataSource}
* @return the most suitable {@link Database}
*/
static Database getDatabase(DataSource dataSource) {
if (dataSource == null) {
return Database.DEFAULT;
}
try {
String url = JdbcUtils.extractDatabaseMetaData(dataSource, DatabaseMetaData::getURL);
DatabaseDriver driver = DatabaseDriver.fromJdbcUrl(url);
Database database = LOOKUP.get(driver);
if (database != null) {
return database;
}
}
catch (MetaDataAccessException ex) {
logger.warn("Unable to determine jdbc url from datasource", ex);
}
return Database.DEFAULT;
}
}

111
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookupTests.java

@ -1,111 +0,0 @@ @@ -1,111 +0,0 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.orm.jpa;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import javax.sql.DataSource;
import org.junit.jupiter.api.Test;
import org.springframework.orm.jpa.vendor.Database;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link DatabaseLookup}.
*
* @author Eddú Meléndez
* @author Phillip Webb
*/
class DatabaseLookupTests {
@Test
void getDatabaseWhenDataSourceIsNullShouldReturnDefault() {
assertThat(DatabaseLookup.getDatabase(null)).isEqualTo(Database.DEFAULT);
}
@Test
void getDatabaseWhenDataSourceIsUnknownShouldReturnDefault() throws Exception {
testGetDatabase("jdbc:idontexist:", Database.DEFAULT);
}
@Test
void getDatabaseWhenDerbyShouldReturnDerby() throws Exception {
testGetDatabase("jdbc:derby:", Database.DERBY);
}
@Test
void getDatabaseWhenH2ShouldReturnH2() throws Exception {
testGetDatabase("jdbc:h2:", Database.H2);
}
@Test
void getDatabaseWhenHsqldbShouldReturnHsqldb() throws Exception {
testGetDatabase("jdbc:hsqldb:", Database.HSQL);
}
@Test
void getDatabaseWhenMysqlShouldReturnMysql() throws Exception {
testGetDatabase("jdbc:mysql:", Database.MYSQL);
}
@Test
void getDatabaseWhenOracleShouldReturnOracle() throws Exception {
testGetDatabase("jdbc:oracle:", Database.ORACLE);
}
@Test
void getDatabaseWhenPostgresShouldReturnPostgres() throws Exception {
testGetDatabase("jdbc:postgresql:", Database.POSTGRESQL);
}
@Test
void getDatabaseWhenSqlserverShouldReturnSqlserver() throws Exception {
testGetDatabase("jdbc:sqlserver:", Database.SQL_SERVER);
}
@Test
void getDatabaseWhenDb2ShouldReturnDb2() throws Exception {
testGetDatabase("jdbc:db2:", Database.DB2);
}
@Test
void getDatabaseWhenInformixShouldReturnInformix() throws Exception {
testGetDatabase("jdbc:informix-sqli:", Database.INFORMIX);
}
@Test
void getDatabaseWhenSapShouldReturnHana() throws Exception {
testGetDatabase("jdbc:sap:", Database.HANA);
}
private void testGetDatabase(String url, Database expected) throws Exception {
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
DatabaseMetaData metaData = mock(DatabaseMetaData.class);
given(dataSource.getConnection()).willReturn(connection);
given(connection.getMetaData()).willReturn(metaData);
given(metaData.getURL()).willReturn(url);
Database database = DatabaseLookup.getDatabase(dataSource);
assertThat(database).isEqualTo(expected);
}
}
Loading…
Cancel
Save