Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<action type="fix" dev="ggregory" due-to="Gary Gregory">Fix malformed Javadoc comments.</action>
<action type="fix" dev="ggregory" due-to="Dan Ziemba, Ken Dombeck, Gary Gregory" issue="CLI-352">Correct HelpFormatter Javadoc #418.</action>
<action type="fix" dev="ggregory" due-to="Elric, Gary Gregory">Fix broken Javadoc links, replace deprecated methods in usage examples, and add a missing method to HelpFormatter (#424).</action>
<action type="fix" dev="ggregory" due-to="Dexter.k, Gary Gregory">Use Locale.ENGLISH when parsing dates in Converter.DATE (#426).</action>
<!-- ADD -->
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Gary Gregory, Dependabot">Bump org.apache.commons:commons-parent from 91 to 102 #414, #416.</action>
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/org/apache/commons/cli/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
* The definition of the functional interface to call when doing a conversion. Like {@code Function<String,T>} but can throw an Exception.
Expand Down Expand Up @@ -76,7 +77,16 @@ public interface Converter<T, E extends Exception> {
/**
* Converts a String to a {@link Date} using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy".
*/
Converter<Date, java.text.ParseException> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
Converter<Date, java.text.ParseException> DATE = s -> {
final String pattern = "EEE MMM dd HH:mm:ss zzz yyyy";
try {
return new SimpleDateFormat(pattern).parse(s);
} catch (final java.text.ParseException e) {
// Date.toString() always emits English month/day names, so fall back to Locale.ENGLISH
// when the default locale rejects the documented format.
return new SimpleDateFormat(pattern, Locale.ENGLISH).parse(s);
}
};

/**
* Applies the conversion function to the String argument.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void printHelp(final String cmdLineSyntax, final Options options) throws
*/
public void printHelp(final String cmdLineSyntax, final String header, final Iterable<Option> options, final String footer, final boolean autoUsage)
throws IOException {
Options optionsObject = new Options();
final Options optionsObject = new Options();
options.forEach(optionsObject::addOption);
printHelp(cmdLineSyntax, header, optionsObject, footer, autoUsage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public TableDefinition getTableDefinition(final Iterable<Option> options) {
rows.add(row);
});
// return the TableDefinition with the proper column headers.
List<String> headers = new ArrayList<>(3);
final List<String> headers = new ArrayList<>(3);
headers.add(LABEL_OPTIONS);
if (showSince) {
headers.add(LABEL_SINCE);
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/apache/commons/cli/ConverterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -93,6 +94,16 @@ void testDateLocaleDe() throws Exception {
assertEquals(expected, Converter.DATE.apply(formatted));
}

@Test
@DefaultLocale(language = "de", country = "DE")
void testDateLocaleDeEnglishInput() throws Exception {
// Date.toString() always emits English month/day names, so the converter must still parse
// them when the default locale is not English.
final Date expected = new Date(1023400137000L);
final String formatted = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).format(expected);
assertEquals(expected, Converter.DATE.apply(formatted));
}

@Test
void testFile() throws Exception {
final URL url = this.getClass().getClassLoader().getResource("./org/apache/commons/cli/existing-readable.file");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ void testAppendTableTest() throws IOException {
final String[] headers = { "one", "two", "three" };
// @formatter:off
final List<List<String>> rows = Arrays.asList(
Arrays.asList(new String[]{"uno", "dos", "tres"}),
Arrays.asList(new String[]{"aon", "dhá", "trí"}),
Arrays.asList(new String[]{"واحد", "اثنين", "ثلاثة"})
Arrays.asList("uno", "dos", "tres"),
Arrays.asList("aon", "dhá", "trí"),
Arrays.asList("واحد", "اثنين", "ثلاثة")
);
// @formatter:on
List<String> expected = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ void testAppendTableTest() throws IOException {
final String[] headers = { "one", "two", "three" };
// @formatter:off
final List<List<String>> rows = Arrays.asList(
Arrays.asList(new String[]{"uno", "dos", "tres"}),
Arrays.asList(new String[]{"aon", "dhá", "trí"}),
Arrays.asList(new String[]{"واحد", "اثنين", "ثلاثة"})
Arrays.asList("uno", "dos", "tres"),
Arrays.asList("aon", "dhá", "trí"),
Arrays.asList("واحد", "اثنين", "ثلاثة")
);
// @formatter:on
List<String> expected = new ArrayList<>();
Expand Down