Skip to content

Commit 6c0d2c5

Browse files
authored
Add JUnit profiles helidon-io#3115 (helidon-io#3391)
* Add configuration profile to JUnit tests. Integration tests included. * Minor formatting rollbacks.
1 parent 786244b commit 6c0d2c5

4 files changed

Lines changed: 93 additions & 2 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates.
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+
* http://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+
17+
package io.helidon.microprofile.tests.junit5;
18+
19+
import org.eclipse.microprofile.config.inject.ConfigProperty;
20+
import org.junit.jupiter.api.Test;
21+
22+
import javax.inject.Inject;
23+
24+
import static org.hamcrest.CoreMatchers.is;
25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
27+
@HelidonTest
28+
@Configuration(profile = "custom")
29+
public class TestConfigurationCustomProfile {
30+
31+
@Inject
32+
@ConfigProperty(name = "mp.config.profile")
33+
private String profile;
34+
35+
@Test
36+
void testCustomProfile(){
37+
assertThat(profile, is("custom"));
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (c) 2021 Oracle and/or its affiliates.
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+
* http://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+
17+
package io.helidon.microprofile.tests.junit5;
18+
19+
import org.eclipse.microprofile.config.inject.ConfigProperty;
20+
import org.junit.jupiter.api.Test;
21+
22+
import javax.inject.Inject;
23+
24+
import static org.hamcrest.CoreMatchers.is;
25+
import static org.hamcrest.MatcherAssert.assertThat;
26+
27+
@HelidonTest
28+
@Configuration
29+
public class TestConfigurationDefaultProfile {
30+
31+
@Inject
32+
@ConfigProperty(name = "mp.config.profile")
33+
private String profile;
34+
35+
@Test
36+
void testProfile(){
37+
assertThat(profile, is("test"));
38+
}
39+
}

microprofile/tests/junit5/src/main/java/io/helidon/microprofile/tests/junit5/Configuration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020 Oracle and/or its affiliates.
2+
* Copyright (c) 2020, 2021 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,4 +48,11 @@
4848
* @return config sources to add
4949
*/
5050
String[] configSources() default {};
51+
52+
/**
53+
* Configuration profile.
54+
*
55+
* @return String with default value "test".
56+
*/
57+
String profile() default "test";
5158
}

microprofile/tests/junit5/src/main/java/io/helidon/microprofile/tests/junit5/HelidonJunitExtension.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,6 @@ private void configure(ConfigMeta configMeta) {
294294
builder.withSources(MpConfigSources.classPath(it).toArray(new ConfigSource[0]));
295295
}
296296
});
297-
298297
config = builder
299298
.withSources(MpConfigSources.create(configMeta.additionalKeys))
300299
.addDefaultSources()
@@ -508,6 +507,7 @@ private static final class ConfigMeta {
508507
private final Map<String, String> additionalKeys = new HashMap<>();
509508
private final List<String> additionalSources = new ArrayList<>();
510509
private boolean useExisting;
510+
private String profile;
511511

512512
private ConfigMeta() {
513513
// to allow SeContainerInitializer (forbidden by default because of native image)
@@ -517,6 +517,8 @@ private ConfigMeta() {
517517
additionalKeys.put("server.port", "0");
518518
// higher ordinal then all the defaults, system props and environment variables
519519
additionalKeys.putIfAbsent(ConfigSource.CONFIG_ORDINAL, "1000");
520+
// profile
521+
additionalKeys.put("mp.config.profile", "test");
520522
}
521523

522524
private void addConfig(AddConfig[] configs) {
@@ -530,7 +532,10 @@ private void configuration(Configuration config) {
530532
return;
531533
}
532534
useExisting = config.useExisting();
535+
profile = config.profile();
533536
additionalSources.addAll(List.of(config.configSources()));
537+
//set additional key for profile
538+
additionalKeys.put("mp.config.profile", profile);
534539
}
535540

536541
ConfigMeta nextMethod() {
@@ -539,6 +544,7 @@ ConfigMeta nextMethod() {
539544
methodMeta.additionalKeys.putAll(this.additionalKeys);
540545
methodMeta.additionalSources.addAll(this.additionalSources);
541546
methodMeta.useExisting = this.useExisting;
547+
methodMeta.profile = this.profile;
542548

543549
return methodMeta;
544550
}

0 commit comments

Comments
 (0)