Skip to content

Commit

Permalink
Test case for FasterXML#1154
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Mar 9, 2016
1 parent ae5695c commit c5fba8a
Showing 1 changed file with 77 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,35 @@ public CalendarAsStringBean(long l) {
}
}

static class DateAsDefaultBean {
public Date date;
public DateAsDefaultBean(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithEmptyJsonFormat {
@JsonFormat
public Date date;
public DateAsDefaultBeanWithEmptyJsonFormat(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithPattern {
@JsonFormat(pattern="yyyy-MM-dd")
public Date date;
public DateAsDefaultBeanWithPattern(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithLocale {
@JsonFormat(locale = "fr")
public Date date;
public DateAsDefaultBeanWithLocale(long l) { date = new java.util.Date(l); }
}

static class DateAsDefaultBeanWithTimezone {
@JsonFormat(timezone="CET")
public Date date;
public DateAsDefaultBeanWithTimezone(long l) { date = new java.util.Date(l); }
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -229,5 +258,53 @@ public void testWithTimeZoneOverride() throws Exception
json = w.writeValueAsString(new Date(0));
assertEquals(quote("1969-12-31/19:00 EST"), json);
}

/**
* Test to ensure that the default shape is correctly inferred as string or numeric,
* when this shape is not explicitly set with a <code>@JsonFormat</code> annotation
*/
public void testDateDefaultShape() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
// No @JsonFormat => default to user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
String json = mapper.writeValueAsString(new DateAsDefaultBean(0L));
assertEquals(aposToQuotes("{'date':0}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBean(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);

// Empty @JsonFormat => default to user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithEmptyJsonFormat(0L));
assertEquals(aposToQuotes("{'date':0}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithEmptyJsonFormat(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);

// @JsonFormat with Shape.ANY and pattern => STRING shape, regardless of user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithPattern(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01'}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithPattern(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01'}"), json);

// @JsonFormat with Shape.ANY and locale => STRING shape, regardless of user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithLocale(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T00:00:00.000+0000'}"), json);

// @JsonFormat with Shape.ANY and timezone => STRING shape, regardless of user config
mapper.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithTimezone(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T01:00:00.000+0100'}"), json);
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
json = mapper.writeValueAsString(new DateAsDefaultBeanWithTimezone(0L));
assertEquals(aposToQuotes("{'date':'1970-01-01T01:00:00.000+0100'}"), json);
}
}

0 comments on commit c5fba8a

Please sign in to comment.