Skip to content

Commit

Permalink
Robustify tests wrt SerializationFeature.WRITE_DATES_AS_TIMESTAMPS
Browse files Browse the repository at this point in the history
…defaults soon changing
  • Loading branch information
cowtowncoder committed Feb 5, 2025
1 parent e62fcd7 commit 42d45f2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/test/java/tools/jackson/datatype/joda/AnnotationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

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

public class AnnotationTest extends DateTimeTest
public class AnnotationTest extends JodaTestBase
{
static class AClass{
public static class AClass
{
@JsonSerialize(using = DateTimeSerializer.class)
@JsonDeserialize(using = DateTimeDeserializer.class)
private DateTime createdOn = DateTime.now(DateTimeZone.UTC);
Expand Down
23 changes: 13 additions & 10 deletions src/test/java/tools/jackson/datatype/joda/DateTimeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

public class DateTimeTest extends JodaTestBase
{
static class DateAsText {
public static class DateAsText {
@JsonFormat(shape=JsonFormat.Shape.STRING)
public DateTime date;

Expand All @@ -30,14 +30,14 @@ public DateAsText(DateTime d) {
}
}

static class DateTimeWrapper {
public static class DateTimeWrapper {
public DateTime value;

public DateTimeWrapper(DateTime v) { value = v; }
protected DateTimeWrapper() { }
public DateTimeWrapper() { }
}

static class CustomDate {
public static class CustomDate {
// note: 'SS' means 'short representation'
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="SS", locale="en")
public DateTime date;
Expand All @@ -47,7 +47,7 @@ public CustomDate(DateTime d) {
}
}

static class EuroDate {
public static class EuroDate {
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd.MM.YYYY' 'HH:mm")
public DateTime date;

Expand All @@ -58,7 +58,7 @@ public EuroDate(DateTime d) {
}

@JsonPropertyOrder({"jodaDateTime", "javaUtilDate" })
static class Beanie {
public static class Beanie {
public final DateTime jodaDateTime;
public final java.util.Date javaUtilDate;
@JsonCreator
Expand All @@ -69,13 +69,13 @@ public Beanie(@JsonProperty("jodaDateTime") DateTime jodaDateTime,
}
}

static class FormattedDateTime {
public static class FormattedDateTime {
@JsonFormat(timezone="EST")
public DateTime dateTime;
}

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.WRAPPER_ARRAY, property = "@class")
private static interface TypeInfoMixIn {
public interface TypeInfoMixIn {
}

/*
Expand All @@ -102,7 +102,9 @@ 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));
assertEquals("0", MAPPER.writer()
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(DATE_JAN_1_1970_UTC));
}

@Test
Expand Down Expand Up @@ -158,7 +160,8 @@ public void testSerializationWithTypeInfo() throws IOException
// let's use epoch time (Jan 1, 1970, UTC)
DateTime dt = new DateTime(0L, DateTimeZone.UTC);
// by default, dates use timestamp, so:
assertEquals("0", MAPPER.writeValueAsString(dt));
assertEquals("0", MAPPER.writer()
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).writeValueAsString(dt));

// but if re-configured, as regular ISO-8601 string
ObjectMapper m = mapperWithModuleBuilder()
Expand Down
5 changes: 4 additions & 1 deletion src/test/java/tools/jackson/datatype/joda/MixedListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import tools.jackson.core.type.TypeReference;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationFeature;

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

Expand All @@ -24,7 +25,9 @@ public void testMixedList() throws Exception
map.put("A", dt);
map.put("B", 0);

final String json = MAPPER.writeValueAsString(map);
final String json = MAPPER.writer()
.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.writeValueAsString(map);
// by default, timestamps should come out as longs...

final Map<String, Object> result = MAPPER.readValue(json,
Expand Down

0 comments on commit 42d45f2

Please sign in to comment.