Skip to content

Commit 9a82081

Browse files
committed
review changes
1 parent 2a0c12a commit 9a82081

File tree

18 files changed

+457
-63
lines changed

18 files changed

+457
-63
lines changed

microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestConfigSynthetic.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
import static io.helidon.microprofile.testing.ReflectionHelper.invoke;
4040
import static io.helidon.microprofile.testing.ReflectionHelper.requireStatic;
41+
import static io.helidon.microprofile.testing.ReflectionHelper.isDefaultMethod;
4142

4243
/**
4344
* The synthetic test configuration that is expressed with annotations.
@@ -61,6 +62,7 @@ class HelidonTestConfigSynthetic extends HelidonTestConfigDelegate {
6162
HelidonTestConfigSynthetic(HelidonTestInfo<?> testInfo, Runnable onUpdate) {
6263
this.testInfo = testInfo;
6364
this.onUpdate = onUpdate;
65+
map.put(ConfigSource.CONFIG_ORDINAL, "1000");
6466
map.put("server.port", "0");
6567
map.put("mp.config.profile", "test");
6668
testInfo.addConfigs().forEach(this::update);
@@ -147,8 +149,7 @@ boolean useExisting() {
147149

148150
private Config buildConfig() {
149151
List<ConfigSource> configSources = new ArrayList<>();
150-
ConfigSource configSource = MpConfigSources.create(testInfo.id(), map);
151-
configSources.add(addConfigOrdinal(configSource, "1000"));
152+
configSources.add(MpConfigSources.create(testInfo.id(), map));
152153
blocks.forEach((type, values) -> {
153154
for (String value : values) {
154155
ConfigSource config = MpConfigSources.create(type, new StringReader(value));
@@ -157,7 +158,7 @@ private Config buildConfig() {
157158
});
158159
for (Method m : methods) {
159160
ConfigSource config = invoke(ConfigSource.class, requireStatic(m), null);
160-
configSources.add(addConfigOrdinal(config, "800"));
161+
configSources.add(new ConfigSourceWrapper(config));
161162
}
162163
for (String source : resources) {
163164
String filename = source.trim();
@@ -176,10 +177,6 @@ private Config buildConfig() {
176177
return builder.build();
177178
}
178179

179-
private ConfigSource addConfigOrdinal(ConfigSource config, String ordinal) {
180-
return addConfigOrdinal(config, "Map", ordinal);
181-
}
182-
183180
private ConfigSource addConfigOrdinal(ConfigSource config, String type, String ordinal) {
184181
Map<String, String> properties = new HashMap<>(config.getProperties());
185182
properties.putIfAbsent(ConfigSource.CONFIG_ORDINAL, ordinal);
@@ -203,4 +200,33 @@ private static Collection<URL> resources(String name) {
203200
"Failed to read '%s' from classpath", name), e);
204201
}
205202
}
203+
204+
private record ConfigSourceWrapper(ConfigSource delegate) implements ConfigSource {
205+
@Override
206+
public Set<String> getPropertyNames() {
207+
return delegate.getPropertyNames();
208+
}
209+
210+
@Override
211+
public String getValue(String propertyName) {
212+
return delegate.getValue(propertyName);
213+
}
214+
215+
@Override
216+
public String getName() {
217+
return delegate.getName();
218+
}
219+
220+
@Override
221+
public Map<String, String> getProperties() {
222+
return delegate.getProperties();
223+
}
224+
225+
@Override
226+
public int getOrdinal() {
227+
boolean isDefault = isDefaultMethod(delegate, "getOrdinal");
228+
boolean isDefaultOrdinal = delegate.getOrdinal() == ConfigSource.DEFAULT_ORDINAL;
229+
return isDefault && isDefaultOrdinal ? 800 : delegate.getOrdinal();
230+
}
231+
}
206232
}

microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/ReflectionHelper.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,29 @@ static List<Method> methodHierarchy(Method method) {
5959
*
6060
* @param method base method
6161
* @param override override method
62-
* @return {@code true} if overrides, {@code false otherwise}
62+
* @return {@code true} if overrides, {@code false} otherwise
6363
*/
6464
static boolean isOverride(Method method, Method override) {
6565
return override.getName().equals(method.getName())
6666
&& override.getReturnType().isAssignableFrom(method.getReturnType())
6767
&& Arrays.equals(override.getParameterTypes(), method.getParameterTypes());
6868
}
6969

