Skip to content

Commit

Permalink
Merge branch '2.19'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 31, 2024
2 parents 5694839 + 0659f2e commit e3f9ff9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ Joda (https://www.joda.org/joda-time/) data types.
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>tools/jackson/**/failing/*.java</exclude>
</excludes>
</configuration>
</plugin>
<!-- 19-Mar-2019, tatu: Add rudimentary JDK9+ module info. To build with JDK 8
will have to use `moduleInfoFile` as anything else requires JDK 9+
-->
Expand Down
22 changes: 12 additions & 10 deletions src/test/java/tools/jackson/datatype/joda/JodaTestBase.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package tools.jackson.datatype.joda;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.TimeZone;

import junit.framework.TestCase;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.CoercionAction;
import tools.jackson.databind.cfg.CoercionInputShape;
import tools.jackson.databind.json.JsonMapper;

import junit.framework.TestCase;

import org.joda.time.Instant;
import org.joda.time.YearMonth;
import org.joda.time.MonthDay;
Expand Down Expand Up @@ -123,17 +121,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,25 @@
package tools.jackson.datatype.joda.failing;

import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.SerializationFeature;

import tools.jackson.datatype.joda.JodaTestBase;

import org.joda.time.*;

// [datatype-joda#146]: disable overwriting of timezone
public class DateTimeSerializationWithOffsets146Test extends JodaTestBase
{
// [datatype-joda#146]
public void testDateTimeSerializationWithOffsets146() 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 @@ -57,7 +57,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 @@ -103,8 +103,7 @@ public void testLocalTimeSer() throws IOException
ObjectMapper mapper = mapperWithModuleBuilder()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.build();
assertEquals(quote("13:20:54.000"), mapper.writeValueAsString(date));

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

public void testLocalTimeSerWithFormatOverride() throws IOException
Expand All @@ -120,9 +119,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 @@ -140,7 +139,6 @@ public void testLocalTimeSerWithTypeInfo() throws IOException
.build();
assertEquals("[\"org.joda.time.LocalTime\",\"13:20:54.000\"]",
mapper.writeValueAsString(date));

}

/*
Expand All @@ -160,7 +158,7 @@ public void testLocalDateTimeSer() throws IOException
ObjectMapper mapper = mapperWithModuleBuilder()
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.build();
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 @@ -187,7 +185,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 @@ -211,7 +209,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 @@ -231,24 +229,24 @@ public void testMonthDaySer() throws Exception
{
MonthDay monthDay = new MonthDay(7, 23);
String json = MAPPER.writeValueAsString(monthDay);
assertEquals(quote("--07-23"), json);
assertEquals(q("--07-23"), json);
}

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

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

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

0 comments on commit e3f9ff9

Please sign in to comment.