Run DateUtilsTest as Unit Test (#4805)

This commit is contained in:
Herbert Reiter 2021-01-02 12:59:26 +01:00 committed by GitHub
parent b1dc96adb3
commit 0b715d5494
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 32 deletions

View File

@ -30,9 +30,12 @@ public class DateUtils {
}
String date = input.trim().replace('/', '-').replaceAll("( ){2,}+", " ");
// remove colon from timezone to avoid differences between Android and Java SimpleDateFormat
date = date.replaceAll("([+-]\\d\\d):(\\d\\d)$", "$1$2");
// CEST is widely used but not in the "ISO 8601 Time zone" list. Let's hack around.
date = date.replaceAll("CEST$", "+02:00");
date = date.replaceAll("CET$", "+01:00");
date = date.replaceAll("CEST$", "+0200");
date = date.replaceAll("CET$", "+0100");
// some generators use "Sept" for September
date = date.replaceAll("\\bSept\\b", "Sep");

View File

@ -1,6 +1,5 @@
package de.danoeh.antennapod.core.util;
import androidx.test.filters.SmallTest;
import org.junit.Test;
import java.util.Calendar;
@ -12,16 +11,11 @@ import static org.junit.Assert.assertEquals;
/**
* Unit test for {@link DateUtils}.
*
* Note: It NEEDS to be run in android devices, i.e., it cannot be run in standard JDK, because
* the test invokes some android platform-specific behavior in the underlying
* {@link java.text.SimpleDateFormat} used by {@link DateUtils}.
*
*/
@SmallTest
public class DateUtilsTest {
@Test
public void testParseDateWithMicroseconds() throws Exception {
public void testParseDateWithMicroseconds() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 13, 31, 4);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 963);
@ -30,7 +24,7 @@ public class DateUtilsTest {
}
@Test
public void testParseDateWithCentiseconds() throws Exception {
public void testParseDateWithCentiseconds() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 13, 31, 4);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 960);
@ -39,17 +33,17 @@ public class DateUtilsTest {
}
@Test
public void testParseDateWithDeciseconds() throws Exception {
public void testParseDateWithDeciseconds() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 13, 31, 4);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 900);
Date actual = DateUtils.parse("2015-03-28T13:31:04.9");
assertEquals(expected.getTime()/1000, actual.getTime()/1000);
assertEquals(900, actual.getTime()%1000);
assertEquals(expected.getTime() / 1000, actual.getTime() / 1000);
assertEquals(900, actual.getTime() % 1000);
}
@Test
public void testParseDateWithMicrosecondsAndTimezone() throws Exception {
public void testParseDateWithMicrosecondsAndTimezone() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 6, 31, 4);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 963);
@ -58,7 +52,7 @@ public class DateUtilsTest {
}
@Test
public void testParseDateWithCentisecondsAndTimezone() throws Exception {
public void testParseDateWithCentisecondsAndTimezone() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 6, 31, 4);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 960);
@ -67,17 +61,17 @@ public class DateUtilsTest {
}
@Test
public void testParseDateWithDecisecondsAndTimezone() throws Exception {
public void testParseDateWithDecisecondsAndTimezone() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 6, 31, 4);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 900);
Date actual = DateUtils.parse("2015-03-28T13:31:04.9 +0700");
assertEquals(expected.getTime()/1000, actual.getTime()/1000);
assertEquals(900, actual.getTime()%1000);
assertEquals(expected.getTime() / 1000, actual.getTime() / 1000);
assertEquals(900, actual.getTime() % 1000);
}
@Test
public void testParseDateWithTimezoneName() throws Exception {
public void testParseDateWithTimezoneName() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 6, 31, 4);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis());
@ -86,7 +80,7 @@ public class DateUtilsTest {
}
@Test
public void testParseDateWithTimezoneName2() throws Exception {
public void testParseDateWithTimezoneName2() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 6, 31, 0);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis());
@ -95,7 +89,7 @@ public class DateUtilsTest {
}
@Test
public void testParseDateWithTimeZoneOffset() throws Exception {
public void testParseDateWithTimeZoneOffset() {
GregorianCalendar exp = new GregorianCalendar(2015, 2, 28, 12, 16, 12);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis());
@ -104,7 +98,7 @@ public class DateUtilsTest {
}
@Test
public void testAsctime() throws Exception {
public void testAsctime() {
GregorianCalendar exp = new GregorianCalendar(2011, 4, 25, 12, 33, 0);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis());
@ -113,7 +107,7 @@ public class DateUtilsTest {
}
@Test
public void testMultipleConsecutiveSpaces() throws Exception {
public void testMultipleConsecutiveSpaces() {
GregorianCalendar exp = new GregorianCalendar(2010, 2, 23, 6, 6, 26);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis());
@ -121,14 +115,8 @@ public class DateUtilsTest {
assertEquals(expected, actual);
}
/**
* Requires Android platform.
*
* Reason: Standard JDK cannot parse timezone <code>-08:00</code> (ISO 8601 format). It only accepts
* <code>-0800</code> (RFC 822 format)
*/
@Test
public void testParseDateWithNoTimezonePadding() throws Exception {
public void testParseDateWithNoTimezonePadding() {
GregorianCalendar exp = new GregorianCalendar(2017, 1, 22, 22, 28, 0);
exp.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected = new Date(exp.getTimeInMillis() + 2);
@ -143,7 +131,7 @@ public class DateUtilsTest {
* @see #testParseDateWithNoTimezonePadding()
*/
@Test
public void testParseDateWithForCest() throws Exception {
public void testParseDateWithForCest() {
GregorianCalendar exp1 = new GregorianCalendar(2017, 0, 28, 22, 0, 0);
exp1.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expected1 = new Date(exp1.getTimeInMillis());