Skip to content

Commit e988e59

Browse files
schaudermp911de
authored andcommitted
DATAJDBC-461 - DatabaseProfileValueSource now supports setting via Environment.
1 parent 8c727dd commit e988e59

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/DatabaseProfileValueSource.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,21 @@
2727
*/
2828
public class DatabaseProfileValueSource implements ProfileValueSource {
2929

30+
static final String SPRING_PROFILES_ACTIVE = "spring.profiles.active";
31+
static final String CURRENT_DATABASE_IS_NOT = "current.database.is.not.";
32+
3033
private final String currentDatabase;
3134

3235
DatabaseProfileValueSource() {
3336

34-
currentDatabase = System.getProperty("spring.profiles.active", "hsqldb");
37+
String fromEnvironment = System.getenv(SPRING_PROFILES_ACTIVE);
38+
currentDatabase = fromEnvironment == null ? System.getProperty(SPRING_PROFILES_ACTIVE, "hsqldb") : fromEnvironment;
3539
}
3640

3741
@Override
3842
public String get(String key) {
3943

40-
if (!key.startsWith("current.database.is.not.")) {
44+
if (!key.startsWith(CURRENT_DATABASE_IS_NOT)) {
4145
return null;
4246
}
4347

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jdbc.testing;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.springframework.data.jdbc.testing.DatabaseProfileValueSource.*;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
/**
26+
* Unit tests for {@link DatabaseProfileValueSource}.
27+
*
28+
* @author Jens Schauder
29+
*/
30+
public class DatabaseProfileValueSourceUnitTests {
31+
32+
String oldSystemPropertyValue;
33+
34+
@Before
35+
public void before() {
36+
oldSystemPropertyValue = System.getProperty(SPRING_PROFILES_ACTIVE);
37+
}
38+
39+
@After
40+
public void after() {
41+
42+
if (oldSystemPropertyValue == null) {
43+
System.clearProperty(SPRING_PROFILES_ACTIVE);
44+
} else {
45+
System.setProperty(SPRING_PROFILES_ACTIVE, oldSystemPropertyValue);
46+
}
47+
}
48+
49+
@Test // DATAJDBC-461
50+
public void returnNullForUnrelatedProperty() {
51+
52+
DatabaseProfileValueSource source = new DatabaseProfileValueSource();
53+
assertThat(source.get("blah")).isNull();
54+
}
55+
56+
@Test // DATAJDBC-461
57+
public void worksWithSystemProperty() {
58+
59+
System.setProperty(SPRING_PROFILES_ACTIVE, "testProfile");
60+
61+
DatabaseProfileValueSource source = new DatabaseProfileValueSource();
62+
63+
assertThat(source.get(CURRENT_DATABASE_IS_NOT + "other")).isEqualTo("true");
64+
assertThat(source.get(CURRENT_DATABASE_IS_NOT + "testProfile")).isEqualTo("false");
65+
}
66+
67+
}

0 commit comments

Comments
 (0)