1
1
package alpine ;
2
2
3
+ import io .smallrye .config .ConfigMapping ;
4
+ import io .smallrye .config .SmallRyeConfigBuilder ;
5
+ import io .smallrye .config .SmallRyeConfigBuilderCustomizer ;
6
+ import io .smallrye .config .WithDefault ;
7
+ import org .eclipse .microprofile .config .ConfigValue ;
3
8
import org .junit .jupiter .api .AfterAll ;
4
- import org .junit .jupiter .api .AfterEach ;
5
9
import org .junit .jupiter .api .Test ;
6
10
import org .junitpioneer .jupiter .RestoreEnvironmentVariables ;
7
11
import org .junitpioneer .jupiter .RestoreSystemProperties ;
8
12
import org .junitpioneer .jupiter .SetEnvironmentVariable ;
13
+ import org .junitpioneer .jupiter .SetSystemProperty ;
9
14
10
15
import java .net .URL ;
16
+ import java .nio .file .Files ;
17
+ import java .nio .file .Path ;
18
+ import java .nio .file .StandardCopyOption ;
11
19
import java .util .Map ;
20
+ import java .util .Optional ;
12
21
13
22
import static org .assertj .core .api .Assertions .assertThat ;
14
23
15
24
public class ConfigTest {
16
25
17
- @ AfterEach
18
- public void tearDown () {
19
- Config .reset ();
26
+ @ AfterAll
27
+ public static void tearDown () {
28
+ Config .reload (); // Ensure we're not affecting other tests
20
29
}
21
30
22
- @ AfterAll
23
- public static void tearDownClass () {
24
- Config .getInstance ().init (); // Ensure we're not affecting other tests
31
+ @ Test
32
+ @ RestoreEnvironmentVariables
33
+ @ SetEnvironmentVariable (key = "ALPINE_NO_PROXY" , value = "foo, bar, baz" )
34
+ void testGetPropertyAsList () {
35
+ Config .reload ();
36
+
37
+ assertThat (Config .getInstance ().getPropertyAsList (Config .AlpineKey .NO_PROXY )).containsExactly ("foo" , "bar" , "baz" );
38
+ }
39
+
40
+ @ Test
41
+ void testGetProperty () {
42
+ Config .reload ();
43
+
44
+ // Property with default value.
45
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .DATABASE_URL )).isEqualTo ("jdbc:h2:mem:alpine" );
46
+
47
+ // Property without default value.
48
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .SECRET_KEY_PATH )).isNull ();
25
49
}
26
50
27
51
@ Test
28
52
public void testGetPassThroughPropertiesEmpty () {
29
- Config .getInstance (). init ();
53
+ Config .reload ();
30
54
31
55
assertThat (Config .getInstance ().getPassThroughProperties ("datanucleus" )).isEmpty ();
32
56
}
@@ -43,21 +67,139 @@ public void testGetPassThroughPropertiesEmpty() {
43
67
@ SetEnvironmentVariable (key = "ALPINE_DATANUCLEUS_FROM_ENV" , value = "fromEnv7" )
44
68
@ SetEnvironmentVariable (key = "alpine_datanucleus_from_env_lowercase" , value = "fromEnv8" )
45
69
@ SetEnvironmentVariable (key = "Alpine_DataNucleus_From_Env_MixedCase" , value = "fromEnv9" )
46
- public void testGetPassThroughProperties () {
70
+ @ SetEnvironmentVariable (key = "ALPINE_DATANUCLEUS_EXPRESSION_FROM_ENV" , value = "${alpine.datanucleus.from.env}" )
71
+ public void testGetPassThroughProperties () throws Exception {
47
72
final URL propertiesUrl = ConfigTest .class .getResource ("/Config_testGetPassThroughProperties.properties" );
48
73
assertThat (propertiesUrl ).isNotNull ();
49
74
50
- System .setProperty ("alpine.application.properties" , propertiesUrl .getPath ());
75
+ final Path tmpPropertiesFile = Files .createTempFile (null , ".properties" );
76
+ Files .copy (propertiesUrl .openStream (), tmpPropertiesFile , StandardCopyOption .REPLACE_EXISTING );
77
+
78
+ System .setProperty ("alpine.application.properties" , tmpPropertiesFile .toUri ().toString ());
51
79
52
- Config .getInstance (). init ();
80
+ Config .reload ();
53
81
54
82
assertThat (Config .getInstance ().getPassThroughProperties ("datanucleus" ))
55
83
.containsExactlyInAnyOrderEntriesOf (Map .of (
56
84
"datanucleus.foo" , "fromEnv3" , // ENV takes precedence over properties
57
85
"datanucleus.foo.bar" , "fromEnv4" , // ENV takes precedence over properties
58
86
"datanucleus.from.env" , "fromEnv7" ,
59
- "datanucleus.from.props" , "fromProps7"
87
+ "datanucleus.from.props" , "fromProps7" ,
88
+ "datanucleus.from.env.lowercase" , "fromEnv8" ,
89
+ "datanucleus.from.env.mixedcase" , "fromEnv9" ,
90
+ "datanucleus.expression.from.props" , "fromEnv3" ,
91
+ "datanucleus.expression.from.env" , "fromEnv7"
60
92
));
61
93
}
62
94
95
+ @ ConfigMapping (prefix = "alpine" )
96
+ public interface TestConfig {
97
+
98
+ DatabaseConfig database ();
99
+
100
+ interface DatabaseConfig {
101
+
102
+ Optional <String > url ();
103
+
104
+ @ WithDefault ("testUser" )
105
+ String username ();
106
+
107
+ Map <String , String > pool ();
108
+
109
+ }
110
+
111
+ }
112
+
113
+ public static class ConfigBuilderCustomizer implements SmallRyeConfigBuilderCustomizer {
114
+
115
+ @ Override
116
+ public void configBuilder (final SmallRyeConfigBuilder configBuilder ) {
117
+ configBuilder
118
+ .withMapping (TestConfig .class )
119
+ .withValidateUnknown (false );
120
+ }
121
+
122
+ }
123
+
124
+ @ Test
125
+ @ RestoreEnvironmentVariables
126
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_URL" , value = "jdbc:h2:mem:alpine" )
127
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_POOL_MAX_SIZE" , value = "666" )
128
+ void testGetMapping () {
129
+ Config .reload ();
130
+
131
+ final var testConfig = Config .getInstance ().getMapping (TestConfig .class );
132
+ assertThat (testConfig ).isNotNull ();
133
+ assertThat (testConfig .database ().url ()).contains ("jdbc:h2:mem:alpine" );
134
+ assertThat (testConfig .database ().username ()).isEqualTo ("testUser" );
135
+ assertThat (testConfig .database ().pool ())
136
+ .containsExactlyInAnyOrderEntriesOf (Map .of ("max.size" , "666" ));
137
+ }
138
+
139
+ @ Test
140
+ @ RestoreEnvironmentVariables
141
+ @ SetEnvironmentVariable (key = "ALPINE_CONFIG_PROFILE" , value = "dev" )
142
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_URL" , value = "defaultUrl" )
143
+ @ SetEnvironmentVariable (key = "_DEV_ALPINE_DATABASE_URL" , value = "devUrl" )
144
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_USERNAME" , value = "defaultUser" )
145
+ void testProfiles () {
146
+ Config .reload ();
147
+
148
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .DATABASE_URL )).isEqualTo ("devUrl" );
149
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .DATABASE_USERNAME )).isEqualTo ("defaultUser" );
150
+ }
151
+
152
+ @ Test
153
+ @ RestoreEnvironmentVariables
154
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_URL" , value = "defaultUrl" )
155
+ @ SetEnvironmentVariable (key = "_PROD_ALPINE_DATABASE_URL" , value = "prodUrl" )
156
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_USERNAME" , value = "defaultUser" )
157
+ void testDefaultProfile () {
158
+ Config .reload ();
159
+
160
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .DATABASE_URL )).isEqualTo ("prodUrl" );
161
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .DATABASE_USERNAME )).isEqualTo ("defaultUser" );
162
+ }
163
+
164
+ @ Test
165
+ @ RestoreEnvironmentVariables
166
+ @ RestoreSystemProperties
167
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_USERNAME" , value = "envUsername" )
168
+ @ SetSystemProperty (key = "alpine.database.password" , value = "propertyPassword" )
169
+ void testGetValue () throws Exception {
170
+ final URL propertiesUrl = ConfigTest .class .getResource ("/Config_testGetValue.properties" );
171
+ assertThat (propertiesUrl ).isNotNull ();
172
+
173
+ final Path tmpPropertiesFile = Files .createTempFile (null , ".properties" );
174
+ Files .copy (propertiesUrl .openStream (), tmpPropertiesFile , StandardCopyOption .REPLACE_EXISTING );
175
+
176
+ System .setProperty ("alpine.application.properties" , tmpPropertiesFile .toUri ().toString ());
177
+
178
+ Config .reload ();
179
+
180
+ ConfigValue configValue = Config .getInstance ().getDelegate ().getConfigValue (Config .AlpineKey .DATABASE_URL .getPropertyName ());
181
+ assertThat (configValue .getValue ()).isEqualTo ("jdbc:h2:mem:alpine" );
182
+ assertThat (configValue .getSourceName ()).matches (
183
+ "PropertiesConfigSource\\ [source=file:.+\\ .properties]" );
184
+
185
+ configValue = Config .getInstance ().getDelegate ().getConfigValue (Config .AlpineKey .DATABASE_USERNAME .getPropertyName ());
186
+ assertThat (configValue .getValue ()).isEqualTo ("envUsername" );
187
+ assertThat (configValue .getSourceName ()).isEqualTo ("EnvConfigSource" );
188
+
189
+ configValue = Config .getInstance ().getDelegate ().getConfigValue (Config .AlpineKey .DATABASE_PASSWORD .getPropertyName ());
190
+ assertThat (configValue .getValue ()).isEqualTo ("propertyPassword" );
191
+ assertThat (configValue .getSourceName ()).isEqualTo ("SysPropConfigSource" );
192
+ }
193
+
194
+ @ Test
195
+ @ RestoreEnvironmentVariables
196
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_USERNAME" , value = "dbUsername" )
197
+ @ SetEnvironmentVariable (key = "ALPINE_DATABASE_PASSWORD" , value = "${alpine.database.username}-123" )
198
+ void testExpression () {
199
+ Config .reload ();
200
+
201
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .DATABASE_USERNAME )).isEqualTo ("dbUsername" );
202
+ assertThat (Config .getInstance ().getProperty (Config .AlpineKey .DATABASE_PASSWORD )).isEqualTo ("dbUsername-123" );
203
+ }
204
+
63
205
}
0 commit comments