|
17 | 17 | package org.springframework.aot.test.boot;
|
18 | 18 |
|
19 | 19 | import java.util.Collections;
|
20 |
| -import java.util.List; |
21 | 20 | import java.util.Map;
|
22 |
| -import java.util.Map.Entry; |
23 |
| -import java.util.Set; |
24 | 21 | import java.util.function.Consumer;
|
25 | 22 | import java.util.function.Supplier;
|
26 |
| -import java.util.stream.Collectors; |
27 | 23 | import java.util.stream.Stream;
|
28 | 24 |
|
29 | 25 | import org.junit.jupiter.api.Test;
|
@@ -95,36 +91,40 @@ void loadContextSetActiveProfiles() {
|
95 | 91 | assertThat(context.getEnvironment().getActiveProfiles()).containsOnly("profile1", "profile2"));
|
96 | 92 | }
|
97 | 93 |
|
| 94 | + static Stream<Arguments> applicationArguments() { |
| 95 | + return Stream.of( |
| 96 | + Arguments.of(new String[] {}, Collections.emptyMap()), |
| 97 | + Arguments.of(new String[] { "--app.test=one" }, Map.of("app.test", "one")), |
| 98 | + Arguments.of(new String[] { "--app.test=one", "--app.name=foo" }, Map.of("app.test", "one", "app.name", "foo")) |
| 99 | + ); |
| 100 | + } |
| 101 | + |
98 | 102 | @ParameterizedTest
|
99 | 103 | @MethodSource("applicationArguments")
|
100 |
| - void loadContextUsesArguments(String[] arguments, List<Entry<String, String>> expectedEntries) { |
| 104 | + void loadContextUsesArguments(String[] arguments, Map<String, String> expectedEntries) { |
101 | 105 | AotSpringBootConfigContextLoader loader = new AotSpringBootConfigContextLoader(TestApplicationContextInitializer.class, arguments);
|
102 |
| - run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)), (context) -> { |
103 |
| - ApplicationArguments args = context.getBean(ApplicationArguments.class); |
104 |
| - Set<String> keys = expectedEntries.stream().map(Entry::getKey).collect(Collectors.toSet()); |
105 |
| - assertThat(args.getOptionNames()).containsExactlyElementsOf(keys); |
106 |
| - for (Entry<String, String> expectedEntry : expectedEntries) { |
107 |
| - String key = expectedEntry.getKey(); |
108 |
| - String value = expectedEntry.getValue(); |
109 |
| - assertThat(args.getOptionValues(key)).containsOnly(value); |
110 |
| - } |
111 |
| - }); |
| 106 | + run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)), |
| 107 | + validateApplicationArguments(expectedEntries)); |
112 | 108 | }
|
113 | 109 |
|
114 | 110 | @ParameterizedTest
|
115 | 111 | @MethodSource("applicationArguments")
|
116 |
| - void loadContextUsesArgumentsAndWebSettings(String[] arguments, List<Entry<String, String>> expectedEntries) { |
117 |
| - AotSpringBootConfigContextLoader loader = new AotSpringBootConfigContextLoader(TestApplicationContextInitializer.class, WebApplicationType.SERVLET, WebEnvironment.MOCK, arguments); |
118 |
| - run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)), (context) -> { |
| 112 | + void loadContextUsesArgumentsAndWebSettings(String[] arguments, Map<String, String> expectedEntries) { |
| 113 | + AotSpringBootConfigContextLoader loader = new AotSpringBootConfigContextLoader( |
| 114 | + TestApplicationContextInitializer.class, WebApplicationType.SERVLET, WebEnvironment.MOCK, arguments); |
| 115 | + run(() -> loader.loadContext(createMergedContextConfiguration(SampleTest.class)), |
| 116 | + validateApplicationArguments(expectedEntries)); |
| 117 | + } |
| 118 | + |
| 119 | + private Consumer<AssertableApplicationContext> validateApplicationArguments( |
| 120 | + Map<String, String> expectedEntries) { |
| 121 | + return (context) -> { |
119 | 122 | ApplicationArguments args = context.getBean(ApplicationArguments.class);
|
120 |
| - Set<String> keys = expectedEntries.stream().map(Entry::getKey).collect(Collectors.toSet()); |
121 |
| - assertThat(args.getOptionNames()).containsExactlyElementsOf(keys); |
122 |
| - for (Entry<String, String> expectedEntry : expectedEntries) { |
123 |
| - String key = expectedEntry.getKey(); |
124 |
| - String value = expectedEntry.getValue(); |
125 |
| - assertThat(args.getOptionValues(key)).containsOnly(value); |
| 123 | + assertThat(args.getOptionNames()).containsExactlyInAnyOrderElementsOf(expectedEntries.keySet()); |
| 124 | + for (String optionName : args.getOptionNames()) { |
| 125 | + assertThat(args.getOptionValues(optionName)).containsOnly(expectedEntries.get(optionName)); |
126 | 126 | }
|
127 |
| - }); |
| 127 | + }; |
128 | 128 | }
|
129 | 129 |
|
130 | 130 | private void run(Supplier<ConfigurableApplicationContext> supplier, Consumer<AssertableApplicationContext> context) {
|
@@ -168,12 +168,4 @@ public void initialize(GenericApplicationContext applicationContext) {
|
168 | 168 |
|
169 | 169 | }
|
170 | 170 |
|
171 |
| - static Stream<Arguments> applicationArguments() { |
172 |
| - return Stream.of( |
173 |
| - Arguments.of(new String[] {}, Collections.emptyList()), |
174 |
| - Arguments.of(new String[] { "--app.test=one"}, List.of(Map.entry("app.test", "one"))), |
175 |
| - Arguments.of(new String[] { "--app.test=one", "--app.name=foo" }, List.of(Map.entry("app.test", "one"), Map.entry("app.name", "foo"))) |
176 |
| - ); |
177 |
| - } |
178 |
| - |
179 | 171 | }
|
0 commit comments