Skip to content

Commit 2a0c12a

Browse files
committed
Make MP test configuration ordinal deterministic
1 parent 5328f3a commit 2a0c12a

File tree

10 files changed

+134
-20
lines changed

10 files changed

+134
-20
lines changed

docs/src/main/asciidoc/mp/testing/testing-common.adoc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,32 @@ include::{sourcedir}/mp/testing/TestingSnippets.java[tag=snippet_11, indent=0]
203203
The ordering of the test configuration can be controlled using the mechanism defined by the
204204
link:{microprofile-config-spec-url}#_configsource_ordering[MicroProfile Config specification].
205205
206-
NOTE: The configuration expressed with link:{mp-testing-javadoc-url}/AddConfig.html[`@AddConfig`] has a fixed ordinal value
207-
of `1000`
208-
209206
[source,java]
210207
.Add a properties text block with ordinal
211208
----
212209
include::{sourcedir}/mp/testing/TestingSnippets.java[tag=snippet_12, indent=0]
213210
----
214211
212+
The default ordering is the following
213+
214+
[cols="1,3"]
215+
|===
216+
|Annotation |Ordinal
217+
218+
|link:{mp-testing-javadoc-url}/AddConfig.html[`@AddConfig`]
219+
|1000
220+
221+
|link:{mp-testing-javadoc-url}/AddConfigBlock.html[`@AddConfigBlock`]
222+
|900
223+
224+
|link:{mp-testing-javadoc-url}/AddConfigSource.html[`@AddConfigSource`]
225+
|800
226+
227+
|link:{mp-testing-javadoc-url}/Configuration.html[`@Configuration`]
228+
|700
229+
230+
|===
231+
215232
=== Injectable Types
216233
217234
Helidon provides injection support for types that reflect the current server. E.g. JAXRS client.

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class HelidonTestConfigSynthetic extends HelidonTestConfigDelegate {
6161
HelidonTestConfigSynthetic(HelidonTestInfo<?> testInfo, Runnable onUpdate) {
6262
this.testInfo = testInfo;
6363
this.onUpdate = onUpdate;
64-
map.put(ConfigSource.CONFIG_ORDINAL, "1000");
6564
map.put("server.port", "0");
6665
map.put("mp.config.profile", "test");
6766
testInfo.addConfigs().forEach(this::update);
@@ -148,20 +147,24 @@ boolean useExisting() {
148147

149148
private Config buildConfig() {
150149
List<ConfigSource> configSources = new ArrayList<>();
151-
configSources.add(MpConfigSources.create(testInfo.id(), map));
150+
ConfigSource configSource = MpConfigSources.create(testInfo.id(), map);
151+
configSources.add(addConfigOrdinal(configSource, "1000"));
152152
blocks.forEach((type, values) -> {
153153
for (String value : values) {
154-
configSources.add(MpConfigSources.create(type, new StringReader(value)));
154+
ConfigSource config = MpConfigSources.create(type, new StringReader(value));
155+
configSources.add(addConfigOrdinal(config, type, "900"));
155156
}
156157
});
157158
for (Method m : methods) {
158-
configSources.add(invoke(ConfigSource.class, requireStatic(m), null));
159+
ConfigSource config = invoke(ConfigSource.class, requireStatic(m), null);
160+
configSources.add(addConfigOrdinal(config, "800"));
159161
}
160162
for (String source : resources) {
161163
String filename = source.trim();
162164
for (URL url : resources(filename)) {
163165
String type = extension(filename);
164-
configSources.add(MpConfigSources.create(type, url));
166+
ConfigSource config = MpConfigSources.create(type, url);
167+
configSources.add(addConfigOrdinal(config, type, "700"));
165168
}
166169
}
167170
ConfigBuilder builder = ConfigProviderResolver.instance()
@@ -173,6 +176,16 @@ private Config buildConfig() {
173176
return builder.build();
174177
}
175178

179+
private ConfigSource addConfigOrdinal(ConfigSource config, String ordinal) {
180+
return addConfigOrdinal(config, "Map", ordinal);
181+
}
182+
183+
private ConfigSource addConfigOrdinal(ConfigSource config, String type, String ordinal) {
184+
Map<String, String> properties = new HashMap<>(config.getProperties());
185+
properties.putIfAbsent(ConfigSource.CONFIG_ORDINAL, ordinal);
186+
return MpConfigSources.create(type, properties);
187+
}
188+
176189
private static String extension(String filename) {
177190
int idx = filename.lastIndexOf('.');
178191
return idx > -1 ? filename.substring(idx + 1) : "properties";

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2025 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.
@@ -16,22 +16,24 @@
1616

1717
package io.helidon.microprofile.tests.testing.junit5;
1818

19-
import static org.hamcrest.CoreMatchers.is;
20-
import static org.hamcrest.MatcherAssert.assertThat;
21-
2219
import jakarta.inject.Inject;
2320

21+
import io.helidon.microprofile.testing.Configuration;
2422
import io.helidon.microprofile.testing.junit5.AddConfigBlock;
2523
import io.helidon.microprofile.testing.junit5.HelidonTest;
2624

2725
import org.eclipse.microprofile.config.inject.ConfigProperty;
2826
import org.junit.jupiter.api.Test;
2927

28+
import static org.hamcrest.CoreMatchers.is;
29+
import static org.hamcrest.MatcherAssert.assertThat;
30+
3031
@HelidonTest
3132
@AddConfigBlock("""
3233
some.key1=some.value1
3334
some.key2=some.value2
34-
""")
35+
""")
36+
@Configuration(configSources = "configBlock.properties")
3537
class TestAddConfigBlockProperties {
3638

3739
@Inject

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024 Oracle and/or its affiliates.
2+
* Copyright (c) 2024, 2025 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.
@@ -16,24 +16,26 @@
1616

1717
package io.helidon.microprofile.tests.testing.junit5;
1818

19-
import static org.hamcrest.CoreMatchers.is;
20-
import static org.hamcrest.MatcherAssert.assertThat;
21-
2219
import jakarta.inject.Inject;
2320

21+
import io.helidon.microprofile.testing.Configuration;
2422
import io.helidon.microprofile.testing.junit5.AddConfigBlock;
2523
import io.helidon.microprofile.testing.junit5.HelidonTest;
2624

2725
import org.eclipse.microprofile.config.inject.ConfigProperty;
2826
import org.junit.jupiter.api.Test;
2927

28+
import static org.hamcrest.CoreMatchers.is;
29+
import static org.hamcrest.MatcherAssert.assertThat;
30+
3031
@HelidonTest
3132
@AddConfigBlock(type = "yaml", value = """
3233
another1:
3334
key: "another1.value"
3435
another2:
3536
key: "another2.value"
36-
""")
37+
""")
38+
@Configuration(configSources = "configBlock.yaml")
3739
class TestAddConfigBlockYaml {
3840

3941
@Inject
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
17+
some.key1=foo
18+
some.key2=bar
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
17+
another1:
18+
key: "foo"
19+
another2:
20+
key: "bar"

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import jakarta.inject.Inject;
2323

24+
import io.helidon.microprofile.testing.Configuration;
2425
import io.helidon.microprofile.testing.testng.AddConfigBlock;
2526
import io.helidon.microprofile.testing.testng.HelidonTest;
2627

@@ -31,7 +32,8 @@
3132
@AddConfigBlock("""
3233
some.key1=some.value1
3334
some.key2=some.value2
34-
""")
35+
""")
36+
@Configuration(configSources = "configBlock.properties")
3537
public class TestAddConfigBlockProperties {
3638

3739
@Inject

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import jakarta.inject.Inject;
2323

24+
import io.helidon.microprofile.testing.Configuration;
2425
import io.helidon.microprofile.testing.testng.AddConfigBlock;
2526
import io.helidon.microprofile.testing.testng.HelidonTest;
2627

@@ -33,7 +34,8 @@
3334
key: "another1.value"
3435
another2:
3536
key: "another2.value"
36-
""")
37+
""")
38+
@Configuration(configSources = "configBlock.yaml")
3739
public class TestAddConfigBlockYaml {
3840

3941
@Inject
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
17+
some.key1=prod.value
18+
some.key2=prod.value
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
17+
another1:
18+
key: "prod.value"
19+
another2:
20+
key: "prod.value"

0 commit comments

Comments
 (0)