diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookup.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookup.java new file mode 100644 index 0000000000..ef4ac3d0b9 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookup.java @@ -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 LOOKUP; + + static { + Map map = new HashMap(); + 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; + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabasePlatform.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabasePlatform.java deleted file mode 100644 index a10a473d5d..0000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DatabasePlatform.java +++ /dev/null @@ -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; - } - -} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java index 148ba6cc01..d5adda332f 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * 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. @@ -21,15 +21,9 @@ import java.util.Map; import javax.sql.DataSource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.NestedConfigurationProperty; -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; import org.springframework.util.StringUtils; @@ -45,8 +39,6 @@ import org.springframework.util.StringUtils; @ConfigurationProperties(prefix = "spring.jpa") public class JpaProperties { - private static final Log logger = LogFactory.getLog(JpaProperties.class); - /** * Additional native properties to set on the JPA provider. */ @@ -144,19 +136,7 @@ public class JpaProperties { if (this.database != null) { return this.database; } - try { - String jdbcUrl = (String) JdbcUtils.extractDatabaseMetaData(dataSource, - "getURL"); - DatabasePlatform databasePlatform = DatabasePlatform.fromDatabaseDriver( - DatabaseDriver.fromJdbcUrl(jdbcUrl)); - if (databasePlatform != null) { - return databasePlatform.getDatabase(); - } - } - catch (MetaDataAccessException ex) { - logger.warn("Unable to determine jdbc url from datasource", ex); - } - return Database.DEFAULT; + return DatabaseLookup.getDatabase(dataSource); } public static class Hibernate { diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/CustomHibernateJpaAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/CustomHibernateJpaAutoConfigurationTests.java index 208e1e88fb..e0fff71ec8 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/CustomHibernateJpaAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/CustomHibernateJpaAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * 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. @@ -41,7 +41,7 @@ import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.mock; +import static org.mockito.Mockito.mock; /** * Tests for {@link HibernateJpaAutoConfiguration}. @@ -120,7 +120,8 @@ public class CustomHibernateJpaAutoConfigurationTests { PropertyPlaceholderAutoConfiguration.class, HibernateJpaAutoConfiguration.class); this.context.refresh(); - HibernateJpaVendorAdapter bean = this.context.getBean(HibernateJpaVendorAdapter.class); + HibernateJpaVendorAdapter bean = this.context + .getBean(HibernateJpaVendorAdapter.class); Database database = (Database) ReflectionTestUtils.getField(bean, "database"); assertThat(database).isEqualTo(Database.H2); } @@ -139,11 +140,11 @@ public class CustomHibernateJpaAutoConfigurationTests { DataSource dataSource = mock(DataSource.class); try { given(dataSource.getConnection()).willReturn(mock(Connection.class)); - given(dataSource.getConnection().getMetaData()).willReturn( - mock(DatabaseMetaData.class)); + given(dataSource.getConnection().getMetaData()) + .willReturn(mock(DatabaseMetaData.class)); } catch (SQLException e) { - //Do nothing + // Do nothing } return dataSource; } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookupTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookupTests.java new file mode 100644 index 0000000000..a3ff71753f --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabaseLookupTests.java @@ -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); + } + +} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabasePlatformTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabasePlatformTests.java deleted file mode 100644 index d91cc6f0b8..0000000000 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/DatabasePlatformTests.java +++ /dev/null @@ -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); - } - -} diff --git a/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java b/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java index 27b7548594..e2ea57888e 100644 --- a/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java +++ b/spring-boot/src/main/java/org/springframework/boot/jdbc/DatabaseDriver.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * 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. @@ -72,8 +72,8 @@ public enum DatabaseDriver { /** * Maria DB. */ - MARIADB("MySQL", "org.mariadb.jdbc.Driver", - "org.mariadb.jdbc.MariaDbDataSource", "SELECT 1") { + MARIADB("MySQL", "org.mariadb.jdbc.Driver", "org.mariadb.jdbc.MariaDbDataSource", + "SELECT 1") { @Override public String getId() { @@ -181,6 +181,7 @@ public enum DatabaseDriver { protected Collection getUrlPrefixes() { return Arrays.asList("informix-sqli", "informix-direct"); } + }; private final String productName;