Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for parseLenientDateTime #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
30 changes: 29 additions & 1 deletion src/tests/junit/org/apache/tools/ant/util/DateUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
*/
package org.apache.tools.ant.util;

import java.text.ParseException;
import java.util.Calendar;
import java.util.TimeZone;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;

/**
* TestCase for DateUtils.
Expand Down Expand Up @@ -103,4 +104,31 @@ public void testPhaseOfMoon() {
cal.set(2002, 2, 12);
assertEquals(0, DateUtils.getPhaseOfMoon(cal));
}

@Test
public void testLenientDateTime() throws ParseException {
// ISO 8601
assertEquals(1488622499999L, DateUtils.parseLenientDateTime("2017-03-04 15:14:59.999 +0500").getTime());
assertEquals(1488622499999L, DateUtils.parseLenientDateTime("2017-03-04T15:14:59.999+05:00").getTime());
assertEquals(1488622499999L, DateUtils.parseLenientDateTime("2017-03-04 10:14:59.999").getTime());
assertEquals(1488622499999L, DateUtils.parseLenientDateTime("2017-03-04T10:14:59.999Z").getTime());
assertEquals(1488622499000L, DateUtils.parseLenientDateTime("2017-03-04 00:14:59 -10").getTime());
assertEquals(1488622440000L, DateUtils.parseLenientDateTime("2017-03-04t00:14-10").getTime());

// US-style
assertEquals(1488622440000L, DateUtils.parseLenientDateTime("03/04/2017 10:14 AM").getTime());
assertEquals(1488622499000L, DateUtils.parseLenientDateTime("03/04/2017 10:14:59 AM").getTime());
assertEquals(1488665640000L, DateUtils.parseLenientDateTime("03/04/2017 10:14 PM").getTime());
assertEquals(1488665699000L, DateUtils.parseLenientDateTime("03/04/2017 10:14:59 PM").getTime());

// Milliseconds
assertEquals(1488622499999L, DateUtils.parseLenientDateTime("1488622499999").getTime());

try {
DateUtils.parseLenientDateTime("not a date");
} catch (ParseException pe) {
return;
}
fail("DateUtils.parseLenientDateTime is accepting malformed input!");
}
}