Skip to content

Commit a5acbe4

Browse files
authored
[CLI-327] Fix for expected textual date values. (#244)
* Fixed test expected values * added testing comments
1 parent ea6ab2c commit a5acbe4

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/test/java/org/apache/commons/cli/ConverterTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2121
import static org.junit.jupiter.api.Assertions.assertThrows;
2222

2323
import java.net.URL;
24+
import java.text.DateFormat;
25+
import java.text.SimpleDateFormat;
2426
import java.util.ArrayList;
2527
import java.util.Date;
2628
import java.util.List;
@@ -75,8 +77,16 @@ public void classTests() throws Exception {
7577
public void dateTests() throws Exception {
7678
assertThrows(java.text.ParseException.class, () -> Converter.DATE.apply("whatever"));
7779

78-
final Date d = new Date(1023400137000L);
79-
assertEquals(d, Converter.DATE.apply("Thu Jun 06 17:48:57 EDT 2002"));
80+
/*
81+
* Dates calculated from strings are dependent upon configuration and environment settings for the
82+
* machine on which the test is running. To avoid this problem, convert the time into a string
83+
* and then unparse that using the converter. This produces strings that always match the correct
84+
* time zone.
85+
*/
86+
final Date expected = new Date(1023400137000L);
87+
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
88+
String formatted = dateFormat.format(expected);
89+
assertEquals(expected, Converter.DATE.apply(formatted));
8090

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

src/test/java/org/apache/commons/cli/PatternOptionBuilderTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2727
import java.io.File;
2828
import java.io.FileInputStream;
2929
import java.net.URL;
30+
import java.text.DateFormat;
31+
import java.text.SimpleDateFormat;
3032
import java.util.Calendar;
3133
import java.util.Date;
3234
import java.util.Vector;
@@ -125,9 +127,17 @@ public void testRequiredOption() throws Exception {
125127

126128
@Test
127129
public void testSimplePattern() throws Exception {
130+
/*
131+
* Dates calculated from strings are dependent upon configuration and environment settings for the
132+
* machine on which the test is running. To avoid this problem, convert the time into a string
133+
* and then unparse that using the converter. This produces strings that always match the correct
134+
* time zone.
135+
*/
128136
final Options options = PatternOptionBuilder.parsePattern("a:b@cde>f+n%t/m*z#");
137+
final Date expectedDate = new Date(1023400137000L);
138+
DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
129139
final String[] args = {"-c", "-a", "foo", "-b", "java.util.Vector", "-e", "build.xml", "-f", "java.util.Calendar", "-n", "4.5", "-t",
130-
"https://commons.apache.org", "-z", "Thu Jun 06 17:48:57 EDT 2002", "-m", "test*"};
140+
"https://commons.apache.org", "-z", dateFormat.format(expectedDate), "-m", "test*"};
131141

132142
final CommandLineParser parser = new PosixParser();
133143
final CommandLine line = parser.parse(options, args);
@@ -161,7 +171,7 @@ public void testSimplePattern() throws Exception {
161171
// expected
162172
}
163173

164-
assertEquals(new Date(1023400137000L), line.getOptionObject('z'), "date flag z");
174+
assertEquals(expectedDate, line.getOptionObject('z'), "date flag z");
165175

166176
}
167177

src/test/java/org/apache/commons/cli/TypeHandlerTest.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2828
import java.net.MalformedURLException;
2929
import java.net.URL;
3030
import java.nio.file.Path;
31+
import java.text.DateFormat;
32+
import java.text.SimpleDateFormat;
3133
import java.util.ArrayList;
3234
import java.util.Date;
3335
import java.util.List;
@@ -62,19 +64,27 @@ private NotInstantiable() {
6264
}
6365

6466
private static Stream<Arguments> createValueTestParameters() {
65-
// forse the PatternOptionBuilder to load / modify the TypeHandler table.
67+
// force the PatternOptionBuilder to load / modify the TypeHandler table.
6668
final Class<?> ignore = PatternOptionBuilder.FILES_VALUE;
6769
// reset the type handler table.
6870
TypeHandler.resetConverters();
6971
final List<Arguments> lst = new ArrayList<>();
7072

73+
/*
74+
* Dates calculated from strings are dependent upon configuration and environment settings for the
75+
* machine on which the test is running. To avoid this problem, convert the time into a string
76+
* and then unparse that using the converter. This produces strings that always match the correct
77+
* time zone.
78+
*/
79+
final Date date = new Date(1023400137000L);
80+
final DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
81+
7182
try {
7283
lst.add(Arguments.of(Instantiable.class.getName(), PatternOptionBuilder.CLASS_VALUE, Instantiable.class));
7384
lst.add(Arguments.of("what ever", PatternOptionBuilder.CLASS_VALUE, ParseException.class));
7485

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

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

0 commit comments

Comments
 (0)