Update time tests to JUnit Jupiter

Upgrade the tests in the time package to use JUnit Jupiter as
part of the effort to remove the dependency on the Vintage Engine.

While most of these changes are drop-in replacements with no functional
benefit, there are some non-obvious changes worth mentioning.

Unlike org.junit.Test, org.junit.jupiter.api.Test does not have an
"expected" argument. Instead, an explicit call to
org.junit.jupiter.api.Assertions.assertThrows is used.

JUnit Jupiter no longer has a concept of runners. Tests previously run
with the org.junit.runners.Parameterized runner were rewritten to use
@ParameterizedTest and a @MethodSource.

JUnit Jupiter also no longer has the concept of Rules. Usages of the
SystemDefaultsSwitch rule and its accompanying annotates were replaced
with the @DefaultLocale and @DefaultTimezone annotations that
Benedikt Ritter contributed to JUnit Pioneer, the semi-official JUnit
extension project.

It's also worth noting this is a minimal patch for migrating the
package's tests to Jupiter. There are several tests that can be made
more elegant with Jupiter's new features, but that work is left for
subsequent patches.
This commit is contained in:
Allon Mureinik 2018-10-02 06:41:37 +03:00
parent 94beded839
commit ca2e59c513
16 changed files with 934 additions and 975 deletions

View File

@ -543,6 +543,12 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>0.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>

View File

