From bcb9f159ca602fb3437123ee3f3fffdaf3572251 Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Sun, 19 Feb 2017 02:24:50 +0900 Subject: [PATCH 1/2] 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 From 23babe27bbae32325a076259672035decfccf9ca Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 22 Nov 2021 13:43:00 +0100 Subject: [PATCH 2/2] Polish "Add tests for AbstractRoutingDataSource" See gh-1330 --- .../AbstractRoutingDataSourceTests.java | 82 +++++++------------ 1 file changed, 29 insertions(+), 53 deletions(-) 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 index a39729161c9..7fb3445c4a7 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2021 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. @@ -16,29 +16,28 @@ 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; +import javax.sql.DataSource; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; + /** * Tests for {@link AbstractRoutingDataSource}. + * * @author Kazuki Shimizu */ -public class AbstractRoutingDataSourceTests { - - @Rule - public final ExpectedException exception = ExpectedException.none(); +class AbstractRoutingDataSourceTests { @Test - public void setTargetDataSources() { + void setTargetDataSources() { final ThreadLocal lookupKey = new ThreadLocal<>(); AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { @Override @@ -59,31 +58,26 @@ public class AbstractRoutingDataSourceTests { routingDataSource.setTargetDataSources(targetDataSources); routingDataSource.afterPropertiesSet(); - lookupKey.set("ds1"); - assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds1)); - + assertThat(routingDataSource.determineTargetDataSource()).isSameAs(ds1); lookupKey.set("ds2"); - assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds2)); + assertThat(routingDataSource.determineTargetDataSource()).isSameAs(ds2); } @Test - public void targetDataSourcesIsNull() { + void targetDataSourcesIsNull() { AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { @Override protected Object determineCurrentLookupKey() { return null; } }; - - exception.expect(IllegalArgumentException.class); - exception.expectMessage("Property 'targetDataSources' is required"); - - routingDataSource.afterPropertiesSet(); + assertThatIllegalArgumentException().isThrownBy(routingDataSource::afterPropertiesSet) + .withMessage("Property 'targetDataSources' is required"); } @Test - public void dataSourceIsUnSupportedType() { + void dataSourceIsUnSupportedType() { AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { @Override protected Object determineCurrentLookupKey() { @@ -93,17 +87,13 @@ public class AbstractRoutingDataSourceTests { 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(); + assertThatIllegalArgumentException().isThrownBy(routingDataSource::afterPropertiesSet) + .withMessage("Illegal data source value - only [javax.sql.DataSource] and String supported: 1"); } - @Test - public void setDefaultTargetDataSource() { + void setDefaultTargetDataSource() { final ThreadLocal lookupKey = new ThreadLocal<>(); AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { @Override @@ -112,18 +102,15 @@ public class AbstractRoutingDataSourceTests { } }; DataSource ds = new StubDataSource(); - routingDataSource.setTargetDataSources(new HashMap<>()); routingDataSource.setDefaultTargetDataSource(ds); - routingDataSource.afterPropertiesSet(); - lookupKey.set("foo"); - assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds)); + assertThat(routingDataSource.determineTargetDataSource()).isSameAs(ds); } @Test - public void setDefaultTargetDataSourceFallbackIsFalse() { + void setDefaultTargetDataSourceFallbackIsFalse() { final ThreadLocal lookupKey = new ThreadLocal<>(); AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { @Override @@ -132,22 +119,17 @@ public class AbstractRoutingDataSourceTests { } }; 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(); + assertThatIllegalStateException().isThrownBy(routingDataSource::determineTargetDataSource) + .withMessage("Cannot determine target DataSource for lookup key [foo]"); } @Test - public void setDefaultTargetDataSourceLookupKeyIsNullWhenFallbackIsFalse() { + void setDefaultTargetDataSourceLookupKeyIsNullWhenFallbackIsFalse() { final ThreadLocal lookupKey = new ThreadLocal<>(); AbstractRoutingDataSource routingDataSource = new AbstractRoutingDataSource() { @Override @@ -156,15 +138,12 @@ public class AbstractRoutingDataSourceTests { } }; DataSource ds = new StubDataSource(); - routingDataSource.setTargetDataSources(new HashMap<>()); routingDataSource.setDefaultTargetDataSource(ds); routingDataSource.setLenientFallback(false); - routingDataSource.afterPropertiesSet(); - lookupKey.set(null); - assertThat(routingDataSource.determineTargetDataSource(), sameInstance(ds)); + assertThat(routingDataSource.determineTargetDataSource()).isSameAs(ds); } @Test @@ -175,11 +154,8 @@ public class AbstractRoutingDataSourceTests { return null; } }; - - exception.expect(IllegalArgumentException.class); - exception.expectMessage("DataSource router not initialized"); - - routingDataSource.determineTargetDataSource(); + assertThatIllegalArgumentException().isThrownBy(routingDataSource::determineTargetDataSource) + .withMessage("DataSource router not initialized"); } } \ No newline at end of file