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
14 changes: 12 additions & 2 deletions src/test/java/org/apache/commons/cli/ConverterTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -75,8 +77,16 @@ public void classTests() throws Exception {
public void dateTests() throws Exception {
assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("whatever"));

final Date d = new Date(1023400137000L);
assertEquals(d, Converter.DATE.apply("Thu Jun 06 17:48:57 EDT 2002"));
/*
* Dates calculated from strings are dependent upon configuration and environment settings for the
* machine on which the test is running. To avoid this 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 expected = new Date(1023400137000L);
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
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"));
}
Expand Down
14 changes: 12 additions & 2 deletions src/test/java/org/apache/commons/cli/PatternOptionBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
Expand Down Expand Up @@ -125,9 +127,17 @@ public void testRequiredOption() throws Exception {

@Test
public void testSimplePattern() throws Exception {
/*
* Dates calculated from strings are dependent upon configuration and environment settings for the
* machine on which the test is running. To avoid this 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 Options options = PatternOptionBuilder.parsePattern("a:b@cde>f+n%t/m*z#");
final Date expectedDate = new Date(1023400137000L);
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
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", "Thu Jun 06 17:48:57 EDT 2002", "-m", "test*"};
"https://commons.apache.org", "-z", dateFormat.format(expectedDate), "-m", "test*"};

final CommandLineParser parser = new PosixParser();
final CommandLine line = parser.parse(options, args);
Expand Down Expand Up @@ -161,7 +171,7 @@ public void testSimplePattern() throws Exception {
// expected
}

assertEquals(new Date(1023400137000L), line.getOptionObject('z'), "date flag z");
assertEquals(expectedDate, line.getOptionObject('z'), "date flag z");

}

Expand Down
16 changes: 13 additions & 3 deletions src/test/java/org/apache/commons/cli/TypeHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
Expand Down Expand Up @@ -62,19 +64,27 @@ private NotInstantiable() {
}

private static Stream<Arguments> createValueTestParameters() {
// forse the PatternOptionBuilder to load / modify the TypeHandler table.
// force the PatternOptionBuilder to load / modify the TypeHandler table.
final Class<?> ignore = PatternOptionBuilder.FILES_VALUE;
// reset the type handler table.
TypeHandler.resetConverters();
final List<Arguments> lst = new ArrayList<>();

/*
* Dates calculated from strings are dependent upon configuration and environment settings for the
* machine on which the test is running. To avoid this 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");

try {
lst.add(Arguments.of(Instantiable.class.getName(), PatternOptionBuilder.CLASS_VALUE, Instantiable.class));
lst.add(Arguments.of("what ever", PatternOptionBuilder.CLASS_VALUE, ParseException.class));

lst.add(Arguments.of("what ever", PatternOptionBuilder.DATE_VALUE, ParseException.class));
lst.add(Arguments.of("Thu Jun 06 17:48:57 EDT 2002", PatternOptionBuilder.DATE_VALUE,
new Date(1023400137000L)));
lst.add(Arguments.of(dateFormat.format(date), PatternOptionBuilder.DATE_VALUE, date));
lst.add(Arguments.of("Jun 06 17:48:57 EDT 2002", PatternOptionBuilder.DATE_VALUE, ParseException.class));

lst.add(Arguments.of("non-existing.file", PatternOptionBuilder.EXISTING_FILE_VALUE, ParseException.class));
Expand Down