Added DriverClassNameProvider used for providing jdbc driver class based on jdbc url

Uses known databases from org.flywaydb.core.internal.util.jdbc.DriverDataSource

Fixes gh-824, fixes gh-809
This commit is contained in:
Maciej Walkowiak
2014-05-08 15:54:31 +02:00
committed by Dave Syer
parent a5543f18b9
commit b33eb95dd2
5 changed files with 187 additions and 3 deletions

View File

@@ -95,6 +95,8 @@ public class DataSourceAutoConfigurationTests {
@Test(expected = BeanCreationException.class)
public void testBadUrl() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.url:jdbc:not-going-to-work");
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
this.context.register(DataSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
@@ -104,6 +106,9 @@ public class DataSourceAutoConfigurationTests {
@Test(expected = BeanCreationException.class)
public void testBadDriverClass() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.driverClassName:org.none.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb");
EmbeddedDatabaseConnection.override = EmbeddedDatabaseConnection.NONE;
this.context.register(DataSourceAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);

View File

@@ -0,0 +1,31 @@
package org.springframework.boot.autoconfigure.jdbc;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Tests for {@link DataSourceProperties}.
*
* @author Maciej Walkowiak
*/
public class DataSourcePropertiesTests {
@Test
public void correctDriverClassNameFromJdbcUrlWhenDriverClassNameNotDefined() {
DataSourceProperties configuration = new DataSourceProperties();
configuration.setUrl("jdbc:mysql://mydb");
String driverClassName = configuration.getDriverClassName();
assertEquals(driverClassName, "com.mysql.jdbc.Driver");
}
@Test
public void driverClassNameFromDriverClassNamePropertyWhenDefined() {
DataSourceProperties configuration = new DataSourceProperties();
configuration.setUrl("jdbc:mysql://mydb");
configuration.setDriverClassName("my.driver.ClassName");
String driverClassName = configuration.getDriverClassName();
assertEquals(driverClassName, "my.driver.ClassName");
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2012-2014 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.jdbc;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/**
* Tests for {@link DriverClassNameProvider}.
*
* @author Maciej Walkowiak
*/
public class DriverClassNameProviderTest {
private DriverClassNameProvider driverClassNameProvider = new DriverClassNameProvider();
@Test
public void testGettingClassNameForKnownDatabase() {
String driverClassName = driverClassNameProvider.getDriverClassName("jdbc:postgresql://hostname/dbname");
assertEquals("org.postgresql.Driver", driverClassName);
}
@Test
public void testReturnsNullForUnknownDatabase() {
String driverClassName = driverClassNameProvider.getDriverClassName("jdbc:unknowndb://hostname/dbname");
assertNull(driverClassName);
}
@Test(expected = IllegalArgumentException.class)
public void testFailureOnNullJdbcUrl() {
driverClassNameProvider.getDriverClassName(null);
}
@Test(expected = IllegalArgumentException.class)
public void testFailureOnMalformedJdbcUrl() {
driverClassNameProvider.getDriverClassName("malformed:url");
}
}