From a48b66743edcdd7da7e67953e9df574599422b44 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Tue, 17 Dec 2019 08:11:10 +0100 Subject: [PATCH] DATAJDBC-461 - DatabaseProfileValueSource now supports setting via Environment. --- .../testing/DatabaseProfileValueSource.java | 8 ++- .../DatabaseProfileValueSourceUnitTests.java | 67 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSourceUnitTests.java diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java index 776a2ea4..bb74ffec 100644 --- a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java @@ -27,17 +27,21 @@ import org.springframework.test.annotation.ProfileValueSource; */ public class DatabaseProfileValueSource implements ProfileValueSource { + static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active"; + static final String CURRENT_DATABASE_IS_NOT = "current.database.is.not."; + private final String currentDatabase; DatabaseProfileValueSource() { - currentDatabase = System.getProperty("spring.profiles.active", "hsqldb"); + String fromEnvironment = System.getenv(SPRING_PROFILES_ACTIVE); + currentDatabase = fromEnvironment == null ? System.getProperty(SPRING_PROFILES_ACTIVE, "hsqldb") : fromEnvironment; } @Override public String get(String key) { - if (!key.startsWith("current.database.is.not.")) { + if (!key.startsWith(CURRENT_DATABASE_IS_NOT)) { return null; } diff --git a/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSourceUnitTests.java b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSourceUnitTests.java new file mode 100644 index 00000000..62e88f29 --- /dev/null +++ b/spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSourceUnitTests.java @@ -0,0 +1,67 @@ +/* + * Copyright 2019 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 + * + * https://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.data.jdbc.testing; + +import static org.assertj.core.api.Assertions.*; +import static org.springframework.data.jdbc.testing.DatabaseProfileValueSource.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests for {@link DatabaseProfileValueSource}. + * + * @author Jens Schauder + */ +public class DatabaseProfileValueSourceUnitTests { + + String oldSystemPropertyValue; + + @Before + public void before() { + oldSystemPropertyValue = System.getProperty(SPRING_PROFILES_ACTIVE); + } + + @After + public void after() { + + if (oldSystemPropertyValue == null) { + System.clearProperty(SPRING_PROFILES_ACTIVE); + } else { + System.setProperty(SPRING_PROFILES_ACTIVE, oldSystemPropertyValue); + } + } + + @Test // DATAJDBC-461 + public void returnNullForUnrelatedProperty() { + + DatabaseProfileValueSource source = new DatabaseProfileValueSource(); + assertThat(source.get("blah")).isNull(); + } + + @Test // DATAJDBC-461 + public void worksWithSystemProperty() { + + System.setProperty(SPRING_PROFILES_ACTIVE, "testProfile"); + + DatabaseProfileValueSource source = new DatabaseProfileValueSource(); + + assertThat(source.get(CURRENT_DATABASE_IS_NOT + "other")).isEqualTo("true"); + assertThat(source.get(CURRENT_DATABASE_IS_NOT + "testProfile")).isEqualTo("false"); + } + +}