Skip to content

Commit

Permalink
Merge branch '2.18' into 2.19
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 31, 2024
2 parents aab40b8 + b04b573 commit dfda904
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
25 changes: 14 additions & 11 deletions src/test/java/com/fasterxml/jackson/datatype/joda/JodaTestBase.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.fasterxml.jackson.datatype.joda;

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 @@ -8,11 +12,6 @@
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
import com.fasterxml.jackson.databind.json.JsonMapper;

import junit.framework.TestCase;

import java.io.IOException;
import java.util.Arrays;

import org.joda.time.Instant;
import org.joda.time.YearMonth;
import org.joda.time.MonthDay;
Expand Down Expand Up @@ -102,17 +101,21 @@ protected void verifyException(Throwable e, String... matches)
fail("Expected an exception with one of substrings ("+Arrays.asList(matches)+"): got one with message \""+msg+"\"");
}

public String quote(String str) {
public String q(String str) {
return '"'+str+'"';
}

protected String aposToQuotes(String json) {
// @Deprecated
public String quote(String str) {
return q(str);
}

protected String a2q(String json) {
return json.replace("'", "\"");
}

protected <T> T readAndMapFromString(ObjectMapper m, String input, Class<T> cls)
throws IOException
{
return (T) m.readValue("\""+input+"\"", cls);
// @Deprecated
protected String aposToQuotes(String json) {
return a2q(json);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.fasterxml.jackson.datatype.joda.failing;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import com.fasterxml.jackson.datatype.joda.JodaTestBase;

import org.joda.time.*;

// [datatype-joda#146]: disable overwriting of timezone
public class DateTimeSerializationWithOffsets146Test extends JodaTestBase
{
private final ObjectMapper MAPPER = jodaMapper();
{
MAPPER.enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
MAPPER.enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS);
}

// [datatype-joda#146]
public void testLocalDateSer() throws Exception
{
final ObjectMapper MAPPER = mapperWithModuleBuilder()
.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
.disable(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE)
.build();
final String inputStr = "2024-12-01T00:00:00+02:00";

DateTime dateTime = DateTime.parse(inputStr);
assertEquals(q(inputStr), MAPPER.writeValueAsString(dateTime));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void testLocalDateSer() throws IOException

// but we can force it to be a String as well (note: here we assume this is
// dynamically changeable)
assertEquals(quote("2001-05-25"),
assertEquals(q("2001-05-25"),
WRITER.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).writeValueAsString(date));

// We can also configure beans to not include empty values. In this case,
Expand Down Expand Up @@ -96,7 +96,7 @@ public void testLocalTimeSer() throws IOException
// dynamically changeable)
ObjectMapper mapper = jodaMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
assertEquals(quote("13:20:54.000"), mapper.writeValueAsString(date));
assertEquals(q("13:20:54.000"), mapper.writeValueAsString(date));

}

Expand All @@ -113,9 +113,9 @@ public void testLocalTimeSerWithFormatOverride() throws IOException
.addModule(testModule)
.build();

assertEquals(quote("13:20"), mapper.writeValueAsString(date));
assertEquals(q("13:20"), mapper.writeValueAsString(date));

assertEquals(aposToQuotes("{'contents':'13:20'}"), mapper.writeValueAsString(new Container<>(date)));
assertEquals(a2q("{'contents':'13:20'}"), mapper.writeValueAsString(new Container<>(date)));

}

Expand All @@ -132,7 +132,6 @@ public void testLocalTimeSerWithTypeInfo() throws IOException
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
assertEquals("[\"org.joda.time.LocalTime\",\"13:20:54.000\"]",
mapper.writeValueAsString(date));

}

/*
Expand All @@ -151,7 +150,7 @@ public void testLocalDateTimeSer() throws IOException
// dynamically changeable)
ObjectMapper mapper = jodaMapper();
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
assertEquals(quote("2001-05-25T10:15:30.037"), mapper.writeValueAsString(date));
assertEquals(q("2001-05-25T10:15:30.037"), mapper.writeValueAsString(date));
}

public void testLocalDateTimeSerWithTypeInfo() throws IOException
Expand All @@ -177,7 +176,7 @@ public void testLocalDateTimeSerWithTypeInfo() throws IOException
public void testPeriodSer() throws IOException
{
Period in = new Period(1, 2, 3, 4);
assertEquals(quote("PT1H2M3.004S"), MAPPER.writeValueAsString(in));
assertEquals(q("PT1H2M3.004S"), MAPPER.writeValueAsString(in));
}

public void testPeriodSerWithTypeInfo() throws IOException
Expand All @@ -200,7 +199,7 @@ public void testDurationSer() throws IOException
String json = MAPPER.writeValueAsString(d);
assertEquals("3123422", json);

assertEquals(quote("PT3123.422S"), MAPPER.writer()
assertEquals(q("PT3123.422S"), MAPPER.writer()
.without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)
.writeValueAsString(d));
}
Expand All @@ -220,30 +219,30 @@ public void testMonthDaySer() throws Exception
MonthDay monthDay = new MonthDay(7, 23);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(monthDay);
assertEquals(quote("--07-23"), json);
assertEquals(q("--07-23"), json);
}

public void testCustomMonthDaySer() throws Exception
{
MonthDay monthDay = new MonthDay(7, 23);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(new FormattedMonthDay(monthDay));
assertEquals(aposToQuotes("{'value':'07:23'}"), json);
assertEquals(a2q("{'value':'07:23'}"), json);
}

public void testYearMonthSer() throws Exception
{
YearMonth yearMonth = new YearMonth(2013, 8);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(yearMonth);
assertEquals(quote("2013-08"), json);
assertEquals(q("2013-08"), json);
}

public void testCustomYearMonthSer() throws Exception
{
YearMonth yearMonth = new YearMonth(2013, 8);
ObjectMapper mapper = jodaMapper();
String json = mapper.writeValueAsString(new FormattedYearMonth(yearMonth));
assertEquals(aposToQuotes("{'value':'2013/08'}"), json);
assertEquals(a2q("{'value':'2013/08'}"), json);
}
}

0 comments on commit dfda904

Please sign in to comment.