Update DataSource auto-configuration to support XA

Update DataSource and JPA auto-configuration to consider XA datasources.

See gh-947
This commit is contained in:
Phillip Webb
2014-08-22 12:10:58 -07:00
parent 5ef136511f
commit 8219f2be4c
12 changed files with 528 additions and 34 deletions

View File

@@ -20,47 +20,46 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link DriverClassNameProvider}.
* Tests for {@link DatabaseDriver}.
*
* @author Phillip Webb
* @author Maciej Walkowiak
*/
public class DriverClassNameProviderTests {
private DriverClassNameProvider provider = new DriverClassNameProvider();
public class DatabaseDriverTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void classNameForKnownDatabase() {
String driverClassName = this.provider
.getDriverClassName("jdbc:postgresql://hostname/dbname");
String driverClassName = DatabaseDriver.fromJdbcUrl(
"jdbc:postgresql://hostname/dbname").getDriverClassName();
assertEquals("org.postgresql.Driver", driverClassName);
}
@Test
public void nullForUnknownDatabase() {
String driverClassName = this.provider
.getDriverClassName("jdbc:unknowndb://hostname/dbname");
public void nullClassNameForUnknownDatabase() {
String driverClassName = DatabaseDriver.fromJdbcUrl(
"jdbc:unknowndb://hostname/dbname").getDriverClassName();
assertNull(driverClassName);
}
@Test
public void failureOnNullJdbcUrl() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("JdbcUrl must not be null");
this.provider.getDriverClassName(null);
public void unknownOnNullJdbcUrl() {
assertThat(DatabaseDriver.fromJdbcUrl(null), equalTo(DatabaseDriver.UNKNOWN));
}
@Test
public void failureOnMalformedJdbcUrl() {
this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("JdbcUrl must start with");
this.provider.getDriverClassName("malformed:url");
this.thrown.expectMessage("URL must start with");
DatabaseDriver.fromJdbcUrl("malformed:url");
}
}

View File

@@ -0,0 +1,128 @@
/*
* 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 javax.sql.DataSource;
import javax.sql.XADataSource;
import org.hsqldb.jdbc.pool.JDBCXADataSource;
import org.junit.Test;
import org.springframework.boot.jta.XADataSourceWrapper;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link XADataSourceAutoConfiguration}.
*
* @author Phillip Webb
*/
public class XADataSourceAutoConfigurationTests {
@Test
public void wrapExistingXaDataSource() throws Exception {
ApplicationContext context = createContext(WrapExisting.class);
context.getBean(DataSource.class);
XADataSource source = context.getBean(XADataSource.class);
MockXADataSourceWrapper wrapper = context.getBean(MockXADataSourceWrapper.class);
assertThat(wrapper.getXaDataSource(), equalTo(source));
}
@Test
public void createFromUrl() throws Exception {
ApplicationContext context = createContext(FromProperties.class,
"spring.datasource.url:jdbc:hsqldb:mem:test",
"spring.datasource.username:un");
context.getBean(DataSource.class);
MockXADataSourceWrapper wrapper = context.getBean(MockXADataSourceWrapper.class);
JDBCXADataSource dataSource = (JDBCXADataSource) wrapper.getXaDataSource();
assertNotNull(dataSource);
assertThat(dataSource.getUrl(), equalTo("jdbc:hsqldb:mem:test"));
assertThat(dataSource.getUser(), equalTo("un"));
}
@Test
public void createFromClass() throws Exception {
ApplicationContext context = createContext(
FromProperties.class,
"spring.datasource.xa.data-source-class:org.hsqldb.jdbc.pool.JDBCXADataSource",
"spring.datasource.xa.properties.database-name:test");
context.getBean(DataSource.class);
MockXADataSourceWrapper wrapper = context.getBean(MockXADataSourceWrapper.class);
JDBCXADataSource dataSource = (JDBCXADataSource) wrapper.getXaDataSource();
assertNotNull(dataSource);
assertThat(dataSource.getDatabaseName(), equalTo("test"));
}
private ApplicationContext createContext(Class<?> configuration, String... env) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, env);
context.register(configuration, XADataSourceAutoConfiguration.class);
context.refresh();
return context;
}
@Configuration
static class WrapExisting {
@Bean
public MockXADataSourceWrapper wrapper() {
return new MockXADataSourceWrapper();
}
@Bean
public XADataSource xaDataSource() {
return mock(XADataSource.class);
}
}
@Configuration
static class FromProperties {
@Bean
public MockXADataSourceWrapper wrapper() {
return new MockXADataSourceWrapper();
}
}
private static class MockXADataSourceWrapper implements XADataSourceWrapper {
private XADataSource dataSource;
@Override
public DataSource wrapDataSource(XADataSource dataSource) {
this.dataSource = dataSource;
return mock(DataSource.class);
}
public XADataSource getXaDataSource() {
return this.dataSource;
}
}
}