@ -16,10 +16,10 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
@ -29,20 +29,15 @@ import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.lang3.test.SystemDefaultsSwitch;
import org.apache.commons.lang3.test.SystemDefaults;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultLocale;
import org.junitpioneer.jupiter.DefaultTimeZone;
/**
* TestCase for DateFormatUtils.
*/
@SuppressWarnings("deprecation") // tests lots of deprecated items
public class DateFormatUtilsTest {
@Rule
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
//-----------------------------------------------------------------------
@Test
public void testConstructor() {
@ -171,7 +166,7 @@ public class DateFormatUtilsTest {
testUTC("09:11:12Z", DateFormatUtils.ISO_TIME_NO_T_TIME_ZONE_FORMAT.getPattern());
}
@SystemDefaults(locale="en")
@DefaultLocale(language = "en")
@Test
public void testSMTP() {
TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
@ -192,14 +187,14 @@ public class DateFormatUtilsTest {
DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.parse(date);
}
@SystemDefaults(timezone="UTC")
@DefaultTimeZone("UTC")
@Test
public void testLang530() throws ParseException {
final Date d = new Date();
final String isoDateStr = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(d);
final Date d2 = DateUtils.parseDate(isoDateStr, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern());
// the format loses milliseconds so have to reintroduce them
assertEquals("Date not equal to itself ISO formatted and parsed", d.getTime(), d2.getTime() + d.getTime() % 1000);
assertEquals(d.getTime(), d2.getTime() + d.getTime() % 1000, "Date not equal to itself ISO formatted and parsed");
}
/**
@ -218,29 +213,29 @@ public class DateFormatUtilsTest {
// Long.
{
final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/Paris"));
assertEquals("long", "2009-10-16T08:42:16+02:00", value);
assertEquals("2009-10-16T08:42:16+02:00", value, "long");
}
{
final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Asia/Kolkata"));
assertEquals("long", "2009-10-16T12:12:16+05:30", value);
assertEquals("2009-10-16T12:12:16+05:30", value, "long");
}
{
final String value = DateFormatUtils.format(cal.getTimeInMillis(), DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/London"));
assertEquals("long", "2009-10-16T07:42:16+01:00", value);
assertEquals("2009-10-16T07:42:16+01:00", value, "long");
}
// Calendar.
{
final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/Paris"));
assertEquals("calendar", "2009-10-16T08:42:16+02:00", value);
assertEquals("2009-10-16T08:42:16+02:00", value, "calendar");
}
{
final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Asia/Kolkata"));
assertEquals("calendar", "2009-10-16T12:12:16+05:30", value);
assertEquals("2009-10-16T12:12:16+05:30", value, "calendar");
}
{
final String value = DateFormatUtils.format(cal, DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.getPattern(), TimeZone.getTimeZone("Europe/London"));
assertEquals("calendar", "2009-10-16T07:42:16+01:00", value);
assertEquals("2009-10-16T07:42:16+01:00", value, "calendar");
}
}
}

View File

@ -16,11 +16,11 @@
*/
package org.apache.commons.lang3.time;
import org.junit.Test;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.Calendar;
import java.util.Date;
@ -37,7 +37,7 @@ public class DateUtilsFragmentTest {
private Calendar aCalendar;
@Before
@BeforeEach
public void setUp() {
aCalendar = Calendar.getInstance();
aCalendar.set(2005, months, days, hours, minutes, seconds);

View File

@ -16,11 +16,11 @@
*/
package org.apache.commons.lang3.time;
import org.junit.Test;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@ -58,7 +58,7 @@ public class DateUtilsRoundingTest {
FastDateFormat fdf = DateFormatUtils.ISO_DATETIME_FORMAT;
@Before
@BeforeEach
public void setUp() throws Exception {
dateTimeParser = new SimpleDateFormat("MMM dd, yyyy H:mm:ss.SSS", Locale.ENGLISH);
@ -665,9 +665,9 @@ public class DateUtilsRoundingTest {
final Date nextTruncateDate = DateUtils.addMilliseconds(lastTruncateDate, 1);
//Date-comparison
assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date with CalendarField-value "+ calendarField +" must return itself", truncatedDate, DateUtils.truncate(truncatedDate, calendarField));
assertEquals(truncatedDate, DateUtils.truncate(truncatedDate, calendarField), "Truncating "+ fdf.format(truncatedDate) +" as Date with CalendarField-value "+ calendarField +" must return itself");
assertEquals(truncatedDate, DateUtils.truncate(lastTruncateDate, calendarField));
assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate(nextTruncateDate, calendarField)));
assertFalse(truncatedDate.equals(DateUtils.truncate(nextTruncateDate, calendarField)), fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date with CalendarField-value "+ calendarField);
//Calendar-initiations
Calendar truncatedCalendar, lastTruncateCalendar, nextTruncateCalendar;
@ -679,17 +679,17 @@ public class DateUtilsRoundingTest {
nextTruncateCalendar.setTime(nextTruncateDate);
//Calendar-comparison
assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar with CalendarField-value "+ calendarField +" must return itself", truncatedCalendar, DateUtils.truncate(truncatedCalendar, calendarField));
assertEquals(truncatedCalendar, DateUtils.truncate(truncatedCalendar, calendarField), "Truncating "+ fdf.format(truncatedCalendar) +" as Calendar with CalendarField-value "+ calendarField +" must return itself");
assertEquals(truncatedCalendar, DateUtils.truncate(lastTruncateCalendar, calendarField));
assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar with CalendarField-value "+ calendarField, truncatedCalendar.equals(DateUtils.truncate(nextTruncateCalendar, calendarField)));
assertFalse(truncatedCalendar.equals(DateUtils.truncate(nextTruncateCalendar, calendarField)), fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar with CalendarField-value "+ calendarField);
//Object-comparison
assertEquals("Truncating "+ fdf.format(truncatedDate) +" as Date cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedDate, calendarField));
assertEquals(truncatedDate, DateUtils.truncate((Object) truncatedDate, calendarField), "Truncating "+ fdf.format(truncatedDate) +" as Date cast to Object with CalendarField-value "+ calendarField +" must return itself as Date");
assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateDate, calendarField));
assertFalse(fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateDate, calendarField)));
assertEquals("Truncating "+ fdf.format(truncatedCalendar) +" as Calendar cast to Object with CalendarField-value "+ calendarField +" must return itself as Date", truncatedDate, DateUtils.truncate((Object) truncatedCalendar, calendarField));
assertFalse(truncatedDate.equals(DateUtils.truncate((Object) nextTruncateDate, calendarField)), fdf.format(lastTruncateDate) +" is not an extreme when truncating as Date cast to Object with CalendarField-value "+ calendarField);
assertEquals(truncatedDate, DateUtils.truncate((Object) truncatedCalendar, calendarField), "Truncating "+ fdf.format(truncatedCalendar) +" as Calendar cast to Object with CalendarField-value "+ calendarField +" must return itself as Date");
assertEquals(truncatedDate, DateUtils.truncate((Object) lastTruncateCalendar, calendarField));
assertFalse(fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar cast to Object with CalendarField-value "+ calendarField, truncatedDate.equals(DateUtils.truncate((Object) nextTruncateCalendar, calendarField)));
assertFalse(truncatedDate.equals(DateUtils.truncate((Object) nextTruncateCalendar, calendarField)), fdf.format(lastTruncateCalendar) +" is not an extreme when truncating as Calendar cast to Object with CalendarField-value "+ calendarField);
}
/**
@ -703,7 +703,7 @@ public class DateUtilsRoundingTest {
* @since 3.0
*/
protected void roundToJanuaryFirst(final Date minDate, final Date maxDate, final int calendarField) {
assertEquals("Rounding "+ fdf.format(januaryOneDate) +" as Date with CalendarField-value "+ calendarField +" must return itself", januaryOneDate, DateUtils.round(januaryOneDate, calendarField));
assertEquals(januaryOneDate, DateUtils.round(januaryOneDate, calendarField), "Rounding "+ fdf.format(januaryOneDate) +" as Date with CalendarField-value "+ calendarField +" must return itself");
assertEquals(januaryOneDate, DateUtils.round(minDate, calendarField));
assertEquals(januaryOneDate, DateUtils.round(maxDate, calendarField));
@ -711,20 +711,20 @@ public class DateUtilsRoundingTest {
minCalendar.setTime(minDate);
final Calendar maxCalendar = Calendar.getInstance();
maxCalendar.setTime(maxDate);
assertEquals("Rounding "+ fdf.format(januaryOneCalendar) +" as Date with CalendarField-value "+ calendarField +" must return itself", januaryOneCalendar, DateUtils.round(januaryOneCalendar, calendarField));
assertEquals(januaryOneCalendar, DateUtils.round(januaryOneCalendar, calendarField), "Rounding "+ fdf.format(januaryOneCalendar) +" as Date with CalendarField-value "+ calendarField +" must return itself");
assertEquals(januaryOneCalendar, DateUtils.round(minCalendar, calendarField));
assertEquals(januaryOneCalendar, DateUtils.round(maxCalendar, calendarField));
final Date toPrevRoundDate = DateUtils.addMilliseconds(minDate, -1);
final Date toNextRoundDate = DateUtils.addMilliseconds(maxDate, 1);
assertFalse(fdf.format(minDate) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField)));
assertFalse(fdf.format(maxDate) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField)));
assertFalse(januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField)), fdf.format(minDate) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField);
assertFalse(januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField)), fdf.format(maxDate) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField);
final Calendar toPrevRoundCalendar = Calendar.getInstance();
toPrevRoundCalendar.setTime(toPrevRoundDate);
final Calendar toNextRoundCalendar = Calendar.getInstance();
toNextRoundCalendar.setTime(toNextRoundDate);
assertFalse(fdf.format(minCalendar) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField)));
assertFalse(fdf.format(maxCalendar) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField, januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField)));
assertFalse(januaryOneDate.equals(DateUtils.round(toPrevRoundDate, calendarField)), fdf.format(minCalendar) +" is not an lower-extreme when rounding as Date with CalendarField-value "+ calendarField);
assertFalse(januaryOneDate.equals(DateUtils.round(toNextRoundDate, calendarField)), fdf.format(maxCalendar) +" is not an upper-extreme when rounding as Date with CalendarField-value "+ calendarField);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -17,18 +17,19 @@
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;
import java.util.Calendar;
import java.util.TimeZone;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* TestCase for DurationFormatUtils.
@ -156,9 +157,9 @@ public class DurationFormatUtilsTest {
assertEquals("1 day 1 hour 1 minute 1 second", text);
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDurationWords() throws Exception {
DurationFormatUtils.formatDurationWords(-5000, true, true);
@Test
public void testFormatNegativeDurationWords() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationWords(-5000, true, true));
}
@Test
@ -191,9 +192,9 @@ public class DurationFormatUtilsTest {
assertEquals("01:02:12.789", DurationFormatUtils.formatDurationHMS(time));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDurationHMS() throws Exception {
DurationFormatUtils.formatDurationHMS(-5000);
@Test
public void testFormatNegativeDurationHMS() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationHMS(-5000));
}
@Test
@ -205,9 +206,9 @@ public class DurationFormatUtilsTest {
assertEquals("P0Y0M0DT0H1M15.321S", DurationFormatUtils.formatDurationISO(75321L));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDurationISO() throws Exception {
DurationFormatUtils.formatDurationISO(-5000);
@Test
public void testFormatNegativeDurationISO() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDurationISO(-5000));
}
@Test
@ -248,9 +249,9 @@ public class DurationFormatUtilsTest {
assertEquals("0 0 " + days, DurationFormatUtils.formatDuration(duration, "y M d"));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDuration() throws Exception {
DurationFormatUtils.formatDuration(-5000, "S", true);
@Test
public void testFormatNegativeDuration() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatDuration(-5000, "S", true));
}
@SuppressWarnings("deprecation")
@ -282,9 +283,9 @@ public class DurationFormatUtilsTest {
// assertEquals("P1Y2M3DT10H30M", text);
}
@Test(expected = IllegalArgumentException.class)
public void testFormatPeriodISOStartGreaterEnd() throws Exception {
DurationFormatUtils.formatPeriodISO(5000, 2000);
@Test
public void testFormatPeriodISOStartGreaterEnd() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriodISO(5000, 2000));
}
@Test
@ -348,9 +349,9 @@ public class DurationFormatUtilsTest {
assertEquals("048", DurationFormatUtils.formatPeriod(time1970, time, "MMM"));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatPeriodeStartGreaterEnd() throws Exception {
DurationFormatUtils.formatPeriod(5000, 2500, "yy/MM");
@Test
public void testFormatPeriodeStartGreaterEnd() {
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.formatPeriod(5000, 2500, "yy/MM"));
}
@Test
@ -396,13 +397,13 @@ public class DurationFormatUtilsTest {
// test failures in equals
final DurationFormatUtils.Token token = new DurationFormatUtils.Token(DurationFormatUtils.y, 4);
assertFalse("Token equal to non-Token class. ", token.equals(new Object()));
assertFalse("Token equal to Token with wrong value class. ", token.equals(new DurationFormatUtils.Token(
new Object())));
assertFalse("Token equal to Token with different count. ", token.equals(new DurationFormatUtils.Token(
DurationFormatUtils.y, 1)));
assertFalse(token.equals(new Object()), "Token equal to non-Token class. ");
assertFalse(token.equals(new DurationFormatUtils.Token(new Object())),
"Token equal to Token with wrong value class. ");
assertFalse(token.equals(new DurationFormatUtils.Token(DurationFormatUtils.y, 1)),
"Token equal to Token with different count. ");
final DurationFormatUtils.Token numToken = new DurationFormatUtils.Token(Integer.valueOf(1), 4);
assertTrue("Token with Number value not equal to itself. ", numToken.equals(numToken));
assertTrue(numToken.equals(numToken), "Token with Number value not equal to itself. ");
}
@ -575,9 +576,9 @@ public class DurationFormatUtilsTest {
//bruteForce(1996, 1, 29, "M", Calendar.MONTH); // this will fail
}
@Test(expected=IllegalArgumentException.class)
@Test
public void testLANG981() { // unmatched quote char in lexx
DurationFormatUtils.lexx("'yMdHms''S");
assertThrows(IllegalArgumentException.class, () -> DurationFormatUtils.lexx("'yMdHms''S"));
}
private static final int FOUR_YEARS = 365 * 3 + 366;
@ -624,7 +625,7 @@ public class DurationFormatUtilsTest {
if (message == null) {
assertEquals(expected, result);
} else {
assertEquals(message, expected, result);
assertEquals(expected, result, message);
}
}

View File

@ -16,12 +16,12 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.text.FieldPosition;
import java.text.Format;
@ -36,10 +36,9 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLongArray;
import org.apache.commons.lang3.test.SystemDefaults;
import org.apache.commons.lang3.test.SystemDefaultsSwitch;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultLocale;
import org.junitpioneer.jupiter.DefaultTimeZone;
/**
* Unit tests {@link org.apache.commons.lang3.time.FastDateFormat}.
@ -47,10 +46,6 @@ import org.junit.Test;
* @since 2.0
*/
public class FastDateFormatTest {
@Rule
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
/*
* Only the cache methods need to be tested here.
* The print methods are tested by {@link FastDateFormat_PrinterTest}
@ -76,7 +71,8 @@ public class FastDateFormatTest {
assertEquals(TimeZone.getDefault(), format2.getTimeZone());
}
@SystemDefaults(timezone="America/New_York", locale="en_US")
@DefaultLocale(language = "en", country = "US")
@DefaultTimeZone("America/New_York")
@Test
public void test_getInstance_String_TimeZone() {
@ -96,7 +92,7 @@ public class FastDateFormatTest {
assertNotSame(format4, format6);
}
@SystemDefaults(locale="en_US")
@DefaultLocale(language = "en", country = "US")
@Test
public void test_getInstance_String_Locale() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy", Locale.GERMANY);
@ -108,7 +104,7 @@ public class FastDateFormatTest {
assertEquals(Locale.GERMANY, format1.getLocale());
}
@SystemDefaults(locale="en_US")
@DefaultLocale(language = "en", country = "US")
@Test
public void test_changeDefault_Locale_DateInstance() {
final FastDateFormat format1 = FastDateFormat.getDateInstance(FastDateFormat.FULL, Locale.GERMANY);
@ -123,7 +119,7 @@ public class FastDateFormatTest {
assertNotSame(format2, format3);
}
@SystemDefaults(locale="en_US")
@DefaultLocale(language = "en", country = "US")
@Test
public void test_changeDefault_Locale_DateTimeInstance() {
final FastDateFormat format1 = FastDateFormat.getDateTimeInstance(FastDateFormat.FULL, FastDateFormat.FULL, Locale.GERMANY);
@ -138,7 +134,8 @@ public class FastDateFormatTest {
assertNotSame(format2, format3);
}
@SystemDefaults(locale="en_US", timezone="America/New_York")
@DefaultLocale(language = "en", country = "US")
@DefaultTimeZone("America/New_York")
@Test
public void test_getInstance_String_TimeZone_Locale() {
final FastDateFormat format1 = FastDateFormat.getInstance("MM/DD/yyyy",

View File

@ -16,140 +16,131 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
/**
* Compare FastDateParser with SimpleDateFormat
*/
@RunWith(Parameterized.class)
public class FastDateParserSDFTest {
@Parameters(name= "{index}: {0} {1} {2}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object [][]{
public static Stream<Arguments> data() {
return Stream.of(
// General Time zone tests
{"z yyyy", "GMT 2010", Locale.UK, true}, // no offset specified, but this is allowed as a TimeZone name
{"z yyyy", "GMT-123 2010", Locale.UK, false},
{"z yyyy", "GMT-1234 2010", Locale.UK, false},
{"z yyyy", "GMT-12:34 2010", Locale.UK, true},
{"z yyyy", "GMT-1:23 2010", Locale.UK, true},
Arguments.of("z yyyy", "GMT 2010", Locale.UK, true), // no offset specified, but this is allowed as a TimeZone name
Arguments.of("z yyyy", "GMT-123 2010", Locale.UK, false),
Arguments.of("z yyyy", "GMT-1234 2010", Locale.UK, false),
Arguments.of("z yyyy", "GMT-12:34 2010", Locale.UK, true),
Arguments.of("z yyyy", "GMT-1:23 2010", Locale.UK, true),
// RFC 822 tests
{"z yyyy", "-1234 2010", Locale.UK, true},
{"z yyyy", "-12:34 2010", Locale.UK, false},
{"z yyyy", "-123 2010", Locale.UK, false},
Arguments.of("z yyyy", "-1234 2010", Locale.UK, true),
Arguments.of("z yyyy", "-12:34 2010", Locale.UK, false),
Arguments.of("z yyyy", "-123 2010", Locale.UK, false),
// year tests
{ "MM/dd/yyyy", "01/11/12", Locale.UK, true},
{ "MM/dd/yy", "01/11/12", Locale.UK, true},
Arguments.of( "MM/dd/yyyy", "01/11/12", Locale.UK, true),
Arguments.of( "MM/dd/yy", "01/11/12", Locale.UK, true),
// LANG-1089
{ "HH", "00", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "00", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "00", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "00", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "00", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "00", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "00", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "00", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "01", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "01", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "01", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "01", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "01", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "01", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "01", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "01", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "11", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "11", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "11", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "11", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "11", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "11", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "11", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "11", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "12", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "12", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "12", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "12", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "12", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "12", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "12", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "12", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "13", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "13", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "13", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "13", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "13", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "13", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "13", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "13", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "23", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "23", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "23", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "23", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "23", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "23", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "23", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "23", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "24", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "24", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "24", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "24", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "24", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "24", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "24", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "24", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "25", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "25", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "25", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "25", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
Arguments.of( "HH", "25", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "25", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "25", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "25", Locale.UK, true), // Hour in day (1-24), i.e. midnight is 24, not 0
{ "HH", "48", Locale.UK, true}, // Hour in day (0-23)
{ "KK", "48", Locale.UK, true}, // Hour in am/pm (0-11)
{ "hh", "48", Locale.UK, true}, // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
{ "kk", "48", Locale.UK, true}, // Hour in day (1-24), i.e. midnight is 24, not 0
});
Arguments.of( "HH", "48", Locale.UK, true), // Hour in day (0-23)
Arguments.of( "KK", "48", Locale.UK, true), // Hour in am/pm (0-11)
Arguments.of( "hh", "48", Locale.UK, true), // Hour in am/pm (1-12), i.e. midday/midnight is 12, not 0
Arguments.of( "kk", "48", Locale.UK, true) // Hour in day (1-24), i.e. midnight is 24, not 0
);
}
private final String format;
private final String input;
private final Locale locale;
private final boolean valid;
private final TimeZone timeZone = TimeZone.getDefault();
private static final TimeZone timeZone = TimeZone.getDefault();
public FastDateParserSDFTest(final String format, final String input, final Locale locale, final boolean valid) {
this.format = format;
this.input = input;
this.locale = locale;
this.valid = valid;
@ParameterizedTest
@MethodSource("data")
public void testOriginal(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input, format, locale, valid);
}
@Test
public void testOriginal() throws Exception {
checkParse(input);
@ParameterizedTest
@MethodSource("data")
public void testOriginalPP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input, format, locale, valid);
}
@Test
public void testOriginalPP() throws Exception {
checkParsePosition(input);
@ParameterizedTest
@MethodSource("data")
public void testUpperCase(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input.toUpperCase(locale), format, locale, valid);
}
@Test
public void testUpperCase() throws Exception {
checkParse(input.toUpperCase(locale));
@ParameterizedTest
@MethodSource("data")
public void testUpperCasePP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input.toUpperCase(locale), format, locale, valid);
}
@Test
public void testUpperCasePP() throws Exception {
checkParsePosition(input.toUpperCase(locale));
@ParameterizedTest
@MethodSource("data")
public void testLowerCase(final String format, final String input, final Locale locale, final boolean valid) {
checkParse(input.toLowerCase(locale), format, locale, valid);
}
@Test
public void testLowerCase() throws Exception {
checkParse(input.toLowerCase(locale));
@ParameterizedTest
@MethodSource("data")
public void testLowerCasePP(final String format, final String input, final Locale locale, final boolean valid) {
checkParsePosition(input.toLowerCase(locale), format, locale, valid);
}
@Test
public void testLowerCasePP() throws Exception {
checkParsePosition(input.toLowerCase(locale));
}
private void checkParse(final String formattedDate) {
private void checkParse(final String formattedDate, String format, Locale locale, boolean valid) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
final DateParser fdf = new FastDateParser(format, timeZone, locale);
@ -184,12 +175,12 @@ public class FastDateParserSDFTest {
fdfE = e.getClass();
}
if (valid) {
assertEquals(locale.toString()+" "+formattedDate +"\n",expectedTime, actualTime);
assertEquals(expectedTime, actualTime, locale.toString()+" "+formattedDate +"\n");
} else {
assertEquals(locale.toString()+" "+formattedDate + " expected same Exception ", sdfE, fdfE);
assertEquals(sdfE, fdfE, locale.toString()+" "+formattedDate + " expected same Exception ");
}
}
private void checkParsePosition(final String formattedDate) {
private void checkParsePosition(final String formattedDate, String format, Locale locale, boolean valid) {
final SimpleDateFormat sdf = new SimpleDateFormat(format, locale);
sdf.setTimeZone(timeZone);
final DateParser fdf = new FastDateParser(format, timeZone, locale);
@ -198,7 +189,7 @@ public class FastDateParserSDFTest {
final Date expectedTime = sdf.parse(formattedDate, sdfP);
final int sdferrorIndex = sdfP.getErrorIndex();
if (valid) {
assertEquals("Expected SDF error index -1 ", -1, sdferrorIndex);
assertEquals(-1, sdferrorIndex, "Expected SDF error index -1 ");
final int endIndex = sdfP.getIndex();
final int length = formattedDate.length();
if (endIndex != length) {
@ -216,15 +207,15 @@ public class FastDateParserSDFTest {
final Date actualTime = fdf.parse(formattedDate, fdfP);
final int fdferrorIndex = fdfP.getErrorIndex();
if (valid) {
assertEquals("Expected FDF error index -1 ", -1, fdferrorIndex);
assertEquals(-1, fdferrorIndex, "Expected FDF error index -1 ");
final int endIndex = fdfP.getIndex();
final int length = formattedDate.length();
assertEquals("Expected FDF to parse full string " + fdfP, length, endIndex);
assertEquals(locale.toString()+" "+formattedDate +"\n", expectedTime, actualTime);
assertEquals(length, endIndex, "Expected FDF to parse full string " + fdfP);
assertEquals(expectedTime, actualTime, locale.toString()+" "+formattedDate +"\n");
} else {
assertNotEquals("Test data error: expected FDF parse to fail, but got " + actualTime, -1, fdferrorIndex);
assertTrue("FDF error index ("+ fdferrorIndex + ") should approximate SDF index (" + sdferrorIndex + ")",
sdferrorIndex - fdferrorIndex <= 4);
assertNotEquals(-1, fdferrorIndex, "Test data error: expected FDF parse to fail, but got " + actualTime);
assertTrue(sdferrorIndex - fdferrorIndex <= 4,
"FDF error index ("+ fdferrorIndex + ") should approximate SDF index (" + sdferrorIndex + ")");
}
}
}

View File

@ -16,10 +16,11 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.Serializable;
import java.text.ParseException;
@ -34,7 +35,7 @@ import java.util.Map;
import java.util.TimeZone;
import org.apache.commons.lang3.LocaleUtils;
import org.apache.commons.lang3.SerializationUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Unit tests {@link org.apache.commons.lang3.time.FastDateParser}.
@ -224,7 +225,7 @@ public class FastDateParserTest {
final String fmt = sdf.format(in);
try {
final Date out = fdp.parse(fmt);
assertEquals(locale.toString()+" "+in+" "+ format+ " "+tz.getID(), in, out);
assertEquals(in, out, locale.toString()+" "+in+" "+ format+ " "+tz.getID());
} catch (final ParseException pe) {
if (year >= 1868 || !locale.getCountry().equals("JP")) {// LANG-978
throw pe;
@ -271,7 +272,7 @@ public class FastDateParserTest {
final Date expected= cal.getTime();
final Date actual = fdp.parse("2000/02/10 "+tz.getDisplayName(locale));
assertEquals("tz:"+tz.getID()+" locale:"+locale.getDisplayName(), expected, actual);
assertEquals(expected, actual, "tz:"+tz.getID()+" locale:"+locale.getDisplayName());
}
}
}
@ -385,7 +386,7 @@ public class FastDateParserTest {
private void checkParse(final Locale locale, final SimpleDateFormat sdf, final DateParser fdf, final String formattedDate) throws ParseException {
final Date expectedTime = sdf.parse(formattedDate);
final Date actualTime = fdf.parse(formattedDate);
assertEquals(locale.toString()+" "+formattedDate +"\n",expectedTime, actualTime);
assertEquals(expectedTime, actualTime, locale.toString()+" "+formattedDate +"\n");
}
@Test
@ -473,8 +474,8 @@ public class FastDateParserTest {
}
}
// SDF and FDF should produce equivalent results
assertTrue("Should both or neither throw Exceptions", (f==null)==(s==null));
assertEquals("Parsed dates should be equal", dsdf, dfdp);
assertTrue((f==null)==(s==null), "Should both or neither throw Exceptions");
assertEquals(dsdf, dfdp, "Parsed dates should be equal");
}
@Test
@ -604,9 +605,9 @@ public class FastDateParserTest {
assertEquals(expected.getTime(), fdp.parse("14May2014"));
}
@Test(expected = IllegalArgumentException.class)
@Test
public void test1806Argument() {
getInstance("XXXX");
assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX"));
}
private static Calendar initializeCalendar(final TimeZone tz) {
@ -652,13 +653,13 @@ public class FastDateParserTest {
final String message = trial.zone.getDisplayName()+";";
DateParser parser = getInstance(formatStub+"X", trial.zone);
assertEquals(message+trial.one, cal.getTime().getTime(), parser.parse(dateStub+trial.one).getTime()-trial.offset);
assertEquals(cal.getTime().getTime(), parser.parse(dateStub+trial.one).getTime()-trial.offset, message+trial.one);
parser = getInstance(formatStub+"XX", trial.zone);
assertEquals(message+trial.two, cal.getTime(), parser.parse(dateStub+trial.two));
assertEquals(cal.getTime(), parser.parse(dateStub+trial.two), message+trial.two);
parser = getInstance(formatStub+"XXX", trial.zone);
assertEquals(message+trial.three, cal.getTime(), parser.parse(dateStub+trial.three));
assertEquals(cal.getTime(), parser.parse(dateStub+trial.three), message+trial.three);
}
}

View File

@ -16,9 +16,9 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import java.text.ParsePosition;
import java.util.Calendar;
@ -26,7 +26,7 @@ import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.junit.Test;
import org.junit.jupiter.api.Test;
public class FastDateParser_MoreOrLessTest {

View File

@ -16,9 +16,10 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.Serializable;
import java.text.FieldPosition;
@ -31,10 +32,9 @@ import java.util.Locale;
import java.util.TimeZone;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.test.SystemDefaults;
import org.apache.commons.lang3.test.SystemDefaultsSwitch;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultLocale;
import org.junitpioneer.jupiter.DefaultTimeZone;
/**
* Unit tests {@link org.apache.commons.lang3.time.FastDatePrinter}.
@ -76,10 +76,8 @@ public class FastDatePrinterTest {
return new FastDatePrinter(format, timeZone, locale);
}
@Rule
public SystemDefaultsSwitch defaults = new SystemDefaultsSwitch();
@SystemDefaults(timezone="America/New_York", locale="en_US")
@DefaultLocale(language = "en", country = "US")
@DefaultTimeZone("America/New_York")
@Test
public void testFormat() {
final GregorianCalendar cal1 = new GregorianCalendar(2003, 0, 10, 15, 33, 20);
@ -210,8 +208,8 @@ public class FastDatePrinterTest {
cal.set(2009, Calendar.OCTOBER, 16, 8, 42, 16);
final DatePrinter format = getInstance("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", TimeZone.getTimeZone("GMT"));
assertEquals("dateTime", "2009-10-16T16:42:16.000Z", format.format(cal.getTime()));
assertEquals("dateTime", "2009-10-16T16:42:16.000Z", format.format(cal));
assertEquals("2009-10-16T16:42:16.000Z", format.format(cal.getTime()), "dateTime");
assertEquals("2009-10-16T16:42:16.000Z", format.format(cal), "dateTime");
}
@Test
@ -262,7 +260,7 @@ public class FastDatePrinterTest {
assertEquals(NEW_YORK, printer.getTimeZone());
}
@SystemDefaults(timezone="UTC")
@DefaultTimeZone("UTC")
@Test
public void testTimeZoneAsZ() throws Exception {
final Calendar c = Calendar.getInstance(FastTimeZone.getGmtTimeZone());
@ -288,9 +286,9 @@ public class FastDatePrinterTest {
return cal;
}
@Test(expected = IllegalArgumentException.class)
@Test
public void test1806Argument() {
getInstance("XXXX");
assertThrows(IllegalArgumentException.class, () -> getInstance("XXXX"));
}
private enum Expected1806 {
@ -354,15 +352,15 @@ public class FastDatePrinterTest {
// calendar fast.
{
final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZone.getTimeZone("Europe/Paris")).format(cal);
assertEquals("calendar", "2009-10-16T08:42:16 +0200", value);
assertEquals("2009-10-16T08:42:16 +0200", value, "calendar");
}
{
final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZone.getTimeZone("Asia/Kolkata")).format(cal);
assertEquals("calendar", "2009-10-16T12:12:16 +0530", value);
assertEquals("2009-10-16T12:12:16 +0530", value, "calendar");
}
{
final String value = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss Z", TimeZone.getTimeZone("Europe/London")).format(cal);
assertEquals("calendar", "2009-10-16T07:42:16 +0100", value);
assertEquals("2009-10-16T07:42:16 +0100", value, "calendar");
}
}

View File

@ -16,48 +16,34 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import java.util.TimeZone;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@RunWith(Parameterized.class)
public class FastDatePrinterTimeZonesTest {
private static final String PATTERN = "h:mma z";
@Parameterized.Parameters
public static Collection<TimeZone> data() {
final String[] zoneIds = TimeZone.getAvailableIDs();
final List<TimeZone> timeZones = new ArrayList<>();
for (final String zoneId : zoneIds) {
timeZones.add(TimeZone.getTimeZone(zoneId));
}
return timeZones;
public static Stream<TimeZone> data() {
return Arrays.stream(TimeZone.getAvailableIDs()).map(TimeZone::getTimeZone);
}
private final TimeZone timeZone;
public FastDatePrinterTimeZonesTest(final TimeZone timeZone) {
this.timeZone = timeZone;
}
@Test
public void testCalendarTimezoneRespected() {
@ParameterizedTest
@MethodSource("data")
public void testCalendarTimezoneRespected(TimeZone timeZone) {
final Calendar cal = Calendar.getInstance(timeZone);
final SimpleDateFormat sdf = new SimpleDateFormat(PATTERN);
sdf.setTimeZone(timeZone);
final String expectedValue = sdf.format(cal.getTime());
final String actualValue = FastDateFormat.getInstance(PATTERN, this.timeZone).format(cal);
final String actualValue = FastDateFormat.getInstance(PATTERN, timeZone).format(cal);
assertEquals(expectedValue, actualValue);
}

View File

@ -16,9 +16,9 @@
*/
package org.apache.commons.lang3.time;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.TimeZone;

View File

@ -16,19 +16,20 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* Tests for GmtTimeZone
*/
public class GmtTimeZoneTest {
@Test(expected = IllegalArgumentException.class)
@Test
public void hoursOutOfRange() {
new GmtTimeZone(false, 24, 0);
assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 24, 0));
}
@Test
@ -36,9 +37,9 @@ public class GmtTimeZoneTest {
assertEquals(23 * 60 * 60 * 1000, new GmtTimeZone(false, 23, 0).getRawOffset());
}
@Test(expected = IllegalArgumentException.class)
@Test
public void minutesOutOfRange() {
new GmtTimeZone(false, 0, 60);
assertThrows(IllegalArgumentException.class, () -> new GmtTimeZone(false, 0, 60));
}
@Test
@ -51,9 +52,9 @@ public class GmtTimeZoneTest {
assertEquals(0, new GmtTimeZone(false, 0, 0).getOffset(234304));
}
@Test(expected = UnsupportedOperationException.class)
@Test
public void setRawOffset() {
new GmtTimeZone(false, 0, 0).setRawOffset(0);
assertThrows(UnsupportedOperationException.class, () -> new GmtTimeZone(false, 0, 0).setRawOffset(0));
}
@Test

View File

@ -16,15 +16,15 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.util.concurrent.TimeUnit;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
/**
* TestCase for StopWatch.
@ -109,8 +109,7 @@ public class StopWatchTest {
watch.stop();
final long totalTime = watch.getTime();
assertEquals("Formatted split string not the correct length",
splitStr.length(), 12);
assertEquals(splitStr.length(), 12, "Formatted split string not the correct length");
assertTrue(splitTime >= 500);
assertTrue(splitTime < 700);
assertTrue(totalTime >= 1500);

View File

@ -16,58 +16,45 @@
*/
package org.apache.commons.lang3.time;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.text.ParseException;
import java.text.ParsePosition;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.stream.Stream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@RunWith(Parameterized.class)
public class WeekYearTest {
@Parameters(name = "{index}: {3}")
public static Collection<Object[]> data() {
return Arrays
.asList(new Object[][] {
{ 2005, Calendar.JANUARY, 1, "2004-W53-6" },
{ 2005, Calendar.JANUARY, 2, "2004-W53-7" },
{ 2005, Calendar.DECEMBER, 31, "2005-W52-6" },
{ 2007, Calendar.JANUARY, 1, "2007-W01-1" },
{ 2007, Calendar.DECEMBER, 30, "2007-W52-7" },
{ 2007, Calendar.DECEMBER, 31, "2008-W01-1" },
{ 2008, Calendar.JANUARY, 1, "2008-W01-2" },
{ 2008, Calendar.DECEMBER, 28, "2008-W52-7" },
{ 2008, Calendar.DECEMBER, 29, "2009-W01-1" },
{ 2008, Calendar.DECEMBER, 30, "2009-W01-2" },
{ 2008, Calendar.DECEMBER, 31, "2009-W01-3" },
{ 2009, Calendar.JANUARY, 1, "2009-W01-4" },
{ 2009, Calendar.DECEMBER, 31, "2009-W53-4" },
{ 2010, Calendar.JANUARY, 1, "2009-W53-5" },
{ 2010, Calendar.JANUARY, 2, "2009-W53-6" },
{ 2010, Calendar.JANUARY, 3, "2009-W53-7" }
});
public static Stream<Arguments> data() {
return Stream.of(
Arguments.of(new GregorianCalendar( 2005, Calendar.JANUARY, 1), "2004-W53-6"),
Arguments.of(new GregorianCalendar( 2005, Calendar.JANUARY, 2), "2004-W53-7"),
Arguments.of(new GregorianCalendar( 2005, Calendar.DECEMBER, 31), "2005-W52-6"),
Arguments.of(new GregorianCalendar( 2007, Calendar.JANUARY, 1), "2007-W01-1"),
Arguments.of(new GregorianCalendar( 2007, Calendar.DECEMBER, 30), "2007-W52-7"),
Arguments.of(new GregorianCalendar( 2007, Calendar.DECEMBER, 31), "2008-W01-1"),
Arguments.of(new GregorianCalendar( 2008, Calendar.JANUARY, 1), "2008-W01-2"),
Arguments.of(new GregorianCalendar( 2008, Calendar.DECEMBER, 28), "2008-W52-7"),
Arguments.of(new GregorianCalendar( 2008, Calendar.DECEMBER, 29), "2009-W01-1"),
Arguments.of(new GregorianCalendar( 2008, Calendar.DECEMBER, 30), "2009-W01-2"),
Arguments.of(new GregorianCalendar( 2008, Calendar.DECEMBER, 31), "2009-W01-3"),
Arguments.of(new GregorianCalendar( 2009, Calendar.JANUARY, 1), "2009-W01-4"),
Arguments.of(new GregorianCalendar( 2009, Calendar.DECEMBER, 31), "2009-W53-4"),
Arguments.of(new GregorianCalendar( 2010, Calendar.JANUARY, 1), "2009-W53-5"),
Arguments.of(new GregorianCalendar( 2010, Calendar.JANUARY, 2), "2009-W53-6"),
Arguments.of(new GregorianCalendar( 2010, Calendar.JANUARY, 3), "2009-W53-7")
);
}
final Calendar vulgar;
final String isoForm;
public WeekYearTest(final int year, final int month, final int day, final String isoForm) {
vulgar = new GregorianCalendar(year, month, day);
this.isoForm = isoForm;
}
@Test
public void testParser() throws ParseException {
@ParameterizedTest
@MethodSource("data")
public void testParser(Calendar vulgar, String isoForm) {
final DateParser parser = new FastDateParser("YYYY-'W'ww-u", TimeZone.getDefault(), Locale.getDefault());
final Calendar cal = Calendar.getInstance();
@ -79,8 +66,9 @@ public class WeekYearTest {
assertEquals(vulgar.getTime(), cal.getTime());
}
@Test
public void testPrinter() {
@ParameterizedTest
@MethodSource("data")
public void testPrinter(Calendar vulgar, String isoForm) {
final FastDatePrinter printer = new FastDatePrinter("YYYY-'W'ww-u", TimeZone.getDefault(), Locale.getDefault());
vulgar.setMinimalDaysInFirstWeek(4);