Skip to content

Commit 113c101

Browse files
committed
Merge branch '6.2.x'
2 parents ef05b82 + 886ca7f commit 113c101

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
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.
@@ -26,8 +26,7 @@
2626
/**
2727
* Abstract base class for {@link PropertySource} implementations backed by command line
2828
* arguments. The parameterized type {@code T} represents the underlying source of command
29-
* line options. For instance, {@link SimpleCommandLinePropertySource} uses a String
30-
* array.
29+
* line options.
3130
*
3231
* <h3>Purpose and General Usage</h3>
3332
*
@@ -260,10 +259,11 @@ public final boolean containsProperty(String name) {
260259
* This implementation first checks to see if the name specified is the special
261260
* {@linkplain #setNonOptionArgsPropertyName(String) "non-option arguments" property},
262261
* and if so delegates to the abstract {@link #getNonOptionArgs()} method. If so
263-
* and the collection of non-option arguments is empty, this method returns {@code
264-
* null}. If not empty, it returns a comma-separated String of all non-option
265-
* arguments. Otherwise, delegates to and returns the result of the abstract {@link
266-
* #getOptionValues(String)} method.
262+
* and the collection of non-option arguments is empty, this method returns
263+
* {@code null}. If not empty, it returns a comma-separated String of all non-option
264+
* arguments. Otherwise, this method delegates to and returns a comma-separated String
265+
* of the results of the abstract {@link #getOptionValues(String)} method or
266+
* {@code null} if there are no such option values.
267267
*/
268268
@Override
269269
public final @Nullable String getProperty(String name) {

spring-core/src/main/java/org/springframework/core/env/SimpleCommandLineArgsParser.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
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.
@@ -28,7 +28,9 @@
2828
* <p>That is, options must be prefixed with "{@code --}" and may or may not
2929
* specify a value. If a value is specified, the name and value must be separated
3030
* <em>without spaces</em> by an equals sign ("="). The value may optionally be
31-
* an empty string.
31+
* an empty string. If an option is present multiple times with different values
32+
* &mdash; for example, {@code --foo=bar --foo=baz} &mdash; all supplied values
33+
* will be stored for the option.
3234
*
3335
* <h4>Valid examples of option arguments</h4>
3436
* <pre class="code">
@@ -37,14 +39,14 @@
3739
* --foo=""
3840
* --foo=bar
3941
* --foo="bar then baz"
40-
* --foo=bar,baz,biz</pre>
42+
* --foo=bar,baz,biz
43+
* --foo=bar --foo=baz --foo=biz</pre>
4144
*
4245
* <h4>Invalid examples of option arguments</h4>
4346
* <pre class="code">
4447
* -foo
4548
* --foo bar
46-
* --foo = bar
47-
* --foo=bar --foo=baz --foo=biz</pre>
49+
* --foo = bar</pre>
4850
*
4951
* <h3>End of option arguments</h3>
5052
* <p>This parser supports the POSIX "end of options" delimiter, meaning that any

spring-core/src/main/java/org/springframework/core/env/SimpleCommandLinePropertySource.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
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.
@@ -23,7 +23,8 @@
2323
import org.springframework.util.StringUtils;
2424

2525
/**
26-
* {@link CommandLinePropertySource} implementation backed by a simple String array.
26+
* {@link CommandLinePropertySource} implementation backed by an instance of
27+
* {@link CommandLineArgs}.
2728
*
2829
* <h3>Purpose</h3>
2930
* <p>This {@code CommandLinePropertySource} implementation aims to provide the simplest
@@ -41,7 +42,9 @@
4142
* <p>That is, options must be prefixed with "{@code --}" and may or may not
4243
* specify a value. If a value is specified, the name and value must be separated
4344
* <em>without spaces</em> by an equals sign ("="). The value may optionally be
44-
* an empty string.
45+
* an empty string. If an option is present multiple times with different values
46+
* &mdash; for example, {@code --foo=bar --foo=baz} &mdash; all supplied values
47+
* will be stored for the option.
4548
*
4649
* <h4>Valid examples of option arguments</h4>
4750
* <pre class="code">
@@ -50,14 +53,14 @@
5053
* --foo=""
5154
* --foo=bar
5255
* --foo="bar then baz"
53-
* --foo=bar,baz,biz</pre>
56+
* --foo=bar,baz,biz
57+
* --foo=bar --foo=baz --foo=biz</pre>
5458
*
5559
* <h4>Invalid examples of option arguments</h4>
5660
* <pre class="code">
5761
* -foo
5862
* --foo bar
59-
* --foo = bar
60-
* --foo=bar --foo=baz --foo=biz</pre>
63+
* --foo = bar</pre>
6164
*
6265
* <h3>End of option arguments</h3>
6366
* <p>The underlying parser supports the POSIX "end of options" delimiter, meaning

spring-core/src/test/java/org/springframework/core/env/SimpleCommandLineArgsParserTests.java

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
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.
@@ -46,7 +46,7 @@ void withNoOptions() {
4646
void withSingleOptionAndNoValue() {
4747
CommandLineArgs args = parser.parse("--o1");
4848
assertThat(args.containsOption("o1")).isTrue();
49-
assertThat(args.getOptionValues("o1")).isEqualTo(Collections.EMPTY_LIST);
49+
assertThat(args.getOptionValues("o1")).isEmpty();
5050
}
5151

5252
@Test
@@ -56,6 +56,20 @@ void withSingleOptionAndValue() {
5656
assertThat(args.getOptionValues("o1")).containsExactly("v1");
5757
}
5858

59+
@Test
60+
void withRepeatedOptionAndSameValues() {
61+
CommandLineArgs args = parser.parse("--o1=v1", "--o1=v1", "--o1=v1");
62+
assertThat(args.containsOption("o1")).isTrue();
63+
assertThat(args.getOptionValues("o1")).containsExactly("v1", "v1", "v1");
64+
}
65+
66+
@Test
67+
void withRepeatedOptionAndDifferentValues() {
68+
CommandLineArgs args = parser.parse("--o1=v1", "--o1=v2", "--o1=v3");
69+
assertThat(args.containsOption("o1")).isTrue();
70+
assertThat(args.getOptionValues("o1")).containsExactly("v1", "v2", "v3");
71+
}
72+
5973
@Test
6074
void withMixOfOptionsHavingValueAndOptionsHavingNoValue() {
6175
CommandLineArgs args = parser.parse("--o1=v1", "--o2");
@@ -95,17 +109,17 @@ void withNonOptionArguments() {
95109
}
96110

97111
@Test
98-
void assertOptionNamesIsUnmodifiable() {
112+
void optionNamesSetIsUnmodifiable() {
99113
CommandLineArgs args = new SimpleCommandLineArgsParser().parse();
100-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
101-
args.getOptionNames().add("bogus"));
114+
assertThatExceptionOfType(UnsupportedOperationException.class)
115+
.isThrownBy(() -> args.getOptionNames().add("bogus"));
102116
}
103117

104118
@Test
105-
void assertNonOptionArgsIsUnmodifiable() {
119+
void nonOptionArgsListIsUnmodifiable() {
106120
CommandLineArgs args = new SimpleCommandLineArgsParser().parse();
107-
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(() ->
108-
args.getNonOptionArgs().add("foo"));
121+
assertThatExceptionOfType(UnsupportedOperationException.class)
122+
.isThrownBy(() -> args.getNonOptionArgs().add("foo"));
109123
}
110124

111125
@Test

spring-core/src/test/java/org/springframework/core/env/SimpleCommandLinePropertySourceTests.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
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.
@@ -61,6 +61,15 @@ void withOptionArgsOnly() {
6161
assertThat(ps.getProperty("o3")).isNull();
6262
}
6363

64+
@Test // gh-34282
65+
void withRepeatedOptionArgs() {
66+
CommandLinePropertySource<?> ps = new SimpleCommandLinePropertySource("--o1=v1", "--o1=v2", "--o1=v3");
67+
assertThat(ps.containsProperty("o1")).isTrue();
68+
assertThat(ps.containsProperty("o2")).isFalse();
69+
assertThat(ps.getProperty("o1")).isEqualTo("v1,v2,v3");
70+
assertThat(ps.getProperty("o2")).isNull();
71+
}
72+
6473
@Test // gh-24464
6574
void withOptionalArg_andArgIsEmpty() {
6675
EnumerablePropertySource<?> ps = new SimpleCommandLinePropertySource("--foo=");

0 commit comments

Comments
 (0)