70+
/**
71+
* Test if the given instance has a default method by name. If the method does not exist, return {@code false}.
72+
*
73+
* @param instance instance
74+
* @param name method name
75+
* @return {@code true} if is default, {@code false} otherwise
76+
*/
77+
static boolean isDefaultMethod(Object instance, String name) {
78+
try {
79+
return instance.getClass().getMethod(name).isDefault();
80+
} catch (NoSuchMethodException e) {
81+
return false;
82+
}
83+
}
84+
7085
/**
7186
* Collect all types in the type hiearchy of the given type.
7287
*

microprofile/tests/testing/junit5/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@
8585
<java.util.logging.config.file>
8686
${project.build.testOutputDirectory}/logging.properties
8787
</java.util.logging.config.file>
88+
<foo>systemProperty</foo>
8889
</systemPropertyVariables>
90+
<environmentVariables>
91+
<foo>environmentProperty</foo>
92+
</environmentVariables>
8993
<redirectTestOutputToFile>${redirectTestOutputToFile}</redirectTestOutputToFile>
9094
</configuration>
9195
</plugin>

microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockProperties.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import jakarta.inject.Inject;
2020

21-
import io.helidon.microprofile.testing.Configuration;
2221
import io.helidon.microprofile.testing.junit5.AddConfigBlock;
2322
import io.helidon.microprofile.testing.junit5.HelidonTest;
2423

@@ -33,7 +32,6 @@
3332
some.key1=some.value1
3433
some.key2=some.value2
3534
""")
36-
@Configuration(configSources = "configBlock.properties")
3735
class TestAddConfigBlockProperties {
3836

3937
@Inject

microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockYaml.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import jakarta.inject.Inject;
2020

21-
import io.helidon.microprofile.testing.Configuration;
2221
import io.helidon.microprofile.testing.junit5.AddConfigBlock;
2322
import io.helidon.microprofile.testing.junit5.HelidonTest;
2423

@@ -35,7 +34,6 @@
3534
another2:
3635
key: "another2.value"
3736
""")
38-
@Configuration(configSources = "configBlock.yaml")
3937
class TestAddConfigBlockYaml {
4038

4139
@Inject
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2025 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+
package io.helidon.microprofile.tests.testing.junit5;
17+
18+
import java.util.Iterator;
19+
import java.util.List;
20+
import java.util.Set;
21+
22+
import io.helidon.microprofile.testing.AddConfig;
23+
import io.helidon.microprofile.testing.AddConfigBlock;
24+
import io.helidon.microprofile.testing.AddConfigSource;
25+
import io.helidon.microprofile.testing.Configuration;
26+
import io.helidon.microprofile.testing.junit5.HelidonTest;
27+
28+
import jakarta.inject.Inject;
29+
import org.eclipse.microprofile.config.Config;
30+
import org.eclipse.microprofile.config.spi.ConfigSource;
31+
import org.junit.jupiter.api.Test;
32+
33+
import static org.hamcrest.MatcherAssert.assertThat;
34+
import static org.hamcrest.Matchers.is;
35+
import static org.hamcrest.Matchers.iterableWithSize;
36+
37+
@HelidonTest
38+
@AddConfig(key = "foo", value = "addConfig")
39+
@AddConfig(key = "config_ordinal", value = "999")
40+
@AddConfigBlock(value = """
41+
foo=configBlock
42+
config_ordinal=998
43+
""")
44+
@Configuration(configSources = "ordinal-custom.properties")
45+
public class TestConfigSourceOrderingCustom {
46+
47+
private final List<Ordering> ORDERINGS = List.of(
48+
new Ordering(999, "addConfig"),
49+
new Ordering(998, "configBlock"),
50+
new Ordering(997, "configSource"),
51+
new Ordering(996, "configuration"),
52+
new Ordering(400, "systemProperty"),
53+
new Ordering(300, "environmentProperty"));
54+
55+
@Inject
56+
@SuppressWarnings("CdiInjectionPointsInspection")
57+
private Config config;
58+
59+
@AddConfigSource
60+
static ConfigSource config() {
61+
return new CustomConfigSource();
62+
}
63+
64+
@Test
65+
void testOrdering() {
66+
Iterator<Ordering> ordering = ORDERINGS.iterator();
67+
Iterable<ConfigSource> configSources = config.getConfigSources();
68+
69+
assertThat(configSources, iterableWithSize(6));
70+
71+
for (ConfigSource configSource : configSources) {
72+
Ordering it = ordering.next();
73+
assertThat(it.ordinal(), is(configSource.getOrdinal()));
74+
assertThat(it.value(), is(configSource.getValue("foo")));
75+
}
76+
}
77+
78+
static class CustomConfigSource implements ConfigSource {
79+
@Override
80+
public Set<String> getPropertyNames() {
81+
return Set.of();
82+
}
83+
84+
@Override
85+
public String getValue(String propertyName) {
86+
return "foo".equals(propertyName) ? "configSource" : null;
87+
}
88+
89+
@Override
90+
public String getName() {
91+
return CustomConfigSource.class.getSimpleName();
92+
}
93+
94+
@Override
95+
public int getOrdinal() {
96+
return 997;
97+
}
98+
}
99+
100+
record Ordering(int ordinal, String value) {
101+
}
102+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2025 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+
package io.helidon.microprofile.tests.testing.junit5;
17+
18+
import java.util.Iterator;
19+
import java.util.List;
20+
import java.util.Set;
21+
22+
import io.helidon.microprofile.testing.AddConfig;
23+
import io.helidon.microprofile.testing.AddConfigBlock;
24+
import io.helidon.microprofile.testing.AddConfigSource;
25+
import io.helidon.microprofile.testing.Configuration;
26+
import io.helidon.microprofile.testing.junit5.HelidonTest;
27+
28+
import jakarta.inject.Inject;
29+
import org.eclipse.microprofile.config.Config;
30+
import org.eclipse.microprofile.config.spi.ConfigSource;
31+
import org.junit.jupiter.api.Test;
32+
33+
import static org.hamcrest.MatcherAssert.assertThat;
34+
import static org.hamcrest.Matchers.is;
35+
import static org.hamcrest.Matchers.iterableWithSize;
36+
37+
@HelidonTest
38+
@AddConfig(key = "foo", value = "addConfig")
39+
@AddConfigBlock(value = """
40+
foo=configBlock
41+
""")
42+
@Configuration(configSources = "ordinal-default.properties")
43+
public class TestConfigSourceOrderingDefault {
44+
45+
private static final List<Ordering> ORDERINGS = List.of(
46+
new Ordering(1000, "addConfig"),
47+
new Ordering(900, "configBlock"),
48+
new Ordering(800, "configSource"),
49+
new Ordering(700, "configuration"),
50+
new Ordering(400, "systemProperty"),
51+
new Ordering(300, "environmentProperty"));
52+
53+
@Inject
54+
@SuppressWarnings("CdiInjectionPointsInspection")
55+
private Config config;
56+
57+
@AddConfigSource
58+
static ConfigSource config() {
59+
return new CustomConfigSource();
60+
}
61+
62+
@Test
63+
void testConfigSourceOrdering() {
64+
Iterator<Ordering> ordering = ORDERINGS.iterator();
65+
Iterable<ConfigSource> configSources = config.getConfigSources();
66+
67+
assertThat(configSources, iterableWithSize(6));
68+
69+
for (ConfigSource configSource : configSources) {
70+
Ordering it = ordering.next();
71+
assertThat(it.ordinal(), is(configSource.getOrdinal()));
72+
assertThat(it.value(), is(configSource.getValue("foo")));
73+
}
74+
}
75+
76+
static class CustomConfigSource implements ConfigSource {
77+
@Override
78+
public Set<String> getPropertyNames() {
79+
return Set.of();
80+
}
81+
82+
@Override
83+
public String getValue(String propertyName) {
84+
return "foo".equals(propertyName) ? "configSource" : null;
85+
}
86+
87+
@Override
88+
public String getName() {
89+
return CustomConfigSource.class.getSimpleName();
90+
}
91+
}
92+
93+
record Ordering(int ordinal, String value) {
94+
}
95+
}

microprofile/tests/testing/junit5/src/test/resources/testConfigSources.properties renamed to microprofile/tests/testing/junit5/src/test/resources/ordinal-custom.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
# limitations under the License.
1515
#
1616

17-
some.key=some.value
17+
foo=configuration
18+
config_ordinal=996

microprofile/tests/testing/junit5/src/test/resources/configBlock.properties renamed to microprofile/tests/testing/junit5/src/test/resources/ordinal-default.properties

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@
1414
# limitations under the License.
1515
#
1616

17-
some.key1=foo
18-
some.key2=bar
17+
foo=configuration

microprofile/tests/testing/junit5/src/test/resources/testConfigSources.yaml

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)