Skip to content

Commit

Permalink
JSTEP-10 Migrate tests to Junit 5 (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHyukKim authored Jan 26, 2025
1 parent ddb5297 commit e5ba92a
Show file tree
Hide file tree
Showing 27 changed files with 231 additions and 26 deletions.
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,15 @@
<version>${version.joda}</version>
</dependency>

<!-- 20-Apr-2024, tatu: JUnit4 no longer from jackson-base, so: -->
<!-- Use JUnit 5 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fasterxml.jackson.datatype.joda;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand All @@ -9,6 +11,8 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AnnotationTest extends DateTimeTest
{
static class AClass{
Expand All @@ -25,6 +29,7 @@ public void setCreatedOn(DateTime createdOn) {
}
}

@Test
public void testDateTimeViaAnnotation() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();
AClass initialObject = new AClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -13,6 +15,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import static org.junit.jupiter.api.Assertions.*;

public class DateTimeTest extends JodaTestBase
{
static class DateAsText {
Expand Down Expand Up @@ -91,13 +95,15 @@ private static interface TypeInfoMixIn {
* First: let's ensure that serialization does not fail
* with an error (see [JACKSON-157]).
*/
@Test
public void testSerializationDefaultAsTimestamp() throws IOException
{
// let's use epoch time (Jan 1, 1970, UTC)
// by default, dates use timestamp, so:
assertEquals("0", MAPPER.writeValueAsString(DATE_JAN_1_1970_UTC));
}

@Test
public void testSerializationFeatureNoTimestamp() throws IOException
{
String json = MAPPER.writer()
Expand All @@ -106,6 +112,7 @@ public void testSerializationFeatureNoTimestamp() throws IOException
assertEquals(quote("1970-01-01T00:00:00.000Z"), json);
}

@Test
public void testAnnotationAsText() throws IOException
{
ObjectMapper m = jodaMapper();
Expand All @@ -116,12 +123,14 @@ public void testAnnotationAsText() throws IOException
}

// for [datatype-joda#70]
@Test
public void testAsTextNoMilliseconds() throws Exception
{
DateTime value = MAPPER.readValue(quote("2015-07-27T08:11:07-07:00"), DateTime.class);
assertNotNull(value);
}

@Test
public void testCustomPatternStyle() throws IOException
{
// or, using annotations
Expand All @@ -140,6 +149,7 @@ public void testCustomPatternStyle() throws IOException
assertEquals(aposToQuotes("{'date':'1/1/70 12:00 AM'}"), json);
}

@Test
public void testSerializationWithTypeInfo() throws IOException
{
// let's use epoch time (Jan 1, 1970, UTC)
Expand All @@ -155,6 +165,7 @@ public void testSerializationWithTypeInfo() throws IOException
m.writeValueAsString(dt));
}

@Test
public void testIso8601ThroughJoda() throws Exception {
ObjectMapper mapper = new ObjectMapper()
.registerModule(new JodaModule())
Expand All @@ -181,6 +192,7 @@ public void testIso8601ThroughJoda() throws Exception {
// assertEquals(expectedBean.jodaDateTime, actualBean.jodaDateTime);
}

@Test
public void testCustomFormat() throws Exception
{
String STR = "2015-06-19T19:05Z";
Expand All @@ -199,6 +211,7 @@ public void testCustomFormat() throws Exception
assertEquals(inputDate.getMillis(), output.date.getMillis());
}

@Test
public void testWithTimeZoneOverride() throws Exception
{
DateTime date = MAPPER.readValue(quote("2014-01-20T08:59:01.000-0500"),
Expand All @@ -223,6 +236,7 @@ public void testWithTimeZoneOverride() throws Exception
}

// since 2.8
@Test
public void testConfigOverrides() throws Exception
{
ObjectMapper mapper = jodaMapper();
Expand All @@ -244,6 +258,7 @@ public void testConfigOverrides() throws Exception
}

// [datatype-joda#113] (NPE)
@Test
public void testWithoutLeniency() throws Exception
{
ObjectMapper mapper = mapperWithModuleBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package com.fasterxml.jackson.datatype.joda;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.jupiter.api.Assertions.*;

public class JodaMapperTest
{

@Test
public void test_writeDatesAsTimestamps_property()
{
JodaMapper objectUnderTest = new JodaMapper();

objectUnderTest.setWriteDatesAsTimestamps(true);
assertThat(objectUnderTest.getWriteDatesAsTimestamps(), is(true));
assertTrue(objectUnderTest.getWriteDatesAsTimestamps());

objectUnderTest.setWriteDatesAsTimestamps(false);
assertThat(objectUnderTest.getWriteDatesAsTimestamps(), is(false));
assertFalse(objectUnderTest.getWriteDatesAsTimestamps());
}

@Test
Expand All @@ -27,6 +28,6 @@ public void setWriteDatesAsTimestamps_sets_the_WRITE_DATES_AS_TIMESTAMPS_configu

objectUnderTest.setWriteDatesAsTimestamps(true);

assertThat(objectUnderTest.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS), is(true));
assertTrue(objectUnderTest.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.util.Arrays;

import junit.framework.TestCase;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -16,9 +14,9 @@
import org.joda.time.YearMonth;
import org.joda.time.MonthDay;

import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;

public abstract class JodaTestBase extends TestCase
public abstract class JodaTestBase
{
protected static class FormattedInstant {
@JsonFormat(pattern = "dd/MM/yyyy HH_mm_ss_SSS")
Expand Down Expand Up @@ -78,10 +76,7 @@ protected static ObjectMapper mapperWithFailFromEmptyString() {
/**********************************************************
*/

protected void assertEquals(int[] exp, int[] act) {
assertArrayEquals(exp, act);
}


/*
/**********************************************************
/* Helper methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import static org.junit.jupiter.api.Assertions.*;

public class MixedListTest extends JodaTestBase
{
private final ObjectMapper MAPPER = jodaMapper();

@Test
public void testMixedList() throws Exception
{
final Map<String, Object> map = new HashMap<String, Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import java.io.*;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.core.Versioned;

import static org.junit.jupiter.api.Assertions.*;

/**
* Simple verification that version access works.
*/
public class TestVersions extends JodaTestBase
{
@Test
public void testVersions() throws IOException
{
JodaModule m = new JodaModule();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.*;

import static org.junit.jupiter.api.Assertions.*;

// for [datatype-joda#44]
public class TimeZoneTest extends JodaTestBase
{
Expand Down Expand Up @@ -39,6 +43,7 @@ private static interface TypeInfoMixIn {

private final ObjectMapper MAPPER = jodaMapper();

@Test
public void testSimple() throws Exception
{
// First, no zone id included
Expand Down Expand Up @@ -73,6 +78,7 @@ public void testSimple() throws Exception
*
* https://github.com/FasterXML/jackson-datatype-joda/issues/73
*/
@Test
public void testWriteDatesWithZoneIdAndConsistentZoneOffset() throws Exception
{
ObjectWriter w = MAPPER.writer();
Expand All @@ -84,6 +90,7 @@ public void testWriteDatesWithZoneIdAndConsistentZoneOffset() throws Exception
.writeValueAsString(DATE_JAN_1_1970_UTC_IN_AMERICA_LA));
}

@Test
public void testRoundTrip() throws Exception
{
ObjectWriter w = MAPPER.writer()
Expand All @@ -99,14 +106,14 @@ public void testRoundTrip() throws Exception
ObjectMapper mapper = jodaMapper();
mapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
result = mapper.readValue(json, DateTime.class);
assertEquals("Actual timepoints differ", input.getMillis(), result.getMillis());
assertEquals("TimeZones differ", input, result);
assertEquals(input.getMillis(), result.getMillis(), "Actual timepoints differ");
assertEquals(input, result, "TimeZones differ");

// Then timestamp: will not currently (2.6) write out timezone id
json = w.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(input);
result = mapper.readValue(json, DateTime.class);
assertEquals("Actual timepoints differ", input.getMillis(), result.getMillis());
assertEquals(input.getMillis(), result.getMillis(), "Actual timepoints differ");

// .. meaning we can not test this:
// assertEquals("TimeZones differ", input, result);
Expand All @@ -116,6 +123,7 @@ public void testRoundTrip() throws Exception
* Test that de/serializing an ambiguous time (e.g. a 'fall back' DST transition) works and preserves the proper
* instants in time and time zones.
*/
@Test
public void testFallBackTransition() throws Exception
{
DateTime firstOneAmUtc = new DateTime(FALL_BACK_YEAR, FALL_BACK_MONTH, FALL_BACK_DAY, FIRST_FALL_BACK_HOUR, 0, 0,
Expand All @@ -139,13 +147,14 @@ public void testFallBackTransition() throws Exception
DateTime firstRoundTrip = mapper.readValue(firstOneAmStr, DateTime.class);
DateTime secondRoundTrip = mapper.readValue(secondOneAmStr, DateTime.class);

assertEquals("Actual timepoints differ", firstOneAm.getMillis(), firstRoundTrip.getMillis());
assertEquals("TimeZones differ", firstOneAm, firstRoundTrip);
assertEquals(firstOneAm.getMillis(), firstRoundTrip.getMillis(), "Actual timepoints differ");
assertEquals(firstOneAm, firstRoundTrip, "TimeZones differ");

assertEquals("Actual timepoints differ", secondOneAm.getMillis(), secondRoundTrip.getMillis());
assertEquals("TimeZones differ", secondOneAm, secondRoundTrip);
assertEquals(secondOneAm.getMillis(), secondRoundTrip.getMillis(), "Actual timepoints differ");
assertEquals(secondOneAm, secondRoundTrip, "TimeZones differ");
}

@Test
public void testSerializationWithTypeInfo() throws Exception
{
// but if re-configured to include the time zone
Expand Down
Loading

0 comments on commit e5ba92a

Please sign in to comment.