DATAJDBC-461 - DatabaseProfileValueSource now supports setting via Environment.

This commit is contained in:
Jens Schauder
2019-12-17 08:11:10 +01:00
parent 85bc2ee73a
commit a48b66743e
2 changed files with 73 additions and 2 deletions

View File

@@ -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;
}

View File

@@ -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");
}
}