Skip to content

Commit 6a4901b

Browse files
authored
Merge pull request #10 from ericdallo/issue-9
[ISSUE-9] Fallback to os properties if java property was not found
2 parents 52e1999 + a4846aa commit 6a4901b

File tree

2 files changed

+45
-29
lines changed

2 files changed

+45
-29
lines changed

src/main/java/com/spring/loader/util/SystemPropertyResolver.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
import static java.lang.String.format;
44
import static org.springframework.util.StringUtils.isEmpty;
55

6-
import com.spring.loader.exception.EnviromentPropertyNotFoundException;
7-
import com.spring.loader.exception.InvalidS3LocationException;
8-
96
import java.util.regex.Matcher;
107
import java.util.regex.Pattern;
118

9+
import com.spring.loader.exception.EnviromentPropertyNotFoundException;
10+
import com.spring.loader.exception.InvalidS3LocationException;
11+
1212
/**
13-
* Resolver for properties that will be retrieved from system environment.
14-
*
13+
* Resolver for properties that will be retrieved from system environment.
14+
*
1515
* @author Eric Dallo
1616
* @since 2.0
1717
*/
@@ -40,15 +40,19 @@ public String getFormattedValue(String value) {
4040

4141
return bucket;
4242
}
43-
43+
4444
return value;
4545
}
4646

4747
private String getFromEnv(String key) {
48-
String valueFromEnv = System.getenv(key);
48+
String valueFromEnv = System.getProperty(key);
4949

5050
if (isEmpty(valueFromEnv)) {
51-
throw new EnviromentPropertyNotFoundException(format("Environment variable %s not found in system", key));
51+
valueFromEnv = System.getenv(key);
52+
53+
if (isEmpty(valueFromEnv)) {
54+
throw new EnviromentPropertyNotFoundException(format("Environment variable %s not found in system and java properties", key));
55+
}
5256
}
5357

5458
return valueFromEnv;

src/test/java/com/spring/loader/util/SystemPropertyResolverTest.java

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
package com.spring.loader.util;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.mockito.Matchers.eq;
45

56
import org.junit.Before;
67
import org.junit.Test;
78
import org.junit.runner.RunWith;
8-
import org.mockito.Mockito;
99
import org.powermock.api.mockito.PowerMockito;
1010
import org.powermock.core.classloader.annotations.PrepareForTest;
1111
import org.powermock.modules.junit4.PowerMockRunner;
1212

1313
import com.spring.loader.exception.EnviromentPropertyNotFoundException;
1414
import com.spring.loader.exception.InvalidS3LocationException;
15-
import com.spring.loader.util.SystemPropertyResolver;
1615

1716
@PrepareForTest(SystemPropertyResolver.class)
1817
@RunWith(PowerMockRunner.class)
1918
public class SystemPropertyResolverTest {
20-
19+
2120
private SystemPropertyResolver subject;
2221

2322
@Before
@@ -29,13 +28,26 @@ public void setup() {
2928
@Test
3029
public void shouldGetFormattedValueWhenValueIsValid() {
3130
String expected = "someValue";
32-
33-
PowerMockito.when(System.getenv(Mockito.eq("AWS_S3"))).thenReturn(expected);
34-
31+
32+
PowerMockito.when(System.getenv(eq("AWS_S3"))).thenReturn(expected);
33+
3534
String env = "${AWS_S3}";
36-
35+
3736
String formattedValue = subject.getFormattedValue(env);
38-
37+
38+
assertEquals(expected, formattedValue);
39+
}
40+
41+
@Test
42+
public void shouldGetFormattedValueFromPropertiesWhenValueIsValid() {
43+
String expected = "someValue";
44+
45+
PowerMockito.when(System.getProperty(eq("AWS_S3"))).thenReturn(expected);
46+
47+
String env = "${AWS_S3}";
48+
49+
String formattedValue = subject.getFormattedValue(env);
50+
3951
assertEquals(expected, formattedValue);
4052
}
4153

@@ -44,8 +56,8 @@ public void shouldGetCombinedFormattedValueWhenValueIsValid() {
4456
String region = "someRegion";
4557
String environment = "someEnvironment";
4658

47-
PowerMockito.when(System.getenv(Mockito.eq("S3_BUCKET_REGION"))).thenReturn(region);
48-
PowerMockito.when(System.getenv(Mockito.eq("S3_BUCKET_ENVIRONMENT"))).thenReturn(environment);
59+
PowerMockito.when(System.getenv(eq("S3_BUCKET_REGION"))).thenReturn(region);
60+
PowerMockito.when(System.getenv(eq("S3_BUCKET_ENVIRONMENT"))).thenReturn(environment);
4961

5062
String configValue = "${S3_BUCKET_REGION}/${S3_BUCKET_ENVIRONMENT}/myApplication/application.properties";
5163
String expected = String.format("%s/%s/myApplication/application.properties", region, environment);
@@ -59,7 +71,7 @@ public void shouldGetCombinedFormattedValueWhenValueIsValid() {
5971
public void shouldReplaceMultiple() {
6072
String environment = "dev";
6173

62-
PowerMockito.when(System.getenv(Mockito.eq("EC2_ENVIRONMENT"))).thenReturn(environment);
74+
PowerMockito.when(System.getenv(eq("EC2_ENVIRONMENT"))).thenReturn(environment);
6375

6476
String configValue = "region-${EC2_ENVIRONMENT}/deploy-${EC2_ENVIRONMENT}/application.properties";
6577
String expected = String.format("region-%s/deploy-%s/application.properties", environment, environment);
@@ -72,32 +84,32 @@ public void shouldReplaceMultiple() {
7284
@Test
7385
public void shouldGetFormattedValueWhenValueIsValidAndNotASystemEnv() {
7486
String expected = "someValue";
75-
87+
7688
String formattedValue = subject.getFormattedValue(expected);
77-
89+
7890
assertEquals(expected, formattedValue);
7991
}
80-
92+
8193
@Test(expected = InvalidS3LocationException.class)
8294
public void shouldNotGetFormattedValueWhenValueIsEmpty() {
8395
String expected = "";
84-
96+
8597
subject.getFormattedValue(expected);
8698
}
87-
99+
88100
@Test(expected = InvalidS3LocationException.class)
89101
public void shouldNotGetFormattedValueWhenValueHasAInvalidSyntax() {
90102
String expected = "${AWS_S3";
91-
103+
92104
subject.getFormattedValue(expected);
93105
}
94-
106+
95107
@Test(expected = EnviromentPropertyNotFoundException.class)
96108
public void shouldNotGetFormattedValueWhenSystemDoesNotHasTheEnvironmentVariable() {
97-
PowerMockito.when(System.getenv(Mockito.eq("AWS_S3"))).thenReturn(null);
98-
109+
PowerMockito.when(System.getenv(eq("AWS_S3"))).thenReturn(null);
110+
99111
String env = "${AWS_S3}";
100-
112+
101113
subject.getFormattedValue(env);
102114
}
103115

0 commit comments

Comments
 (0)