7 changed files with 204 additions and 190 deletions
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
/* |
||||
* Copyright 2012-2017 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 |
||||
* |
||||
* http://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.util.Collections; |
||||
import java.util.HashMap; |
||||
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 HashMap<DatabaseDriver, Database>(); |
||||
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); |
||||
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 sutable {@link Database} |
||||
*/ |
||||
public static Database getDatabase(DataSource dataSource) { |
||||
if (dataSource == null) { |
||||
return Database.DEFAULT; |
||||
} |
||||
try { |
||||
String url = (String) JdbcUtils.extractDatabaseMetaData(dataSource, "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; |
||||
} |
||||
|
||||
} |
||||
@ -1,73 +0,0 @@
@@ -1,73 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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 org.springframework.boot.jdbc.DatabaseDriver; |
||||
import org.springframework.orm.jpa.vendor.Database; |
||||
|
||||
/** |
||||
* Mapper between {@link Database} and {@link DatabaseDriver}. |
||||
* |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
enum DatabasePlatform { |
||||
|
||||
DB2(Database.DB2, DatabaseDriver.DB2), |
||||
|
||||
DERBY(Database.DERBY, DatabaseDriver.DERBY), |
||||
|
||||
H2(Database.H2, DatabaseDriver.H2), |
||||
|
||||
HSQL(Database.HSQL, DatabaseDriver.HSQLDB), |
||||
|
||||
INFORMIX(Database.INFORMIX, DatabaseDriver.INFORMIX), |
||||
|
||||
MYSQL(Database.MYSQL, DatabaseDriver.MYSQL), |
||||
|
||||
ORACLE(Database.ORACLE, DatabaseDriver.ORACLE), |
||||
|
||||
POSTGRESQL(Database.POSTGRESQL, DatabaseDriver.POSTGRESQL), |
||||
|
||||
SQL_SERVER(Database.SQL_SERVER, DatabaseDriver.SQLSERVER); |
||||
|
||||
private final Database database; |
||||
|
||||
private final DatabaseDriver driver; |
||||
|
||||
DatabasePlatform(Database database, DatabaseDriver driver) { |
||||
this.database = database; |
||||
this.driver = driver; |
||||
} |
||||
|
||||
public Database getDatabase() { |
||||
return this.database; |
||||
} |
||||
|
||||
public DatabaseDriver getDriver() { |
||||
return this.driver; |
||||
} |
||||
|
||||
public static DatabasePlatform fromDatabaseDriver(DatabaseDriver driver) { |
||||
for (DatabasePlatform mapper : values()) { |
||||
if (mapper.getDriver() == driver) { |
||||
return mapper; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,106 @@
@@ -0,0 +1,106 @@
|
||||
/* |
||||
* Copyright 2012-2017 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 |
||||
* |
||||
* http://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.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 |
||||
*/ |
||||
public class DatabaseLookupTests { |
||||
|
||||
@Test |
||||
public void getDatabaseWhenDataSourceIsNullShouldReturnDefault() throws Exception { |
||||
assertThat(DatabaseLookup.getDatabase(null)).isEqualTo(Database.DEFAULT); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenDataSourceIsUnknownShouldReturnDefault() throws Exception { |
||||
testGetDatabase("jdbc:idontexist:", Database.DEFAULT); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenDerbyShouldReturnDerby() throws Exception { |
||||
testGetDatabase("jdbc:derby:", Database.DERBY); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenH2ShouldReturnH2() throws Exception { |
||||
testGetDatabase("jdbc:h2:", Database.H2); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenHsqldbShouldReturnHsqldb() throws Exception { |
||||
testGetDatabase("jdbc:hsqldb:", Database.HSQL); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenMysqlShouldReturnMysql() throws Exception { |
||||
testGetDatabase("jdbc:mysql:", Database.MYSQL); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenOracleShouldReturnOracle() throws Exception { |
||||
testGetDatabase("jdbc:oracle:", Database.ORACLE); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenPostgresShouldReturnPostgres() throws Exception { |
||||
testGetDatabase("jdbc:postgresql:", Database.POSTGRESQL); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenSqlserverShouldReturnSqlserver() throws Exception { |
||||
testGetDatabase("jdbc:sqlserver:", Database.SQL_SERVER); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenDb2ShouldReturnDb2() throws Exception { |
||||
testGetDatabase("jdbc:db2:", Database.DB2); |
||||
} |
||||
|
||||
@Test |
||||
public void getDatabaseWhenInformixShouldReturnInformix() throws Exception { |
||||
testGetDatabase("jdbc:informix-sqli:", Database.INFORMIX); |
||||
} |
||||
|
||||
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); |
||||
} |
||||
|
||||
} |
||||
@ -1,86 +0,0 @@
@@ -1,86 +0,0 @@
|
||||
/* |
||||
* Copyright 2012-2016 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 |
||||
* |
||||
* http://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 org.junit.Test; |
||||
|
||||
import org.springframework.boot.jdbc.DatabaseDriver; |
||||
import org.springframework.orm.jpa.vendor.Database; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
||||
/** |
||||
* Tests for {@link DatabasePlatform}. |
||||
* |
||||
* @author Eddú Meléndez |
||||
*/ |
||||
public class DatabasePlatformTests { |
||||
|
||||
@Test |
||||
public void databaseDriverLookups() { |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.DB2)) |
||||
.isEqualTo(DatabasePlatform.DB2); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.DERBY)) |
||||
.isEqualTo(DatabasePlatform.DERBY); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.H2)) |
||||
.isEqualTo(DatabasePlatform.H2); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.HSQLDB)) |
||||
.isEqualTo(DatabasePlatform.HSQL); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.INFORMIX)) |
||||
.isEqualTo(DatabasePlatform.INFORMIX); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.MYSQL)) |
||||
.isEqualTo(DatabasePlatform.MYSQL); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.ORACLE)) |
||||
.isEqualTo(DatabasePlatform.ORACLE); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.POSTGRESQL)) |
||||
.isEqualTo(DatabasePlatform.POSTGRESQL); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.SQLSERVER)) |
||||
.isEqualTo(DatabasePlatform.SQL_SERVER); |
||||
} |
||||
|
||||
@Test |
||||
public void databaseLookups() { |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.DB2) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.DB2); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.DERBY) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.DERBY); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.H2) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.H2); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.HSQLDB) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.HSQL); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.INFORMIX) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.INFORMIX); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.MYSQL) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.MYSQL); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.ORACLE) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.ORACLE); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.POSTGRESQL) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.POSTGRESQL); |
||||
assertThat(DatabasePlatform.fromDatabaseDriver(DatabaseDriver.SQLSERVER) |
||||
.getDatabase()) |
||||
.isEqualTo(Database.SQL_SERVER); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue