From bcb9f159ca602fb3437123ee3f3fffdaf3572251 Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Sun, 19 Feb 2017 02:24:50 +0900 Subject: [PATCH] Add tests for AbstractRoutingDataSource See gh-1330 --- .../AbstractRoutingDataSourceTests.java | 185 ++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSourceTests.java diff --git a/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSourceTests.java b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSourceTests.java new file mode 100644 index 00000000000..a39729161c9 --- /dev/null +++ b/spring-jdbc/src/test/java/org/springframework/jdbc/datasource/lookup/AbstractRoutingDataSourceTests.java @@ -0,0 +1,185 @@ +/* + * Copyright 2002-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.jdbc.datasource.lookup; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import javax.sql.DataSource; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.core.IsSame.sameInstance; +import static org.junit.Assert.assertThat; + +/** + * Tests for {@link AbstractRoutingDataSource}. + * @author Kazuki Shimizu + */ +public class AbstractRoutingDataSourceTests { + + @Rule + public final ExpectedException exception = ExpectedException.none(); + + @Test + public void setTargetDataSources() { + final ThreadLocal lookupKey = new ThreadLocal<>(); + AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { + @Override + protected Object determineCurrentLookupKey() { + return lookupKey.get(); + } + }; + DataSource ds1 = new StubDataSource(); + DataSource ds2 = new StubDataSource(); + + MapDataSourceLookup dataSourceLookup = new MapDataSourceLookup(); + dataSourceLookup.addDataSource("dataSource2", ds2); + routingDataSource.setDataSourceLookup(dataSourceLookup); + + Map targetDataSources = new HashMap<>(); + targetDataSources.put("ds1", ds1); + targetDataSources.put("ds2", "dataSource2"); + routingDataSource.setTargetDataSources(targetDataSources); + + routingDataSource.afterPropertiesSet(); + + lookupKey.set("ds1"); + assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds1)); + + lookupKey.set("ds2"); + assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds2)); + } + + @Test + public void targetDataSourcesIsNull() { + AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { + @Override + protected Object determineCurrentLookupKey() { + return null; + } + }; + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Property 'targetDataSources' is required"); + + routingDataSource.afterPropertiesSet(); + } + + @Test + public void dataSourceIsUnSupportedType() { + AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { + @Override + protected Object determineCurrentLookupKey() { + return null; + } + }; + Map targetDataSources = new HashMap<>(); + targetDataSources.put("ds1", 1); + routingDataSource.setTargetDataSources(targetDataSources); + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Illegal data source value - only [javax.sql.DataSource] and String supported: 1"); + + routingDataSource.afterPropertiesSet(); + } + + + + @Test + public void setDefaultTargetDataSource() { + final ThreadLocal lookupKey = new ThreadLocal<>(); + AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { + @Override + protected Object determineCurrentLookupKey() { + return lookupKey.get(); + } + }; + DataSource ds = new StubDataSource(); + + routingDataSource.setTargetDataSources(new HashMap<>()); + routingDataSource.setDefaultTargetDataSource(ds); + + routingDataSource.afterPropertiesSet(); + + lookupKey.set("foo"); + assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds)); + } + + @Test + public void setDefaultTargetDataSourceFallbackIsFalse() { + final ThreadLocal lookupKey = new ThreadLocal<>(); + AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { + @Override + protected Object determineCurrentLookupKey() { + return lookupKey.get(); + } + }; + DataSource ds = new StubDataSource(); + + routingDataSource.setTargetDataSources(new HashMap<>()); + routingDataSource.setDefaultTargetDataSource(ds); + routingDataSource.setLenientFallback(false); + + routingDataSource.afterPropertiesSet(); + + exception.expect(IllegalStateException.class); + exception.expectMessage("Cannot determine target DataSource for lookup key [foo]"); + + lookupKey.set("foo"); + routingDataSource.determineTargetDataSource(); + } + + @Test + public void setDefaultTargetDataSourceLookupKeyIsNullWhenFallbackIsFalse() { + final ThreadLocal lookupKey = new ThreadLocal<>(); + AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { + @Override + protected Object determineCurrentLookupKey() { + return lookupKey.get(); + } + }; + DataSource ds = new StubDataSource(); + + routingDataSource.setTargetDataSources(new HashMap<>()); + routingDataSource.setDefaultTargetDataSource(ds); + routingDataSource.setLenientFallback(false); + + routingDataSource.afterPropertiesSet(); + + lookupKey.set(null); + assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds)); + } + + @Test + public void notInitialized() { + AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { + @Override + protected Object determineCurrentLookupKey() { + return null; + } + }; + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("DataSource router not initialized"); + + routingDataSource.determineTargetDataSource(); + } + +} \ No newline at end of file