Skip to content
Open
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
3 changes: 2 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,7 @@ public interface Converter<T, E extends Exception> {
/**
* Converts to a 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 -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH).parse(s);

/**
* Applies the conversion function to the String argument.
Expand Down
14 changes: 13 additions & 1 deletion src/test/java/org/apache/commons/cli/ConverterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ 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;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junitpioneer.jupiter.DefaultLocale;

/**
* Tests for standard Converters.
Expand Down Expand Up @@ -84,13 +86,23 @@ void testDate() throws Exception {
* time zone.
*/
final Date expected = new Date(1023400137000L);
final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
final String formatted = dateFormat.format(expected);
assertEquals(expected, Converter.DATE.apply(formatted));

assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("Jun 06 17:48:57 EDT 2002"));
}

@Test
@DefaultLocale(language = "de", country = "DE")
void testDateLocaleIndependent() throws Exception {
// Date.toString() always emits English month/day names, so the converter must parse
// them even when the JVM 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 @@ -32,6 +32,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -118,7 +119,7 @@ void testSimplePattern() throws Exception {
*/
final Options options = PatternOptionBuilder.parsePattern("a:b@cde>f+n%t/m*z#");
final Date expectedDate = new Date(1023400137000L);
final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
final String[] args = {"-c", "-a", "foo", "-b", "java.util.Vector", "-e", "build.xml", "-f", "java.util.Calendar", "-n", "4.5", "-t",
"https://commons.apache.org", "-z", dateFormat.format(expectedDate), "-m", "test*"};
final CommandLineParser parser = new PosixParser();
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/apache/commons/cli/TypeHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,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.Map;
import java.util.stream.Stream;

Expand Down Expand Up @@ -93,7 +94,7 @@ private static Stream<Arguments> createValueTestParameters() throws MalformedURL
* problem, convert the time into a string and then unparse that using the converter. This produces strings that always match the correct time zone.
*/
final Date date = new Date(1023400137000L);
final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);

list.add(Arguments.of(Instantiable.class.getName(), PatternOptionBuilder.CLASS_VALUE, Instantiable.class));
list.add(Arguments.of("what ever", PatternOptionBuilder.CLASS_VALUE, ParseException.class));
Expand Down