diff --git a/.github/workflows/dep_build_v2.yml b/.github/workflows/dep_build_v2.yml index 626cb550..f80d3222 100644 --- a/.github/workflows/dep_build_v2.yml +++ b/.github/workflows/dep_build_v2.yml @@ -10,7 +10,7 @@ permissions: jobs: build: - runs-on: 'ubuntu-22.04' + runs-on: ubuntu-latest strategy: fail-fast: false matrix: @@ -19,6 +19,8 @@ jobs: JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" steps: - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 + with: + ref: 2.x - name: Set up JDK uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1 with: diff --git a/.github/workflows/dep_build_v3.yml b/.github/workflows/dep_build_v3.yml index cf461a51..8f56e555 100644 --- a/.github/workflows/dep_build_v3.yml +++ b/.github/workflows/dep_build_v3.yml @@ -14,13 +14,13 @@ jobs: strategy: fail-fast: false matrix: - java_version: ['8', '17', '21'] + java_version: ['17', '21'] env: JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" steps: - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 with: - ref: master + ref: 3.x - name: Set up JDK uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9 # v4.2.1 with: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 23e1c1ef..778ed7bf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,7 @@ name: Build and Deploy Snapshot on: push: + branches: ['2.*'] paths-ignore: - "README.md" - "release-notes/*" @@ -14,17 +15,20 @@ permissions: jobs: build: - runs-on: 'ubuntu-latest' + runs-on: 'ubuntu-24.04' strategy: fail-fast: false matrix: java_version: ['8', '11', '17', '21'] + include: + - java_version: '8' + release_build: 'R' env: JAVA_OPTS: "-XX:+TieredCompilation -XX:TieredStopAtLevel=1" steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - name: Set up JDK - uses: actions/setup-java@v4 + uses: actions/setup-java@8df1039502a15bceb9433410b1a100fbe190c53b # v4.5.0 with: distribution: 'temurin' java-version: ${{ matrix.java_version }} @@ -41,19 +45,19 @@ jobs: id: projectVersion run: echo "version=$(./mvnw org.apache.maven.plugins:maven-help-plugin:3.5.1:evaluate -DforceStdout -Dexpression=project.version -q)" >> $GITHUB_OUTPUT - name: Deploy snapshot - if: ${{ github.event_name != 'pull_request' && matrix.java_version == '8' && endsWith(steps.projectVersion.outputs.version, '-SNAPSHOT') }} + if: ${{ matrix.release_build && github.event_name != 'pull_request' && endsWith(steps.projectVersion.outputs.version, '-SNAPSHOT') }} env: CI_DEPLOY_USERNAME: ${{ secrets.CENTRAL_DEPLOY_USERNAME }} CI_DEPLOY_PASSWORD: ${{ secrets.CENTRAL_DEPLOY_PASSWORD }} # MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} run: ./mvnw -B -q -ff -DskipTests -ntp source:jar deploy - name: Generate code coverage - if: ${{ github.event_name != 'pull_request' && matrix.java_version == '8' }} + if: ${{ matrix.release_build && github.event_name != 'pull_request' }} run: ./mvnw -B -q -ff -ntp test - name: Publish code coverage - if: ${{ github.event_name != 'pull_request' && matrix.java_version == '8' }} - uses: codecov/codecov-action@v4 + if: ${{ matrix.release_build && github.event_name != 'pull_request' }} + uses: codecov/codecov-action@015f24e6818733317a2da2edd6290ab26238649a # v5.0.7 with: token: ${{ secrets.CODECOV_TOKEN }} - file: ./target/site/jacoco/jacoco.xml + files: ./target/site/jacoco/jacoco.xml flags: unittests diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties index 642d572c..b9b1153a 100644 --- a/.mvn/wrapper/maven-wrapper.properties +++ b/.mvn/wrapper/maven-wrapper.properties @@ -1,2 +1,18 @@ -distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.3/apache-maven-3.6.3-bin.zip -wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.3.2/maven-wrapper-3.3.2.jar diff --git a/README.md b/README.md index dee05669..1bbeef26 100644 --- a/README.md +++ b/README.md @@ -20,15 +20,11 @@ When used with Jackson 2.x, Java 8 support is provided via 3 separate modules: all of which are built from this repository, and accessed and used as separate Jackson modules (with separate Maven artifacts). -### Jackson 3.0 +### Jackson 3.x -Jackson 3.0 changes things as it requires Java 8 to work and can thereby directly supported features. +Jackson 3.0 changes things as it requires Java 17 to work and can thereby directly supported features. -Because of this `parameter-names` and `datatypes` modules are merged into `jackson-databind` -and need not be registered; `datetime` module (`JavaTimeModule`) remains separate module due to its size -and configurability options. - -So you will only need to separately add "Java 8 Date/time" module (see above for description) +Because of this, all 3 modules are merged into `jackson-databind` and need not be registered (as of Jackson `3.0.0`) ## License @@ -36,7 +32,7 @@ All modules are licensed under [Apache License 2.0](http://www.apache.org/licens ## Status -[![Build Status](https://travis-ci.org/FasterXML/jackson-modules-java8.svg)](https://travis-ci.org/FasterXML/jackson-modules-java8) +[![Build status (github)](https://github.com/FasterXML/jackson-modules-java8/actions/workflows/main.yml/badge.svg)](https://github.com/FasterXML/jackson-modules-java8/actions/workflows/main.yml) [![Tidelift](https://tidelift.com/badges/package/maven/com.fasterxml.jackson.datatype:jackson-datatype-jsr310)](https://tidelift.com/subscription/pkg/maven-com-fasterxml-jackson-datatype-jackson-datatype-jsr310?utm_source=maven-com-fasterxml-jackson-datatype-jackson-datatype-jsr310&utm_medium=referral&utm_campaign=readme) ## Usage @@ -46,19 +42,19 @@ All modules are licensed under [Apache License 2.0](http://www.apache.org/licens To include modules, you use some or all of: ```xml - + com.fasterxml.jackson.module jackson-module-parameter-names - + com.fasterxml.jackson.datatype jackson-datatype-jsr310 - + com.fasterxml.jackson.datatype jackson-datatype-jdk8 @@ -132,4 +128,4 @@ Following developers have committer access to this project. ## More -See [Wiki](../../wiki) for more information (javadocs). +See [Wiki](../../wiki) for more information. diff --git a/datatypes/pom.xml b/datatypes/pom.xml index b8ddd70d..c1c67e2c 100644 --- a/datatypes/pom.xml +++ b/datatypes/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.18.11-SNAPSHOT + 2.23.0-SNAPSHOT com.fasterxml.jackson.datatype jackson-datatype-jdk8 @@ -35,22 +35,16 @@ JDK 8 data types. org.apache.maven.plugins ${version.plugin.surefire} maven-surefire-plugin - - - com/fasterxml/jackson/failing/*.java - - com.google.code.maven-replacer-plugin replacer - - - process-packageVersion - generate-sources - - + + + + org.cyclonedx + cyclonedx-maven-plugin + + + org.gradlex + gradle-module-metadata-maven-plugin + diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ContextualOptional17Test.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ContextualOptional17Test.java index 37f9e448..64239a2e 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ContextualOptional17Test.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ContextualOptional17Test.java @@ -5,10 +5,14 @@ import java.util.Optional; import java.util.TimeZone; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonPropertyOrder; import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class ContextualOptional17Test extends ModuleTestBase { // [datatypes-java8#17] @@ -30,6 +34,7 @@ static class ContextualOptionals /********************************************************** */ + @Test public void testContextualOptionals() throws Exception { final ObjectMapper mapper = mapperWithModule(); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/CreatorTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/CreatorTest.java index 66f41e80..dd33b8cf 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/CreatorTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/CreatorTest.java @@ -2,11 +2,15 @@ import java.util.Optional; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; +import static org.junit.jupiter.api.Assertions.*; + public class CreatorTest extends ModuleTestBase { static class CreatorWithOptionalStrings @@ -35,6 +39,7 @@ public CreatorWithOptionalStrings(@JsonProperty("a") Optional a, * Test to ensure that creator parameters use defaulting * (introduced in Jackson 2.6) */ + @Test public void testCreatorWithOptionalDefault() throws Exception { CreatorWithOptionalStrings bean = MAPPER.readValue( @@ -47,6 +52,7 @@ public void testCreatorWithOptionalDefault() throws Exception assertEquals("foo", bean.a.get()); } + @Test public void testCreatorWithOptionalAbsentAsNull() throws Exception { Jdk8Module module = new Jdk8Module() diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/DoubleStreamSerializerTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/DoubleStreamSerializerTest.java index db29ce40..9b845777 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/DoubleStreamSerializerTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/DoubleStreamSerializerTest.java @@ -1,15 +1,15 @@ package com.fasterxml.jackson.datatype.jdk8; -import static org.junit.Assert.*; - import java.io.IOException; import java.util.stream.DoubleStream; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.*; + @SuppressWarnings({ "unqualified-field-access", "javadoc" }) public class DoubleStreamSerializerTest extends StreamTestBase { @@ -21,7 +21,7 @@ public class DoubleStreamSerializerTest extends StreamTestBase { final String exceptionMessage = "DoubleStream peek threw"; - @Before + @BeforeEach public void setUp() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module()); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/IntStreamSerializerTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/IntStreamSerializerTest.java index bc06a70e..09360df5 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/IntStreamSerializerTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/IntStreamSerializerTest.java @@ -1,11 +1,11 @@ package com.fasterxml.jackson.datatype.jdk8; -import static org.junit.Assert.*; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.util.stream.IntStream; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; @SuppressWarnings({ "unqualified-field-access", "javadoc" }) public class IntStreamSerializerTest extends StreamTestBase { diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/JDKSerializabilityTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/JDKSerializabilityTest.java index 195bec9e..f48d86d6 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/JDKSerializabilityTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/JDKSerializabilityTest.java @@ -3,10 +3,12 @@ import java.io.*; import java.util.Optional; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + public class JDKSerializabilityTest extends ModuleTestBase { static class BooleanBean { diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/Java8OptionalUnwrappedTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/Java8OptionalUnwrappedTest.java index 1c4639cb..35804af1 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/Java8OptionalUnwrappedTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/Java8OptionalUnwrappedTest.java @@ -3,6 +3,8 @@ import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.BeanProperty; import com.fasterxml.jackson.databind.JavaType; @@ -11,6 +13,8 @@ import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor; import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider; +import static org.junit.jupiter.api.Assertions.*; + public class Java8OptionalUnwrappedTest extends ModuleTestBase { static class Child { @@ -46,6 +50,7 @@ static class Bean2 { public String name; } + @Test public void testUntypedWithOptionalsNotNulls() throws Exception { final ObjectMapper mapper = mapperWithModule(false); @@ -55,6 +60,7 @@ public void testUntypedWithOptionalsNotNulls() throws Exception } // for [datatype-jdk8#20] + @Test public void testShouldSerializeUnwrappedOptional() throws Exception { final ObjectMapper mapper = mapperWithModule(false); @@ -63,6 +69,7 @@ public void testShouldSerializeUnwrappedOptional() throws Exception { } // for [datatype-jdk8#26] + @Test public void testPropogatePrefixToSchema() throws Exception { final ObjectMapper mapper = mapperWithModule(false); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/LongStreamSerializerTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/LongStreamSerializerTest.java index 1d483a54..fc70b207 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/LongStreamSerializerTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/LongStreamSerializerTest.java @@ -1,11 +1,11 @@ package com.fasterxml.jackson.datatype.jdk8; -import static org.junit.Assert.*; - import java.io.IOException; import java.util.stream.LongStream; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; @SuppressWarnings({ "unqualified-field-access", "javadoc" }) public class LongStreamSerializerTest extends StreamTestBase { diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ModuleTestBase.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ModuleTestBase.java index f72498c6..c0240db6 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ModuleTestBase.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/ModuleTestBase.java @@ -8,7 +8,9 @@ import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; -public abstract class ModuleTestBase extends junit.framework.TestCase +import static org.junit.jupiter.api.Assertions.fail; + +public abstract class ModuleTestBase { public static class NoCheckSubTypeValidator extends PolymorphicTypeValidator.Base diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBasicTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBasicTest.java index 29eaae6e..ed883906 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBasicTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBasicTest.java @@ -2,6 +2,8 @@ import java.util.*; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonTypeInfo.As; @@ -9,6 +11,8 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.*; + public class OptionalBasicTest extends ModuleTestBase { public static final class OptionalData { @@ -59,6 +63,7 @@ public static class ContainedImpl implements Contained { } private final ObjectMapper MAPPER = mapperWithModule(); + @Test public void testOptionalTypeResolution() throws Exception { // With 2.6, we need to recognize it as ReferenceType JavaType t = MAPPER.constructType(Optional.class); @@ -67,6 +72,7 @@ public void testOptionalTypeResolution() throws Exception { assertTrue(t.isReferenceType()); } + @Test public void testDeserAbsent() throws Exception { Optional value = MAPPER.readValue("null", new TypeReference>() { @@ -74,6 +80,7 @@ public void testDeserAbsent() throws Exception { assertFalse(value.isPresent()); } + @Test public void testDeserSimpleString() throws Exception { Optional value = MAPPER.readValue("\"simpleString\"", new TypeReference>() { @@ -82,6 +89,7 @@ public void testDeserSimpleString() throws Exception { assertEquals("simpleString", value.get()); } + @Test public void testDeserInsideObject() throws Exception { OptionalData data = MAPPER.readValue("{\"myString\":\"simpleString\"}", OptionalData.class); @@ -89,6 +97,7 @@ public void testDeserInsideObject() throws Exception { assertEquals("simpleString", data.myString.get()); } + @Test public void testDeserComplexObject() throws Exception { TypeReference> type = new TypeReference>() { }; @@ -99,6 +108,7 @@ public void testDeserComplexObject() throws Exception { assertEquals("simpleString", data.get().myString.get()); } + @Test public void testDeserGeneric() throws Exception { TypeReference>> type = new TypeReference>>() { }; @@ -109,16 +119,19 @@ public void testDeserGeneric() throws Exception { assertEquals("simpleString", data.get().myData.get()); } + @Test public void testSerAbsent() throws Exception { String value = MAPPER.writeValueAsString(Optional.empty()); assertEquals("null", value); } + @Test public void testSerSimpleString() throws Exception { String value = MAPPER.writeValueAsString(Optional.of("simpleString")); assertEquals("\"simpleString\"", value); } + @Test public void testSerInsideObject() throws Exception { OptionalData data = new OptionalData(); data.myString = Optional.of("simpleString"); @@ -126,6 +139,7 @@ public void testSerInsideObject() throws Exception { assertEquals("{\"myString\":\"simpleString\"}", value); } + @Test public void testSerComplexObject() throws Exception { OptionalData data = new OptionalData(); data.myString = Optional.of("simpleString"); @@ -133,6 +147,7 @@ public void testSerComplexObject() throws Exception { assertEquals("{\"myString\":\"simpleString\"}", value); } + @Test public void testSerGeneric() throws Exception { OptionalGenericData data = new OptionalGenericData(); data.myData = Optional.of("simpleString"); @@ -140,6 +155,7 @@ public void testSerGeneric() throws Exception { assertEquals("{\"myData\":\"simpleString\"}", value); } + @Test public void testSerNonNull() throws Exception { OptionalData data = new OptionalData(); data.myString = Optional.empty(); @@ -149,6 +165,7 @@ public void testSerNonNull() throws Exception { assertEquals("{}", value); } + @Test public void testSerOptDefault() throws Exception { OptionalData data = new OptionalData(); data.myString = Optional.empty(); @@ -157,6 +174,7 @@ public void testSerOptDefault() throws Exception { assertEquals("{\"myString\":null}", value); } + @Test public void testSerOptNull() throws Exception { OptionalData data = new OptionalData(); data.myString = null; @@ -166,6 +184,7 @@ public void testSerOptNull() throws Exception { } @SuppressWarnings("deprecation") + @Test public void testSerOptDisableAsNull() throws Exception { final OptionalData data = new OptionalData(); data.myString = Optional.empty(); @@ -187,6 +206,7 @@ public void testSerOptDisableAsNull() throws Exception { assertEquals("{}", mapper.writeValueAsString(data)); } + @Test public void testSerOptNonEmpty() throws Exception { OptionalData data = new OptionalData(); data.myString = null; @@ -195,6 +215,7 @@ public void testSerOptNonEmpty() throws Exception { assertEquals("{}", value); } + @Test public void testWithTypingEnabled() throws Exception { final ObjectMapper objectMapper = mapperWithModule(); // ENABLE TYPING @@ -210,6 +231,7 @@ public void testWithTypingEnabled() throws Exception { assertEquals(myData.myString, deserializedMyData.myString); } + @Test public void testObjectId() throws Exception { final Unit input = new Unit(); input.link(input); @@ -222,6 +244,7 @@ public void testObjectId() throws Exception { assertSame(result, base); } + @Test public void testOptionalCollection() throws Exception { TypeReference>> typeReference = new TypeReference>>() { @@ -238,10 +261,11 @@ public void testOptionalCollection() throws Exception { List> result = MAPPER.readValue(str, typeReference); assertEquals(list.size(), result.size()); for (int i = 0; i < list.size(); ++i) { - assertEquals("Entry #" + i, list.get(i), result.get(i)); + assertEquals(list.get(i), result.get(i), "Entry #" + i); } } + @Test public void testPolymorphic() throws Exception { final Container dto = new Container(); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBooleanTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBooleanTest.java index 56ac1fb4..26475cb3 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBooleanTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalBooleanTest.java @@ -2,8 +2,12 @@ import java.util.Optional; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.*; + public class OptionalBooleanTest extends ModuleTestBase { static class BooleanBean { @@ -18,6 +22,7 @@ public BooleanBean(Boolean b) { private final ObjectMapper MAPPER = mapperWithModule(); // for [datatype-jdk8#23] + @Test public void testBoolean() throws Exception { // First, serialization diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalConverterTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalConverterTest.java index f01e7e42..fb508da9 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalConverterTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalConverterTest.java @@ -2,11 +2,15 @@ import java.util.Optional; +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; import com.fasterxml.jackson.databind.util.StdConverter; +import static org.junit.jupiter.api.Assertions.*; + // [modules-java#294]: Optional + converters public class OptionalConverterTest extends ModuleTestBase { @@ -56,6 +60,7 @@ public PointReferenceBean(int x, int y) { private final ObjectMapper MAPPER = mapperWithModule(); // [modules-java#294]: Optional + converters, deser + @Test public void testDeserializeOptionalConverting() throws Exception { PointReferenceBean w = MAPPER.readerFor(PointReferenceBean.class) .readValue("{\"opt\": [1,2]}"); @@ -68,6 +73,7 @@ public void testDeserializeOptionalConverting() throws Exception { } // [modules-java#294]: Optional + converters, ser + @Test public void testSerializeOptionalConverting() throws Exception { assertEquals("{\"opt\":[3,4]}", MAPPER.writeValueAsString(new PointReferenceBean(3, 4))); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalMergeTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalMergeTest.java index cd852ee7..bea56026 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalMergeTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalMergeTest.java @@ -4,9 +4,13 @@ import java.util.List; import java.util.Optional; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonMerge; import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.*; + public class OptionalMergeTest extends ModuleTestBase { // [modules-java8#214] @@ -18,6 +22,7 @@ static class OptionalListWrapper { private final ObjectMapper MAPPER = mapperWithModule(); // [modules-java8#214]: ReferenceType of List, merge + @Test public void testMergeToListViaRef() throws Exception { OptionalListWrapper base = MAPPER.readValue(a2q("{'list':['a']}"), diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalNumbersTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalNumbersTest.java index b5c6849c..667d0b6d 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalNumbersTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalNumbersTest.java @@ -4,13 +4,14 @@ import java.util.OptionalInt; import java.util.OptionalLong; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.exc.MismatchedInputException; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.*; public class OptionalNumbersTest extends ModuleTestBase { @@ -52,12 +53,14 @@ static class OptionalDoubleBean { /********************************************************** */ + @Test public void testOptionalIntAbsent() throws Exception { String json = MAPPER.writeValueAsString(OptionalInt.empty()); assertFalse(MAPPER.readValue(json, OptionalInt.class).isPresent()); } + @Test public void testOptionalIntInArrayAbsent() throws Exception { OptionalInt[] ints = MAPPER.readValue("[null]", OptionalInt[].class); @@ -66,11 +69,13 @@ public void testOptionalIntInArrayAbsent() throws Exception assertFalse(ints[0].isPresent()); } + @Test public void testOptionalIntPresent() throws Exception { assertEquals(5, MAPPER.readValue(MAPPER.writeValueAsBytes(OptionalInt.of(5)), OptionalInt.class).getAsInt()); } + @Test public void testOptionalIntCoerceFromString() throws Exception { OptionalInt opt = MAPPER.readValue(q("123"), OptionalInt.class); @@ -95,11 +100,13 @@ public void testOptionalIntCoerceFromString() throws Exception /********************************************************** */ + @Test public void testOptionalLongAbsent() throws Exception { assertFalse(MAPPER.readValue(MAPPER.writeValueAsBytes(OptionalLong.empty()), OptionalLong.class).isPresent()); } + @Test public void testOptionalLongInArrayAbsent() throws Exception { OptionalLong[] arr = MAPPER.readValue("[null]", OptionalLong[].class); @@ -107,12 +114,14 @@ public void testOptionalLongInArrayAbsent() throws Exception assertNotNull(arr[0]); assertFalse(arr[0].isPresent()); } - + + @Test public void testOptionalLongPresent() throws Exception { assertEquals(Long.MAX_VALUE, MAPPER.readValue(MAPPER.writeValueAsBytes(OptionalLong.of(Long.MAX_VALUE)), OptionalLong.class).getAsLong()); } + @Test public void testOptionalLongCoerceFromString() throws Exception { OptionalLong opt = MAPPER.readValue(q("123"), OptionalLong.class); @@ -132,7 +141,8 @@ public void testOptionalLongCoerceFromString() throws Exception assertNotNull(bean.value); assertEquals(19L, bean.value.getAsLong()); } - + + @Test public void testOptionalLongSerializeFilter() throws Exception { ObjectMapper mapper = mapperWithModule() @@ -158,11 +168,13 @@ public void testOptionalLongSerializeFilter() throws Exception /********************************************************** */ + @Test public void testOptionalDoubleAbsent() throws Exception { assertFalse(MAPPER.readValue(MAPPER.writeValueAsBytes(OptionalInt.empty()), OptionalInt.class).isPresent()); } + @Test public void testOptionalDoubleInArrayAbsent() throws Exception { OptionalDouble[] arr = MAPPER.readValue("[null]", OptionalDouble[].class); @@ -171,11 +183,13 @@ public void testOptionalDoubleInArrayAbsent() throws Exception assertFalse(arr[0].isPresent()); } + @Test public void testOptionalDoublePresent() throws Exception { assertEquals(Double.MIN_VALUE, MAPPER.readValue(MAPPER.writeValueAsBytes(OptionalDouble.of(Double.MIN_VALUE)), OptionalDouble.class).getAsDouble()); } + @Test public void testOptionalDoubleCoerceFromString() throws Exception { OptionalDouble opt = MAPPER.readValue(q("0.25"), OptionalDouble.class); @@ -196,6 +210,7 @@ public void testOptionalDoubleCoerceFromString() throws Exception assertEquals(0.5, bean.value.getAsDouble()); } + @Test public void testOptionalDoubleInArraySpecialValues() throws Exception { OptionalDouble[] actual = MAPPER.readValue( @@ -212,6 +227,7 @@ public void testOptionalDoubleInArraySpecialValues() throws Exception assertArrayEquals(expected, actual); } + @Test public void testOptionalDoubleInArraySpecialValuesWithoutCoercion() throws Exception { OptionalDouble[] actual = MAPPER_WITHOUT_COERCION.readValue( @@ -227,12 +243,14 @@ public void testOptionalDoubleInArraySpecialValuesWithoutCoercion() throws Excep assertArrayEquals(expected, actual); } + @Test public void testQuotedOptionalDoubleWithoutCoercion() { assertThrows(MismatchedInputException.class, () -> MAPPER_WITHOUT_COERCION.readValue(a2q("['1']"), OptionalDouble[].class)); } + @Test public void testOptionalDoubleBeanSpecialValuesWithoutCoercion_null() throws Exception { OptionalDoubleBean bean = MAPPER_WITHOUT_COERCION.readValue( @@ -240,6 +258,7 @@ public void testOptionalDoubleBeanSpecialValuesWithoutCoercion_null() throws Exc assertEquals(OptionalDouble.empty(), bean.value); } + @Test public void testOptionalDoubleBeanSpecialValuesWithoutCoercion_nan() throws Exception { OptionalDoubleBean bean = MAPPER_WITHOUT_COERCION.readValue( @@ -247,6 +266,7 @@ public void testOptionalDoubleBeanSpecialValuesWithoutCoercion_nan() throws Exce assertEquals(OptionalDouble.of(Double.NaN), bean.value); } + @Test public void testOptionalDoubleBeanSpecialValuesWithoutCoercion_positiveInfinity() throws Exception { OptionalDoubleBean bean = MAPPER_WITHOUT_COERCION.readValue( @@ -254,6 +274,7 @@ public void testOptionalDoubleBeanSpecialValuesWithoutCoercion_positiveInfinity( assertEquals(OptionalDouble.of(Double.POSITIVE_INFINITY), bean.value); } + @Test public void testOptionalDoubleBeanSpecialValuesWithoutCoercion_negativeInfinity() throws Exception { OptionalDoubleBean bean = MAPPER_WITHOUT_COERCION.readValue( diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalTest.java index 0ff5aa0f..d4bc805c 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalTest.java @@ -4,6 +4,8 @@ import java.util.*; import java.util.concurrent.atomic.AtomicReference; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.JsonGenerator; @@ -18,6 +20,8 @@ import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer; import com.fasterxml.jackson.databind.type.TypeFactory; +import static org.junit.jupiter.api.Assertions.*; + public class OptionalTest extends ModuleTestBase { private static final TypeReference> OPTIONAL_STRING_TYPE = new TypeReference>() {}; @@ -126,21 +130,25 @@ public String deserialize(JsonParser p, DeserializationContext ctxt) /********************************************************** */ + @Test public void testStringAbsent() throws Exception { assertFalse(roundtrip(Optional.empty(), OPTIONAL_STRING_TYPE).isPresent()); } + @Test public void testStringPresent() throws Exception { assertEquals("test", roundtrip(Optional.of("test"), OPTIONAL_STRING_TYPE).get()); } + @Test public void testBeanAbsent() throws Exception { assertFalse(roundtrip(Optional.empty(), OPTIONAL_BEAN_TYPE).isPresent()); } + @Test public void testBeanPresent() throws Exception { final TestBean bean = new TestBean(Integer.MAX_VALUE, "woopwoopwoopwoopwoop"); @@ -148,6 +156,7 @@ public void testBeanPresent() throws Exception } // [issue#4] + @Test public void testBeanWithCreator() throws Exception { final Issue4Entity emptyEntity = new Issue4Entity(Optional.empty()); @@ -160,6 +169,7 @@ public void testBeanWithCreator() throws Exception } // [issue#4] + @Test public void testOptionalStringInBean() throws Exception { OptionalStringBean bean = MAPPER.readValue("{\"value\":\"xyz\"}", OptionalStringBean.class); @@ -168,6 +178,7 @@ public void testOptionalStringInBean() throws Exception } // To support [datatype-jdk8#8] + @Test public void testExcludeIfOptionalAbsent() throws Exception { ObjectMapper mapper = mapperWithModule() @@ -187,6 +198,7 @@ public void testExcludeIfOptionalAbsent() throws Exception mapper.writeValueAsString(new OptionalStringBean(null))); } + @Test public void testWithCustomDeserializer() throws Exception { CaseChangingStringWrapper w = MAPPER.readValue(a2q("{'value':'FoobaR'}"), @@ -195,6 +207,7 @@ public void testWithCustomDeserializer() throws Exception } // [modules-java8#36] + @Test public void testWithCustomDeserializerIfOptionalAbsent() throws Exception { // 10-Aug-2017, tatu: Actually this is not true: missing value does not trigger @@ -208,6 +221,7 @@ public void testWithCustomDeserializerIfOptionalAbsent() throws Exception CaseChangingStringWrapper.class).value); } + @Test public void testCustomSerializer() throws Exception { final String VALUE = "fooBAR"; @@ -215,6 +229,7 @@ public void testCustomSerializer() throws Exception assertEquals(json, a2q("{'value':'FOOBAR'}")); } + @Test public void testCustomSerializerIfOptionalAbsent() throws Exception { ObjectMapper mapper = mapperWithModule() @@ -235,6 +250,7 @@ public void testCustomSerializerIfOptionalAbsent() throws Exception } // [modules-java8#33]: Verify against regression... + @Test public void testOtherRefSerializers() throws Exception { String json = MAPPER.writeValueAsString(new AtomicReference("foo")); @@ -242,6 +258,7 @@ public void testOtherRefSerializers() throws Exception } // Check [databind#2796] here too + @Test public void testTypeResolution() throws Exception { // Should be able to construct using parametric `constructType()` diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalWithEmptyTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalWithEmptyTest.java index ca96c4fd..dd415a65 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalWithEmptyTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalWithEmptyTest.java @@ -2,9 +2,13 @@ import java.util.Optional; +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 OptionalWithEmptyTest extends ModuleTestBase { private final ObjectMapper MAPPER = mapperWithModule(); @@ -18,12 +22,14 @@ public BooleanBean(Boolean b) { } } + @Test public void testOptionalFromEmpty() throws Exception { Optional value = MAPPER.readValue(q(""), new TypeReference>() {}); assertEquals(false, value.isPresent()); } // for [datatype-jdk8#23] + @Test public void testBooleanWithEmpty() throws Exception { // and looks like a special, somewhat non-conforming case is what a user had diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalnclusionTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalnclusionTest.java index 0ddb1885..32508055 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalnclusionTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalnclusionTest.java @@ -4,6 +4,8 @@ import java.util.Map; import java.util.Optional; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; @@ -11,6 +13,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.*; + public class OptionalnclusionTest extends ModuleTestBase { @JsonAutoDetect(fieldVisibility=Visibility.ANY) @@ -55,6 +59,7 @@ public OptMapBean(String key, Optional v) { private final ObjectMapper MAPPER = mapperWithModule(); + @Test public void testSerOptNonEmpty() throws Exception { OptionalData data = new OptionalData(); @@ -64,6 +69,7 @@ public void testSerOptNonEmpty() throws Exception assertEquals("{}", value); } + @Test public void testSerOptNonDefault() throws Exception { OptionalData data = new OptionalData(); @@ -73,6 +79,7 @@ public void testSerOptNonDefault() throws Exception assertEquals("{}", value); } + @Test public void testSerOptNonAbsent() throws Exception { OptionalData data = new OptionalData(); @@ -82,6 +89,7 @@ public void testSerOptNonAbsent() throws Exception assertEquals("{}", value); } + @Test public void testExcludeEmptyStringViaOptional() throws Exception { String json = MAPPER.writeValueAsString(new OptionalNonEmptyStringBean("x")); @@ -92,6 +100,7 @@ public void testExcludeEmptyStringViaOptional() throws Exception assertEquals("{}", json); } + @Test public void testSerPropInclusionAlways() throws Exception { JsonInclude.Value incl = @@ -101,6 +110,7 @@ public void testSerPropInclusionAlways() throws Exception mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE))); } + @Test public void testSerPropInclusionNonNull() throws Exception { JsonInclude.Value incl = @@ -110,6 +120,7 @@ public void testSerPropInclusionNonNull() throws Exception mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE))); } + @Test public void testSerPropInclusionNonAbsent() throws Exception { JsonInclude.Value incl = @@ -119,6 +130,7 @@ public void testSerPropInclusionNonAbsent() throws Exception mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE))); } + @Test public void testSerPropInclusionNonEmpty() throws Exception { JsonInclude.Value incl = @@ -128,6 +140,7 @@ public void testSerPropInclusionNonEmpty() throws Exception mapper.writeValueAsString(OptionalGenericData.construct(Boolean.TRUE))); } + @Test public void testMapElementInclusion() throws Exception { ObjectMapper mapper = mapperWithModule().setDefaultPropertyInclusion( diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/PolymorphicOptionalTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/PolymorphicOptionalTest.java index 0d8741dc..d9c82c60 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/PolymorphicOptionalTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/PolymorphicOptionalTest.java @@ -2,11 +2,15 @@ import java.util.Optional; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.annotation.JsonTypeInfo.As; import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.*; + public class PolymorphicOptionalTest extends ModuleTestBase { // For [datatype-jdk8#14] @@ -25,6 +29,7 @@ public static class ContainedImpl implements Contained { } private final ObjectMapper MAPPER = mapperWithModule(); // [datatype-jdk8#14] + @Test public void testPolymorphic14() throws Exception { final Container dto = new Container(); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/SchemaVisitorTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/SchemaVisitorTest.java index fac77e9a..f1283b9a 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/SchemaVisitorTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/SchemaVisitorTest.java @@ -3,17 +3,22 @@ import java.util.*; import java.util.concurrent.atomic.AtomicReference; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser.NumberType; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.jsonFormatVisitors.*; +import static org.junit.jupiter.api.Assertions.*; + // trivial tests visitor used (mostly) for JSON Schema generation public class SchemaVisitorTest extends ModuleTestBase { private final ObjectMapper MAPPER = mapperWithModule(); // for [datatype-jdk8#25] + @Test public void testOptionalInteger() throws Exception { final AtomicReference result = new AtomicReference<>(); @@ -33,6 +38,7 @@ public void numberType(NumberType t) { } // for [datatype-jdk8#25] + @Test public void testOptionalLong() throws Exception { final AtomicReference result = new AtomicReference<>(); @@ -52,6 +58,7 @@ public void numberType(NumberType t) { } // for [datatype-jdk8#25] + @Test public void testOptionalDouble() throws Exception { final AtomicReference result = new AtomicReference<>(); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamSerializerTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamSerializerTest.java index 1d7389a1..670fd66a 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamSerializerTest.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamSerializerTest.java @@ -1,7 +1,5 @@ package com.fasterxml.jackson.datatype.jdk8; -import static org.junit.Assert.*; - import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; @@ -9,11 +7,13 @@ import java.util.List; import java.util.stream.Stream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.datatype.jdk8.OptionalTest.TestBean; +import static org.junit.jupiter.api.Assertions.*; + @SuppressWarnings({ "javadoc", "unqualified-field-access" }) public class StreamSerializerTest extends StreamTestBase { diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamTestBase.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamTestBase.java index 5083a6a5..e6f77c9c 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamTestBase.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/StreamTestBase.java @@ -1,27 +1,18 @@ package com.fasterxml.jackson.datatype.jdk8; -import static org.junit.Assert.*; - import java.io.IOException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.function.Consumer; import java.util.stream.BaseStream; -import org.hamcrest.CustomMatcher; -import org.hamcrest.Description; -import org.hamcrest.core.AllOf; -import org.hamcrest.core.Is; -import org.junit.Before; -import org.junit.Rule; -import org.junit.rules.ExpectedException; +import org.junit.jupiter.api.BeforeEach; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; -public class StreamTestBase { +import static org.junit.jupiter.api.Assertions.*; - @Rule - public final ExpectedException expectedException = ExpectedException.none(); +public class StreamTestBase { ObjectMapper objectMapper; @@ -47,7 +38,7 @@ static void castAndThrow(final Throwable t) throws T { throw (T) t; } - @Before + @BeforeEach public void initObjectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module()); @@ -63,77 +54,53 @@ > void assertClosesOnSuccess(S baseStream, Consume assertTrue(closed.get()); } - > void assertClosesOnRuntimeException(String exceptionMessage, Consumer roundTrip, + > void assertClosesOnRuntimeException(String exMessage, Consumer roundTrip, S baseStream) { AtomicBoolean closed = new AtomicBoolean(); - initExpectedException(RuntimeException.class, exceptionMessage,closed); - - roundTrip.accept(baseStream.onClose(() -> closed.set(true))); + Exception actualEx = assertThrows(Exception.class, + () -> roundTrip.accept(baseStream.onClose(() -> closed.set(true)))); + initExpectedException(actualEx, exMessage, closed); } - > void assertClosesOnIoException(String exceptionMessage, Consumer roundTrip, + > void assertClosesOnIoException(String exMessage, Consumer roundTrip, S baseStream) { AtomicBoolean closed = new AtomicBoolean(); - initExpectedExceptionIoException(exceptionMessage,closed); - - roundTrip.accept(baseStream.onClose(() -> closed.set(true))); + Exception actual = assertThrows(Exception.class, + () -> roundTrip.accept(baseStream.onClose(() -> closed.set(true)))); + initExpectedExceptionIoException(actual, closed, + "Unexpected IOException (of type java.io.IOException): " + exMessage); } - > void assertClosesOnWrappedIoException(String exceptionMessage, + > void assertClosesOnWrappedIoException(String expectedMsg, Consumer roundTrip, S baseStream) { AtomicBoolean closed = new AtomicBoolean(); - final String actualMessage = "Unexpected IOException (of type java.io.IOException): " + exceptionMessage; - - initExpectedExceptionIoException(actualMessage,closed); - - roundTrip.accept(baseStream.onClose(() -> closed.set(true))); + Exception actual = assertThrows(Exception.class, + () -> roundTrip.accept(baseStream.onClose(() -> closed.set(true)))); + initExpectedExceptionIoException(actual, closed, + "Unexpected IOException (of type java.io.IOException): " + expectedMsg); } - void initExpectedExceptionIoException(final String exceptionMessage, AtomicBoolean closed) { - - this.expectedException.expect(new IsClosedMatcher(closed)); - this.expectedException.expect(Is.isA(IOException.class)); - this.expectedException.expectMessage(exceptionMessage); + void initExpectedExceptionIoException(final Exception actualException, AtomicBoolean closed, final String exceptionMessage) + { + assertInstanceOf(IOException.class, actualException); + assertTrue(closed.get()); + assertTrue(actualException.getMessage().contains(exceptionMessage)); } - void initExpectedException(Class cause, final String exceptionMessage, AtomicBoolean closed) { - this.expectedException.expect(AllOf.allOf(Is.isA(JsonMappingException.class), new IsClosedMatcher(closed))); - this.expectedException.expect(Is.isA(JsonMappingException.class)); - this.expectedException.expectCause(Is.isA(cause)); - this.expectedException.expectMessage(exceptionMessage); + void initExpectedException(final Exception actualException, final String exceptionMessage, AtomicBoolean closed) + { + assertInstanceOf(JsonMappingException.class, actualException); + assertTrue(closed.get()); + assertTrue(actualException.getMessage().contains(exceptionMessage)); } - /** - * Matcher that matches when the {@link StreamTestBase#closed} value is set to true. - */ - static class IsClosedMatcher extends CustomMatcher { - - final AtomicBoolean closed; - - public IsClosedMatcher(AtomicBoolean closed) { - super("Check flag closed"); - this.closed = closed; - } - - @Override - public void describeMismatch(Object item, Description description) { - description.appendText("The onClose method was not called"); - } - - @Override - public boolean matches(Object item) { - - return closed.get(); - } - - } } diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestConfigureAbsentsAsNulls.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestConfigureAbsentsAsNulls.java index 2769f1f6..68aa2db8 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestConfigureAbsentsAsNulls.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestConfigureAbsentsAsNulls.java @@ -5,11 +5,15 @@ import java.util.OptionalInt; import java.util.OptionalLong; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.jupiter.api.Assertions.*; + @SuppressWarnings("deprecation") public class TestConfigureAbsentsAsNulls extends ModuleTestBase { @@ -39,6 +43,7 @@ public static final class OptionalDoubleWrapper { /********************************************************************** */ + @Test public void testConfigAbsentsAsNullsTrue() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true)); @@ -48,6 +53,7 @@ public void testConfigAbsentsAsNullsTrue() throws Exception { assertEquals("{}", value); } + @Test public void testConfigAbsentsAsNullsFalse() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(false)); @@ -57,6 +63,7 @@ public void testConfigAbsentsAsNullsFalse() throws Exception { assertEquals("{\"myString\":null}", value); } + @Test public void testConfigNonAbsentAbsentsAsNullsTrue() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true)); @@ -66,6 +73,7 @@ public void testConfigNonAbsentAbsentsAsNullsTrue() throws Exception { assertEquals("{}", value); } + @Test public void testConfigNonAbsentAbsentsAsNullsFalse() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(false)); @@ -81,6 +89,7 @@ public void testConfigNonAbsentAbsentsAsNullsFalse() throws Exception { /********************************************************************** */ + @Test public void testOptionalIntAbsentAsNulls() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true)); @@ -88,6 +97,7 @@ public void testOptionalIntAbsentAsNulls() throws Exception { assertEquals("{}", value); } + @Test public void testOptionalLongAbsentAsNulls() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true)); @@ -95,6 +105,7 @@ public void testOptionalLongAbsentAsNulls() throws Exception { assertEquals("{}", value); } + @Test public void testOptionalDoubleAbsentAsNulls() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Jdk8Module().configureAbsentsAsNulls(true)); diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestOptionalWithPolymorphic.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestOptionalWithPolymorphic.java index ab23043c..6d8bd2fc 100644 --- a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestOptionalWithPolymorphic.java +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/TestOptionalWithPolymorphic.java @@ -4,6 +4,8 @@ import java.util.Map; import java.util.Optional; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSubTypes; @@ -11,6 +13,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import static org.junit.jupiter.api.Assertions.*; + public class TestOptionalWithPolymorphic extends ModuleTestBase { static class ContainerA { @@ -69,6 +73,7 @@ static class AbstractOptional { final ObjectMapper MAPPER = mapperWithModule(); + @Test public void testOptionalMapsFoo() throws Exception { Map foo = new LinkedHashMap<>(); @@ -82,6 +87,7 @@ public void testOptionalMapsFoo() throws Exception { _test(MAPPER, foo); } + @Test public void testOptionalMapsBar() throws Exception { Map bar = new LinkedHashMap<>(); @@ -95,6 +101,7 @@ public void testOptionalMapsBar() throws Exception { _test(MAPPER, bar); } + @Test public void testOptionalMapsBaz() throws Exception { Map baz = new LinkedHashMap<>(); Map loop = new LinkedHashMap<>(); @@ -107,6 +114,7 @@ public void testOptionalMapsBaz() throws Exception { _test(MAPPER, baz); } + @Test public void testOptionalWithTypeAnnotation13() throws Exception { AbstractOptional result = MAPPER.readValue("{\"value\" : 5}", diff --git a/datetime/pom.xml b/datetime/pom.xml index 68ea3644..2790b394 100644 --- a/datetime/pom.xml +++ b/datetime/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.18.11-SNAPSHOT + 2.23.0-SNAPSHOT com.fasterxml.jackson.datatype jackson-datatype-jsr310 @@ -46,6 +46,18 @@ com.fasterxml.jackson.core jackson-annotations + + + + org.junit.jupiter + junit-jupiter + test + + + org.junit.jupiter + junit-jupiter-api + test + @@ -86,29 +98,21 @@ com.google.code.maven-replacer-plugin replacer - - - process-packageVersion - generate-sources - - + + + + org.cyclonedx + cyclonedx-maven-plugin org.apache.maven.plugins maven-compiler-plugin - 3.0 true ${javac.src.version} ${javac.target.version} true true - true - - 10000 - 10000 - - + + + org.gradlex + gradle-module-metadata-maven-plugin + diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/DecimalUtils.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/DecimalUtils.java index 7d8484db..a76c0139 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/DecimalUtils.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/DecimalUtils.java @@ -30,8 +30,6 @@ */ public final class DecimalUtils { - private static final BigDecimal ONE_BILLION = new BigDecimal(1_000_000_000L); - private DecimalUtils() { } public static String toDecimal(long seconds, int nanoseconds) @@ -86,6 +84,14 @@ public static String toDecimal(long seconds, int nanoseconds) */ public static BigDecimal toBigDecimal(long seconds, int nanoseconds) { + // [modules-java8#359] For negative seconds with positive nanos (times before epoch), + // we need to compute the proper decimal value: seconds + (nanos / 1_000_000_000) + // Example: Instant{epochSecond=-1, nano=999000000} represents -0.001 seconds + if (seconds < 0 && nanoseconds > 0) { + return BigDecimal.valueOf(seconds) + .add(BigDecimal.valueOf(nanoseconds).scaleByPowerOfTen(-9)); + } + if (nanoseconds == 0L) { // 14-Mar-2015, tatu: Let's retain one zero to avoid interpretation // as integral number @@ -98,15 +104,17 @@ public static BigDecimal toBigDecimal(long seconds, int nanoseconds) } /** - * @deprecated due to potential unbounded latency on some JRE releases. + * Extracts the seconds and nanoseconds component of {@code seconds} as {@code long} and {@code int} + * values, passing them to the given converter. The implementation avoids latency issues present + * on some JRE releases. + * + * @since 2.9.8 + * @deprecated Since 2.19: use {@link #extractSecondsAndNanos(BigDecimal, BiFunction, boolean)} instead. */ - @Deprecated // since 2.9.8 - public static int extractNanosecondDecimal(BigDecimal value, long integer) + @Deprecated // since 2.19 + public static T extractSecondsAndNanos(BigDecimal seconds, BiFunction convert) { - // !!! 14-Mar-2016, tatu: Somewhat inefficient; should replace with functionally - // equivalent code that just subtracts integral part? (or, measure and show - // there's no difference and do nothing... ) - return value.subtract(BigDecimal.valueOf(integer)).multiply(ONE_BILLION).intValue(); + return extractSecondsAndNanos(seconds, convert, true); } /** @@ -114,13 +122,12 @@ public static int extractNanosecondDecimal(BigDecimal value, long integer) * values, passing them to the given converter. The implementation avoids latency issues present * on some JRE releases. * - * @since 2.9.8 + * @since 2.19 */ - public static T extractSecondsAndNanos(BigDecimal seconds, BiFunction convert) - { + public static T extractSecondsAndNanos(BigDecimal seconds, + BiFunction convert, boolean negativeAdjustment) { // Complexity is here to workaround unbounded latency in some BigDecimal operations. // https://github.com/FasterXML/jackson-databind/issues/2141 - long secondsOnly; int nanosOnly; @@ -141,8 +148,11 @@ else if (seconds.scale() < -63) { nanosOnly = nanoseconds.subtract(BigDecimal.valueOf(secondsOnly).scaleByPowerOfTen(9)).intValue(); if (secondsOnly < 0 && secondsOnly > Instant.MIN.getEpochSecond()) { - // Issue #69 and Issue #120: avoid sending a negative adjustment to the Instant constructor, we want this as the actual nanos - nanosOnly = Math.abs(nanosOnly); + // [modules-java8#337] since 2.19, not always we need to adjust nanos + if (negativeAdjustment) { + // Issue #69 and Issue #120: avoid sending a negative adjustment to the Instant constructor, we want this as the actual nanos + nanosOnly = Math.abs(nanosOnly); + } } } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java index a8b7e68a..d12cc119 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JSR310Module.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.databind.deser.ValueInstantiators; import com.fasterxml.jackson.databind.deser.std.StdValueInstantiator; import com.fasterxml.jackson.databind.introspect.AnnotatedClass; +import com.fasterxml.jackson.databind.introspect.AnnotatedClassResolver; import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; @@ -184,7 +185,8 @@ public ValueInstantiator findValueInstantiator(DeserializationConfig config, } else { // we don't need Annotations, so constructing directly is fine here // even if it's not generally recommended - ac = AnnotatedClass.construct(config.constructType(ZoneId.class), config); + ac = AnnotatedClassResolver.resolve(config, + config.constructType(ZoneId.class), config); } if (!inst.canCreateFromString()) { AnnotatedMethod factory = _findFactory(ac, "of", String.class); diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeFeature.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeFeature.java index 94912d27..36bebe27 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeFeature.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeFeature.java @@ -19,6 +19,19 @@ public enum JavaTimeFeature implements JacksonFeature */ NORMALIZE_DESERIALIZED_ZONE_ID(true), + /** + * Feature that determines whether the {@link java.util.TimeZone} of the + * {@link com.fasterxml.jackson.databind.DeserializationContext} is used + * when leniently deserializing {@link java.time.LocalDate} or + * {@link java.time.LocalDateTime} from the UTC/ISO instant format. + *

+ * Default setting is disabled, for backwards-compatibility with + * Jackson 2.18. + * + * @since 2.19 + */ + USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING(false), + /** * Feature that controls whether stringified numbers (Strings that without * quotes would be legal JSON Numbers) may be interpreted as @@ -38,7 +51,40 @@ public enum JavaTimeFeature implements JacksonFeature *

* Default setting is false, meaning that Month is serialized/deserialized as a zero-based index. */ - ONE_BASED_MONTHS(false) + ONE_BASED_MONTHS(false), + + /** + * Feature that determines whether sub-second digits are always written when + * serializing {@link java.time.Instant}, {@link java.time.OffsetDateTime}, + * {@link java.time.ZonedDateTime} and {@link java.time.LocalDateTime} values + * as ISO-8601 Strings using the default format. + *

+ * When disabled (the default), the JDK-provided ISO formatters are used and + * a zero sub-second value is omitted altogether -- {@code 2017-09-14T04:28:48Z} + * -- which means that output width varies with the value, breaking systems + * that expect fixed-precision timestamps (or that sort timestamps as text). + *

+ * When enabled, at least 3 (millisecond) sub-second digits are always written, + * zero-padded if necessary -- {@code 2017-09-14T04:28:48.000Z}. Higher precision + * is preserved: a value with microsecond or nanosecond precision is written with + * 6 or 9 digits respectively, so no information is lost. + *

+ * Only affects the default format: an explicit {@code DateTimeFormatter} or + * a {@link com.fasterxml.jackson.annotation.JsonFormat} pattern takes precedence, + * as does writing values as numeric timestamps. + *

+ * Also note that this only applies to values, and NOT to {@link java.util.Map} + * keys: date/time keys keep being written using the JDK-provided ISO formatters, + * so a zero sub-second value is still omitted there. Types other than the four + * listed above -- notably {@link java.time.LocalTime} and + * {@link java.time.OffsetTime}, whose ISO formats also omit the seconds field -- + * are likewise unaffected. + *

+ * Default setting is disabled, for backwards compatibility. + * + * @since 2.23 + */ + ALWAYS_WRITE_SUBSECOND_DIGITS(false) ; /** diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java index 2325c5f0..80ede099 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/JavaTimeModule.java @@ -17,6 +17,7 @@ package com.fasterxml.jackson.datatype.jsr310; import java.time.*; +import java.time.temporal.TemporalAdjuster; import com.fasterxml.jackson.core.util.JacksonFeatureSet; @@ -31,6 +32,7 @@ import com.fasterxml.jackson.databind.module.SimpleKeyDeserializers; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleSerializers; +import com.fasterxml.jackson.databind.ser.std.ToEmptyObjectSerializer; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import com.fasterxml.jackson.datatype.jsr310.deser.*; import com.fasterxml.jackson.datatype.jsr310.deser.key.*; @@ -129,8 +131,8 @@ public void setupModule(SetupContext context) { // // Other deserializers desers.addDeserializer(Duration.class, DurationDeserializer.INSTANCE); - desers.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); - desers.addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE); + desers.addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE.withFeatures(_features)); + desers.addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE.withFeatures(_features)); desers.addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE); desers.addDeserializer(MonthDay.class, MonthDayDeserializer.INSTANCE); desers.addDeserializer(OffsetTime.class, OffsetTimeDeserializer.INSTANCE); @@ -152,15 +154,15 @@ public void setupModule(SetupContext context) { context.addDeserializers(_deserializers); } - SimpleSerializers sers = new SimpleSerializers(); + JavaTimeSerializers sers = new JavaTimeSerializers(); sers.addSerializer(Duration.class, DurationSerializer.INSTANCE); - sers.addSerializer(Instant.class, InstantSerializer.INSTANCE); - sers.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE); + sers.addSerializer(Instant.class, InstantSerializer.INSTANCE.withFeatures(_features)); + sers.addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE.withFeatures(_features)); sers.addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE); sers.addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE); sers.addSerializer(MonthDay.class, MonthDaySerializer.INSTANCE); - sers.addSerializer(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE); + sers.addSerializer(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE.withFeatures(_features)); sers.addSerializer(OffsetTime.class, OffsetTimeSerializer.INSTANCE); sers.addSerializer(Period.class, new ToStringSerializer(Period.class)); sers.addSerializer(Year.class, YearSerializer.INSTANCE); @@ -171,7 +173,7 @@ public void setupModule(SetupContext context) { * serialization with timezone offset only, not timezone id. * But this is configurable. */ - sers.addSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE); + sers.addSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE.withFeatures(_features)); // since 2.11: need to override Type Id handling // (actual concrete type is `ZoneRegion`, but that's not visible) @@ -264,18 +266,50 @@ protected AnnotatedMethod _findFactory(AnnotatedClass cls, String name, Class { final int argCount = argTypes.length; for (AnnotatedMethod method : cls.getFactoryMethods()) { - if (!name.equals(method.getName()) - || (method.getParameterCount() != argCount)) { - continue; + if (name.equals(method.getName()) + && (method.getParameterCount() == argCount) + && _allArgTypesMatch(argTypes, method)) { + return method; } - for (int i = 0; i < argCount; ++i) { - Class argType = method.getParameter(i).getRawType(); - if (!argType.isAssignableFrom(argTypes[i])) { - continue; + } + return null; + } + + // @since 2.21 + private boolean _allArgTypesMatch(Class[] expectedArgTypes, AnnotatedMethod method) + { + for (int i = 0, len = expectedArgTypes.length; i < len; ++i) { + Class argType = method.getParameter(i).getRawType(); + if (!argType.isAssignableFrom(expectedArgTypes[i])) { + return false; + } + } + return true; + } + + /** + * Container for serializers, with one tweak; specific lookup we need to deal + * with specific {@code TemporalAdjuster} closure subtypes. + * + * @since 2.19 + */ + @SuppressWarnings("serial") + class JavaTimeSerializers extends SimpleSerializers { + @Override + public JsonSerializer findSerializer(SerializationConfig config, + JavaType type, BeanDescription beanDesc) + { + JsonSerializer ser = super.findSerializer(config, type, beanDesc); + if (ser == null) { + // 27-Jan-2025, tatu: [modules-java8#207] Need actual "ToEmptySerializer" + // with Jackson 2.x, to avoid "No Bean Properties found" error + // But! Must be careful to only apply to lambdas + if (type.isTypeOrSubTypeOf(TemporalAdjuster.class) + && type.getRawClass().isSynthetic()) { + return new ToEmptyObjectSerializer(type); } } - return method; + return ser; } - return null; } } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java index dee6d35f..79a1fc25 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserializer.java @@ -156,22 +156,26 @@ public Duration deserialize(JsonParser parser, DeserializationContext context) t { case JsonTokenId.ID_NUMBER_FLOAT: BigDecimal value = parser.getDecimalValue(); - return DecimalUtils.extractSecondsAndNanos(value, Duration::ofSeconds); + // [modules-java8#337] since 2.19, Duration does not need negative adjustment + return DecimalUtils.extractSecondsAndNanos(value, Duration::ofSeconds, false); case JsonTokenId.ID_NUMBER_INT: return _fromTimestamp(context, parser.getLongValue()); case JsonTokenId.ID_STRING: return _fromString(parser, context, parser.getText()); - // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - case JsonTokenId.ID_START_OBJECT: - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); case JsonTokenId.ID_EMBEDDED_OBJECT: // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded // values quite easily return (Duration) parser.getEmbeddedObject(); - case JsonTokenId.ID_START_ARRAY: return _deserializeFromArray(parser, context); + // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) + case JsonTokenId.ID_START_OBJECT: + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = context.extractScalarFromObject(parser, this, handledType()); + if (str != null) { + return _fromString(parser, context, str); + } + // fall through } return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT); diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java index c80bd5e6..ee50ac63 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java @@ -200,12 +200,12 @@ protected InstantDeserializer(Class supportedType, */ @Deprecated() protected InstantDeserializer(Class supportedType, - DateTimeFormatter formatter, - Function parsedToValue, - Function fromMilliseconds, - Function fromNanoseconds, - BiFunction adjust, - boolean replaceZeroOffsetAsZ + DateTimeFormatter formatter, + Function parsedToValue, + Function fromMilliseconds, + Function fromNanoseconds, + BiFunction adjust, + boolean replaceZeroOffsetAsZ ) { this(supportedType, formatter, parsedToValue, fromMilliseconds, fromNanoseconds, adjust, replaceZeroOffsetAsZ, @@ -299,8 +299,11 @@ protected InstantDeserializer(InstantDeserializer base, _alwaysAllowStringifiedDateTimestamps = features.isEnabled(JavaTimeFeature.ALWAYS_ALLOW_STRINGIFIED_DATE_TIMESTAMPS); } + /** + * NOTE: {@code public} since 2.21 + */ @Override - protected InstantDeserializer withDateFormat(DateTimeFormatter dtf) { + public InstantDeserializer withDateFormat(DateTimeFormatter dtf) { if (dtf == _formatter) { return this; } @@ -355,10 +358,6 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I return _fromLong(context, parser.getLongValue()); case JsonTokenId.ID_STRING: return _fromString(parser, context, parser.getText()); - // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - case JsonTokenId.ID_START_OBJECT: - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); case JsonTokenId.ID_EMBEDDED_OBJECT: // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded // values quite easily @@ -366,6 +365,14 @@ public T deserialize(JsonParser parser, DeserializationContext context) throws I case JsonTokenId.ID_START_ARRAY: return _deserializeFromArray(parser, context); + // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) + case JsonTokenId.ID_START_OBJECT: + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = context.extractScalarFromObject(parser, this, handledType()); + if (str != null) { + return _fromString(parser, context, str); + } + // fall through } return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT, JsonToken.VALUE_NUMBER_FLOAT); @@ -477,7 +484,10 @@ protected T _fromLong(DeserializationContext context, long timestamp) protected T _fromDecimal(DeserializationContext context, BigDecimal value) { FromDecimalArguments args = - DecimalUtils.extractSecondsAndNanos(value, (s, ns) -> new FromDecimalArguments(s, ns, getZone(context))); + DecimalUtils.extractSecondsAndNanos(value, (s, ns) -> new FromDecimalArguments(s, ns, getZone(context)), + // [modules-java8#359] since 2.21, Instant.ofEpochSecond() correctly handles + // negative nanoseconds, so no adjustment needed + false); return fromNanoseconds.apply(args); } @@ -528,14 +538,42 @@ private static String replaceZeroOffsetAsZ(String text) } // @since 2.13 - private String addInColonToOffsetIfMissing(String text) + private static String addInColonToOffsetIfMissing(String text) { - final Matcher matcher = ISO8601_COLONLESS_OFFSET_REGEX.matcher(text); - if (matcher.find()){ - StringBuilder sb = new StringBuilder(matcher.group(0)); - sb.insert(3, ":"); + int timeIndex = text.indexOf('T'); + if (timeIndex < 0 || timeIndex > text.length() - 1) { + return text; + } + + int offsetIndex = text.indexOf('+', timeIndex + 1); + if (offsetIndex < 0) { + offsetIndex = text.indexOf('-', timeIndex + 1); + } + + if (offsetIndex < 0 || offsetIndex > text.length() - 5) { + return text; + } - return matcher.replaceFirst(sb.toString()); + int colonIndex = text.indexOf(':', offsetIndex); + if (colonIndex == offsetIndex + 3) { + return text; + } + + if (Character.isDigit(text.charAt(offsetIndex + 1)) + && Character.isDigit(text.charAt(offsetIndex + 2)) + && Character.isDigit(text.charAt(offsetIndex + 3)) + && Character.isDigit(text.charAt(offsetIndex + 4))) { + String match = text.substring(offsetIndex, offsetIndex + 5); + return text.substring(0, offsetIndex) + + match.substring(0, 3) + ':' + match.substring(3) + + text.substring(offsetIndex + match.length()); + } + + // fallback to slow regex path, should be fully handled by the above + final Matcher matcher = ISO8601_COLONLESS_OFFSET_REGEX.matcher(text); + if (matcher.find()) { + String match = matcher.group(0); + return matcher.replaceFirst(match.substring(0, 3) + ':' + match.substring(3)); } return text; } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310DateTimeDeserializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310DateTimeDeserializerBase.java index 8ec7efba..68e42954 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310DateTimeDeserializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310DateTimeDeserializerBase.java @@ -27,7 +27,7 @@ public abstract class JSR310DateTimeDeserializerBase protected final DateTimeFormatter _formatter; /** - * Setting that indicates the {@Link JsonFormat.Shape} specified for this deserializer + * Setting that indicates the {@link JsonFormat.Shape} specified for this deserializer * as a {@link com.fasterxml.jackson.annotation.JsonFormat.Shape} annotation on * property or class, or due to per-type "config override", or from global settings: * If Shape is NUMBER_INT, the input value is considered to be epoch days. If not a diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310StringParsableDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310StringParsableDeserializer.java index 5cb8e465..96596a34 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310StringParsableDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/JSR310StringParsableDeserializer.java @@ -122,15 +122,17 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) if (p.isExpectedStartObjectToken()) { - return _fromString(p, ctxt, - ctxt.extractScalarFromObject(p, this, handledType())); - } - if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + // fall through + } else if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { // 20-Apr-2016, tatu: Related to [databind#1208], can try supporting embedded // values quite easily return p.getEmbeddedObject(); - } - if (p.isExpectedStartArrayToken()) { + } else if (p.isExpectedStartArrayToken()) { return _deserializeFromArray(p, ctxt); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java index dd33ce41..5775a1d4 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserializer.java @@ -18,16 +18,19 @@ import java.io.IOException; import java.time.DateTimeException; +import java.time.Instant; import java.time.LocalDate; import java.time.format.DateTimeFormatter; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.*; +import com.fasterxml.jackson.core.util.JacksonFeatureSet; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; /** * Deserializer for Java 8 temporal {@link LocalDate}s. @@ -38,16 +41,31 @@ public class LocalDateDeserializer extends JSR310DateTimeDeserializerBase features) { + super(LocalDate.class, base._formatter); + _useTimeZoneForLenientDateParsing = features.isEnabled(JavaTimeFeature.USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING); } @Override @@ -84,62 +113,77 @@ protected LocalDateDeserializer withLeniency(Boolean leniency) { @Override protected LocalDateDeserializer withShape(JsonFormat.Shape shape) { return new LocalDateDeserializer(this, shape); } + /** + * Since 2.19 + */ + public LocalDateDeserializer withFeatures(JacksonFeatureSet features) { + if (_useTimeZoneForLenientDateParsing == + features.isEnabled(JavaTimeFeature.USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING)) { + return this; + } + return new LocalDateDeserializer(this, features); + } + @Override - public LocalDate deserialize(JsonParser parser, DeserializationContext context) throws IOException + public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - if (parser.hasToken(JsonToken.VALUE_STRING)) { - return _fromString(parser, context, parser.getText()); + if (p.hasToken(JsonToken.VALUE_STRING)) { + return _fromString(p, ctxt, p.getText()); } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - if (parser.isExpectedStartObjectToken()) { - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); + if (p.isExpectedStartObjectToken()) { + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + return _handleUnexpectedToken(ctxt, p, "Expected array or string"); } - if (parser.isExpectedStartArrayToken()) { - JsonToken t = parser.nextToken(); + if (p.isExpectedStartArrayToken()) { + JsonToken t = p.nextToken(); if (t == JsonToken.END_ARRAY) { return null; } - if (context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) + if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) && (t == JsonToken.VALUE_STRING || t==JsonToken.VALUE_EMBEDDED_OBJECT)) { - final LocalDate parsed = deserialize(parser, context); - if (parser.nextToken() != JsonToken.END_ARRAY) { - handleMissingEndArrayForSingle(parser, context); + final LocalDate parsed = deserialize(p, ctxt); + if (p.nextToken() != JsonToken.END_ARRAY) { + handleMissingEndArrayForSingle(p, ctxt); } return parsed; } if (t == JsonToken.VALUE_NUMBER_INT) { - int year = parser.getIntValue(); - int month = parser.nextIntValue(-1); - int day = parser.nextIntValue(-1); + int year = p.getIntValue(); + int month = p.nextIntValue(-1); + int day = p.nextIntValue(-1); - if (parser.nextToken() != JsonToken.END_ARRAY) { - throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY, + if (p.nextToken() != JsonToken.END_ARRAY) { + throw ctxt.wrongTokenException(p, handledType(), JsonToken.END_ARRAY, "Expected array to end"); } return LocalDate.of(year, month, day); } - context.reportInputMismatch(handledType(), + ctxt.reportInputMismatch(handledType(), "Unexpected token (%s) within Array, expected VALUE_NUMBER_INT", t); } - if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { - return (LocalDate) parser.getEmbeddedObject(); + if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { + return (LocalDate) p.getEmbeddedObject(); } // 06-Jan-2018, tatu: Is this actually safe? Do users expect such coercion? - if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) { - CoercionAction act = context.findCoercionAction(logicalType(), _valueClass, + if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) { + CoercionAction act = ctxt.findCoercionAction(logicalType(), _valueClass, CoercionInputShape.Integer); - _checkCoercionFail(context, act, handledType(), parser.getLongValue(), - "Integer value (" + parser.getLongValue() + ")"); + _checkCoercionFail(ctxt, act, handledType(), p.getLongValue(), + "Integer value (" + p.getLongValue() + ")"); // issue 58 - also check for NUMBER_INT, which needs to be specified when serializing. if (_shape == JsonFormat.Shape.NUMBER_INT || isLenient()) { - return LocalDate.ofEpochDay(parser.getLongValue()); + return LocalDate.ofEpochDay(p.getLongValue()); } - return _failForNotLenient(parser, context, JsonToken.VALUE_STRING); + return _failForNotLenient(p, ctxt, JsonToken.VALUE_STRING); } - return _handleUnexpectedToken(context, parser, "Expected array or string."); + return _handleUnexpectedToken(ctxt, p, "Expected array or string"); } protected LocalDate _fromString(JsonParser p, DeserializationContext ctxt, @@ -161,6 +205,9 @@ protected LocalDate _fromString(JsonParser p, DeserializationContext ctxt, if (string.length() > 10 && string.charAt(10) == 'T') { if (isLenient()) { if (string.endsWith("Z")) { + if (_useTimeZoneForLenientDateParsing) { + return Instant.parse(string).atZone(ctxt.getTimeZone().toZoneId()).toLocalDate(); + } return LocalDate.parse(string.substring(0, string.length() - 1), DateTimeFormatter.ISO_LOCAL_DATE_TIME); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserializer.java index 79ccc35d..c76fdb0d 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserializer.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.time.DateTimeException; +import java.time.Instant; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Objects; @@ -27,10 +28,12 @@ import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.JsonTokenId; +import com.fasterxml.jackson.core.util.JacksonFeatureSet; import com.fasterxml.jackson.databind.BeanProperty; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; /** * Deserializer for Java 8 temporal {@link LocalDateTime}s. @@ -43,6 +46,9 @@ public class LocalDateTimeDeserializer { private static final long serialVersionUID = 1L; + private final static boolean DEFAULT_USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING + = JavaTimeFeature.USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING.enabledByDefault(); + private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME; public static final LocalDateTimeDeserializer INSTANCE = new LocalDateTimeDeserializer(); @@ -54,6 +60,17 @@ public class LocalDateTimeDeserializer */ protected final Boolean _readTimestampsAsNanosOverride; + /** + * Flag set from + * {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature#USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING} + * to determine whether the {@link java.util.TimeZone} of the + * {@link com.fasterxml.jackson.databind.DeserializationContext} is used + * when leniently deserializing from the UTC/ISO instant format. + * + * @since 2.19 + */ + protected final boolean _useTimeZoneForLenientDateParsing; + protected LocalDateTimeDeserializer() { // was private before 2.12 this(DEFAULT_FORMATTER); } @@ -61,6 +78,7 @@ protected LocalDateTimeDeserializer() { // was private before 2.12 public LocalDateTimeDeserializer(DateTimeFormatter formatter) { super(LocalDateTime.class, formatter); _readTimestampsAsNanosOverride = null; + _useTimeZoneForLenientDateParsing = DEFAULT_USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING; } /** @@ -69,6 +87,7 @@ public LocalDateTimeDeserializer(DateTimeFormatter formatter) { protected LocalDateTimeDeserializer(LocalDateTimeDeserializer base, Boolean leniency) { super(base, leniency); _readTimestampsAsNanosOverride = base._readTimestampsAsNanosOverride; + _useTimeZoneForLenientDateParsing = base._useTimeZoneForLenientDateParsing; } /** @@ -81,6 +100,16 @@ protected LocalDateTimeDeserializer(LocalDateTimeDeserializer base, Boolean readTimestampsAsNanosOverride) { super(base, leniency, formatter, shape); _readTimestampsAsNanosOverride = readTimestampsAsNanosOverride; + _useTimeZoneForLenientDateParsing = base._useTimeZoneForLenientDateParsing; + } + + /** + * Since 2.19 + */ + protected LocalDateTimeDeserializer(LocalDateTimeDeserializer base, JacksonFeatureSet features) { + super(LocalDateTime.class, base._formatter); + _readTimestampsAsNanosOverride = base._readTimestampsAsNanosOverride; + _useTimeZoneForLenientDateParsing = features.isEnabled(JavaTimeFeature.USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING); } @Override @@ -107,53 +136,68 @@ protected JSR310DateTimeDeserializerBase _withFormatOverrides(Deserialization return deser; } + /** + * Since 2.19 + */ + public LocalDateTimeDeserializer withFeatures(JacksonFeatureSet features) { + if (_useTimeZoneForLenientDateParsing == + features.isEnabled(JavaTimeFeature.USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING)) { + return this; + } + return new LocalDateTimeDeserializer(this, features); + } + @Override - public LocalDateTime deserialize(JsonParser parser, DeserializationContext context) throws IOException + public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - if (parser.hasTokenId(JsonTokenId.ID_STRING)) { - return _fromString(parser, context, parser.getText()); + if (p.hasTokenId(JsonTokenId.ID_STRING)) { + return _fromString(p, ctxt, p.getText()); } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - if (parser.isExpectedStartObjectToken()) { - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); + if (p.isExpectedStartObjectToken()) { + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + return _handleUnexpectedToken(ctxt, p, "Expected array or string"); } - if (parser.isExpectedStartArrayToken()) { - JsonToken t = parser.nextToken(); + if (p.isExpectedStartArrayToken()) { + JsonToken t = p.nextToken(); if (t == JsonToken.END_ARRAY) { return null; } if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT) - && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { - final LocalDateTime parsed = deserialize(parser, context); - if (parser.nextToken() != JsonToken.END_ARRAY) { - handleMissingEndArrayForSingle(parser, context); + && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { + final LocalDateTime parsed = deserialize(p, ctxt); + if (p.nextToken() != JsonToken.END_ARRAY) { + handleMissingEndArrayForSingle(p, ctxt); } return parsed; } if (t == JsonToken.VALUE_NUMBER_INT) { LocalDateTime result; - int year = parser.getIntValue(); - int month = parser.nextIntValue(-1); - int day = parser.nextIntValue(-1); - int hour = parser.nextIntValue(-1); - int minute = parser.nextIntValue(-1); + int year = p.getIntValue(); + int month = p.nextIntValue(-1); + int day = p.nextIntValue(-1); + int hour = p.nextIntValue(-1); + int minute = p.nextIntValue(-1); - t = parser.nextToken(); + t = p.nextToken(); if (t == JsonToken.END_ARRAY) { result = LocalDateTime.of(year, month, day, hour, minute); } else { - int second = parser.getIntValue(); - t = parser.nextToken(); + int second = p.getIntValue(); + t = p.nextToken(); if (t == JsonToken.END_ARRAY) { result = LocalDateTime.of(year, month, day, hour, minute, second); } else { - int partialSecond = parser.getIntValue(); - if (partialSecond < 1_000 && !shouldReadTimestampsAsNanoseconds(context)) + int partialSecond = p.getIntValue(); + if (partialSecond < 1_000 && !shouldReadTimestampsAsNanoseconds(ctxt)) partialSecond *= 1_000_000; // value is milliseconds, convert it to nanoseconds - if (parser.nextToken() != JsonToken.END_ARRAY) { - throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY, + if (p.nextToken() != JsonToken.END_ARRAY) { + throw ctxt.wrongTokenException(p, handledType(), JsonToken.END_ARRAY, "Expected array to end"); } result = LocalDateTime.of(year, month, day, hour, minute, second, partialSecond); @@ -161,17 +205,17 @@ public LocalDateTime deserialize(JsonParser parser, DeserializationContext conte } return result; } - context.reportInputMismatch(handledType(), + ctxt.reportInputMismatch(handledType(), "Unexpected token (%s) within Array, expected VALUE_NUMBER_INT", t); } - if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { - return (LocalDateTime) parser.getEmbeddedObject(); + if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { + return (LocalDateTime) p.getEmbeddedObject(); } - if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) { - _throwNoNumericTimestampNeedTimeZone(parser, context); + if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) { + _throwNoNumericTimestampNeedTimeZone(p, ctxt); } - return _handleUnexpectedToken(context, parser, "Expected array or string."); + return _handleUnexpectedToken(ctxt, p, "Expected array or string"); } protected boolean shouldReadTimestampsAsNanoseconds(DeserializationContext context) { @@ -195,11 +239,12 @@ protected LocalDateTime _fromString(JsonParser p, DeserializationContext ctxt, if (_formatter == DEFAULT_FORMATTER) { // ... only allow iff lenient mode enabled since // JavaScript by default includes time and zone in JSON serialized Dates (UTC/ISO instant format). - // And if so, do NOT use zoned date parsing as that can easily produce - // incorrect answer. if (string.length() > 10 && string.charAt(10) == 'T') { if (string.endsWith("Z")) { if (isLenient()) { + if (_useTimeZoneForLenientDateParsing) { + return Instant.parse(string).atZone(ctxt.getTimeZone().toZoneId()).toLocalDateTime(); + } return LocalDateTime.parse(string.substring(0, string.length()-1), _formatter); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserializer.java index da860cf3..f023be9a 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserializer.java @@ -103,51 +103,55 @@ protected JSR310DateTimeDeserializerBase _withFormatOverrides(Deserialization } @Override - public LocalTime deserialize(JsonParser parser, DeserializationContext context) throws IOException + public LocalTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - if (parser.hasToken(JsonToken.VALUE_STRING)) { - return _fromString(parser, context, parser.getText()); + if (p.hasToken(JsonToken.VALUE_STRING)) { + return _fromString(p, ctxt, p.getText()); } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - if (parser.isExpectedStartObjectToken()) { - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); + if (p.isExpectedStartObjectToken()) { + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + return _handleUnexpectedToken(ctxt, p, "Expected array or string"); } - if (parser.isExpectedStartArrayToken()) { - JsonToken t = parser.nextToken(); + if (p.isExpectedStartArrayToken()) { + JsonToken t = p.nextToken(); if (t == JsonToken.END_ARRAY) { return null; } - if (context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) + if (ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) && (t == JsonToken.VALUE_STRING || t==JsonToken.VALUE_EMBEDDED_OBJECT)) { - final LocalTime parsed = deserialize(parser, context); - if (parser.nextToken() != JsonToken.END_ARRAY) { - handleMissingEndArrayForSingle(parser, context); + final LocalTime parsed = deserialize(p, ctxt); + if (p.nextToken() != JsonToken.END_ARRAY) { + handleMissingEndArrayForSingle(p, ctxt); } return parsed; } if (t == JsonToken.VALUE_NUMBER_INT) { - int hour = parser.getIntValue(); + int hour = p.getIntValue(); - parser.nextToken(); - int minute = parser.getIntValue(); + p.nextToken(); + int minute = p.getIntValue(); LocalTime result; - t = parser.nextToken(); + t = p.nextToken(); if (t == JsonToken.END_ARRAY) { result = LocalTime.of(hour, minute); } else { - int second = parser.getIntValue(); - t = parser.nextToken(); + int second = p.getIntValue(); + t = p.nextToken(); if (t == JsonToken.END_ARRAY) { result = LocalTime.of(hour, minute, second); } else { - int partialSecond = parser.getIntValue(); - if(partialSecond < 1_000 && !shouldReadTimestampsAsNanoseconds(context)) + int partialSecond = p.getIntValue(); + if(partialSecond < 1_000 && !shouldReadTimestampsAsNanoseconds(ctxt)) partialSecond *= 1_000_000; // value is milliseconds, convert it to nanoseconds - t = parser.nextToken(); + t = p.nextToken(); if (t != JsonToken.END_ARRAY) { - throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY, + throw ctxt.wrongTokenException(p, handledType(), JsonToken.END_ARRAY, "Expected array to end"); } result = LocalTime.of(hour, minute, second, partialSecond); @@ -155,17 +159,17 @@ public LocalTime deserialize(JsonParser parser, DeserializationContext context) } return result; } - context.reportInputMismatch(handledType(), + ctxt.reportInputMismatch(handledType(), "Unexpected token (%s) within Array, expected VALUE_NUMBER_INT", t); } - if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { - return (LocalTime) parser.getEmbeddedObject(); + if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { + return (LocalTime) p.getEmbeddedObject(); } - if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) { - _throwNoNumericTimestampNeedTimeZone(parser, context); + if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) { + _throwNoNumericTimestampNeedTimeZone(p, ctxt); } - return _handleUnexpectedToken(context, parser, "Expected array or string."); + return _handleUnexpectedToken(ctxt, p, "Expected array or string"); } protected boolean shouldReadTimestampsAsNanoseconds(DeserializationContext context) { diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserializer.java index 449cf718..88d71697 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserializer.java @@ -62,50 +62,52 @@ protected MonthDayDeserializer withDateFormat(DateTimeFormatter dtf) { } @Override - public MonthDay deserialize(JsonParser parser, DeserializationContext context) throws IOException + public MonthDay deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - if (parser.hasToken(JsonToken.VALUE_STRING)) { - return _fromString(parser, context, parser.getText()); + if (p.hasToken(JsonToken.VALUE_STRING)) { + return _fromString(p, ctxt, p.getText()); } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - if (parser.isExpectedStartObjectToken()) { - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); - } - if (parser.isExpectedStartArrayToken()) { - JsonToken t = parser.nextToken(); + if (p.isExpectedStartObjectToken()) { + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + // fall through + } else if (p.isExpectedStartArrayToken()) { + JsonToken t = p.nextToken(); if (t == JsonToken.END_ARRAY) { return null; } if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT) - && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { - final MonthDay parsed = deserialize(parser, context); - if (parser.nextToken() != JsonToken.END_ARRAY) { - handleMissingEndArrayForSingle(parser, context); + && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { + final MonthDay parsed = deserialize(p, ctxt); + if (p.nextToken() != JsonToken.END_ARRAY) { + handleMissingEndArrayForSingle(p, ctxt); } return parsed; } if (t != JsonToken.VALUE_NUMBER_INT) { - _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "month"); + _reportWrongToken(ctxt, JsonToken.VALUE_NUMBER_INT, "month"); } - int month = parser.getIntValue(); - int day = parser.nextIntValue(-1); + int month = p.getIntValue(); + int day = p.nextIntValue(-1); if (day == -1) { - if (!parser.hasToken(JsonToken.VALUE_NUMBER_INT)) { - _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "day"); + if (!p.hasToken(JsonToken.VALUE_NUMBER_INT)) { + _reportWrongToken(ctxt, JsonToken.VALUE_NUMBER_INT, "day"); } - day = parser.getIntValue(); + day = p.getIntValue(); } - if (parser.nextToken() != JsonToken.END_ARRAY) { - throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY, + if (p.nextToken() != JsonToken.END_ARRAY) { + throw ctxt.wrongTokenException(p, handledType(), JsonToken.END_ARRAY, "Expected array to end"); } return MonthDay.of(month, day); + } else if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { + return (MonthDay) p.getEmbeddedObject(); } - if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { - return (MonthDay) parser.getEmbeddedObject(); - } - return _handleUnexpectedToken(context, parser, + return _handleUnexpectedToken(ctxt, p, JsonToken.VALUE_STRING, JsonToken.START_ARRAY); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserializer.java index 8331af7f..0a23ac39 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserializer.java @@ -99,75 +99,79 @@ protected JSR310DateTimeDeserializerBase _withFormatOverrides(Deserialization } @Override - public OffsetTime deserialize(JsonParser parser, DeserializationContext context) throws IOException + public OffsetTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - if (parser.hasToken(JsonToken.VALUE_STRING)) { - return _fromString(parser, context, parser.getText()); + if (p.hasToken(JsonToken.VALUE_STRING)) { + return _fromString(p, ctxt, p.getText()); } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - if (parser.isExpectedStartObjectToken()) { - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); + if (p.isExpectedStartObjectToken()) { + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + // fall through } - if (!parser.isExpectedStartArrayToken()) { - if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { - return (OffsetTime) parser.getEmbeddedObject(); + if (!p.isExpectedStartArrayToken()) { + if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { + return (OffsetTime) p.getEmbeddedObject(); } - if (parser.hasToken(JsonToken.VALUE_NUMBER_INT)) { - _throwNoNumericTimestampNeedTimeZone(parser, context); + if (p.hasToken(JsonToken.VALUE_NUMBER_INT)) { + _throwNoNumericTimestampNeedTimeZone(p, ctxt); } - throw context.wrongTokenException(parser, handledType(), JsonToken.START_ARRAY, + throw ctxt.wrongTokenException(p, handledType(), JsonToken.START_ARRAY, "Expected array or string."); } - JsonToken t = parser.nextToken(); + JsonToken t = p.nextToken(); if (t != JsonToken.VALUE_NUMBER_INT) { if (t == JsonToken.END_ARRAY) { return null; } if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT) - && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { - final OffsetTime parsed = deserialize(parser, context); - if (parser.nextToken() != JsonToken.END_ARRAY) { - handleMissingEndArrayForSingle(parser, context); + && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { + final OffsetTime parsed = deserialize(p, ctxt); + if (p.nextToken() != JsonToken.END_ARRAY) { + handleMissingEndArrayForSingle(p, ctxt); } return parsed; } - context.reportInputMismatch(handledType(), + ctxt.reportInputMismatch(handledType(), "Unexpected token (%s) within Array, expected VALUE_NUMBER_INT", t); } - int hour = parser.getIntValue(); - int minute = parser.nextIntValue(-1); + int hour = p.getIntValue(); + int minute = p.nextIntValue(-1); if (minute == -1) { - t = parser.getCurrentToken(); + t = p.getCurrentToken(); if (t == JsonToken.END_ARRAY) { return null; } if (t != JsonToken.VALUE_NUMBER_INT) { - _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "minutes"); + _reportWrongToken(ctxt, JsonToken.VALUE_NUMBER_INT, "minutes"); } - minute = parser.getIntValue(); + minute = p.getIntValue(); } int partialSecond = 0; int second = 0; - if (parser.nextToken() == JsonToken.VALUE_NUMBER_INT) { - second = parser.getIntValue(); - if (parser.nextToken() == JsonToken.VALUE_NUMBER_INT) { - partialSecond = parser.getIntValue(); - if (partialSecond < 1_000 && !shouldReadTimestampsAsNanoseconds(context)) { + if (p.nextToken() == JsonToken.VALUE_NUMBER_INT) { + second = p.getIntValue(); + if (p.nextToken() == JsonToken.VALUE_NUMBER_INT) { + partialSecond = p.getIntValue(); + if (partialSecond < 1_000 && !shouldReadTimestampsAsNanoseconds(ctxt)) { partialSecond *= 1_000_000; // value is milliseconds, convert it to nanoseconds } - parser.nextToken(); + p.nextToken(); } } - if (parser.getCurrentToken() == JsonToken.VALUE_STRING) { - OffsetTime result = OffsetTime.of(hour, minute, second, partialSecond, ZoneOffset.of(parser.getText())); - if (parser.nextToken() != JsonToken.END_ARRAY) { - _reportWrongToken(context, JsonToken.END_ARRAY, "timezone"); + if (p.getCurrentToken() == JsonToken.VALUE_STRING) { + OffsetTime result = OffsetTime.of(hour, minute, second, partialSecond, ZoneOffset.of(p.getText())); + if (p.nextToken() != JsonToken.END_ARRAY) { + _reportWrongToken(ctxt, JsonToken.END_ARRAY, "timezone"); } return result; } - throw context.wrongTokenException(parser, handledType(), JsonToken.VALUE_STRING, + throw ctxt.wrongTokenException(p, handledType(), JsonToken.VALUE_STRING, "Expected string for TimeZone after numeric values"); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserializer.java index ed626849..d6b71680 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserializer.java @@ -2,10 +2,9 @@ import java.io.IOException; import java.time.Month; -import java.util.regex.Pattern; import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; + import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.deser.std.DelegatingDeserializer; @@ -17,31 +16,57 @@ public class OneBasedMonthDeserializer extends DelegatingDeserializer { private static final long serialVersionUID = 1L; - private static final Pattern HAS_ONE_OR_TWO_DIGITS = Pattern.compile("^\\d{1,2}$"); - public OneBasedMonthDeserializer(JsonDeserializer defaultDeserializer) { super(defaultDeserializer); } @Override - public Object deserialize(JsonParser parser, DeserializationContext context) throws IOException { - JsonToken token = parser.currentToken(); - Month zeroBaseMonth = (Month) getDelegatee().deserialize(parser, context); - if (!_isNumericValue(parser.getText(), token)) { - return zeroBaseMonth; - } - if (zeroBaseMonth == Month.JANUARY) { - throw new InvalidFormatException(parser, "Month.JANUARY value not allowed for 1-based Month.", zeroBaseMonth, Month.class); + public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + switch (p.currentToken()) { + case VALUE_NUMBER_INT: + return _decodeMonth(p.getIntValue(), p); + case VALUE_STRING: + String monthSpec = p.getText(); + int oneBasedMonthNumber = _decodeNumber(monthSpec); + if (oneBasedMonthNumber >= 0) { + return _decodeMonth(oneBasedMonthNumber, p); + } + // Otherwise fall through to default handling + break; + default: } - return zeroBaseMonth.minus(1); + return getDelegatee().deserialize(p, ctxt); } - private boolean _isNumericValue(String text, JsonToken token) { - return token == JsonToken.VALUE_NUMBER_INT || _isNumberAsString(text, token); + /** + * @return Numeric value of input text that represents a 1-digit or 2-digit number. + * Negative value in other cases (empty string, not a number, 3 or more digits). + */ + private int _decodeNumber(String text) { + int numValue; + switch (text.length()) { + case 1: + char c = text.charAt(0); + boolean cValid = ('0' <= c && c <= '9'); + numValue = cValid ? (c - '0') : -1; + break; + case 2: + char c1 = text.charAt(0); + char c2 = text.charAt(1); + boolean c12valid = ('0' <= c1 && c1 <= '9' && '0' <= c2 && c2 <= '9'); + numValue = c12valid ? (10 * (c1 - '0') + (c2 - '0')) : -1; + break; + default: + numValue = -1; + } + return numValue; } - private boolean _isNumberAsString(String text, JsonToken token) { - return token == JsonToken.VALUE_STRING && HAS_ONE_OR_TWO_DIGITS.matcher(text).matches(); + private Month _decodeMonth(int oneBasedMonthNumber, JsonParser parser) throws InvalidFormatException { + if (Month.JANUARY.getValue() <= oneBasedMonthNumber && oneBasedMonthNumber <= Month.DECEMBER.getValue()) { + return Month.of(oneBasedMonthNumber); + } + throw new InvalidFormatException(parser, "Month number " + oneBasedMonthNumber + " not allowed for 1-based Month.", oneBasedMonthNumber, Integer.class); } @Override diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserializer.java index d34db205..7e6c3603 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserializer.java @@ -79,27 +79,28 @@ protected YearDeserializer withLeniency(Boolean leniency) { } @Override - public Year deserialize(JsonParser parser, DeserializationContext context) throws IOException + public Year deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - JsonToken t = parser.currentToken(); + JsonToken t = p.currentToken(); if (t == JsonToken.VALUE_STRING) { - return _fromString(parser, context, parser.getText()); + return _fromString(p, ctxt, p.getText()); } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) if (t == JsonToken.START_OBJECT) { - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); - } - if (t == JsonToken.VALUE_NUMBER_INT) { - return _fromNumber(context, parser.getIntValue()); - } - if (t == JsonToken.VALUE_EMBEDDED_OBJECT) { - return (Year) parser.getEmbeddedObject(); - } - if (parser.hasToken(JsonToken.START_ARRAY)){ - return _deserializeFromArray(parser, context); + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + // fall through + } else if (t == JsonToken.VALUE_NUMBER_INT) { + return _fromNumber(ctxt, p.getIntValue()); + } else if (t == JsonToken.VALUE_EMBEDDED_OBJECT) { + return (Year) p.getEmbeddedObject(); + } else if (p.isExpectedStartArrayToken()){ + return _deserializeFromArray(p, ctxt); } - return _handleUnexpectedToken(context, parser, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT); + return _handleUnexpectedToken(ctxt, p, JsonToken.VALUE_STRING, JsonToken.VALUE_NUMBER_INT); } protected Year _fromString(JsonParser p, DeserializationContext ctxt, diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserializer.java index 58709ffa..4114343e 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserializer.java @@ -81,50 +81,52 @@ protected YearMonthDeserializer withLeniency(Boolean leniency) { } @Override - public YearMonth deserialize(JsonParser parser, DeserializationContext context) throws IOException + public YearMonth deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { - if (parser.hasToken(JsonToken.VALUE_STRING)) { - return _fromString(parser, context, parser.getText()); + if (p.hasToken(JsonToken.VALUE_STRING)) { + return _fromString(p, ctxt, p.getText()); } // 30-Sep-2020, tatu: New! "Scalar from Object" (mostly for XML) - if (parser.isExpectedStartObjectToken()) { - return _fromString(parser, context, - context.extractScalarFromObject(parser, this, handledType())); - } - if (parser.isExpectedStartArrayToken()) { - JsonToken t = parser.nextToken(); + if (p.isExpectedStartObjectToken()) { + // 17-May-2025, tatu: [databind#4656] need to check for `null` + String str = ctxt.extractScalarFromObject(p, this, handledType()); + if (str != null) { + return _fromString(p, ctxt, str); + } + // fall through + } else if (p.isExpectedStartArrayToken()) { + JsonToken t = p.nextToken(); if (t == JsonToken.END_ARRAY) { return null; } if ((t == JsonToken.VALUE_STRING || t == JsonToken.VALUE_EMBEDDED_OBJECT) - && context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { - final YearMonth parsed = deserialize(parser, context); - if (parser.nextToken() != JsonToken.END_ARRAY) { - handleMissingEndArrayForSingle(parser, context); + && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) { + final YearMonth parsed = deserialize(p, ctxt); + if (p.nextToken() != JsonToken.END_ARRAY) { + handleMissingEndArrayForSingle(p, ctxt); } return parsed; } if (t != JsonToken.VALUE_NUMBER_INT) { - _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "years"); + _reportWrongToken(ctxt, JsonToken.VALUE_NUMBER_INT, "years"); } - int year = parser.getIntValue(); - int month = parser.nextIntValue(-1); + int year = p.getIntValue(); + int month = p.nextIntValue(-1); if (month == -1) { - if (!parser.hasToken(JsonToken.VALUE_NUMBER_INT)) { - _reportWrongToken(context, JsonToken.VALUE_NUMBER_INT, "months"); + if (!p.hasToken(JsonToken.VALUE_NUMBER_INT)) { + _reportWrongToken(ctxt, JsonToken.VALUE_NUMBER_INT, "months"); } - month = parser.getIntValue(); + month = p.getIntValue(); } - if (parser.nextToken() != JsonToken.END_ARRAY) { - throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY, + if (p.nextToken() != JsonToken.END_ARRAY) { + throw ctxt.wrongTokenException(p, handledType(), JsonToken.END_ARRAY, "Expected array to end"); } return YearMonth.of(year, month); + } else if (p.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { + return (YearMonth) p.getEmbeddedObject(); } - if (parser.hasToken(JsonToken.VALUE_EMBEDDED_OBJECT)) { - return (YearMonth) parser.getEmbeddedObject(); - } - return _handleUnexpectedToken(context, parser, + return _handleUnexpectedToken(ctxt, p, JsonToken.VALUE_STRING, JsonToken.START_ARRAY); } diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java index 0ed65bcd..0340d177 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializer.java @@ -22,6 +22,10 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import com.fasterxml.jackson.core.util.JacksonFeatureSet; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; + /** * Serializer for Java 8 temporal {@link Instant}s, {@link OffsetDateTime}, and {@link ZonedDateTime}s. * @@ -34,10 +38,20 @@ public class InstantSerializer extends InstantSerializerBase public static final InstantSerializer INSTANCE = new InstantSerializer(); + /** + * Whether {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature#ALWAYS_WRITE_SUBSECOND_DIGITS} + * is enabled: if so, the default representation is padded to at least 3 sub-second + * digits. + * + * @since 2.23 + */ + private final boolean _alwaysWriteSubsecondDigits; + protected InstantSerializer() { super(Instant.class, Instant::toEpochMilli, Instant::getEpochSecond, Instant::getNano, // null -> use 'value.toString()', default format null); + _alwaysWriteSubsecondDigits = false; } @Deprecated // since 2.14 @@ -52,11 +66,63 @@ protected InstantSerializer(InstantSerializer base, protected InstantSerializer(InstantSerializer base, Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) { super(base, useTimestamp, base._useNanoseconds, formatter, shape); + _alwaysWriteSubsecondDigits = base._alwaysWriteSubsecondDigits; } protected InstantSerializer(InstantSerializer base, Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter formatter) { super(base, useTimestamp, useNanoseconds, formatter); + _alwaysWriteSubsecondDigits = base._alwaysWriteSubsecondDigits; + } + + /** + * @since 2.23 + */ + protected InstantSerializer(InstantSerializer base, boolean alwaysWriteSubsecondDigits) { + super(base, base._useTimestamp, base._useNanoseconds, base._formatter, base._shape); + _alwaysWriteSubsecondDigits = alwaysWriteSubsecondDigits; + } + + /** + * Method called by {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeModule} + * to apply module-level {@link JavaTimeFeature} settings. + * + * @since 2.23 + */ + public InstantSerializer withFeatures(JacksonFeatureSet features) { + if (features.isEnabled(JavaTimeFeature.ALWAYS_WRITE_SUBSECOND_DIGITS)) { + return new InstantSerializer(this, true); + } + return this; + } + + /** + * Overridden to implement + * {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature#ALWAYS_WRITE_SUBSECOND_DIGITS} + * by padding the default representation, instead of swapping in a different formatter. + *

+ * Rationale: the default representation is {@link Instant#toString()}, that is, + * {@link DateTimeFormatter#ISO_INSTANT}, which writes exactly 0, 3, 6 or 9 sub-second + * digits -- so the only case needing a fix is the zero one. Formatting through a + * zone-bound {@code DateTimeFormatter} instead would resolve the value via + * {@link java.time.LocalDateTime}, whose year range is narrower than that of + * {@code Instant}, and would thereby fail for {@link Instant#MIN} / {@link Instant#MAX}. + * + * @since 2.23 + */ + @Override + protected String formatValue(Instant value, SerializerProvider provider) + { + String formatted = super.formatValue(value, provider); + // Only applies to the default representation: an explicit formatter wins + if (_alwaysWriteSubsecondDigits && (_formatter == null) && (value.getNano() == 0)) { + final int last = formatted.length() - 1; + // Defensive: `ISO_INSTANT` always ends in 'Z', but do not corrupt output if not + if ((last >= 0) && (formatted.charAt(last) == 'Z')) { + formatted = formatted.substring(0, last) + ".000Z"; + } + } + return formatted; } @Override diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java index 1305a1c2..81c3ebd8 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerializerBase.java @@ -47,8 +47,6 @@ public abstract class InstantSerializerBase extends JSR310FormattedSerializerBase { - private final DateTimeFormatter defaultFormat; - private final ToLongFunction getEpochMillis; private final ToLongFunction getEpochSeconds; @@ -61,8 +59,7 @@ protected InstantSerializerBase(Class supportedType, ToLongFunction getEpo { // Bit complicated, just because we actually want to "hide" default formatter, // so that it won't accidentally force use of textual presentation - super(supportedType, null); - this.defaultFormat = defaultFormat; + super(supportedType, null, defaultFormat); this.getEpochMillis = getEpochMillis; this.getEpochSeconds = getEpochSeconds; this.getNanoseconds = getNanoseconds; @@ -86,7 +83,18 @@ protected InstantSerializerBase(InstantSerializerBase base, protected InstantSerializerBase(InstantSerializerBase base, Boolean useTimestamp, Boolean useNanoseconds, DateTimeFormatter dtf, JsonFormat.Shape shape) { super(base, useTimestamp, useNanoseconds, dtf, shape); - defaultFormat = base.defaultFormat; + getEpochMillis = base.getEpochMillis; + getEpochSeconds = base.getEpochSeconds; + getNanoseconds = base.getNanoseconds; + } + + /** + * @since 2.23 + */ + protected InstantSerializerBase(InstantSerializerBase base, + DateTimeFormatter defaultFormat) + { + super(base, defaultFormat); getEpochMillis = base.getEpochMillis; getEpochSeconds = base.getEpochSeconds; getNanoseconds = base.getNanoseconds; @@ -147,7 +155,7 @@ protected JsonToken serializationShape(SerializerProvider provider) { // @since 2.12 protected String formatValue(T value, SerializerProvider provider) { - DateTimeFormatter formatter = (_formatter == null) ? defaultFormat :_formatter; + DateTimeFormatter formatter = (_formatter == null) ? _defaultFormat :_formatter; if (formatter != null) { if (formatter.getZone() == null) { // timezone set if annotated on property // If the user specified to use the context TimeZone explicitly, and the formatter provided doesn't contain a TZ diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java index 870b0268..b17bd3f3 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/JSR310FormattedSerializerBase.java @@ -65,6 +65,18 @@ abstract class JSR310FormattedSerializerBase */ protected final DateTimeFormatter _formatter; + /** + * Format to use when no explicit {@link #_formatter} is configured. Unlike + * {@code _formatter}, a non-null value here does NOT force serialization as a + * JSON String -- which is exactly why the two cannot be collapsed into one. + *

+ * May be {@code null}, in which case the sub-class decides the fallback + * (typically either a JDK {@code ISO_*} constant or {@code value.toString()}). + * + * @since 2.23 + */ + protected final DateTimeFormatter _defaultFormat; + protected final JsonFormat.Shape _shape; /** @@ -81,13 +93,22 @@ protected JSR310FormattedSerializerBase(Class supportedType) { protected JSR310FormattedSerializerBase(Class supportedType, DateTimeFormatter formatter) { + this(supportedType, formatter, null); + } + + /** + * @since 2.23 + */ + protected JSR310FormattedSerializerBase(Class supportedType, + DateTimeFormatter formatter, DateTimeFormatter defaultFormat) { super(supportedType); _useTimestamp = null; _useNanoseconds = null; _shape = null; _formatter = formatter; + _defaultFormat = defaultFormat; } - + protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase base, Boolean useTimestamp, DateTimeFormatter dtf, JsonFormat.Shape shape) { @@ -103,6 +124,27 @@ protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase base, _useNanoseconds = useNanoseconds; _formatter = dtf; _shape = shape; + _defaultFormat = base._defaultFormat; + } + + /** + * Copy-constructor used for replacing the default format -- and only that -- + * of an existing serializer; needed for + * {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature#ALWAYS_WRITE_SUBSECOND_DIGITS}. + * Note that the replacement must NOT be passed as {@code _formatter}, since a + * non-null {@code _formatter} also forces serialization as a JSON String. + * + * @since 2.23 + */ + protected JSR310FormattedSerializerBase(JSR310FormattedSerializerBase base, + DateTimeFormatter defaultFormat) + { + super(base.handledType()); + _useTimestamp = base._useTimestamp; + _useNanoseconds = base._useNanoseconds; + _formatter = base._formatter; + _shape = base._shape; + _defaultFormat = defaultFormat; } protected abstract JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, @@ -219,7 +261,7 @@ protected SerializationFeature getTimestampsFeature() { return SerializationFeature.WRITE_DATES_AS_TIMESTAMPS; } - protected boolean useTimestamp(SerializerProvider provider) { + protected boolean useTimestamp(SerializerProvider ctxt) { if (_useTimestamp != null) { return _useTimestamp.booleanValue(); } @@ -232,8 +274,13 @@ protected boolean useTimestamp(SerializerProvider provider) { } } // assume that explicit formatter definition implies use of textual format - return (_formatter == null) && (provider != null) - && provider.isEnabled(getTimestampsFeature()); + return (_formatter == null) && useTimestampFromGlobalDefaults(ctxt); + } + + // @since 2.19 + protected boolean useTimestampFromGlobalDefaults(SerializerProvider ctxt) { + return (ctxt != null) + && ctxt.isEnabled(getTimestampsFeature()); } protected boolean _useTimestampExplicitOnly(SerializerProvider provider) { diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java index 4383a36a..f22bbf9d 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerializer.java @@ -25,8 +25,10 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.type.WritableTypeId; +import com.fasterxml.jackson.core.util.JacksonFeatureSet; import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.jsontype.TypeSerializer; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; /** * Serializer for Java 8 temporal {@link LocalDateTime}s. @@ -39,13 +41,13 @@ public class LocalDateTimeSerializer extends JSR310FormattedSerializerBase features) { + if (features.isEnabled(JavaTimeFeature.ALWAYS_WRITE_SUBSECOND_DIGITS)) { + return new LocalDateTimeSerializer(this, SubSecondFormatters.LOCAL_DATE_TIME); + } + return this; + } + @Override protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter f, JsonFormat.Shape shape) { return new LocalDateTimeSerializer(this, useTimestamp, _useNanoseconds, f); } protected DateTimeFormatter _defaultFormatter() { - return DateTimeFormatter.ISO_LOCAL_DATE_TIME; + return _defaultFormat; } @Override diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java index ff7bd4c7..7bdd0e9c 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerializer.java @@ -4,6 +4,9 @@ import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import com.fasterxml.jackson.core.util.JacksonFeatureSet; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; + public class OffsetDateTimeSerializer extends InstantSerializerBase { private static final long serialVersionUID = 1L; @@ -35,6 +38,49 @@ public OffsetDateTimeSerializer(OffsetDateTimeSerializer base, Boolean useTimest super(base, useTimestamp, base._useNanoseconds, formatter, shape); } + /** + * @since 2.23 + */ + protected OffsetDateTimeSerializer(OffsetDateTimeSerializer base, + DateTimeFormatter defaultFormat) { + super(base, defaultFormat); + } + + /** + * Method called by {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeModule} + * to apply module-level {@link JavaTimeFeature} settings. + * + * @since 2.23 + */ + public OffsetDateTimeSerializer withFeatures(JacksonFeatureSet features) { + if (features.isEnabled(JavaTimeFeature.ALWAYS_WRITE_SUBSECOND_DIGITS)) { + return new OffsetDateTimeSerializer(this, SubSecondFormatters.OFFSET_DATE_TIME); + } + return this; + } + + /** + * Method for constructing a new {@code OffsetDateTimeSerializer} with settings + * of this serializer but with custom {@link DateTimeFormatter} overrides. + * Commonly used on {@code INSTANCE} like so: + *

+     *  DateTimeFormatter dtf = new DateTimeFormatterBuilder()
+     *          .append(DateTimeFormatter.ISO_LOCAL_DATE)
+     *          .appendLiteral('T')
+     *          // and so on
+     *          .toFormatter();
+     *  OffsetDateTimeSerializer ser = OffsetDateTimeSerializer.INSTANCE
+     *          .withFormatter(dtf);
+     *  // register via Module
+     *
+ * + * @since 2.21 + */ + public OffsetDateTimeSerializer withFormatter(DateTimeFormatter formatter) + { + return new OffsetDateTimeSerializer(this, _useTimestamp, formatter, _shape); + } + @Override protected JSR310FormattedSerializerBase withFormat(Boolean useTimestamp, DateTimeFormatter formatter, JsonFormat.Shape shape) diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java index f1dec8b9..30fc1037 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerializer.java @@ -89,7 +89,7 @@ public void serializeWithType(OffsetTime value, JsonGenerator g, SerializerProvi typeSer.writeTypeSuffix(g, typeIdDef); } - private final void _serializeAsArrayContents(OffsetTime value, JsonGenerator g, + protected void _serializeAsArrayContents(OffsetTime value, JsonGenerator g, SerializerProvider provider) throws IOException { g.writeNumber(value.getHour()); @@ -99,7 +99,7 @@ private final void _serializeAsArrayContents(OffsetTime value, JsonGenerator g, if ((secs > 0) || (nanos > 0)) { g.writeNumber(secs); if (nanos > 0) { - if(useNanoseconds(provider)) { + if (useNanoseconds(provider)) { g.writeNumber(nanos); } else { g.writeNumber(value.get(ChronoField.MILLI_OF_SECOND)); diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/SubSecondFormatters.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/SubSecondFormatters.java new file mode 100644 index 00000000..825bf845 --- /dev/null +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/SubSecondFormatters.java @@ -0,0 +1,91 @@ +package com.fasterxml.jackson.datatype.jsr310.ser; + +import java.time.chrono.IsoChronology; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.time.format.ResolverStyle; +import java.time.temporal.ChronoField; + +/** + * Container for the ISO-8601 {@link DateTimeFormatter}s used in place of the + * JDK-provided defaults when + * {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature#ALWAYS_WRITE_SUBSECOND_DIGITS} + * is enabled. + *

+ * These differ from the JDK counterparts only in the sub-second field: instead of + * omitting it when zero, at least 3 (millisecond) digits are always written, and up + * to 9 when the value carries higher precision (so nothing is truncated). They are + * otherwise built the same way -- including {@link ResolverStyle#STRICT} and the ISO + * chronology -- so that they remain drop-in replacements even though they are only + * ever used for printing here. + *

+ * Note that there is deliberately no counterpart of {@link DateTimeFormatter#ISO_INSTANT} + * here: formatting an {@link java.time.Instant} through a zone-bound formatter goes via + * {@link java.time.LocalDateTime}, whose year range is narrower than {@code Instant}'s, + * so {@link java.time.Instant#MIN} / {@link java.time.Instant#MAX} would fail. See + * {@link InstantSerializer#formatValue} for the handling used instead. + * + * @since 2.23 + */ +class SubSecondFormatters +{ + private SubSecondFormatters() { } + + /** + * Date and time down to the seconds field, followed by 3 to 9 sub-second digits: + * the shared prefix of all formatters here. + */ + private static DateTimeFormatterBuilder _localDateTimeBuilder() { + return new DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(ChronoField.HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(ChronoField.MINUTE_OF_HOUR, 2) + .appendLiteral(':') + .appendValue(ChronoField.SECOND_OF_MINUTE, 2) + .appendFraction(ChronoField.NANO_OF_SECOND, 3, 9, true); + } + + /** + * {@link #_localDateTimeBuilder()} followed by the offset: the shared prefix of the + * offset- and zone-based formatters. + */ + private static DateTimeFormatterBuilder _offsetDateTimeBuilder() { + return _localDateTimeBuilder().appendOffsetId(); + } + + /** + * Completes a builder the way the JDK completes its own {@code ISO_*} constants. + *

+ * Note that {@link DateTimeFormatterBuilder#toFormatter()} alone would yield + * {@link ResolverStyle#SMART} and no chronology, which is not what the constants + * these replace do. + */ + private static DateTimeFormatter _isoFormatter(DateTimeFormatterBuilder b) { + return b.toFormatter() + .withResolverStyle(ResolverStyle.STRICT) + .withChronology(IsoChronology.INSTANCE); + } + + /** + * Counterpart of {@link DateTimeFormatter#ISO_LOCAL_DATE_TIME}. + */ + final static DateTimeFormatter LOCAL_DATE_TIME = _isoFormatter(_localDateTimeBuilder()); + + /** + * Counterpart of {@link DateTimeFormatter#ISO_OFFSET_DATE_TIME}. + */ + final static DateTimeFormatter OFFSET_DATE_TIME = _isoFormatter(_offsetDateTimeBuilder()); + + /** + * Counterpart of {@link DateTimeFormatter#ISO_ZONED_DATE_TIME}, that is, + * {@link #OFFSET_DATE_TIME} with the optional {@code [Zone/Id]} suffix. + */ + final static DateTimeFormatter ZONED_DATE_TIME = _isoFormatter(_offsetDateTimeBuilder() + .optionalStart() + .appendLiteral('[') + .parseCaseSensitive() + .appendZoneRegionId() + .appendLiteral(']')); +} diff --git a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java index f84f950b..58d0817c 100644 --- a/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java +++ b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerializer.java @@ -7,8 +7,10 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonToken; +import com.fasterxml.jackson.core.util.JacksonFeatureSet; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; public class ZonedDateTimeSerializer extends InstantSerializerBase { private static final long serialVersionUID = 1L; @@ -21,7 +23,21 @@ public class ZonedDateTimeSerializer extends InstantSerializerBase + * Separate from the inherited {@code _defaultFormat} on purpose: writing with the + * zone id is a second output shape that has always used {@code ISO_ZONED_DATE_TIME} + * regardless of the default format (including a custom one passed to + * {@link #ZonedDateTimeSerializer(DateTimeFormatter)}), so the two cannot be + * collapsed without changing existing behaviour. + * + * @since 2.23 + */ + protected final DateTimeFormatter _zoneIdFormat; + protected ZonedDateTimeSerializer() { // ISO_ZONED_DATE_TIME is an extended version of ISO compliant format // ISO_OFFSET_DATE_TIME with additional information :Zone Id @@ -34,6 +50,7 @@ public ZonedDateTimeSerializer(DateTimeFormatter formatter) { ZonedDateTime::toEpochSecond, ZonedDateTime::getNano, formatter); _writeZoneId = null; + _zoneIdFormat = null; } protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base, @@ -56,6 +73,31 @@ protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base, JsonFormat.Shape shape, Boolean writeZoneId) { super(base, useTimestamp, useNanoseconds, formatter, shape); _writeZoneId = writeZoneId; + _zoneIdFormat = base._zoneIdFormat; + } + + /** + * @since 2.23 + */ + protected ZonedDateTimeSerializer(ZonedDateTimeSerializer base, + DateTimeFormatter defaultFormat, DateTimeFormatter zoneIdFormat) { + super(base, defaultFormat); + _writeZoneId = base._writeZoneId; + _zoneIdFormat = zoneIdFormat; + } + + /** + * Method called by {@link com.fasterxml.jackson.datatype.jsr310.JavaTimeModule} + * to apply module-level {@link JavaTimeFeature} settings. + * + * @since 2.23 + */ + public ZonedDateTimeSerializer withFeatures(JacksonFeatureSet features) { + if (features.isEnabled(JavaTimeFeature.ALWAYS_WRITE_SUBSECOND_DIGITS)) { + return new ZonedDateTimeSerializer(this, + SubSecondFormatters.OFFSET_DATE_TIME, SubSecondFormatters.ZONED_DATE_TIME); + } + return this; } @Override @@ -89,7 +131,7 @@ public void serialize(ZonedDateTime value, JsonGenerator g, SerializerProvider p ; // use default handling } else if (shouldWriteWithZoneId(provider)) { // write with zone - g.writeString(DateTimeFormatter.ISO_ZONED_DATE_TIME.format(value)); + g.writeString(_zoneIdFormatter().format(value)); return; } } @@ -110,6 +152,13 @@ protected String formatValue(ZonedDateTime value, SerializerProvider provider) { return formatted; } + /** + * @since 2.23 + */ + protected DateTimeFormatter _zoneIdFormatter() { + return (_zoneIdFormat == null) ? DateTimeFormatter.ISO_ZONED_DATE_TIME : _zoneIdFormat; + } + /** * @since 2.8 */ diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java index 1ed01eb9..272ec0c4 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ModuleTestBase.java @@ -12,6 +12,9 @@ import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + public class ModuleTestBase { protected static final ZoneId UTC = ZoneId.of("UTC"); diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDecimalUtils.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDecimalUtils.java index 5b9be3dc..b961fb39 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDecimalUtils.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestDecimalUtils.java @@ -1,10 +1,12 @@ package com.fasterxml.jackson.datatype.jsr310; -import org.junit.Test; - import java.math.BigDecimal; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class TestDecimalUtils extends ModuleTestBase { @@ -12,87 +14,34 @@ public class TestDecimalUtils extends ModuleTestBase public void testToDecimal01() { String decimal = DecimalUtils.toDecimal(0, 0); - assertEquals("The returned decimal is not correct.", NO_NANOSECS_SER, decimal); + assertEquals(NO_NANOSECS_SER, decimal, "The returned decimal is not correct."); decimal = DecimalUtils.toDecimal(15, 72); - assertEquals("The returned decimal is not correct.", "15.000000072", decimal); + assertEquals("15.000000072", decimal, "The returned decimal is not correct."); decimal = DecimalUtils.toDecimal(19827342231L, 192837465); - assertEquals("The returned decimal is not correct.", "19827342231.192837465", decimal); + assertEquals("19827342231.192837465", decimal, "The returned decimal is not correct."); decimal = DecimalUtils.toDecimal(19827342231L, 0); - assertEquals("The returned decimal is not correct.", - "19827342231"+NO_NANOSECS_SUFFIX, decimal); + assertEquals("19827342231"+NO_NANOSECS_SUFFIX, decimal, + "The returned decimal is not correct."); decimal = DecimalUtils.toDecimal(19827342231L, 999888000); - assertEquals("The returned decimal is not correct.", - "19827342231.999888000", decimal); + assertEquals("19827342231.999888000", decimal, + "The returned decimal is not correct."); decimal = DecimalUtils.toDecimal(-22704862, 599000000); - assertEquals("The returned decimal is not correct.", - "-22704862.599000000", decimal); - } - - @SuppressWarnings("deprecation") - private void checkExtractNanos(long expectedSeconds, int expectedNanos, BigDecimal decimal) - { - long seconds = decimal.longValue(); - assertEquals("The second part is not correct.", expectedSeconds, seconds); - - int nanoseconds = DecimalUtils.extractNanosecondDecimal(decimal, seconds); - assertEquals("The nanosecond part is not correct.", expectedNanos, nanoseconds); - } - - @Test - public void testExtractNanosecondDecimal01() - { - BigDecimal value = new BigDecimal("0"); - checkExtractNanos(0L, 0, value); - } - - @Test - public void testExtractNanosecondDecimal02() - { - BigDecimal value = new BigDecimal("15.000000072"); - checkExtractNanos(15L, 72, value); - } - - @Test - public void testExtractNanosecondDecimal03() - { - BigDecimal value = new BigDecimal("15.72"); - checkExtractNanos(15L, 720000000, value); - } - - @Test - public void testExtractNanosecondDecimal04() - { - BigDecimal value = new BigDecimal("19827342231.192837465"); - checkExtractNanos(19827342231L, 192837465, value); + assertEquals("-22704862.599000000", decimal, + "The returned decimal is not correct."); } - @Test - public void testExtractNanosecondDecimal05() - { - BigDecimal value = new BigDecimal("19827342231"); - checkExtractNanos(19827342231L, 0, value); - } - - @Test - public void testExtractNanosecondDecimal06() - { - BigDecimal value = new BigDecimal("19827342231.999999999"); - checkExtractNanos(19827342231L, 999999999, value); - } - - private void checkExtractSecondsAndNanos(long expectedSeconds, int expectedNanos, BigDecimal decimal) { DecimalUtils.extractSecondsAndNanos(decimal, (Long s, Integer ns) -> { - assertEquals("The second part is not correct.", expectedSeconds, s.longValue()); - assertEquals("The nanosecond part is not correct.", expectedNanos, ns.intValue()); + assertEquals(expectedSeconds, s.longValue(), "The second part is not correct."); + assertEquals(expectedNanos, ns.intValue(), "The nanosecond part is not correct."); return null; - }); + }, true); } @Test @@ -144,7 +93,8 @@ public void testExtractSecondsAndNanosFromNegativeBigDecimal() checkExtractSecondsAndNanos(-22704862L, 599000000, value); } - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testExtractSecondsAndNanos07() { BigDecimal value = new BigDecimal("1e10000000"); diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestFeatures.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestFeatures.java index 7b1952a9..a327a8c5 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestFeatures.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/TestFeatures.java @@ -16,32 +16,33 @@ package com.fasterxml.jackson.datatype.jsr310; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class TestFeatures { @Test public void testWriteDateTimestampsAsNanosecondsSettingEnabledByDefault() { - assertTrue("Write date timestamps as nanoseconds setting should be enabled by default.", - SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault()); + assertTrue(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault(), + "Write date timestamps as nanoseconds setting should be enabled by default."); } @Test public void testReadDateTimestampsAsNanosecondsSettingEnabledByDefault() { - assertTrue("Read date timestamps as nanoseconds setting should be enabled by default.", - DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault()); + assertTrue(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault(), + "Read date timestamps as nanoseconds setting should be enabled by default."); } @Test public void testAdjustDatesToContextTimeZoneSettingEnabledByDefault() { - assertTrue("Adjust dates to context time zone setting should be enabled by default.", - DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE.enabledByDefault()); + assertTrue(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE.enabledByDefault(), + "Adjust dates to context time zone setting should be enabled by default."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DefaultTypingTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DefaultTypingTest.java index 17bf71e5..ffdb92f1 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DefaultTypingTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DefaultTypingTest.java @@ -2,13 +2,13 @@ import java.time.ZoneId; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + public class DefaultTypingTest extends ModuleTestBase { private final ObjectMapper TYPING_MAPPER = newMapper(); diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/DurationDeser337Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeser337Test.java similarity index 92% rename from datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/DurationDeser337Test.java rename to datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeser337Test.java index 94f1f0f1..6787935e 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/DurationDeser337Test.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeser337Test.java @@ -1,14 +1,14 @@ -package com.fasterxml.jackson.datatype.jsr310.failing; +package com.fasterxml.jackson.datatype.jsr310.deser; import java.time.Duration; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; public class DurationDeser337Test extends ModuleTestBase { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserTest.java index 9e891477..2be9a3c1 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/DurationDeserTest.java @@ -5,6 +5,10 @@ import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAmount; import java.util.Map; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat.Feature; @@ -17,12 +21,8 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; public class DurationDeserTest extends ModuleTestBase { @@ -63,7 +63,7 @@ public void testDeserializationAsFloat01() throws Exception { Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60.0"); - assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + assertEquals(Duration.ofSeconds(60L, 0), value, "The value is not correct."); } @Test @@ -71,7 +71,7 @@ public void testDeserializationAsFloat02() throws Exception { Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60.0"); - assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + assertEquals(Duration.ofSeconds(60L, 0), value, "The value is not correct."); } @Test @@ -79,7 +79,7 @@ public void testDeserializationAsFloat03() throws Exception { Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498.000008374"); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value); + assertEquals(Duration.ofSeconds(13498L, 8374), value, "The value is not correct."); } @Test @@ -87,7 +87,7 @@ public void testDeserializationAsFloat04() throws Exception { Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498.000008374"); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value); + assertEquals(Duration.ofSeconds(13498L, 8374), value, "The value is not correct."); } /** @@ -116,12 +116,14 @@ public void testDeserializationAsFloatEdgeCase02() throws Exception assertEquals(0, value.getNano()); } - @Test(expected = ArithmeticException.class) + @Test public void testDeserializationAsFloatEdgeCase03() throws Exception { // Duration can't go this low - READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) - .readValue(Long.MIN_VALUE + ".1"); + assertThrows(ArithmeticException.class, () -> { + READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) + .readValue(Long.MIN_VALUE + ".1"); + }); } /* @@ -174,7 +176,8 @@ public void testDeserializationAsFloatEdgeCase07() throws Exception * Numbers with very large exponents can take a long time, but still result in zero. * https://github.com/FasterXML/jackson-databind/issues/2141 */ - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase08() throws Exception { String input = "1e10000000"; @@ -183,7 +186,8 @@ public void testDeserializationAsFloatEdgeCase08() throws Exception assertEquals(0, value.getSeconds()); } - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase09() throws Exception { String input = "-1e10000000"; @@ -195,7 +199,8 @@ public void testDeserializationAsFloatEdgeCase09() throws Exception /** * Same for large negative exponents. */ - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase10() throws Exception { String input = "1e-10000000"; @@ -204,7 +209,8 @@ public void testDeserializationAsFloatEdgeCase10() throws Exception assertEquals(0, value.getSeconds()); } - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase11() throws Exception { String input = "-1e-10000000"; @@ -218,7 +224,7 @@ public void testDeserializationAsInt01() throws Exception { Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60"); - assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + assertEquals(Duration.ofSeconds(60L, 0), value, "The value is not correct."); } @Test @@ -226,7 +232,7 @@ public void testDeserializationAsInt02() throws Exception { Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60000"); - assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + assertEquals(Duration.ofSeconds(60L, 0), value, "The value is not correct."); } @Test @@ -234,7 +240,7 @@ public void testDeserializationAsInt03() throws Exception { Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498"); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 0), value); + assertEquals(Duration.ofSeconds(13498L, 0), value, "The value is not correct."); } @Test @@ -242,7 +248,7 @@ public void testDeserializationAsInt04() throws Exception { Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498000"); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 0), value); + assertEquals(Duration.ofSeconds(13498L, 0), value, "The value is not correct."); } @Test @@ -253,7 +259,7 @@ public void testDeserializationAsInt05() throws Exception new WrapperWithReadTimestampsAsNanosEnabled(Duration.ofSeconds(13498L, 0)); WrapperWithReadTimestampsAsNanosEnabled actual = reader.readValue(wrapperPayload(13498)); - assertEquals("The value is not correct.", expected.value, actual.value); + assertEquals(expected.value, actual.value, "The value is not correct."); } @Test @@ -264,7 +270,7 @@ public void testDeserializationAsInt06() throws Exception new WrapperWithReadTimestampsAsNanosDisabled(Duration.ofSeconds(13498L, 0)); WrapperWithReadTimestampsAsNanosDisabled actual = reader.readValue(wrapperPayload(13498000)); - assertEquals("The value is not correct.", expected.value, actual.value); + assertEquals(expected.value, actual.value, "The value is not correct."); } @Test @@ -272,7 +278,7 @@ public void testDeserializationAsString01() throws Exception { Duration exp = Duration.ofSeconds(60L, 0); Duration value = READER.readValue('"' + exp.toString() + '"'); - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } @Test @@ -280,13 +286,13 @@ public void testDeserializationAsString02() throws Exception { Duration exp = Duration.ofSeconds(13498L, 8374); Duration value = READER.readValue('"' + exp.toString() + '"'); - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } @Test public void testDeserializationAsString03() throws Exception { - assertNull("The value should be null.", READER.readValue("\" \"")); + assertNull(READER.readValue("\" \""), "The value should be null."); } @Test @@ -301,8 +307,8 @@ public void testDeserializationWithTypeInfo01() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + "13498.000008374]", TemporalAmount.class); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", duration, value); + assertTrue(value instanceof Duration, "The value should be a Duration."); + assertEquals(duration, value, "The value is not correct."); } @Test @@ -315,8 +321,8 @@ public void testDeserializationWithTypeInfo02() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + "13498]", TemporalAmount.class); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L), value); + assertTrue(value instanceof Duration, "The value should be a Duration."); + assertEquals(Duration.ofSeconds(13498L), value, "The value is not correct."); } @Test @@ -329,8 +335,8 @@ public void testDeserializationWithTypeInfo03() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + "13498837]", TemporalAmount.class); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 837000000), value); + assertTrue(value instanceof Duration, "The value should be a Duration."); + assertEquals(Duration.ofSeconds(13498L, 837000000), value, "The value is not correct."); } @Test @@ -344,8 +350,8 @@ public void testDeserializationWithTypeInfo04() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + '"' + duration.toString() + "\"]", TemporalAmount.class); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", duration, value); + assertTrue(value instanceof Duration, "The value should be a Duration."); + assertEquals(duration, value, "The value is not correct."); } @Test @@ -386,7 +392,7 @@ public void testDeserializationAsArrayEnabled() throws Exception { .configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true) .readerFor(Duration.class).readValue("[\"" + exp.toString() + "\"]"); - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } @Test @@ -423,10 +429,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr)); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); Duration actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test ( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "duration"; @@ -444,7 +450,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception { String dateValAsEmptyStr = ""; String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr)); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } /* diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeser291Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeser291Test.java index 78804c0f..7081a921 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeser291Test.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeser291Test.java @@ -3,7 +3,7 @@ import java.time.Instant; import java.util.Locale; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.databind.ObjectReader; @@ -13,8 +13,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.*; // [modules-java8#291] InstantDeserializer fails to parse negative numeric timestamp strings for // pre-1970 values. diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserTest.java index 6222ac4f..66dec2cd 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserTest.java @@ -5,9 +5,11 @@ import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.util.Map; +import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import com.fasterxml.jackson.core.StreamReadConstraints; import com.fasterxml.jackson.core.exc.StreamConstraintsException; @@ -24,8 +26,9 @@ import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; import static com.fasterxml.jackson.datatype.jsr310.deser.InstantDeserializer.ISO8601_COLONLESS_OFFSET_REGEX; -import static org.junit.Assert.*; -import static org.junit.Assume.assumeTrue; + +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeTrue; public class InstantDeserTest extends ModuleTestBase { @@ -138,12 +141,12 @@ public void testDeserializationAsFloatEdgeCase02() throws Exception assertEquals(0, value.getNano()); } - @Test(expected = DateTimeException.class) + @Test public void testDeserializationAsFloatEdgeCase03() throws Exception { // Instant can't go this low String input = Instant.MIN.getEpochSecond() + ".1"; - READER.readValue(input); + assertThrows(DateTimeException.class, () -> READER.readValue(input)); } /* @@ -151,21 +154,20 @@ public void testDeserializationAsFloatEdgeCase03() throws Exception * for numbers outside the range of Long. Numbers less than 1e64 will result in the lower 64 bits. * Numbers at or above 1e64 will always result in zero. */ - - @Test(expected = DateTimeException.class) + @Test public void testDeserializationAsFloatEdgeCase04() throws Exception { // 1ns beyond the upper-bound of Instant. String input = (Instant.MAX.getEpochSecond() + 1) + ".0"; - READER.readValue(input); + assertThrows(DateTimeException.class, () -> READER.readValue(input)); } - @Test(expected = DateTimeException.class) + @Test public void testDeserializationAsFloatEdgeCase05() throws Exception { // 1ns beyond the lower-bound of Instant. String input = (Instant.MIN.getEpochSecond() - 1) + ".0"; - READER.readValue(input); + assertThrows(DateTimeException.class, () -> READER.readValue(input)); } @Test @@ -188,14 +190,16 @@ public void testDeserializationAsFloatEdgeCase07() throws Exception * Numbers with very large exponents can take a long time, but still result in zero. * https://github.com/FasterXML/jackson-databind/issues/2141 */ - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase08() throws Exception { Instant value = READER.readValue("1e10000000"); assertEquals(0, value.getEpochSecond()); } - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase09() throws Exception { Instant value = READER.readValue("-1e10000000"); @@ -205,14 +209,16 @@ public void testDeserializationAsFloatEdgeCase09() throws Exception /** * Same for large negative exponents. */ - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase10() throws Exception { Instant value = READER.readValue("1e-10000000"); assertEquals(0, value.getEpochSecond()); } - @Test(timeout = 100) + @Timeout(value = 100, unit = TimeUnit.MILLISECONDS) + @Test public void testDeserializationAsFloatEdgeCase11() throws Exception { Instant value = READER.readValue("-1e-10000000"); @@ -368,7 +374,7 @@ public void testDeserializationWithTypeInfo01() throws Exception Temporal value = m.readValue( "[\"" + Instant.class.getName() + "\",123456789.183917322]", Temporal.class ); - assertTrue("The value should be an Instant.", value instanceof Instant); + assertTrue(value instanceof Instant, "The value should be an Instant."); assertEquals(date, value); } @@ -382,7 +388,7 @@ public void testDeserializationWithTypeInfo02() throws Exception Temporal value = m.readValue( "[\"" + Instant.class.getName() + "\",123456789]", Temporal.class ); - assertTrue("The value should be an Instant.", value instanceof Instant); + assertTrue(value instanceof Instant, "The value should be an Instant."); assertEquals(date, value); } @@ -397,7 +403,7 @@ public void testDeserializationWithTypeInfo03() throws Exception "[\"" + Instant.class.getName() + "\",123456789422]", Temporal.class ); - assertTrue("The value should be an Instant.", value instanceof Instant); + assertTrue(value instanceof Instant, "The value should be an Instant."); assertEquals(date, value); } @@ -410,7 +416,7 @@ public void testDeserializationWithTypeInfo04() throws Exception Temporal value = m.readValue( "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertTrue("The value should be an Instant.", value instanceof Instant); + assertTrue(value instanceof Instant, "The value should be an Instant."); assertEquals(date, value); } @@ -444,12 +450,12 @@ public void testCustomPatternWithAnnotations02() throws Exception final WrapperWithCustomPattern input = new WrapperWithCustomPattern(instant); String json = MAPPER.writeValueAsString(input); - assertTrue("Instant in UTC timezone was not serialized as expected.", - json.contains(a2q("'valueInUTC':'" + valueInUTC + "'"))); + assertTrue(json.contains(a2q("'valueInUTC':'" + valueInUTC + "'")), + "Instant in UTC timezone was not serialized as expected."); WrapperWithCustomPattern result = MAPPER.readValue(json, WrapperWithCustomPattern.class); - assertEquals("Instant in UTC timezone was not deserialized as expected.", - input.valueInUTC, result.valueInUTC); + assertEquals(input.valueInUTC, result.valueInUTC, + "Instant in UTC timezone was not deserialized as expected."); } /* @@ -586,10 +592,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr)); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); Duration actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test ( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "instant"; @@ -604,7 +610,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } /* @@ -616,31 +622,31 @@ public void testStrictDeserializeFromEmptyString() throws Exception { public void testISO8601ColonlessRegexFindsOffset() { Matcher matcher = ISO8601_COLONLESS_OFFSET_REGEX.matcher("2000-01-01T12:00+0100"); - assertTrue("Matcher finds +0100 as an colonless offset", matcher.find()); - assertEquals("Matcher groups +0100 as an colonless offset", matcher.group(), "+0100"); + assertTrue(matcher.find(), "Matcher finds +0100 as an colonless offset"); + assertEquals(matcher.group(), "+0100", "Matcher groups +0100 as an colonless offset"); } @Test public void testISO8601ColonlessRegexFindsOffsetWithTZ() { Matcher matcher = ISO8601_COLONLESS_OFFSET_REGEX.matcher("2000-01-01T12:00+0100[Europe/Paris]"); - assertTrue("Matcher finds +0100 as an colonless offset", matcher.find()); - assertEquals("Matcher groups +0100 as an colonless offset", matcher.group(), "+0100"); + assertTrue(matcher.find(), "Matcher finds +0100 as an colonless offset"); + assertEquals(matcher.group(), "+0100", "Matcher groups +0100 as an colonless offset"); } @Test public void testISO8601ColonlessRegexDoesNotAffectNegativeYears() { Matcher matcher = ISO8601_COLONLESS_OFFSET_REGEX.matcher("-2000-01-01T12:00+01:00[Europe/Paris]"); - assertFalse("Matcher does not find -2000 (years) as an offset without colon", matcher.find()); + assertFalse(matcher.find(), "Matcher does not find -2000 (years) as an offset without colon"); } @Test public void testISO8601ColonlessRegexDoesNotAffectNegativeYearsWithColonless() { Matcher matcher = ISO8601_COLONLESS_OFFSET_REGEX.matcher("-2000-01-01T12:00+0100[Europe/Paris]"); - assertTrue("Matcher finds +0100 as an colonless offset", matcher.find()); - assertEquals("Matcher groups +0100 as an colonless offset", matcher.group(), "+0100"); + assertTrue(matcher.find(), "Matcher finds +0100 as an colonless offset"); + assertEquals(matcher.group(), "+0100", "Matcher groups +0100 as an colonless offset"); } // [modules-java8#387]: StreamReadConstraints should limit numeric string lengths diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializerNegative359Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializerNegative359Test.java new file mode 100644 index 00000000..ac4e5089 --- /dev/null +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializerNegative359Test.java @@ -0,0 +1,37 @@ +package com.fasterxml.jackson.datatype.jsr310.deser; + +import java.time.Instant; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +// [modules-java8#359] InstantDeserializer deserializes the nanosecond portion of +// fractional timestamps incorrectly: -1.000000001 deserializes to 1969-12-31T23:59:59.000000001Z +// instead of 1969-12-31T23:59:58.999999999Z +public class InstantDeserializerNegative359Test + extends ModuleTestBase +{ + private final ObjectReader READER = newMapper().readerFor(Instant.class); + + @Test + public void testDeserializationAsFloat04() + throws Exception + { + Instant actual = READER.readValue("-1.000000001"); + Instant expected = Instant.ofEpochSecond(-1L, -1L); + assertEquals(expected, actual); + } + + @Test + public void testDeserializationAsFloat05() + throws Exception + { + Instant actual = READER.readValue("-0.000000001"); + Instant expected = Instant.ofEpochSecond(0L, -1L); + assertEquals(expected, actual); + } +} diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/InstanceViaBigDecimal307Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantViaBigDecimal307Test.java similarity index 69% rename from datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/InstanceViaBigDecimal307Test.java rename to datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantViaBigDecimal307Test.java index 5f27f033..d1104a90 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/InstanceViaBigDecimal307Test.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantViaBigDecimal307Test.java @@ -1,18 +1,20 @@ -package com.fasterxml.jackson.datatype.jsr310.failing; +package com.fasterxml.jackson.datatype.jsr310.deser; import java.time.Instant; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.cfg.JsonNodeFeature; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; // [modules-java8#307]: Loss of precision via JsonNode for BigDecimal-valued // things (like Instant) -public class InstanceViaBigDecimal307Test extends ModuleTestBase +// Solved via `JsonNodeFeature.USE_BIG_DECIMAL_FOR_FLOATS` added in Jackson 2.19 +public class InstantViaBigDecimal307Test extends ModuleTestBase { static class Wrapper307 { public Instant value; @@ -23,7 +25,10 @@ protected Wrapper307() { } private final Instant ISSUED_AT = Instant.ofEpochSecond(1234567890).plusNanos(123456789); - private ObjectMapper MAPPER = newMapper(); + private ObjectMapper MAPPER = mapperBuilder() + // added in 2.19 + .enable(JsonNodeFeature.USE_BIG_DECIMAL_FOR_FLOATS) + .build(); @Test public void instantViaReadValue() throws Exception { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserTest.java index e3fe9e92..f047643a 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateDeserTest.java @@ -9,12 +9,16 @@ import java.time.format.DateTimeParseException; import java.time.temporal.Temporal; import java.util.Map; +import java.util.TimeZone; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import com.fasterxml.jackson.annotation.OptBoolean; import com.fasterxml.jackson.databind.cfg.CoercionAction; import com.fasterxml.jackson.databind.cfg.CoercionInputShape; import com.fasterxml.jackson.databind.exc.InvalidFormatException; -import org.junit.Test; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat.Feature; @@ -27,18 +31,23 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.exc.MismatchedInputException; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.*; -import static org.junit.Assert.assertThrows; +import static org.junit.jupiter.api.Assertions.*; public class LocalDateDeserTest extends ModuleTestBase { private final ObjectMapper MAPPER = newMapper(); private final ObjectReader READER = MAPPER.readerFor(LocalDate.class); + private final ObjectReader READER_USING_TIME_ZONE = JsonMapper.builder() + .addModule(new JavaTimeModule().enable(JavaTimeFeature.USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING)) + .build() + .readerFor(LocalDate.class); + private final TypeReference> MAP_TYPE_REF = new TypeReference>() { }; final static class Wrapper { @@ -103,14 +112,14 @@ public StrictWrapperWithFormat() { } @Test public void testDeserializationAsTimestamp01() throws Exception { - assertEquals("The value is not correct.", LocalDate.of(1986, Month.JANUARY, 17), + assertEquals(LocalDate.of(1986, Month.JANUARY, 17), READER.readValue("[1986,1,17]")); } @Test public void testDeserializationAsTimestamp02() throws Exception { - assertEquals("The value is not correct.", LocalDate.of(2013, Month.AUGUST, 21), + assertEquals(LocalDate.of(2013, Month.AUGUST, 21), READER.readValue("[2013,8,21]")); } @@ -123,34 +132,59 @@ public void testDeserializationAsTimestamp02() throws Exception @Test public void testDeserializationAsString01() throws Exception { - assertEquals("The value is not correct.", LocalDate.of(2000, Month.JANUARY, 1), - READER.readValue(q("2000-01-01"))); + assertEquals(LocalDate.of(2000, Month.JANUARY, 1), READER.readValue(q("2000-01-01"))); LocalDate date = LocalDate.of(1986, Month.JANUARY, 17); - assertEquals("The value is not correct.", date, - READER.readValue('"' + date.toString() + '"')); + assertEquals(date, READER.readValue('"' + date.toString() + '"')); date = LocalDate.of(2013, Month.AUGUST, 21); - assertEquals("The value is not correct.", date, - READER.readValue('"' + date.toString() + '"')); + assertEquals(date, READER.readValue('"' + date.toString() + '"')); } @Test public void testDeserializationAsString02() throws Exception { LocalDateTime date = LocalDateTime.now(); - assertEquals("The value is not correct.", date.toLocalDate(), - READER.readValue('"' + date.toString() + '"')); + assertEquals(date.toLocalDate(), READER.readValue('"' + date.toString() + '"')); + } + + @Test + public void testLenientDeserializationAsString01() throws Exception + { + Instant instant = Instant.now(); + LocalDate value = READER.readValue(q(instant.toString())); + assertEquals(LocalDateTime.ofInstant(instant, ZoneOffset.UTC).toLocalDate(), value); + } + + @Test + public void testLenientDeserializationAsString02() throws Exception + { + ObjectReader reader = READER.with(TimeZone.getTimeZone(Z_BUDAPEST)); + Instant instant = Instant.now(); + LocalDate value = reader.readValue(q(instant.toString())); + assertEquals(LocalDateTime.ofInstant(instant, ZoneOffset.UTC).toLocalDate(), value); } @Test - public void testDeserializationAsString03() throws Exception + public void testLenientDeserializationAsString03() throws Exception { Instant instant = Instant.now(); - LocalDate value = READER.readValue('"' + instant.toString() + '"'); - assertEquals("The value is not correct.", - LocalDateTime.ofInstant(instant, ZoneOffset.UTC).toLocalDate(), - value); + LocalDate value = READER_USING_TIME_ZONE.readValue(q(instant.toString())); + assertEquals(LocalDateTime.ofInstant(instant, ZoneOffset.UTC).toLocalDate(), value); + } + + @ParameterizedTest + @CsvSource({ + "Europe/Budapest, 2024-07-21T21:59:59Z, 2024-07-21", + "Europe/Budapest, 2024-07-21T22:00:00Z, 2024-07-22", + "America/Chicago, 2024-07-22T04:59:59Z, 2024-07-21", + "America/Chicago, 2024-07-22T05:00:00Z, 2024-07-22" + }) + public void testLenientDeserializationAsString04(TimeZone zone, String string, LocalDate expected) throws Exception + { + ObjectReader reader = READER_USING_TIME_ZONE.with(zone); + LocalDate value = reader.readValue(q(string)); + assertEquals(expected, value); } @Test @@ -187,7 +221,7 @@ public void testDeserializationWithTypeInfo01() throws Exception Temporal value = mapper.readValue( "[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class ); - assertEquals("The value is not correct.", date, value); + assertEquals(date, value); } /* @@ -201,11 +235,9 @@ public void testDeserializationWithTypeInfo01() throws Exception @Test public void testLenientDeserializeFromInt() throws Exception { - assertEquals("The value is not correct.", LocalDate.of(1970, Month.JANUARY, 3), - READER.readValue("2")); + assertEquals(LocalDate.of(1970, Month.JANUARY, 3), READER.readValue("2")); - assertEquals("The value is not correct.", LocalDate.of(1970, Month.FEBRUARY, 10), - READER.readValue("40")); + assertEquals(LocalDate.of(1970, Month.FEBRUARY, 10), READER.readValue("40")); } // But with alternate setting, not so @@ -252,10 +284,11 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr)); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); LocalDate actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting",actualDateFromNullStr, actualDateFromEmptyStr); + assertEquals(actualDateFromNullStr, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test( expected = MismatchedInputException.class) + @Test + // ( expected = MismatchedInputException.class) public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "date"; @@ -274,7 +307,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception { // TODO: nothing stops us from writing an empty string, maybe there should be a check there too? String valueFromEmptyStr = mapper.writeValueAsString(asMap("date", dateValAsEmptyStr)); // with strict, deserializing an empty string is not permitted - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } /* @@ -307,7 +340,7 @@ public void testDeserializationAsArrayEnabled() throws Throwable LocalDate actual = READER .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) .readValue("[\"2000-01-01\"]"); - assertEquals("The value is not correct.", LocalDate.of(2000, 1, 1), actual); + assertEquals(LocalDate.of(2000, 1, 1), actual); } @Test @@ -358,22 +391,28 @@ public void testStrictCustomFormat() throws Exception */ // for [modules-java8#148] - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidFormat() throws Exception { - /*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"2019-11-30\"}", StrictWrapper.class); + assertThrows(InvalidFormatException.class, () -> { + /*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"2019-11-30\"}", StrictWrapper.class); + }); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidFormatWithEra() throws Exception { - /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30\"}", StrictWrapperWithYearOfEra.class); + assertThrows(InvalidFormatException.class, () -> { + /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30\"}", StrictWrapperWithYearOfEra.class); + }); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidDateWithEra() throws Exception { - /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 AD\"}", StrictWrapperWithYearOfEra.class); + assertThrows(InvalidFormatException.class, () -> { + /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 AD\"}", StrictWrapperWithYearOfEra.class); + }); } @Test @@ -384,16 +423,20 @@ public void testStrictCustomFormatForValidDateWithEra() throws Exception assertEquals(w.value, LocalDate.of(2019, 11, 30)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidFormatWithoutEra() throws Exception { - /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 AD\"}", StrictWrapperWithYearWithoutEra.class); + assertThrows(InvalidFormatException.class, () -> { + /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 AD\"}", StrictWrapperWithYearWithoutEra.class); + }); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidDateWithoutEra() throws Exception { - /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31\"}", StrictWrapperWithYearWithoutEra.class); + assertThrows(InvalidFormatException.class, () -> { + /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31\"}", StrictWrapperWithYearWithoutEra.class); + }); } @Test @@ -478,8 +521,8 @@ public void testLenientDeserializeFromNumberInt() throws Exception { mapper.configOverride(LocalDate.class) .setFormat(JsonFormat.Value.forShape(JsonFormat.Shape.NUMBER_INT)); - assertEquals("The value is not correct.", LocalDate.of(1970, Month.MAY, 4), - mapper.readValue("123", LocalDate.class)); + assertEquals(LocalDate.of(1970, Month.MAY, 4), + mapper.readValue("123", LocalDate.class)); } @Test @@ -492,18 +535,18 @@ public void testStrictDeserializeFromNumberInt() throws Exception ShapeWrapper w = mapper.readValue("{\"date\":123}", ShapeWrapper.class); LocalDate localDate = w.date; - assertEquals("The value is not correct.", LocalDate.of(1970, Month.MAY, 4), - localDate); + assertEquals(LocalDate.of(1970, Month.MAY, 4), localDate); } - @Test(expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromString() throws Exception { ObjectMapper mapper = newMapper(); mapper.configOverride(LocalDate.class) .setFormat(JsonFormat.Value.forLeniency(false)); - mapper.readValue("{\"value\":123}", Wrapper.class); + assertThrows(MismatchedInputException.class, + () -> mapper.readValue("{\"value\":123}", Wrapper.class)); } /********************************************************************** @@ -521,8 +564,7 @@ public void testDeserializeFromIntegerWithCoercionActionFail() { MismatchedInputException exception = assertThrows(MismatchedInputException.class, () -> mapper.readValue("123", LocalDate.class)); - assertThat(exception.getMessage(), - containsString("Cannot coerce Integer value (123) to `java.time.LocalDate`")); + assertTrue(exception.getMessage().contains("Cannot coerce Integer value (123) to `java.time.LocalDate`")); } @Test @@ -534,8 +576,7 @@ public void testDeserializeFromEmptyStringWithCoercionActionFail() { MismatchedInputException exception = assertThrows(MismatchedInputException.class, () -> mapper.readValue(a2q("{'value':''}"), Wrapper.class)); - assertThat(exception.getMessage(), - containsString("Cannot coerce empty String (\"\") to `java.time.LocalDate`")); + assertTrue(exception.getMessage().contains("Cannot coerce empty String (\"\") to `java.time.LocalDate`")); } /* @@ -559,10 +600,8 @@ private void expectFailure(ObjectReader reader, String json) throws Throwable { private void expectSuccess(ObjectReader reader, Object exp, String json) throws IOException { final LocalDate value = reader.readValue(a2q(json)); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", exp, value); + assertNotNull(value); + assertEquals(exp, value); } -} - - +} diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserTest.java index 3fb59fab..7cd13493 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalDateTimeDeserTest.java @@ -26,6 +26,10 @@ import java.util.Map; import java.util.TimeZone; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat.Feature; import com.fasterxml.jackson.annotation.JsonFormat.Value; @@ -38,11 +42,13 @@ import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.fasterxml.jackson.databind.exc.MismatchedInputException; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class LocalDateTimeDeserTest extends ModuleTestBase @@ -50,6 +56,11 @@ public class LocalDateTimeDeserTest private final static ObjectMapper MAPPER = newMapper(); private final static ObjectReader READER = MAPPER.readerFor(LocalDateTime.class); + private final ObjectReader READER_USING_TIME_ZONE = JsonMapper.builder() + .addModule(new JavaTimeModule().enable(JavaTimeFeature.USE_TIME_ZONE_FOR_LENIENT_DATE_PARSING)) + .build() + .readerFor(LocalDateTime.class); + private final static ObjectMapper STRICT_MAPPER; static { STRICT_MAPPER = newMapper(); @@ -117,7 +128,7 @@ public void testDeserializationAsTimestamp01() throws Exception { LocalDateTime value = MAPPER.readValue("[1986,1,17,15,43]", LocalDateTime.class); LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -125,7 +136,7 @@ public void testDeserializationAsTimestamp02() throws Exception { LocalDateTime value = MAPPER.readValue("[2013,8,21,9,22,57]", LocalDateTime.class); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -135,7 +146,7 @@ public void testDeserializationAsTimestamp03Nanoseconds() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2013,8,21,9,22,0,57]"); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -145,7 +156,7 @@ public void testDeserializationAsTimestamp03Milliseconds() throws Exception .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2013,8,21,9,22,0,57]"); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57000000); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -155,7 +166,7 @@ public void testDeserializationAsTimestamp04Nanoseconds() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829837]"); LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -166,7 +177,7 @@ public void testDeserializationAsTimestamp04Milliseconds01() throws Exception LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829837]"); LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -176,7 +187,7 @@ public void testDeserializationAsTimestamp04Milliseconds02() throws Exception .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829]"); LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829000000); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -186,7 +197,7 @@ public void testDeserializationAsTimestamp05Nanoseconds() throws Exception WrapperWithReadTimestampsAsNanosEnabled actual = r.readValue(a2q("{'value':[2013,8,21,9,22,0,57]}")); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57); - assertEquals("The value is not correct.", time, actual.value); + assertEquals(time, actual.value, "The value is not correct."); } @Test @@ -196,7 +207,7 @@ public void testDeserializationAsTimestamp05Milliseconds01() throws Exception WrapperWithReadTimestampsAsNanosDisabled actual = r.readValue(a2q("{'value':[2013,8,21,9,22,0,57]}")); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57000000); - assertEquals("The value is not correct.", time, actual.value); + assertEquals(time, actual.value, "The value is not correct."); } @Test @@ -206,7 +217,7 @@ public void testDeserializationAsTimestamp05Milliseconds02() throws Exception WrapperWithReadTimestampsAsNanosDisabled actual = r.readValue(a2q("{'value':[2013,8,21,9,22,0,4257]}")); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 4257); - assertEquals("The value is not correct.", time, actual.value); + assertEquals(time, actual.value, "The value is not correct."); } /* @@ -220,11 +231,11 @@ public void testDeserializationAsString01() throws Exception { LocalDateTime exp = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); LocalDateTime value = READER.readValue(q(exp.toString())); - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); - assertEquals("The value is not correct.", - LocalDateTime.of(2000, Month.JANUARY, 1, 12, 0), - READER.readValue(q("2000-01-01T12:00"))); + assertEquals(LocalDateTime.of(2000, Month.JANUARY, 1, 12, 0), + READER.readValue(q("2000-01-01T12:00")), + "The value is not correct."); } @Test @@ -232,7 +243,7 @@ public void testDeserializationAsString02() throws Exception { LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); LocalDateTime value = MAPPER.readValue(q(time.toString()), LocalDateTime.class); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -240,7 +251,7 @@ public void testDeserializationAsString03() throws Exception { LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); LocalDateTime value = MAPPER.readValue(q(time.toString()), LocalDateTime.class); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } /* @@ -259,16 +270,33 @@ public void testAllowZuluIfLenient() throws Exception final ObjectReader r = MAPPER.readerFor(LocalDateTime.class); // First, defaults: - assertEquals("The value is not correct.", EXP, r.readValue(input)); + assertEquals(EXP, r.readValue(input), "The value is not correct."); // but ensure that global timezone setting doesn't matter LocalDateTime value = r.with(TimeZone.getTimeZone(Z_CHICAGO)) .readValue(input); - assertEquals("The value is not correct.", EXP, value); + assertEquals(EXP, value, "The value is not correct."); value = r.with(TimeZone.getTimeZone(Z_BUDAPEST)) .readValue(input); - assertEquals("The value is not correct.", EXP, value); + assertEquals(EXP, value, "The value is not correct."); + } + + @ParameterizedTest + @CsvSource({ + "UTC, 2020-10-22T04:16:20.504Z, 2020-10-22T04:16:20.504", + "Europe/Budapest, 2020-10-22T04:16:20.504Z, 2020-10-22T06:16:20.504", + "Europe/Budapest, 2020-10-25T00:16:20.504Z, 2020-10-25T02:16:20.504", + "Europe/Budapest, 2020-10-25T01:16:20.504Z, 2020-10-25T02:16:20.504", + "America/Chicago, 2020-10-22T04:16:20.504Z, 2020-10-21T23:16:20.504", + "America/Chicago, 2020-11-01T06:16:20.504Z, 2020-11-01T01:16:20.504", + "America/Chicago, 2020-11-01T07:16:20.504Z, 2020-11-01T01:16:20.504" + }) + public void testUseTimeZoneForZuluIfEnabled(TimeZone zone, String string, LocalDateTime expected) throws Exception + { + ObjectReader reader = READER_USING_TIME_ZONE.with(zone); + LocalDateTime value = reader.readValue(q(string)); + assertEquals(expected, value); } // [modules-java#94]: "Z" offset not allowed if strict mode @@ -320,10 +348,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr)); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); LocalDateTime actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting",actualDateFromNullStr, actualDateFromEmptyStr); + assertEquals(actualDateFromNullStr, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "datetime"; @@ -339,7 +367,8 @@ public void testStrictDeserializeFromEmptyString() throws Exception { // TODO: nothing stops us from writing an empty string, maybe there should be a check there too? String valueFromEmptyStr = STRICT_MAPPER.writeValueAsString(asMap("date", dateValAsEmptyStr)); // with strict, deserializing an empty string is not permitted - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, + () -> objectReader.readValue(valueFromEmptyStr)); } /* @@ -371,8 +400,8 @@ public void testDeserializationAsArrayEnabled() throws Throwable LocalDateTime value = READER .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) .readValue("[\"2000-01-01T12:00\"]"); - assertEquals("The value is not correct.", - LocalDateTime.of(2000, 1, 1, 12, 0, 0, 0), value); + assertEquals(LocalDateTime.of(2000, 1, 1, 12, 0, 0, 0), + value, "The value is not correct."); } @Test @@ -400,8 +429,8 @@ public void testDeserializationWithTypeInfo01() throws Exception Temporal value = m.readValue( "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", Temporal.class ); - assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); - assertEquals("The value is not correct.", time, value); + assertTrue(value instanceof LocalDateTime, "The value should be a LocalDateTime."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -414,8 +443,8 @@ public void testDeserializationWithTypeInfo02() throws Exception Temporal value = m.readValue( "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", Temporal.class ); - assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); - assertEquals("The value is not correct.", time, value); + assertTrue(value instanceof LocalDateTime, "The value should be a LocalDateTime."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -427,8 +456,8 @@ public void testDeserializationWithTypeInfo03() throws Exception Temporal value = m.readValue( "[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class ); - assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); - assertEquals("The value is not correct.", time, value); + assertTrue(value instanceof LocalDateTime, "The value should be a LocalDateTime."); + assertEquals(time, value, "The value is not correct."); } /* @@ -584,34 +613,40 @@ public void testDeserializationCaseInsensitiveDisabled_InvalidDate() throws Thro */ // [modules-java8#148]: handle strict deserializaiton for date/time - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidFormat() throws Exception { - /*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 15:45\"}", StrictWrapper.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 15:45\"}", StrictWrapper.class)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidFormatWithEra() throws Exception { - /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 15:45\"}", StrictWrapperWithYearOfEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 15:45\"}", StrictWrapperWithYearOfEra.class)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidDateWithEra() throws Exception { - /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 15:45 AD\"}", StrictWrapperWithYearOfEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 15:45 AD\"}", StrictWrapperWithYearOfEra.class)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidTimeWithEra() throws Exception { - /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 25:45 AD\"}", StrictWrapperWithYearOfEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 25:45 AD\"}", StrictWrapperWithYearOfEra.class)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidDateAndTimeWithEra() throws Exception { - /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 25:45 AD\"}", StrictWrapperWithYearOfEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearOfEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 25:45 AD\"}", StrictWrapperWithYearOfEra.class)); + } @Test @@ -622,28 +657,32 @@ public void testStrictCustomFormatValidDateAndTimeWithEra() throws Exception assertEquals(w.value, LocalDateTime.of(2019, 11, 30, 20, 45)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidFormatWithoutEra() throws Exception { - /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 15:45 AD\"}", StrictWrapperWithYearWithoutEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 15:45 AD\"}", StrictWrapperWithYearWithoutEra.class)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidTimeWithoutEra() throws Exception { - /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 25:45\"}", StrictWrapperWithYearWithoutEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-30 25:45\"}", StrictWrapperWithYearWithoutEra.class)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidDateWithoutEra() throws Exception { - /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 15:45\"}", StrictWrapperWithYearWithoutEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 15:45\"}", StrictWrapperWithYearWithoutEra.class)); } - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatForInvalidDateAndTimeWithoutEra() throws Exception { - /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 25:45\"}", StrictWrapperWithYearWithoutEra.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapperWithYearWithoutEra w =*/ MAPPER.readValue("{\"value\":\"2019-11-31 25:45\"}", StrictWrapperWithYearWithoutEra.class)); } @Test @@ -680,8 +719,8 @@ private void _testLocalDateTimeRoundTrip(ObjectMapper mapper, LocalDateTime loca private void expectSuccess(ObjectReader reader, Object exp, String json) throws IOException { final LocalDateTime value = reader.readValue(a2q(json)); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", exp, value); + assertNotNull(value, "The value should not be null."); + assertEquals(exp, value, "The value is not correct."); } private void expectFailure(ObjectReader reader, String json) throws Throwable { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserTest.java index 785f6ff1..5c7cc070 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/LocalTimeDeserTest.java @@ -22,11 +22,7 @@ import java.time.temporal.Temporal; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat.Feature; @@ -41,7 +37,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class LocalTimeDeserTest extends ModuleTestBase { @@ -83,7 +79,7 @@ public void testDeserializationAsTimestamp01() throws Exception { LocalTime time = LocalTime.of(15, 43); LocalTime value = reader.readValue("[15,43]"); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -91,7 +87,7 @@ public void testDeserializationAsTimestamp02() throws Exception { LocalTime time = LocalTime.of(9, 22, 57); LocalTime value = reader.readValue("[9,22,57]"); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -100,7 +96,7 @@ public void testDeserializationAsTimestamp03Nanoseconds() throws Exception LocalTime value = reader .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[9,22,0,57]"); - assertEquals("The value is not correct.", LocalTime.of(9, 22, 0, 57), value); + assertEquals(LocalTime.of(9, 22, 0, 57), value, "The value is not correct."); } @Test @@ -109,7 +105,7 @@ public void testDeserializationAsTimestamp03Milliseconds() throws Exception LocalTime value = reader .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[9,22,0,57]"); - assertEquals("The value is not correct.", LocalTime.of(9, 22, 0, 57000000), value); + assertEquals(LocalTime.of(9, 22, 0, 57000000), value, "The value is not correct."); } @Test @@ -118,7 +114,7 @@ public void testDeserializationAsTimestamp04Nanoseconds() throws Exception LocalTime value = reader .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[22,31,5,829837]"); - assertEquals("The value is not correct.", LocalTime.of(22, 31, 5, 829837), value); + assertEquals(LocalTime.of(22, 31, 5, 829837), value, "The value is not correct."); } @Test @@ -127,7 +123,7 @@ public void testDeserializationAsTimestamp04Milliseconds01() throws Exception LocalTime value = reader .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[22,31,5,829837]"); - assertEquals("The value is not correct.", LocalTime.of(22, 31, 5, 829837), value); + assertEquals(LocalTime.of(22, 31, 5, 829837), value, "The value is not correct."); } @Test @@ -136,7 +132,7 @@ public void testDeserializationAsTimestamp04Milliseconds02() throws Exception LocalTime value = reader .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[22,31,5,829]"); - assertEquals("The value is not correct.", LocalTime.of(22, 31, 5, 829000000), value); + assertEquals(LocalTime.of(22, 31, 5, 829000000), value, "The value is not correct."); } @Test @@ -146,7 +142,7 @@ public void testDeserializationAsTimestamp05Nanoseconds() throws Exception newMapper().readerFor(WrapperWithReadTimestampsAsNanosEnabled.class); WrapperWithReadTimestampsAsNanosEnabled actual = wrapperReader .readValue(a2q("{'value':[9,22,0,57]}")); - assertEquals("The value is not correct.", LocalTime.of(9, 22, 0, 57), actual.value); + assertEquals(LocalTime.of(9, 22, 0, 57), actual.value, "The value is not correct."); } @Test @@ -156,7 +152,7 @@ public void testDeserializationAsTimestamp05Milliseconds01() throws Exception newMapper().readerFor(WrapperWithReadTimestampsAsNanosDisabled.class); WrapperWithReadTimestampsAsNanosDisabled actual = wrapperReader .readValue(a2q("{'value':[9,22,0,57]}")); - assertEquals("The value is not correct.", LocalTime.of(9, 22, 0, 57000000), actual.value); + assertEquals(LocalTime.of(9, 22, 0, 57000000), actual.value, "The value is not correct."); } @Test @@ -166,7 +162,7 @@ public void testDeserializationAsTimestamp05Milliseconds02() throws Exception newMapper().readerFor(WrapperWithReadTimestampsAsNanosDisabled.class); WrapperWithReadTimestampsAsNanosDisabled actual = wrapperReader .readValue(a2q("{'value':[9,22,0,4257]}")); - assertEquals("The value is not correct.", LocalTime.of(9, 22, 0, 4257), actual.value); + assertEquals(LocalTime.of(9, 22, 0, 4257), actual.value, "The value is not correct."); } @Test @@ -174,17 +170,17 @@ public void testDeserializationFromString() throws Exception { LocalTime time = LocalTime.of(15, 43); LocalTime value = reader.readValue('"' + time.toString() + '"'); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); expectSuccess(LocalTime.of(12, 0), "'12:00'"); time = LocalTime.of(9, 22, 57); value = reader.readValue('"' + time.toString() + '"'); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); time = LocalTime.of(22, 31, 5, 829837); value = reader.readValue('"' + time.toString() + '"'); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -239,9 +235,9 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalTime.", value instanceof LocalTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertTrue(value instanceof LocalTime, "The value should be a LocalTime."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -256,9 +252,9 @@ public void testDeserializationWithTypeInfo02() throws Exception "[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalTime.", value instanceof LocalTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertTrue(value instanceof LocalTime, "The value should be a LocalTime."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -271,9 +267,9 @@ public void testDeserializationWithTypeInfo03() throws Exception "[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalTime.", value instanceof LocalTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertTrue(value instanceof LocalTime, "The value should be a LocalTime."); + assertEquals(time, value, "The value is not correct."); } /* @@ -299,10 +295,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr)); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); LocalTime actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting",null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "localTime"; @@ -316,7 +312,8 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap("date", "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, + () -> objectReader.readValue(valueFromEmptyStr)); } /* @@ -327,10 +324,11 @@ public void testStrictDeserializeFromEmptyString() throws Exception { // [modules-java8#148]: handle strict deserializaiton for date/time - @Test(expected = InvalidFormatException.class) + @Test public void testStrictCustomFormatInvalidTime() throws Exception { - /*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"25:45\"}", StrictWrapper.class); + assertThrows(InvalidFormatException.class, + () -> /*StrictWrapper w =*/ MAPPER.readValue("{\"value\":\"25:45\"}", StrictWrapper.class)); } private void expectFailure(String aposJson) throws Throwable { @@ -354,10 +352,10 @@ private void expectSuccess(Object exp, String aposJson) throws IOException { } private static void notNull(Object value) { - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); } private static void expect(Object exp, Object value) { - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserTest.java index 7d115df0..9774e971 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/MonthDayDeserTest.java @@ -7,6 +7,8 @@ import java.time.temporal.TemporalAccessor; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonProcessingException; @@ -21,12 +23,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; public class MonthDayDeserTest extends ModuleTestBase { @@ -111,15 +108,16 @@ public void testDeserializationAsEmptyArrayEnabled() throws Throwable @Test public void testDeserialization01() throws Exception { - assertEquals("The value is not correct.", MonthDay.of(Month.JANUARY, 17), - MAPPER.readValue("\"--01-17\"", MonthDay.class)); + assertEquals(MonthDay.of(Month.JANUARY, 17), MAPPER.readValue("\"--01-17\"", MonthDay.class), + "The value is not correct."); } @Test public void testDeserialization02() throws Exception { - assertEquals("The value is not correct.", MonthDay.of(Month.AUGUST, 21), - MAPPER.readValue("\"--08-21\"", MonthDay.class)); + assertEquals(MonthDay.of(Month.AUGUST, 21), + MAPPER.readValue("\"--08-21\"", MonthDay.class), + "The value is not correct."); } @Test @@ -131,7 +129,7 @@ public void testDeserializationWithTypeInfo01() throws Exception MonthDay monthDay = MonthDay.of(Month.NOVEMBER, 5); TemporalAccessor value = mapper.readValue("[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", TemporalAccessor.class); - assertEquals("The value is not correct.", monthDay, value); + assertEquals(monthDay, value, "The value is not correct."); } @Test @@ -220,10 +218,10 @@ private MonthDay read(final String aposJson) throws IOException { } private static void notNull(Object value) { - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); } private static void expect(Object exp, Object value) { - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java index e676230d..9c21a4e5 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetDateTimeDeserTest.java @@ -5,24 +5,25 @@ import java.time.temporal.ChronoField; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; +import java.util.Arrays; import java.util.Map; import java.util.TimeZone; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonFormat.Feature; + import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.ObjectReader; -import com.fasterxml.jackson.databind.SerializationFeature; + +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class OffsetDateTimeDeserTest extends ModuleTestBase @@ -74,9 +75,9 @@ public void testDeserializationAsFloat01WithoutTimeZone() throws Exception OffsetDateTime date = OffsetDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); OffsetDateTime value = MAPPER.readValue("0.000000000", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -87,9 +88,9 @@ public void testDeserializationAsFloat01WithTimeZone() throws Exception .setTimeZone(TimeZone.getDefault()); OffsetDateTime value = m.readValue("0.000000000", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -99,9 +100,9 @@ public void testDeserializationAsFloat02WithoutTimeZone() throws Exception OffsetDateTime value = MAPPER.readValue("123456789.183917322", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -113,9 +114,9 @@ public void testDeserializationAsFloat02WithTimeZone() throws Exception .with(TimeZone.getDefault()) .readValue("123456789.183917322"); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -127,9 +128,9 @@ public void testDeserializationAsFloat03WithoutTimeZone() throws Exception DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class ); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -143,9 +144,9 @@ public void testDeserializationAsFloat03WithTimeZone() throws Exception DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class ); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -156,9 +157,9 @@ public void testDeserializationAsInt01NanosecondsWithoutTimeZone() throws Except .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetDateTime value = m.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -170,9 +171,9 @@ public void testDeserializationAsInt01NanosecondsWithTimeZone() throws Exception .setTimeZone(TimeZone.getDefault()); OffsetDateTime value = m.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -183,9 +184,9 @@ public void testDeserializationAsInt01MillisecondsWithoutTimeZone() throws Excep .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); OffsetDateTime value = m.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -197,9 +198,9 @@ public void testDeserializationAsInt01MillisecondsWithTimeZone() throws Exceptio .setTimeZone(TimeZone.getDefault()); OffsetDateTime value = m.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -211,9 +212,9 @@ public void testDeserializationAsInt02NanosecondsWithoutTimeZone() throws Except .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetDateTime value = m.readValue("123456789", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -225,9 +226,9 @@ public void testDeserializationAsInt02NanosecondsWithTimeZone() throws Exception .setTimeZone(TimeZone.getDefault()); OffsetDateTime value = m.readValue("123456789", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -238,9 +239,9 @@ public void testDeserializationAsInt02MillisecondsWithoutTimeZone() throws Excep .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); OffsetDateTime value = m.readValue("123456789422", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -252,9 +253,9 @@ public void testDeserializationAsInt02MillisecondsWithTimeZone() throws Exceptio .setTimeZone(TimeZone.getDefault()); OffsetDateTime value = m.readValue("123456789422", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -267,9 +268,9 @@ public void testDeserializationAsInt03NanosecondsWithoutTimeZone() throws Except .configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetDateTime value = m.readValue(Long.toString(date.toEpochSecond()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -283,9 +284,9 @@ public void testDeserializationAsInt03NanosecondsWithTimeZone() throws Exception .setTimeZone(TimeZone.getDefault()); OffsetDateTime value = m.readValue(Long.toString(date.toEpochSecond()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -299,9 +300,9 @@ public void testDeserializationAsInt03MillisecondsWithoutTimeZone() throws Excep OffsetDateTime value = m.readValue(Long.toString(date.toInstant().toEpochMilli()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -316,9 +317,9 @@ public void testDeserializationAsInt03MillisecondsWithTimeZone() throws Exceptio OffsetDateTime value = m.readValue(Long.toString(date.toInstant().toEpochMilli()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -330,10 +331,10 @@ public void testDeserializationAsInt04NanosecondsWithoutTimeZone() throws Except a2q("{'value':123456789}"), WrapperWithReadTimestampsAsNanosEnabled.class); - assertNotNull("The actual should not be null.", actual); - assertNotNull("The actual value should not be null.", actual.value); + assertNotNull(actual, "The actual should not be null."); + assertNotNull(actual.value, "The actual value should not be null."); assertIsEqual(date, actual.value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, actual.value.getOffset()); + assertEquals(ZoneOffset.UTC, actual.value.getOffset(), "The time zone is not correct."); } @Test @@ -346,10 +347,10 @@ public void testDeserializationAsInt04NanosecondsWithTimeZone() throws Exception a2q("{'value':123456789}"), WrapperWithReadTimestampsAsNanosEnabled.class); - assertNotNull("The actual should not be null.", actual); - assertNotNull("The actual value should not be null.", actual.value); + assertNotNull(actual, "The actual should not be null."); + assertNotNull(actual.value, "The actual value should not be null."); assertIsEqual(date, actual.value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), actual.value.getOffset()); + assertEquals(getDefaultOffset(date), actual.value.getOffset(), "The time zone is not correct."); } @Test @@ -361,10 +362,10 @@ public void testDeserializationAsInt04MillisecondsWithoutTimeZone() throws Excep a2q("{'value':123456789422}"), WrapperWithReadTimestampsAsNanosDisabled.class); - assertNotNull("The actual should not be null.", actual); - assertNotNull("The actual value should not be null.", actual.value); + assertNotNull(actual, "The actual should not be null."); + assertNotNull(actual.value, "The actual value should not be null."); assertIsEqual(date, actual.value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, actual.value.getOffset()); + assertEquals(ZoneOffset.UTC, actual.value.getOffset(), "The time zone is not correct."); } @Test @@ -377,10 +378,10 @@ public void testDeserializationAsInt04MillisecondsWithTimeZone() throws Exceptio a2q("{'value':123456789422}"), WrapperWithReadTimestampsAsNanosDisabled.class); - assertNotNull("The actual should not be null.", actual); - assertNotNull("The actual value should not be null.", actual.value); + assertNotNull(actual, "The actual should not be null."); + assertNotNull(actual.value, "The actual value should not be null."); assertIsEqual(date, actual.value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), actual.value.getOffset()); + assertEquals(getDefaultOffset(date), actual.value.getOffset(), "The time zone is not correct."); } @Test @@ -392,7 +393,7 @@ public void testDeserializationAsString01WithoutTimeZone() throws Exception OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -405,7 +406,7 @@ public void testDeserializationAsString01WithTimeZone() throws Exception OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -418,7 +419,7 @@ public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exceptio OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z1), value.getOffset()); + assertEquals(getOffset(value, Z1), value.getOffset(), "The time zone is not correct."); } @Test @@ -432,7 +433,7 @@ public void testDeserializationAsString01WithTimeZoneColonless() throws Exceptio OffsetDateTime value = r.readValue('"' + sDate + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z1), value.getOffset()); + assertEquals(getOffset(value, Z1), value.getOffset(), "The time zone is not correct."); } @Test @@ -444,7 +445,7 @@ public void testDeserializationAsString02WithoutTimeZone() throws Exception OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -457,7 +458,7 @@ public void testDeserializationAsString02WithTimeZone() throws Exception OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -469,9 +470,9 @@ public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exceptio .setTimeZone(TimeZone.getDefault()); OffsetDateTime value = m.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z2), value.getOffset()); + assertEquals(getOffset(value, Z2), value.getOffset(), "The time zone is not correct."); } @Test @@ -485,9 +486,9 @@ public void testDeserializationAsString02WithTimeZoneColonless() throws Exceptio OffsetDateTime value = m.readValue('"' + sDate + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z2), value.getOffset()); + assertEquals(getOffset(value, Z2), value.getOffset(), "The time zone is not correct."); } @Test @@ -499,7 +500,7 @@ public void testDeserializationAsString03WithoutTimeZone() throws Exception OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -512,7 +513,7 @@ public void testDeserializationAsString03WithTimeZone() throws Exception OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -525,7 +526,7 @@ public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exceptio OffsetDateTime value = r.readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z3), value.getOffset()); + assertEquals(getOffset(value, Z3), value.getOffset(), "The time zone is not correct."); } @@ -540,7 +541,7 @@ public void testDeserializationAsString03WithTimeZoneColonless() throws Exceptio OffsetDateTime value = r.readValue('"' + sDate + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z3), value.getOffset()); + assertEquals(getOffset(value, Z3), value.getOffset(), "The time zone is not correct."); } @Test @@ -553,9 +554,9 @@ public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -569,9 +570,9 @@ public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -585,9 +586,9 @@ public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -602,9 +603,9 @@ public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -618,9 +619,9 @@ public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -635,9 +636,9 @@ public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -651,9 +652,9 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -668,9 +669,9 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -686,10 +687,10 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); OffsetDateTime cast = (OffsetDateTime) value; assertIsEqual(date, cast); - assertEquals("The time zone is not correct.", getOffset(cast, Z3), cast.getOffset()); + assertEquals(getOffset(cast, Z3), cast.getOffset(), "The time zone is not correct."); } @Test @@ -743,10 +744,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); OffsetDateTime actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test ( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "OffsetDateTime"; @@ -761,7 +762,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } // [module-java8#166] @@ -777,8 +778,8 @@ public void testDeserializationNoAdjustIfMIN() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value, "The value should not be null."); + assertTrue(value instanceof OffsetDateTime, "The value should be an OffsetDateTime."); OffsetDateTime actualValue = (OffsetDateTime) value; assertIsEqual(date, actualValue); assertEquals(date.getOffset(),actualValue.getOffset()); @@ -796,8 +797,8 @@ public void testDeserializationNoAdjustIfMAX() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value, "The value should not be null."); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); OffsetDateTime actualValue = (OffsetDateTime) value; assertIsEqual(date, actualValue); assertEquals(date.getOffset(),actualValue.getOffset()); @@ -811,6 +812,40 @@ public void testOffsetDateTimeMinOrMax() throws Exception _testOffsetDateTimeMinOrMax(OffsetDateTime.MAX); } + @Test + public void OffsetDateTime_with_offset_can_be_deserialized() throws Exception { + ObjectReader r = MAPPER.readerFor(OffsetDateTime.class) + .without(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); + + String base = "2015-07-24T12:23:34.184"; + for (String offset : Arrays.asList("+00", "-00")) { + String time = base + offset; + if (!System.getProperty("java.version").startsWith("1.8")) { + // JDK 8 cannot parse hour offsets without minutes + assertIsEqual(OffsetDateTime.parse("2015-07-24T12:23:34.184Z"), r.readValue('"' + time + '"')); + } + assertIsEqual(OffsetDateTime.parse("2015-07-24T12:23:34.184Z"), r.readValue('"' + time + "00" + '"')); + assertIsEqual(OffsetDateTime.parse("2015-07-24T12:23:34.184Z"), r.readValue('"' + time + ":00" + '"')); + assertIsEqual(OffsetDateTime.parse("2015-07-24T12:23:34.184" + offset + ":30"), r.readValue('"' + time + "30" + '"')); + assertIsEqual(OffsetDateTime.parse("2015-07-24T12:23:34.184" + offset + ":30"), r.readValue('"' + time + ":30" + '"')); + } + + for (String prefix : Arrays.asList("-", "+")) { + for (String hours : Arrays.asList("00", "01", "02", "03", "11", "12")) { + String time = base + prefix + hours; + OffsetDateTime expectedHour = OffsetDateTime.parse(time + ":00"); + if (!System.getProperty("java.version").startsWith("1.8")) { + // JDK 8 cannot parse hour offsets without minutes + assertIsEqual(expectedHour, r.readValue('"' + time + '"')); + } + assertIsEqual(expectedHour, r.readValue('"' + time + "00" + '"')); + assertIsEqual(expectedHour, r.readValue('"' + time + ":00" + '"')); + assertIsEqual(OffsetDateTime.parse(time + ":30"), r.readValue('"' + time + "30" + '"')); + assertIsEqual(OffsetDateTime.parse(time + ":30"), r.readValue('"' + time + ":30" + '"')); + } + } + } + private void _testOffsetDateTimeMinOrMax(OffsetDateTime offsetDateTime) throws Exception { @@ -821,8 +856,8 @@ private void _testOffsetDateTimeMinOrMax(OffsetDateTime offsetDateTime) private static void assertIsEqual(OffsetDateTime expected, OffsetDateTime actual) { - assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.", - expected.isEqual(actual)); + assertTrue(expected.isEqual(actual), + "The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">."); } private static ZoneOffset getDefaultOffset(OffsetDateTime date) @@ -838,4 +873,93 @@ private static ZoneOffset getOffset(OffsetDateTime date, ZoneId zone) private static String offsetWithoutColon(String string){ return new StringBuilder(string).deleteCharAt(string.lastIndexOf(":")).toString(); } + + /* + /********************************************************************** + /* Tests for custom formatter (modules-java8#376) + /********************************************************************** + */ + + @Test + public void testDeserializationWithCustomFormatter() throws Exception + { + // Create a custom formatter that can parse ISO_LOCAL_DATE_TIME and default offset to 0 + DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + .optionalStart() + .parseLenient() + .appendOffsetId() + .parseStrict() + .optionalEnd() + .parseDefaulting(java.time.temporal.ChronoField.OFFSET_SECONDS, 0) + .toFormatter(); + + // Create custom deserializer with the custom formatter + InstantDeserializer customDeserializer = + InstantDeserializer.OFFSET_DATE_TIME.withDateFormat(customFormatter); + + // Create a custom module to override the default deserializer + SimpleModule customModule = new SimpleModule("CustomOffsetDateTimeModule") + .addDeserializer(OffsetDateTime.class, customDeserializer); + + // Add both JavaTimeModule (for other types) and our custom module + // The custom module will override OffsetDateTime deserialization + ObjectMapper mapper = mapperBuilder() + .addModule(customModule) + .build(); + + // Test deserializing date-time without offset (should default to +00:00) + // This is the main use case from issue #376 - parsing ISO_LOCAL_DATE_TIME with default offset + String jsonWithoutOffset = q("2025-01-01T22:01:05"); + OffsetDateTime result = mapper.readValue(jsonWithoutOffset, OffsetDateTime.class); + + assertNotNull(result); + assertEquals(2025, result.getYear()); + assertEquals(1, result.getMonthValue()); + assertEquals(1, result.getDayOfMonth()); + assertEquals(22, result.getHour()); + assertEquals(1, result.getMinute()); + assertEquals(5, result.getSecond()); + assertEquals(ZoneOffset.UTC, result.getOffset()); + + // Test that standard ISO format with offset still works + String jsonWithOffset = q("2025-01-01T22:01:05+02:00"); + OffsetDateTime resultWithOffset = mapper.readValue(jsonWithOffset, OffsetDateTime.class); + + assertNotNull(resultWithOffset); + assertEquals(2025, resultWithOffset.getYear()); + // Verify parsing succeeded - the exact time may be adjusted based on offset conversion + assertTrue(resultWithOffset.toInstant().equals(OffsetDateTime.parse("2025-01-01T22:01:05+02:00").toInstant())); + } + + @Test + public void testDeserializationWithCustomFormatterRoundTrip() throws Exception + { + // Create a custom formatter that can parse ISO_LOCAL_DATE_TIME and default offset to 0 + DateTimeFormatter customFormatter = new java.time.format.DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + .optionalStart() + .parseLenient() + .appendOffsetId() + .parseStrict() + .optionalEnd() + .parseDefaulting(java.time.temporal.ChronoField.OFFSET_SECONDS, 0) + .toFormatter(); + + InstantDeserializer customDeserializer = + InstantDeserializer.OFFSET_DATE_TIME.withDateFormat(customFormatter); + SimpleModule customModule = new SimpleModule("CustomOffsetDateTimeModule") + .addDeserializer(OffsetDateTime.class, customDeserializer); + + ObjectMapper mapper = mapperBuilder() + .addModule(customModule) + .build(); + + // Verify standard ISO format still works + OffsetDateTime original = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 0, ZoneOffset.UTC); + String json = mapper.writeValueAsString(original); + OffsetDateTime roundTripped = mapper.readValue(json, OffsetDateTime.class); + + assertIsEqual(original, roundTripped); + } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserTest.java index aadf62fc..febeda1f 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OffsetTimeDeserTest.java @@ -12,8 +12,6 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - import java.io.IOException; import java.time.OffsetTime; import java.time.ZoneOffset; @@ -22,11 +20,9 @@ import java.util.List; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; public class OffsetTimeDeserTest extends ModuleTestBase { @@ -39,7 +35,7 @@ static class Pojo45s { public List objects; } - static class Pojo45 { + static class Pojo45 { public java.time.LocalDate partDate; public java.time.OffsetTime starttime; public java.time.OffsetTime endtime; @@ -76,8 +72,8 @@ public void testDeserializationAsTimestamp01() throws Exception .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[15,43,\"+0300\"]"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -86,8 +82,8 @@ public void testDeserializationAsTimestamp02() throws Exception OffsetTime time = OffsetTime.of(9, 22, 57, 0, ZoneOffset.of("-0630")); OffsetTime value = READER.readValue("[9,22,57,\"-06:30\"]"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -98,32 +94,30 @@ public void testDeserializationAsTimestamp03Nanoseconds() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[9,22,0,57,\"-06:30\"]"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertEquals(time, value, "The value is not correct."); } @Test - public void testDeserializationAsTimestamp03Milliseconds() throws Exception - { + public void testDeserializationAsTimestamp03Milliseconds() throws Exception { OffsetTime time = OffsetTime.of(9, 22, 0, 57000000, ZoneOffset.of("-0630")); OffsetTime value = READER .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[9,22,0,57,\"-06:30\"]"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertEquals(time, value, "The value is not correct."); } @Test - public void testDeserializationAsTimestamp04Nanoseconds() throws Exception - { + public void testDeserializationAsTimestamp04Nanoseconds() throws Exception { OffsetTime time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); OffsetTime value = READER .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[22,31,5,829837,\"+11:00\"]"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -133,7 +127,7 @@ public void testDeserializationAsTimestamp04Milliseconds01() throws Exception OffsetTime value = READER .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[22,31,5,829837,\"+11:00\"]"); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -143,7 +137,7 @@ public void testDeserializationAsTimestamp04Milliseconds02() throws Exception OffsetTime value = READER .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("[22,31,5,829,\"+11:00\"]"); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); } @Test @@ -155,8 +149,8 @@ public void testDeserializationAsTimestamp05Nanoseconds() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue(a2q("{'value':[9,22,0,57,'-06:30']}")); - assertNotNull("The value should not be null.", actual); - assertEquals("The value is not correct.", time, actual.value); + assertNotNull(actual, "The value should not be null."); + assertEquals(time, actual.value, "The value is not correct."); } @Test @@ -168,8 +162,8 @@ public void testDeserializationAsTimestamp05Milliseconds01() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue(a2q("{'value':[9,22,0,57,'-06:30']}")); - assertNotNull("The value should not be null.", actual); - assertEquals("The value is not correct.", time, actual.value); + assertNotNull(actual, "The value should not be null."); + assertEquals(time, actual.value, "The value is not correct."); } @Test @@ -181,8 +175,8 @@ public void testDeserializationAsTimestamp05Milliseconds02() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue(a2q("{'value':[9,22,0,4257,'-06:30']}")); - assertNotNull("The value should not be null.", actual); - assertEquals("The value is not correct.", time, actual.value); + assertNotNull(actual, "The value should not be null."); + assertEquals(time, actual.value, "The value is not correct."); } @Test @@ -190,17 +184,18 @@ public void testDeserializationFromString01() throws Exception { OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300")); OffsetTime value = READER.readValue('"' + time.toString() + '"'); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); time = OffsetTime.of(9, 22, 57, 0, ZoneOffset.of("-0630")); value = READER.readValue('"' + time.toString() + '"'); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); time = OffsetTime.of(22, 31, 5, 829837, ZoneOffset.of("+1100")); value = READER.readValue('"' + time.toString() + '"'); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value, "The value is not correct."); - expectSuccess(OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC), "'12:00Z'"); + assertEquals(OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC), + read("'12:00Z'")); } @Test @@ -208,7 +203,7 @@ public void testBadDeserializationFromString01() throws Throwable { expectFailure(q("notanoffsettime")); } - + @Test public void testDeserializationWithTypeInfo01() throws Exception { @@ -221,9 +216,9 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertInstanceOf(OffsetTime.class, value, "The value should be a OffsetTime."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -238,9 +233,9 @@ public void testDeserializationWithTypeInfo02() throws Exception "[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value, "The value should not be null."); + assertInstanceOf(OffsetTime.class, value, "The value should be a OffsetTime."); + assertEquals(time, value, "The value is not correct."); } @Test @@ -252,9 +247,9 @@ public void testDeserializationWithTypeInfo03() throws Exception mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = mapper.readValue( "[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class - ); - assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); - assertEquals("The value is not correct.", time, value); + ); + assertTrue(value instanceof OffsetTime, "The value should be a OffsetTime."); + assertEquals(time, value, "The value is not correct."); } // for [datatype-jsr310#45] @@ -279,20 +274,20 @@ public void testDeserializationAsArrayDisabled() throws Throwable // OK } } - + @Test public void testDeserializationAsEmptyArrayDisabled() throws Throwable { // works even without the feature enabled assertNull(read("[]")); } - + @Test public void testDeserializationAsArrayEnabled() throws Throwable { OffsetTime value = READER.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) .readValue(a2q("['12:00Z']")); - expect(OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC), value); + assertEquals(OffsetTime.of(12, 0, 0, 0, ZoneOffset.UTC), value); } @Test @@ -325,10 +320,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); OffsetTime actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test ( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "OffsetTime"; @@ -343,7 +338,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } private void expectFailure(String json) throws Throwable { @@ -360,21 +355,7 @@ private void expectFailure(String json) throws Throwable { } } - private void expectSuccess(Object exp, String json) throws IOException { - final OffsetTime value = read(json); - notNull(value); - expect(exp, value); - } - private OffsetTime read(final String json) throws IOException { return READER.readValue(a2q(json)); } - - private static void notNull(Object value) { - assertNotNull("The value should not be null.", value); - } - - private static void expect(Object exp, Object value) { - assertEquals("The value is not correct.", exp, value); - } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserTest.java index 223552db..9377f5f4 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/OneBasedMonthDeserTest.java @@ -3,8 +3,8 @@ import java.time.Month; import java.time.temporal.TemporalAccessor; -import org.junit.Test; -import org.junit.function.ThrowingRunnable; +import org.junit.jupiter.api.function.Executable; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; @@ -17,8 +17,11 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.EnumSource; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class OneBasedMonthDeserTest extends ModuleTestBase { @@ -29,43 +32,57 @@ static class Wrapper { public Wrapper() { } } - @Test - public void testDeserializationAsString01_oneBased() throws Exception + @ParameterizedTest + @EnumSource(Month.class) + public void testDeserializationAsString01_oneBased(Month expectedMonth) throws Exception { - assertEquals(Month.JANUARY, readerForOneBased().readValue("\"1\"")); + int monthNum = expectedMonth.getValue(); + assertEquals(expectedMonth, readerForOneBased().readValue("\"" + monthNum + '"')); } - @Test - public void testDeserializationAsString01_zeroBased() throws Exception + @ParameterizedTest + @EnumSource(Month.class) + public void testDeserializationAsString01_zeroBased(Month expectedMonth) throws Exception { - assertEquals(Month.FEBRUARY, readerForZeroBased().readValue("\"1\"")); + int monthNum = expectedMonth.ordinal(); + assertEquals(expectedMonth, readerForZeroBased().readValue("\"" + monthNum + '"')); } - @Test - public void testDeserializationAsString02_oneBased() throws Exception + @ParameterizedTest + @EnumSource(Month.class) + public void testDeserializationAsString02_oneBased(Month month) throws Exception { - assertEquals(Month.JANUARY, readerForOneBased().readValue("\"JANUARY\"")); + assertEquals(month, readerForOneBased().readValue("\"" + month.name() + '"')); } - @Test - public void testDeserializationAsString02_zeroBased() throws Exception + @ParameterizedTest + @EnumSource(Month.class) + public void testDeserializationAsString02_zeroBased(Month month) throws Exception { - assertEquals(Month.JANUARY, readerForZeroBased().readValue("\"JANUARY\"")); - } - - @Test - public void testBadDeserializationAsString01_oneBased() { + assertEquals(month, readerForOneBased().readValue("\"" + month.name() + '"')); + } + + @ParameterizedTest + @CsvSource({ + "notamonth , 'Cannot deserialize value of type `java.time.Month` from String \"notamonth\": not one of the values accepted for Enum class: [OCTOBER, SEPTEMBER, JUNE, MARCH, MAY, APRIL, JULY, JANUARY, FEBRUARY, DECEMBER, AUGUST, NOVEMBER]'", + "JANUAR , 'Cannot deserialize value of type `java.time.Month` from String \"JANUAR\": not one of the values accepted for Enum class: [OCTOBER, SEPTEMBER, JUNE, MARCH, MAY, APRIL, JULY, JANUARY, FEBRUARY, DECEMBER, AUGUST, NOVEMBER]'", + "march , 'Cannot deserialize value of type `java.time.Month` from String \"march\": not one of the values accepted for Enum class: [OCTOBER, SEPTEMBER, JUNE, MARCH, MAY, APRIL, JULY, JANUARY, FEBRUARY, DECEMBER, AUGUST, NOVEMBER]'", + "0 , 'Month number 0 not allowed for 1-based Month.'", + "13 , 'Month number 13 not allowed for 1-based Month.'", + }) + public void testBadDeserializationAsString01_oneBased(String monthSpec, String expectedMessage) { + String value = "\"" + monthSpec + '"'; assertError( - () -> readerForOneBased().readValue("\"notamonth\""), + () -> readerForOneBased().readValue(value), InvalidFormatException.class, - "Cannot deserialize value of type `java.time.Month` from String \"notamonth\": not one of the values accepted for Enum class: [OCTOBER, SEPTEMBER, JUNE, MARCH, MAY, APRIL, JULY, JANUARY, FEBRUARY, DECEMBER, AUGUST, NOVEMBER]" + expectedMessage ); } - static void assertError(ThrowingRunnable codeToRun, Class expectedException, String expectedMessage) { + static void assertError(Executable codeToRun, Class expectedException, String expectedMessage) { try { - codeToRun.run(); + codeToRun.execute(); fail(String.format("Expecting %s, but nothing was thrown!", expectedException.getName())); } catch (Throwable actualException) { if (!expectedException.isInstance(actualException)) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/PeriodDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/PeriodDeserTest.java index b1190924..53f5ff29 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/PeriodDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/PeriodDeserTest.java @@ -20,6 +20,8 @@ import java.time.temporal.TemporalAmount; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -28,10 +30,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.*; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.*; public class PeriodDeserTest extends ModuleTestBase { @@ -43,7 +42,7 @@ public void testDeserialization01() throws Exception { Period period = Period.of(1, 6, 15); Period value = MAPPER.readValue('"' + period.toString() + '"', Period.class); - assertEquals("The value is not correct.", period, value); + assertEquals(period, value, "The value is not correct."); } @Test @@ -51,7 +50,7 @@ public void testDeserialization02() throws Exception { Period period = Period.of(0, 0, 21); Period value = MAPPER.readValue('"' + period.toString() + '"', Period.class); - assertEquals("The value is not correct.", period, value); + assertEquals(period, value, "The value is not correct."); } @Test @@ -66,9 +65,9 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + Period.class.getName() + "\",\"" + period.toString() + "\"]", TemporalAmount.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a Period.", value instanceof Period); - assertEquals("The value is not correct.", period, value); + assertNotNull(value, "The value should not be null."); + assertInstanceOf(Period.class, value, "The value should be a Period."); + assertEquals(period, value, "The value is not correct."); } /* @@ -92,10 +91,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); Period actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting",null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "period"; @@ -109,6 +108,6 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap("date", "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserTest.java index 3ca84695..dffd53e2 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearDeserTest.java @@ -23,6 +23,8 @@ import java.util.Map; import java.util.Objects; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; @@ -34,13 +36,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; public class YearDeserTest extends ModuleTestBase { @@ -62,8 +58,8 @@ public FormattedYear(Year year) { @Test public void testDeserializationAsString01() throws Exception { - assertEquals("The value is not correct.",Year.of(2000), - READER.readValue(q("2000"))); + assertEquals(Year.of(2000), READER.readValue(q("2000")), + "The value is not correct."); } @Test @@ -115,7 +111,7 @@ public void testDeserializationAsArrayEnabled() throws Throwable Year value= newMapper() .configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true) .readerFor(Year.class).readValue("[\"2000\"]"); - assertEquals("The value is not correct.", Year.of(2000), value); + assertEquals(Year.of(2000), value, "The value is not correct."); } @Test @@ -133,9 +129,9 @@ public void testDeserializationAsEmptyArrayEnabled() throws Throwable public void testDefaultDeserialization() throws Exception { Year value = READER.readValue("1986"); - assertEquals("The value is not correct.", Year.of(1986), value); + assertEquals(Year.of(1986), value, "The value is not correct."); value = READER.readValue("2013"); - assertEquals("The value is not correct.", Year.of(2013), value); + assertEquals(Year.of(2013), value, "The value is not correct."); } @Test @@ -144,8 +140,8 @@ public void testDeserializationWithTypeInfo() throws Exception ObjectMapper mapper = newMapper() .addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = mapper.readValue("[\"" + Year.class.getName() + "\",2005]", Temporal.class); - assertTrue("The value should be a Year.", value instanceof Year); - assertEquals("The value is not correct.", Year.of(2005), value); + assertTrue(value instanceof Year, "The value should be a Year."); + assertEquals(Year.of(2005), value, "The value is not correct."); } @Test diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserTest.java index b4740b4e..dc8bc2ae 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/YearMonthDeserTest.java @@ -6,6 +6,8 @@ import java.time.format.DateTimeParseException; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; @@ -17,12 +19,7 @@ import com.fasterxml.jackson.databind.exc.MismatchedInputException; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; public class YearMonthDeserTest extends ModuleTestBase { @@ -119,10 +116,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, dateValAsEmptyStr)); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); YearMonth actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting",null, actualDateFromEmptyStr); + assertNull(actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "YearMonth"; @@ -136,7 +133,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap("date", "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } private void expectFailure(String json) throws Exception { @@ -166,10 +163,10 @@ private YearMonth read(final String json) throws Exception { } private static void notNull(Object value) { - assertNotNull("The value should not be null.", value); + assertNotNull(value, "The value should not be null."); } private static void expect(Object exp, Object value) { - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneIdDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneIdDeserTest.java index 84e0f000..c1420f12 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneIdDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneIdDeserTest.java @@ -16,12 +16,11 @@ package com.fasterxml.jackson.datatype.jsr310.deser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - import java.time.ZoneId; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -30,7 +29,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class ZoneIdDeserTest extends ModuleTestBase { @@ -44,22 +43,25 @@ public class ZoneIdDeserTest extends ModuleTestBase @Test public void testDeserialization01() throws Exception { - assertEquals("The value is not correct.", ZoneId.of("America/Chicago"), - MAPPER.readValue("\"America/Chicago\"", ZoneId.class)); + assertEquals(ZoneId.of("America/Chicago"), + MAPPER.readValue("\"America/Chicago\"", ZoneId.class), + "The value is not correct."); } @Test public void testDeserialization02() throws Exception { - assertEquals("The value is not correct.", ZoneId.of("America/Anchorage"), - MAPPER.readValue("\"America/Anchorage\"", ZoneId.class)); + assertEquals(ZoneId.of("America/Anchorage"), + MAPPER.readValue("\"America/Anchorage\"", ZoneId.class), + "The value is not correct."); } @Test public void testDeserializationWithTypeInfo02() throws Exception { ZoneId value = MOCK_OBJECT_MIXIN_MAPPER.readValue("[\"" + ZoneId.class.getName() + "\",\"America/Denver\"]", ZoneId.class); - assertEquals("The value is not correct.", ZoneId.of("America/Denver"), value); + assertEquals(ZoneId.of("America/Denver"), value, + "The value is not correct."); } /* @@ -83,10 +85,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); ZoneId actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test ( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "zoneId"; @@ -101,6 +103,6 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneOffsetDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneOffsetDeserTest.java index e67ffe09..f067ea30 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneOffsetDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZoneOffsetDeserTest.java @@ -20,10 +20,7 @@ import java.time.ZoneOffset; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.type.TypeReference; @@ -35,7 +32,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class ZoneOffsetDeserTest extends ModuleTestBase { @@ -46,12 +43,12 @@ public class ZoneOffsetDeserTest extends ModuleTestBase @Test public void testDeserializationFromString() throws Exception { - assertEquals("The value is not correct.", ZoneOffset.of("Z"), - READER.readValue("\"Z\"")); - assertEquals("The value is not correct.", ZoneOffset.of("+0300"), - READER.readValue(q("+0300"))); - assertEquals("The value is not correct.", ZoneOffset.of("-0630"), - READER.readValue("\"-06:30\"")); + assertEquals(ZoneOffset.of("Z"), READER.readValue("\"Z\""), + "The value is not correct."); + assertEquals(ZoneOffset.of("+0300"), READER.readValue(q("+0300")), + "The value is not correct."); + assertEquals(ZoneOffset.of("-0630"), READER.readValue("\"-06:30\""), + "The value is not correct."); } @Test @@ -60,8 +57,8 @@ public void testDeserializationWithTypeInfo03() throws Exception ObjectMapper mapper = newMapper() .addMixIn(ZoneId.class, MockObjectConfiguration.class); ZoneId value = mapper.readValue("[\"" + ZoneOffset.class.getName() + "\",\"+0415\"]", ZoneId.class); - assertTrue("The value should be a ZoneOffset.", value instanceof ZoneOffset); - assertEquals("The value is not correct.", ZoneOffset.of("+0415"), value); + assertTrue(value instanceof ZoneOffset, "The value should be a ZoneOffset."); + assertEquals(ZoneOffset.of("+0415"), value, "The value is not correct."); } @Test @@ -111,7 +108,7 @@ public void testDeserializationAsArrayEnabled() throws Throwable ZoneOffset value = READER .with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS) .readValue("[\"+0300\"]"); - assertEquals("The value is not correct.", ZoneOffset.of("+0300"), value); + assertEquals(ZoneOffset.of("+0300"), value, "The value is not correct."); } @Test @@ -145,10 +142,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); ZoneId actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test ( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "zoneOffset"; @@ -163,6 +160,6 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZonedDateTimeDeserTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZonedDateTimeDeserTest.java index 72dc2206..1b101cfa 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZonedDateTimeDeserTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/ZonedDateTimeDeserTest.java @@ -14,21 +14,21 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.IOException; import java.time.LocalDateTime; +import java.time.OffsetDateTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +import java.util.Arrays; import java.util.Map; import java.util.TimeZone; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.*; public class ZonedDateTimeDeserTest extends ModuleTestBase { @@ -69,9 +69,9 @@ public WrapperWithReadTimestampsAsNanosEnabled() { } @Test public void testDeserFromString() throws Exception { - assertEquals("The value is not correct.", - ZonedDateTime.of(2000, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC), - READER.readValue(q("2000-01-01T12:00Z"))); + assertEquals(ZonedDateTime.of(2000, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC), + READER.readValue(q("2000-01-01T12:00Z")), + "The value is not correct."); } // [modules-java#281] @@ -80,9 +80,9 @@ public void testDeserFromStringNoZoneIdNormalization() throws Exception { // 11-Nov-2023, tatu: Not sure this is great test but... does show diff // behavior with and without `JavaTimeFeature.NORMALIZE_DESERIALIZED_ZONE_ID` - assertEquals("The value is not correct.", - ZonedDateTime.of(2000, 1, 1, 12, 0, 0, 0, TimeZone.getTimeZone("UTC").toZoneId()), - READER_NON_NORMALIZED_ZONEID.readValue(q("2000-01-01T12:00Z"))); + assertEquals(ZonedDateTime.of(2000, 1, 1, 12, 0, 0, 0, TimeZone.getTimeZone("UTC").toZoneId()), + READER_NON_NORMALIZED_ZONEID.readValue(q("2000-01-01T12:00Z")), + "The value is not correct."); } @Test @@ -94,7 +94,7 @@ public void testDeserializationAsInt01() throws Exception ZoneOffset.UTC); WrapperWithReadTimestampsAsNanosDisabled actual = reader.readValue(a2q("{'value':1001}")); - assertEquals("The value is not correct.", date, actual.value); + assertEquals(date, actual.value, "The value is not correct."); } @Test @@ -106,7 +106,7 @@ public void testDeserializationAsInt02() throws Exception ZoneOffset.UTC); WrapperWithReadTimestampsAsNanosEnabled actual = reader.readValue(a2q("{'value':1}")); - assertEquals("The value is not correct.", date, actual.value); + assertEquals(date, actual.value, "The value is not correct."); } @Test @@ -114,9 +114,9 @@ public void testDeserializationComparedToStandard() throws Throwable { String inputString = "2021-02-01T19:49:04.0513486Z"; - assertEquals("The value is not correct.", - DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(inputString, ZonedDateTime::from), - READER.readValue(q(inputString))); + assertEquals(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(inputString, ZonedDateTime::from), + READER.readValue(q(inputString)), + "The value is not correct."); } @Test @@ -128,9 +128,9 @@ public void testDeserializationComparedToStandard2() throws Throwable .configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, false) .readerFor(ZonedDateTime.class).readValue(q(inputString)); - assertEquals("The value is not correct.", - DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(inputString, ZonedDateTime::from), - converted); + assertEquals(DateTimeFormatter.ISO_ZONED_DATE_TIME.parse(inputString, ZonedDateTime::from), + converted, + "The value is not correct."); } @Test @@ -183,9 +183,9 @@ public void testDeserializationAsArrayEnabled() throws Throwable ZonedDateTime value = newMapper() .configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true) .readerFor(ZonedDateTime.class).readValue(a2q(json)); - assertEquals("The value is not correct.", - ZonedDateTime.of(2000, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC), - value); + assertEquals(ZonedDateTime.of(2000, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC), + value, + "The value is not correct."); } @@ -206,9 +206,9 @@ public void testDeserializationWithZonePreserved() throws Throwable WrapperWithFeatures wrapper = newMapper() .readerFor(WrapperWithFeatures.class) .readValue("{\"value\":\"2000-01-01T12:00+01:00\"}"); - assertEquals("Timezone should be preserved.", - ZonedDateTime.of(2000, 1, 1, 12, 0, 0 ,0, ZoneOffset.ofHours(1)), - wrapper.value); + assertEquals(ZonedDateTime.of(2000, 1, 1, 12, 0, 0 ,0, ZoneOffset.ofHours(1)), + wrapper.value, + "Timezone should be preserved."); } /* @@ -232,10 +232,10 @@ public void testLenientDeserializeFromEmptyString() throws Exception { String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); Map actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr); ZonedDateTime actualDateFromEmptyStr = actualMapFromEmptyStr.get(key); - assertEquals("empty string failed to deserialize to null with lenient setting", null, actualDateFromEmptyStr); + assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting"); } - @Test ( expected = MismatchedInputException.class) + @Test public void testStrictDeserializeFromEmptyString() throws Exception { final String key = "zonedDateTime"; @@ -250,7 +250,7 @@ public void testStrictDeserializeFromEmptyString() throws Exception { assertNull(actualMapFromNullStr.get(key)); String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, "")); - objectReader.readValue(valueFromEmptyStr); + assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr)); } /* @@ -266,9 +266,9 @@ public void testDeserializationWithoutColonInOffset() throws Throwable .readerFor(WrapperWithFeatures.class) .readValue("{\"value\":\"2000-01-01T12:00+0100\"}"); - assertEquals("Value parses as if it were with colon", - ZonedDateTime.of(2000, 1, 1, 12, 0, 0 ,0, ZoneOffset.ofHours(1)), - wrapper.value); + assertEquals(ZonedDateTime.of(2000, 1, 1, 12, 0, 0 ,0, ZoneOffset.ofHours(1)), + wrapper.value, + "Value parses as if it were with colon"); } @Test @@ -277,11 +277,44 @@ public void testDeserializationWithoutColonInTimeZoneWithTZDB() throws Throwable WrapperWithFeatures wrapper = newMapper() .readerFor(WrapperWithFeatures.class) .readValue("{\"value\":\"2000-01-01T12:00+0100[Europe/Paris]\"}"); - assertEquals("Timezone should be preserved.", - ZonedDateTime.of(2000, 1, 1, 12, 0, 0 ,0, ZoneId.of("Europe/Paris")), - wrapper.value); + assertEquals(ZonedDateTime.of(2000, 1, 1, 12, 0, 0 ,0, ZoneId.of("Europe/Paris")), + wrapper.value, + "Timezone should be preserved."); } + @Test + public void ZonedDateTime_with_offset_can_be_deserialized() throws Exception { + ObjectReader r = newMapper().readerFor(ZonedDateTime.class) + .without(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE); + + String base = "2015-07-24T12:23:34.184"; + for (String offset : Arrays.asList("+00", "-00")) { + String time = base + offset; + if (!System.getProperty("java.version").startsWith("1.8")) { + // JDK 8 cannot parse hour offsets without minutes + assertEquals(ZonedDateTime.parse("2015-07-24T12:23:34.184Z"), r.readValue('"' + time + '"')); + } + assertEquals(ZonedDateTime.parse("2015-07-24T12:23:34.184Z"), r.readValue('"' + time + "00" + '"')); + assertEquals(ZonedDateTime.parse("2015-07-24T12:23:34.184Z"), r.readValue('"' + time + ":00" + '"')); + assertEquals(ZonedDateTime.parse("2015-07-24T12:23:34.184" + offset + ":30" ), r.readValue('"' + time + "30" + '"')); + assertEquals(ZonedDateTime.parse("2015-07-24T12:23:34.184" + offset + ":30" ), r.readValue('"' + time + ":30" + '"')); + } + + for (String prefix : Arrays.asList("-", "+")) { + for (String hours : Arrays.asList("00", "01", "02", "03", "11", "12")) { + String time = base + prefix + hours; + ZonedDateTime expectedHour = ZonedDateTime.parse(time + ":00"); + if (!System.getProperty("java.version").startsWith("1.8")) { + // JDK 8 cannot parse hour offsets without minutes + assertEquals(expectedHour, r.readValue('"' + time + '"')); + } + assertEquals(expectedHour, r.readValue('"' + time + "00" + '"')); + assertEquals(expectedHour, r.readValue('"' + time + ":00" + '"')); + assertEquals(ZonedDateTime.parse(time + ":30"), r.readValue('"' + time + "30" + '"')); + assertEquals(ZonedDateTime.parse(time + ":30"), r.readValue('"' + time + ":30" + '"')); + } + } + } private void expectFailure(String json) throws Throwable { try { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/key/ZonedDateTimeKeyDeserializerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/key/ZonedDateTimeKeyDeserializerTest.java index 02b23c9a..802d10b2 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/key/ZonedDateTimeKeyDeserializerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/deser/key/ZonedDateTimeKeyDeserializerTest.java @@ -2,7 +2,7 @@ import com.fasterxml.jackson.core.type.TypeReference; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; @@ -10,8 +10,9 @@ import java.time.ZonedDateTime; import java.util.Map; -import static org.junit.Assert.assertEquals; -import static org.junit.Assume.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assumptions.assumeFalse; +import static org.junit.jupiter.api.Assumptions.assumeTrue; // for [modules-java8#306] public class ZonedDateTimeKeyDeserializerTest @@ -38,7 +39,8 @@ public void ZonedDateTime_with_zone_name_can_be_deserialized() throws Exception } // 29-Oct-2024, tatu: Following two tests are for Java 8+ only vs Java 9 or later - + + // Java 9+ test @Test public void ZonedDateTime_with_place_name_can_be_deserialized() throws Exception { assumeFalse(System.getProperty("java.version").startsWith("1.8")); @@ -49,6 +51,7 @@ public void ZonedDateTime_with_place_name_can_be_deserialized() throws Exception assertEquals("2015-07-24T13:23:34.184+01:00[Europe/London]", entry.getKey().toString()); } + // Java 8 test @Test public void ZonedDateTime_with_place_name_can_be_deserialized_Java_8() throws Exception { // Java 8 parses this format incorrectly due to https://bugs.openjdk.org/browse/JDK-8066982 diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/OffsetDateTimeDeser279Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/OffsetDateTimeDeser279Test.java deleted file mode 100644 index c0a29463..00000000 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/OffsetDateTimeDeser279Test.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.fasterxml.jackson.datatype.jsr310.failing; - -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.temporal.ChronoUnit; - -import org.junit.Test; - -import com.fasterxml.jackson.annotation.JsonFormat; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; - -import static org.junit.Assert.assertEquals; - -public class OffsetDateTimeDeser279Test extends ModuleTestBase -{ - // For [modules-java8#279] - static class Wrapper279 { - OffsetDateTime date; - - public Wrapper279(OffsetDateTime d) { date = d; } - protected Wrapper279() { } - - @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss'Z'") - public OffsetDateTime getDate() { - return date; - } - public void setDate(OffsetDateTime date) { - this.date = date; - } - } - - private ObjectMapper MAPPER = newMapper(); - - // For [modules-java8#279] - @Test - public void testWrapperWithPattern279() throws Exception - { - final OffsetDateTime date = OffsetDateTime.now(ZoneId.of("UTC")) - .truncatedTo(ChronoUnit.SECONDS); - final Wrapper279 input = new Wrapper279(date); - final String json = MAPPER.writeValueAsString(input); - - Wrapper279 result = MAPPER.readValue(json, Wrapper279.class); - assertEquals(input.date, result.date); - } -} diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/DurationAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/DurationAsKeyTest.java index 9ef5bef8..03f5910f 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/DurationAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/DurationAsKeyTest.java @@ -1,17 +1,17 @@ package com.fasterxml.jackson.datatype.jsr310.key; -import static org.junit.Assert.assertEquals; - import java.time.Duration; import java.util.Collections; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class DurationAsKeyTest extends ModuleTestBase { @@ -23,13 +23,12 @@ public class DurationAsKeyTest extends ModuleTestBase @Test public void testSerialization() throws Exception { - assertEquals("Value is not correct", mapAsString(DURATION_STRING, "test"), + assertEquals(mapAsString(DURATION_STRING, "test"), MAPPER.writeValueAsString(Collections.singletonMap(DURATION, "test"))); } @Test public void testDeserialization() throws Exception { - assertEquals("Value is not correct", Collections.singletonMap(DURATION, "test"), - READER.readValue(mapAsString(DURATION_STRING, "test"))); + assertEquals(Collections.singletonMap(DURATION, "test"), READER.readValue(mapAsString(DURATION_STRING, "test"))); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/InstantAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/InstantAsKeyTest.java index 8f4748d3..41ef8900 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/InstantAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/InstantAsKeyTest.java @@ -1,17 +1,16 @@ package com.fasterxml.jackson.datatype.jsr310.key; -import static org.junit.Assert.assertEquals; - import java.time.Instant; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class InstantAsKeyTest extends ModuleTestBase { @@ -26,26 +25,26 @@ public class InstantAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { String value = MAPPER.writeValueAsString(asMap(INSTANT_0, "test")); - Assert.assertEquals("Value is incorrect", mapAsString(INSTANT_0_STRING, "test"), value); + assertEquals(mapAsString(INSTANT_0_STRING, "test"), value); } @Test public void testSerialization1() throws Exception { String value = MAPPER.writeValueAsString(asMap(INSTANT, "test")); - assertEquals("Value is incorrect", mapAsString(INSTANT_STRING, "test"), value); + assertEquals(mapAsString(INSTANT_STRING, "test"), value); } @Test public void testDeserialization0() throws Exception { Map value = READER.readValue(mapAsString(INSTANT_0_STRING, "test")); Map EXP = asMap(INSTANT_0, "test"); - assertEquals("Value is incorrect", EXP, value); + assertEquals(EXP, value, "Value is incorrect"); } @Test public void testDeserialization1() throws Exception { Map value = READER.readValue(mapAsString(INSTANT_STRING, "test")); Map EXP = asMap(INSTANT, "test"); - assertEquals("Value is incorrect", EXP, value); + assertEquals(EXP, value, "Value is incorrect"); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateAsKeyTest.java index 0cefbf03..23fb95aa 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateAsKeyTest.java @@ -1,16 +1,16 @@ package com.fasterxml.jackson.datatype.jsr310.key; -import static org.junit.Assert.assertEquals; - import java.time.LocalDate; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class LocalDateAsKeyTest extends ModuleTestBase { @@ -22,13 +22,12 @@ public class LocalDateAsKeyTest extends ModuleTestBase @Test public void testSerialization() throws Exception { - assertEquals("Incorrect value", mapAsString(DATE_STRING, "test"), + assertEquals(mapAsString(DATE_STRING, "test"), MAPPER.writeValueAsString(asMap(DATE, "test"))); } @Test public void testDeserialization() throws Exception { - assertEquals("Incorrect value", asMap(DATE, "test"), - READER.readValue(mapAsString(DATE_STRING, "test"))); + assertEquals(asMap(DATE, "test"), READER.readValue(mapAsString(DATE_STRING, "test"))); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateTimeAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateTimeAsKeyTest.java index d0da5f94..0c103104 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateTimeAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalDateTimeAsKeyTest.java @@ -1,12 +1,12 @@ package com.fasterxml.jackson.datatype.jsr310.key; -import static org.junit.Assert.assertEquals; - import java.io.IOException; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.ObjectMapper; @@ -14,7 +14,7 @@ import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class LocalDateTimeAsKeyTest extends ModuleTestBase { @@ -33,27 +33,27 @@ public class LocalDateTimeAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { String value = MAPPER.writeValueAsString(asMap(DATE_TIME_0, "test")); - assertEquals("Value is incorrect", mapAsString(DATE_TIME_0_STRING, "test"), value); + assertEquals(mapAsString(DATE_TIME_0_STRING, "test"), value); } @Test public void testSerialization1() throws Exception { String value = MAPPER.writeValueAsString(asMap(DATE_TIME, "test")); - assertEquals("Value is incorrect", mapAsString(DATE_TIME_STRING, "test"), value); + assertEquals(mapAsString(DATE_TIME_STRING, "test"), value); } @Test public void testDeserialization0() throws Exception { Map value = READER.readValue( mapAsString(DATE_TIME_0_STRING, "test")); - assertEquals("Value is incorrect", asMap(DATE_TIME_0, "test"), value); + assertEquals(asMap(DATE_TIME_0, "test"), value, "Value is incorrect"); } @Test public void testDeserialization1() throws Exception { Map value = READER.readValue( mapAsString(DATE_TIME_STRING, "test")); - assertEquals("Value is incorrect", asMap(DATE_TIME, "test"), value); + assertEquals(asMap(DATE_TIME, "test"), value, "Value is incorrect"); } @Test diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalTimeAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalTimeAsKeyTest.java index 3f349239..1416a2e4 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalTimeAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/LocalTimeAsKeyTest.java @@ -3,13 +3,14 @@ import java.time.LocalTime; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class LocalTimeAsKeyTest extends ModuleTestBase { @@ -28,25 +29,25 @@ public class LocalTimeAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(TIME_0_STRING, "test"), + assertEquals(mapAsString(TIME_0_STRING, "test"), MAPPER.writeValueAsString(asMap(TIME_0, "test"))); } @Test public void testSerialization1() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(TIME_STRING, "test"), + assertEquals(mapAsString(TIME_STRING, "test"), MAPPER.writeValueAsString(asMap(TIME, "test"))); } @Test public void testDeserialization0() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(TIME_0, "test"), - READER.readValue(mapAsString(TIME_0_STRING, "test"))); + assertEquals(asMap(TIME_0, "test"), READER.readValue(mapAsString(TIME_0_STRING, "test")), + "Value is incorrect"); } @Test public void testDeserialization1() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(TIME, "test"), - READER.readValue(mapAsString(TIME_STRING, "test"))); + assertEquals(asMap(TIME, "test"), READER.readValue(mapAsString(TIME_STRING, "test")), + "Value is incorrect"); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/MonthDayAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/MonthDayAsKeyTest.java index add3bccc..99e4c168 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/MonthDayAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/MonthDayAsKeyTest.java @@ -3,13 +3,14 @@ import java.time.MonthDay; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class MonthDayAsKeyTest extends ModuleTestBase { @@ -23,13 +24,13 @@ public class MonthDayAsKeyTest extends ModuleTestBase @Test public void testSerialization() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(MONTH_DAY_STRING, "test"), - MAPPER.writeValueAsString(asMap(MONTH_DAY, "test"))); + assertEquals(mapAsString(MONTH_DAY_STRING, "test"), MAPPER.writeValueAsString(asMap(MONTH_DAY, "test")), + "Value is incorrect"); } @Test public void testDeserialization() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(MONTH_DAY, "test"), - READER.readValue(mapAsString(MONTH_DAY_STRING, "test"))); + assertEquals(asMap(MONTH_DAY, "test"), READER.readValue(mapAsString(MONTH_DAY_STRING, "test")), + "Value is incorrect"); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetDateTimeAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetDateTimeAsKeyTest.java index 7c1ffe42..abbf5d56 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetDateTimeAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetDateTimeAsKeyTest.java @@ -5,13 +5,14 @@ import java.time.ZoneOffset; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class OffsetDateTimeAsKeyTest extends ModuleTestBase { @@ -30,36 +31,40 @@ public class OffsetDateTimeAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { String value = MAPPER.writeValueAsString(asMap(DATE_TIME_0, "test")); - Assert.assertEquals("Value is incorrect", mapAsString(DATE_TIME_0_STRING, "test"), value); + assertEquals(mapAsString(DATE_TIME_0_STRING, "test"), value, + "Value is incorrect"); } @Test public void testSerialization1() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(DATE_TIME_1_STRING, "test"), - MAPPER.writeValueAsString(asMap(DATE_TIME_1, "test"))); + assertEquals(mapAsString(DATE_TIME_1_STRING, "test"), + MAPPER.writeValueAsString(asMap(DATE_TIME_1, "test")), + "Value is incorrect"); } @Test public void testSerialization2() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(DATE_TIME_2_STRING, "test"), - MAPPER.writeValueAsString(asMap(DATE_TIME_2, "test"))); + assertEquals(mapAsString(DATE_TIME_2_STRING, "test"), + MAPPER.writeValueAsString(asMap(DATE_TIME_2, "test")), + "Value is incorrect"); } @Test public void testDeserialization0() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(DATE_TIME_0, "test"), - READER.readValue(mapAsString(DATE_TIME_0_STRING, "test"))); + assertEquals(asMap(DATE_TIME_0, "test"), READER.readValue(mapAsString(DATE_TIME_0_STRING, "test")), + "Value is incorrect"); } @Test public void testDeserialization1() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(DATE_TIME_1, "test"), - READER.readValue(mapAsString(DATE_TIME_1_STRING, "test"))); + assertEquals(asMap(DATE_TIME_1, "test"), READER.readValue(mapAsString(DATE_TIME_1_STRING, "test")), + "Value is incorrect"); } @Test public void testDeserialization2() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(DATE_TIME_2, "test"), - READER.readValue(mapAsString(DATE_TIME_2_STRING, "test"))); + assertEquals(asMap(DATE_TIME_2, "test"), + READER.readValue(mapAsString(DATE_TIME_2_STRING, "test")), + "Value is incorrect"); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetTimeAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetTimeAsKeyTest.java index 00e59e13..f0517b5c 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetTimeAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/OffsetTimeAsKeyTest.java @@ -4,13 +4,14 @@ import java.time.ZoneOffset; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class OffsetTimeAsKeyTest extends ModuleTestBase { @@ -28,37 +29,42 @@ public class OffsetTimeAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { - Assert.assertEquals(mapAsString(TIME_0_STRING, "test"), + assertEquals(mapAsString(TIME_0_STRING, "test"), MAPPER.writeValueAsString(asMap(TIME_0, "test"))); } @Test public void testSerialization1() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(TIME_1_STRING, "test"), - MAPPER.writeValueAsString(asMap(TIME_1, "test"))); + assertEquals(mapAsString(TIME_1_STRING, "test"), + MAPPER.writeValueAsString(asMap(TIME_1, "test")), + "Value is incorrect"); } @Test public void testSerialization2() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(TIME_2_STRING, "test"), - MAPPER.writeValueAsString(asMap(TIME_2, "test"))); + assertEquals(mapAsString(TIME_2_STRING, "test"), + MAPPER.writeValueAsString(asMap(TIME_2, "test")), + "Value is incorrect"); } @Test public void testDeserialization0() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(TIME_0, "test"), - READER.readValue(mapAsString(TIME_0_STRING, "test"))); + assertEquals(asMap(TIME_0, "test"), + READER.readValue(mapAsString(TIME_0_STRING, "test")), + "Value is incorrect"); } @Test public void testDeserialization1() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(TIME_1, "test"), - READER.readValue(mapAsString(TIME_1_STRING, "test"))); + assertEquals(asMap(TIME_1, "test"), + READER.readValue(mapAsString(TIME_1_STRING, "test")), + "Value is incorrect"); } @Test public void testDeserialization2() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(TIME_2, "test"), - READER.readValue(mapAsString(TIME_2_STRING, "test"))); + assertEquals(asMap(TIME_2, "test"), + READER.readValue(mapAsString(TIME_2_STRING, "test")), + "Value is incorrect"); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/PeriodAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/PeriodAsKeyTest.java index 603d22eb..610f35b8 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/PeriodAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/PeriodAsKeyTest.java @@ -3,13 +3,14 @@ import java.time.Period; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class PeriodAsKeyTest extends ModuleTestBase { @@ -25,25 +26,29 @@ public class PeriodAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(PERIOD_0_STRING, "test"), - MAPPER.writeValueAsString(asMap(PERIOD_0, "test"))); + assertEquals(mapAsString(PERIOD_0_STRING, "test"), + MAPPER.writeValueAsString(asMap(PERIOD_0, "test")), + "Value is incorrect"); } @Test public void testSerialization1() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(PERIOD_STRING, "test"), - MAPPER.writeValueAsString(asMap(PERIOD, "test"))); + assertEquals(mapAsString(PERIOD_STRING, "test"), + MAPPER.writeValueAsString(asMap(PERIOD, "test")), + "Value is incorrect"); } @Test public void testDeserialization0() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(PERIOD_0, "test"), - READER.readValue(mapAsString(PERIOD_0_STRING, "test"))); + assertEquals(asMap(PERIOD_0, "test"), + READER.readValue(mapAsString(PERIOD_0_STRING, "test")), + "Value is incorrect"); } @Test public void testDeserialization1() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(PERIOD, "test"), - READER.readValue(mapAsString(PERIOD_STRING, "test"))); + assertEquals(asMap(PERIOD, "test"), + READER.readValue(mapAsString(PERIOD_STRING, "test")), + "Value is incorrect"); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/TestNullKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/TestNullKeySerialization.java index ba239b4a..569c5148 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/TestNullKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/TestNullKeySerialization.java @@ -3,12 +3,14 @@ import java.time.LocalDate; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.*; public class TestNullKeySerialization extends ModuleTestBase { @@ -22,13 +24,13 @@ public void testSerialization() throws Exception { final ObjectMapper mapper = newMapper(); mapper.getSerializerProvider().setNullKeySerializer(new com.fasterxml.jackson.datatype.jsr310.ser.key.Jsr310NullKeySerializer()); String value = mapper.writeValueAsString(asMap(null, "test")); - Assert.assertEquals(mapAsString(com.fasterxml.jackson.datatype.jsr310.ser.key.Jsr310NullKeySerializer.NULL_KEY, "test"), value); + assertEquals(mapAsString(com.fasterxml.jackson.datatype.jsr310.ser.key.Jsr310NullKeySerializer.NULL_KEY, "test"), value); } @SuppressWarnings("deprecation") @Test public void testDeserialization() throws Exception { Map value = READER.readValue(mapAsString(com.fasterxml.jackson.datatype.jsr310.ser.key.Jsr310NullKeySerializer.NULL_KEY, "test")); - Assert.assertEquals(asMap(null, "test"), value); + assertEquals(asMap(null, "test"), value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearAsKeyTest.java index a359c524..ebf79f25 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearAsKeyTest.java @@ -5,13 +5,14 @@ import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; import java.time.Year; import java.util.Collections; import java.util.Map; -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; public class YearAsKeyTest extends ModuleTestBase { @@ -22,29 +23,34 @@ public class YearAsKeyTest extends ModuleTestBase @Test public void testKeySerialization() throws Exception { - assertEquals("Value is incorrect", mapAsString("3141", "test"), - MAPPER.writeValueAsString(asMap(Year.of(3141), "test"))); + assertEquals(mapAsString("3141", "test"), + MAPPER.writeValueAsString(asMap(Year.of(3141), "test")), + "Value is incorrect"); } @Test public void testKeyDeserialization() throws Exception { - assertEquals("Value is incorrect", asMap(Year.of(3141), "test"), - READER.readValue(mapAsString("3141", "test"))); + assertEquals(asMap(Year.of(3141), "test"), READER.readValue(mapAsString("3141", "test")), + "Value is incorrect"); // Test both padded, unpadded - assertEquals("Value is incorrect", asMap(Year.of(476), "test"), - READER.readValue(mapAsString("0476", "test"))); - assertEquals("Value is incorrect", asMap(Year.of(476), "test"), - READER.readValue(mapAsString("476", "test"))); + assertEquals(asMap(Year.of(476), "test"), READER.readValue(mapAsString("0476", "test")), + "Value is incorrect"); + assertEquals(asMap(Year.of(476), "test"), READER.readValue(mapAsString("476", "test")), + "Value is incorrect"); } - @Test(expected = InvalidFormatException.class) + @Test public void deserializeYearKey_notANumber() throws Exception { - READER.readValue(mapAsString("10000BC", "test")); + assertThrows(InvalidFormatException.class, () -> { + READER.readValue(mapAsString("10000BC", "test")); + }); } - @Test(expected = InvalidFormatException.class) + @Test public void deserializeYearKey_notAYear() throws Exception { - READER.readValue(mapAsString(Integer.toString(Year.MAX_VALUE+1), "test")); + assertThrows(InvalidFormatException.class, () -> { + READER.readValue(mapAsString(Integer.toString(Year.MAX_VALUE+1), "test")); + }); } @Test diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearMonthAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearMonthAsKeyTest.java index 9bfeeeab..b94af52f 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearMonthAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/YearMonthAsKeyTest.java @@ -3,13 +3,14 @@ import java.time.YearMonth; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class YearMonthAsKeyTest extends ModuleTestBase { @@ -19,13 +20,15 @@ public class YearMonthAsKeyTest extends ModuleTestBase @Test public void testSerialization() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString("3141-05", "test"), - MAPPER.writeValueAsString(asMap(YearMonth.of(3141, 5), "test"))); + assertEquals(mapAsString("3141-05", "test"), + MAPPER.writeValueAsString(asMap(YearMonth.of(3141, 5), "test")), + "Value is incorrect"); } @Test public void testDeserialization() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(YearMonth.of(3141, 5), "test"), - READER.readValue(mapAsString("3141-05", "test"))); + assertEquals(asMap(YearMonth.of(3141, 5), "test"), + READER.readValue(mapAsString("3141-05", "test")), + "Value is incorrect"); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneIdAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneIdAsKeyTest.java index 22dc496c..b2797535 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneIdAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneIdAsKeyTest.java @@ -3,13 +3,14 @@ import java.time.ZoneId; import java.util.Map; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class ZoneIdAsKeyTest extends ModuleTestBase { @@ -25,37 +26,37 @@ public class ZoneIdAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(ZONE_0_STRING, "test"), + assertEquals(mapAsString(ZONE_0_STRING, "test"), MAPPER.writeValueAsString(asMap(ZONE_0, "test"))); } @Test public void testSerialization1() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(ZONE_1_STRING, "test"), + assertEquals(mapAsString(ZONE_1_STRING, "test"), MAPPER.writeValueAsString(asMap(ZONE_1, "test"))); } @Test public void testSerialization2() throws Exception { - Assert.assertEquals("Value is incorrect", mapAsString(ZONE_2_STRING, "test"), + assertEquals(mapAsString(ZONE_2_STRING, "test"), MAPPER.writeValueAsString(asMap(ZONE_2, "test"))); } @Test public void testDeserialization0() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(ZONE_0, "test"), + assertEquals(asMap(ZONE_0, "test"), READER.readValue(mapAsString(ZONE_0_STRING, "test"))); } @Test public void testDeserialization1() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(ZONE_1, "test"), + assertEquals(asMap(ZONE_1, "test"), READER.readValue(mapAsString(ZONE_1_STRING, "test"))); } @Test public void testDeserialization2() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(ZONE_2, "test"), + assertEquals(asMap(ZONE_2, "test"), READER.readValue(mapAsString(ZONE_2_STRING, "test"))); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneOffsetAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneOffsetAsKeyTest.java index fca1c1a3..9b34a8ea 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneOffsetAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZoneOffsetAsKeyTest.java @@ -4,13 +4,14 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class ZoneOffsetAsKeyTest extends ModuleTestBase { @@ -25,7 +26,7 @@ public class ZoneOffsetAsKeyTest extends ModuleTestBase private Map map; - @Before + @BeforeEach public void setUp() { map = new HashMap<>(); } @@ -36,7 +37,7 @@ public void testSerialization0() throws Exception { String value = MAPPER.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(OFFSET_0_STRING, "test"), value); + assertEquals(map(OFFSET_0_STRING, "test"), value); } @Test @@ -45,7 +46,7 @@ public void testSerialization1() throws Exception { String value = MAPPER.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(OFFSET_1_STRING, "test"), value); + assertEquals(map(OFFSET_1_STRING, "test"), value); } @Test @@ -53,7 +54,7 @@ public void testDeserialization0() throws Exception { Map value = MAPPER.readValue(map(OFFSET_0_STRING, "test"), TYPE_REF); map.put(OFFSET_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -61,7 +62,7 @@ public void testDeserialization1() throws Exception { Map value = MAPPER.readValue(map(OFFSET_1_STRING, "test"), TYPE_REF); map.put(OFFSET_1, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZonedDateTimeAsKeyTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZonedDateTimeAsKeyTest.java index 73d29ea2..24807acd 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZonedDateTimeAsKeyTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/key/ZonedDateTimeAsKeyTest.java @@ -12,8 +12,8 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; public class ZonedDateTimeAsKeyTest extends ModuleTestBase { @@ -41,36 +41,36 @@ public class ZonedDateTimeAsKeyTest extends ModuleTestBase @Test public void testSerialization0() throws Exception { String value = MAPPER.writerFor(TYPE_REF).writeValueAsString(asMap(DATE_TIME_0, "test")); - Assert.assertEquals("Value is incorrect", mapAsString(DATE_TIME_0_STRING, "test"), value); + assertEquals(mapAsString(DATE_TIME_0_STRING, "test"), value); } @Test public void testSerialization1() throws Exception { String value = MAPPER.writerFor(TYPE_REF).writeValueAsString(asMap(DATE_TIME_1, "test")); - Assert.assertEquals("Value is incorrect", mapAsString(DATE_TIME_1_STRING, "test"), value); + assertEquals(mapAsString(DATE_TIME_1_STRING, "test"), value); } @Test public void testSerialization2() throws Exception { String value = MAPPER.writerFor(TYPE_REF).writeValueAsString(asMap(DATE_TIME_2, "test")); - Assert.assertEquals("Value is incorrect", mapAsString(DATE_TIME_2_STRING, "test"), value); + assertEquals(mapAsString(DATE_TIME_2_STRING, "test"), value); } @Test public void testDeserialization0() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(DATE_TIME_0, "test"), + assertEquals(asMap(DATE_TIME_0, "test"), READER.readValue(mapAsString(DATE_TIME_0_STRING, "test"))); } @Test public void testDeserialization1() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(DATE_TIME_1, "test"), + assertEquals(asMap(DATE_TIME_1, "test"), READER.readValue(mapAsString(DATE_TIME_1_STRING, "test"))); } @Test public void testDeserialization2() throws Exception { - Assert.assertEquals("Value is incorrect", asMap(DATE_TIME_2_OFFSET, "test"), + assertEquals(asMap(DATE_TIME_2_OFFSET, "test"), READER.readValue(mapAsString(DATE_TIME_2_STRING, "test"))); } @@ -78,8 +78,7 @@ public void testDeserialization2() throws Exception { public void testSerializationToInstantWithNanos() throws Exception { String value = mapperBuilder().enable(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS).build() .writerFor(TYPE_REF).writeValueAsString(asMap(DATE_TIME_1, "test")); - Assert.assertEquals("Value is incorrect", - mapAsString(String.valueOf(DATE_TIME_1.toEpochSecond()) + '.' + DATE_TIME_1.getNano(), "test"), value); + assertEquals(mapAsString(String.valueOf(DATE_TIME_1.toEpochSecond()) + '.' + DATE_TIME_1.getNano(), "test"), value); } @Test @@ -87,7 +86,6 @@ public void testSerializationToInstantWithoutNanos() throws Exception { String value = mapperBuilder().enable(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS) .disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS).build() .writerFor(TYPE_REF).writeValueAsString(asMap(DATE_TIME_1, "test")); - Assert.assertEquals("Value is incorrect", - mapAsString(String.valueOf(DATE_TIME_1.toInstant().toEpochMilli()), "test"), value); + assertEquals(mapAsString(String.valueOf(DATE_TIME_1.toInstant().toEpochMilli()), "test"), value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeExceptionTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeExceptionTest.java index e9eb3bc5..1af7f228 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeExceptionTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeExceptionTest.java @@ -2,12 +2,12 @@ import java.time.DateTimeException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class DateTimeExceptionTest extends ModuleTestBase { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java index 984ef140..f3be7c98 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DateTimeSchemasTest.java @@ -5,14 +5,15 @@ import java.time.ZonedDateTime; import java.util.*; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.jsonFormatVisitors.*; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + public class DateTimeSchemasTest extends ModuleTestBase { static class VisitorWrapper implements JsonFormatVisitorWrapper { @@ -144,7 +145,7 @@ public void testLocalTimeSchema() throws Exception Map properties = wrapper.getTraversedProperties(); // By default, serialized as an int array, so: - Assert.assertEquals(1, properties.size()); + assertEquals(1, properties.size()); _verifyIntArrayType(properties.get("")); // but becomes date/time @@ -163,7 +164,7 @@ public void testLocalDateSchema() throws Exception Map properties = wrapper.getTraversedProperties(); // By default, serialized as an int array, so: - Assert.assertEquals(1, properties.size()); + assertEquals(1, properties.size()); _verifyIntArrayType(properties.get("")); // but becomes date/time @@ -184,7 +185,7 @@ public void testDateTimeSchema() throws Exception Map properties = wrapper.getTraversedProperties(); // By default, serialized as an int array, so: - Assert.assertEquals(1, properties.size()); + assertEquals(1, properties.size()); _verifyBigDecimalType(properties.get("")); // but becomes long @@ -205,30 +206,30 @@ public void testDateTimeSchema() throws Exception } private void _verifyIntArrayType(String desc) { - Assert.assertEquals("ARRAY/Ljava/util/List;", desc); + assertEquals("ARRAY/Ljava/util/List;", desc); } private void _verifyTimeType(String desc) { - Assert.assertEquals("STRING/TIME", desc); + assertEquals("STRING/TIME", desc); } private void _verifyDateType(String desc) { - Assert.assertEquals("STRING/DATE", desc); + assertEquals("STRING/DATE", desc); } private void _verifyDateTimeType(String desc) { - Assert.assertEquals("STRING/DATE_TIME", desc); + assertEquals("STRING/DATE_TIME", desc); } private void _verifyBigDecimalType(String desc) { - Assert.assertEquals("NUMBER/BIG_DECIMAL", desc); + assertEquals("NUMBER/BIG_DECIMAL", desc); } private void _verifyLongType(String desc) { - Assert.assertEquals("INTEGER/LONG", desc); + assertEquals("INTEGER/LONG", desc); } private void _verifyLongFormat(String desc) { - Assert.assertEquals("INTEGER/UTC_MILLISEC", desc); + assertEquals("INTEGER/UTC_MILLISEC", desc); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java index 08ce9d08..50bc2add 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/DeductionTypeSerialization296Test.java @@ -2,14 +2,15 @@ import java.time.*; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonTypeInfo; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + // for [modules-java8#296]: problem with `JsonTypeInfo.Id.DEDUCTION` public class DeductionTypeSerialization296Test extends ModuleTestBase { @@ -30,7 +31,7 @@ public Wrapper(Object value) { public void testLocalDate() throws Exception { LocalDate date = LocalDate.of(1986, Month.JANUARY, 17); - Assert.assertEquals(a2q("{'value':'1986-01-17'}"), + assertEquals(a2q("{'value':'1986-01-17'}"), MAPPER.writeValueAsString(new Wrapper(date))); } @@ -38,7 +39,7 @@ public void testLocalDate() throws Exception public void testLocalDateTime() throws Exception { LocalDateTime datetime = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57); - Assert.assertEquals(a2q("{'value':'2013-08-21T09:22:00.000000057'}"), + assertEquals(a2q("{'value':'2013-08-21T09:22:00.000000057'}"), MAPPER.writeValueAsString(new Wrapper(datetime))); } @@ -46,7 +47,7 @@ public void testLocalDateTime() throws Exception public void testLocalTime() throws Exception { LocalTime time = LocalTime.of(9, 22, 57); - Assert.assertEquals(a2q("{'value':'09:22:57'}"), + assertEquals(a2q("{'value':'09:22:57'}"), MAPPER.writeValueAsString(new Wrapper(time))); } @@ -54,7 +55,7 @@ public void testLocalTime() throws Exception public void testMonthDate() throws Exception { MonthDay date = MonthDay.of(Month.JANUARY, 17); - Assert.assertEquals(a2q("{'value':'--01-17'}"), + assertEquals(a2q("{'value':'--01-17'}"), MAPPER.writeValueAsString(new Wrapper(date))); } @@ -62,7 +63,7 @@ public void testMonthDate() throws Exception public void testOffsetTime() throws Exception { OffsetTime time = OffsetTime.of(15, 43, 0, 0, ZoneOffset.of("+0300")); - Assert.assertEquals(a2q("{'value':'15:43+03:00'}"), + assertEquals(a2q("{'value':'15:43+03:00'}"), MAPPER.writeValueAsString(new Wrapper(time))); } @@ -70,7 +71,7 @@ public void testOffsetTime() throws Exception public void testYearMonth() throws Exception { YearMonth date = YearMonth.of(1986, Month.JANUARY); - Assert.assertEquals(a2q("{'value':'1986-01'}"), + assertEquals(a2q("{'value':'1986-01'}"), MAPPER.writeValueAsString(new Wrapper(date))); } @@ -78,7 +79,7 @@ public void testYearMonth() throws Exception public void testZoneId() throws Exception { ZoneId zone = ZoneId.of("America/Denver"); - Assert.assertEquals(a2q("{'value':'America/Denver'}"), + assertEquals(a2q("{'value':'America/Denver'}"), MAPPER.writeValueAsString(new Wrapper(zone))); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/UnsupportedTypesTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/UnsupportedTypesTest.java index 824705bf..22794bb9 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/UnsupportedTypesTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/misc/UnsupportedTypesTest.java @@ -3,12 +3,13 @@ import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAdjusters; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + public class UnsupportedTypesTest extends ModuleTestBase { // [modules-java8#207] @@ -27,7 +28,7 @@ public void testTemporalAdjusterSerialization() throws Exception ObjectMapper mapper = newMapper(); // Not 100% sure how this happens, actually; should fail on empty "POJO"? - Assert.assertEquals(a2q("{'a':{}}"), + assertEquals(a2q("{'a':{}}"), mapper.writeValueAsString(new TAWrapper(TemporalAdjusters.firstDayOfMonth()))); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java index 8832fec8..ed1c7362 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/ModuleTestBase.java @@ -2,6 +2,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + public class ModuleTestBase { // 14-Mar-2016, tatu: Serialization of trailing zeroes may change [datatype-jsr310#67] diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java index 761e0c0f..bcc0ec5f 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/PolymorphicTest.java @@ -2,13 +2,13 @@ import java.time.ZoneId; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase.NoCheckSubTypeValidator; +import static org.junit.jupiter.api.Assertions.*; + public class PolymorphicTest extends ModuleTestBase { private final ObjectMapper TYPING_MAPPER = newMapper(); diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDecimalUtils.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDecimalUtils.java deleted file mode 100644 index 3ad09ec2..00000000 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDecimalUtils.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.fasterxml.jackson.datatype.jsr310.old; - -import static org.junit.Assert.assertEquals; - -import java.math.BigDecimal; - -import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; -import org.junit.Test; - -@SuppressWarnings("deprecation") -public class TestDecimalUtils extends ModuleTestBase -{ - @Test - public void testToDecimal01() - { - String decimal = DecimalUtils.toDecimal(0, 0); - assertEquals("The returned decimal is not correct.", NO_NANOSECS_SER, decimal); - - decimal = DecimalUtils.toDecimal(15, 72); - assertEquals("The returned decimal is not correct.", "15.000000072", decimal); - - decimal = DecimalUtils.toDecimal(19827342231L, 192837465); - assertEquals("The returned decimal is not correct.", "19827342231.192837465", - decimal); - - decimal = DecimalUtils.toDecimal(19827342231L, 0); - assertEquals("The returned decimal is not correct.", - "19827342231"+NO_NANOSECS_SUFFIX, decimal); - - decimal = DecimalUtils.toDecimal(19827342231L, 987654321); - assertEquals("The returned decimal is not correct.", - "19827342231.987654321", decimal); - } - - @Test - public void testExtractNanosecondDecimal01() - { - BigDecimal value = new BigDecimal("0"); - - long seconds = value.longValue(); - assertEquals("The second part is not correct.", 0L, seconds); - - int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); - assertEquals("The nanosecond part is not correct.", 0, nanoseconds); - } - - @Test - public void testExtractNanosecondDecimal02() - { - BigDecimal value = new BigDecimal("15.000000072"); - - long seconds = value.longValue(); - assertEquals("The second part is not correct.", 15L, seconds); - - int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); - assertEquals("The nanosecond part is not correct.", 72, nanoseconds); - } - - @Test - public void testExtractNanosecondDecimal03() - { - BigDecimal value = new BigDecimal("15.72"); - - long seconds = value.longValue(); - assertEquals("The second part is not correct.", 15L, seconds); - - int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); - assertEquals("The nanosecond part is not correct.", 720000000, nanoseconds); - } - - @Test - public void testExtractNanosecondDecimal04() - { - BigDecimal value = new BigDecimal("19827342231.192837465"); - - long seconds = value.longValue(); - assertEquals("The second part is not correct.", 19827342231L, seconds); - - int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); - assertEquals("The nanosecond part is not correct.", 192837465, nanoseconds); - } - - @Test - public void testExtractNanosecondDecimal05() - { - BigDecimal value = new BigDecimal("19827342231"); - - long seconds = value.longValue(); - assertEquals("The second part is not correct.", 19827342231L, seconds); - - int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); - assertEquals("The nanosecond part is not correct.", 0, nanoseconds); - } - - @Test - public void testExtractNanosecondDecimal06() - { - BigDecimal value = new BigDecimal("19827342231.999999999"); - - long seconds = value.longValue(); - assertEquals("The second part is not correct.", 19827342231L, seconds); - - int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); - assertEquals("The nanosecond part is not correct.", 999999999, nanoseconds); - } -} diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java index 967011e7..822c171c 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationDeserialization.java @@ -1,19 +1,16 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - import java.time.Duration; import java.time.temporal.TemporalAmount; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; +import static org.junit.jupiter.api.Assertions.*; + public class TestDurationDeserialization extends ModuleTestBase { private final ObjectReader READER = newMapper().readerFor(Duration.class); @@ -24,9 +21,9 @@ public void testDeserializationAsFloat01() throws Exception Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60.0"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); Duration exp = Duration.ofSeconds(60L, 0); - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } @Test @@ -35,9 +32,9 @@ public void testDeserializationAsFloat02() throws Exception Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60.0"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); Duration exp = Duration.ofSeconds(60L, 0); - assertEquals("The value is not correct.", exp, value); + assertEquals(exp, value, "The value is not correct."); } @Test @@ -45,8 +42,8 @@ public void testDeserializationAsFloat03() throws Exception { Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498.000008374"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value); + assertNotNull(value); + assertEquals(Duration.ofSeconds(13498L, 8374), value, "The value is not correct."); } @Test @@ -54,8 +51,8 @@ public void testDeserializationAsFloat04() throws Exception { Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498.000008374"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 8374), value); + assertNotNull(value); + assertEquals(Duration.ofSeconds(13498L, 8374), value, "The value is not correct."); } @Test @@ -63,8 +60,8 @@ public void testDeserializationAsInt01() throws Exception { Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + assertNotNull(value); + assertEquals(Duration.ofSeconds(60L, 0), value, "The value is not correct."); } @Test @@ -73,8 +70,8 @@ public void testDeserializationAsInt02() throws Exception Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("60000"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Duration.ofSeconds(60L, 0), value); + assertNotNull(value); + assertEquals(Duration.ofSeconds(60L, 0), value, "The value is not correct."); } @Test @@ -83,8 +80,8 @@ public void testDeserializationAsInt03() throws Exception Duration value = READER.with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 0), value); + assertNotNull(value); + assertEquals(Duration.ofSeconds(13498L, 0), value, "The value is not correct."); } @Test @@ -93,8 +90,8 @@ public void testDeserializationAsInt04() throws Exception Duration value = READER.without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("13498000"); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 0), value); + assertNotNull(value); + assertEquals(Duration.ofSeconds(13498L, 0), value, "The value is not correct."); } @Test @@ -103,8 +100,8 @@ public void testDeserializationAsString01() throws Exception Duration exp = Duration.ofSeconds(60L, 0); Duration value = READER.readValue('"' + exp.toString() + '"'); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", exp, value); + assertNotNull(value); + assertEquals(exp, value, "The value is not correct."); } @Test @@ -113,15 +110,15 @@ public void testDeserializationAsString02() throws Exception Duration exp = Duration.ofSeconds(13498L, 8374); Duration value = READER.readValue('"' + exp.toString() + '"'); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", exp, value); + assertNotNull(value); + assertEquals(exp, value, "The value is not correct."); } @Test public void testDeserializationAsString03() throws Exception { Duration value = READER.readValue("\" \""); - assertNull("The value should be null.", value); + assertNull(value, "The value should be null."); } @Test @@ -136,9 +133,9 @@ public void testDeserializationWithTypeInfo01() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + "13498.000008374]", TemporalAmount.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", duration, value); + assertNotNull(value); + assertInstanceOf(Duration.class, value, "The value should be a Duration."); + assertEquals(duration, value, "The value is not correct."); } @Test @@ -151,9 +148,9 @@ public void testDeserializationWithTypeInfo02() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + "13498]", TemporalAmount.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L), value); + assertNotNull(value); + assertInstanceOf(Duration.class, value, "The value should be a Duration."); + assertEquals(Duration.ofSeconds(13498L), value, "The value is not correct."); } @Test @@ -166,9 +163,9 @@ public void testDeserializationWithTypeInfo03() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + "13498837]", TemporalAmount.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", Duration.ofSeconds(13498L, 837000000), value); + assertNotNull(value); + assertInstanceOf(Duration.class, value, "The value should be a Duration."); + assertEquals(Duration.ofSeconds(13498L, 837000000), value, "The value is not correct."); } @Test @@ -182,8 +179,8 @@ public void testDeserializationWithTypeInfo04() throws Exception mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); TemporalAmount value = mapper.readValue(prefix + '"' + duration.toString() + "\"]", TemporalAmount.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a Duration.", value instanceof Duration); - assertEquals("The value is not correct.", duration, value); + assertNotNull(value); + assertInstanceOf(Duration.class, value, "The value should be a Duration."); + assertEquals(duration, value, "The value is not correct."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java index d33285be..e2a48711 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationKeySerialization.java @@ -1,15 +1,16 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; - import java.time.Duration; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; -import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.*; public class TestDurationKeySerialization extends ModuleTestBase { @@ -21,7 +22,7 @@ public class TestDurationKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -37,7 +38,7 @@ public void testSerialization() throws Exception { String value = om.writeValueAsString(map); - assertEquals("Value is not correct", map(DURATION_STRING, "test"), value); + assertEquals(map(DURATION_STRING, "test"), value); } @Test @@ -47,7 +48,7 @@ public void testDeserialization() throws Exception { TYPE_REF); map.put(DURATION, "test"); - assertEquals("Value is not correct", map, value); + assertEquals(map, value, "Value is not correct"); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java index 19c79227..3847ba6b 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestDurationSerialization.java @@ -4,19 +4,19 @@ import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.time.Duration; import java.time.temporal.TemporalAmount; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class TestDurationSerialization extends ModuleTestBase { private ObjectWriter WRITER; - @Before + @BeforeEach public void setUp() { WRITER = newMapper().writer(); @@ -31,9 +31,8 @@ public void testSerializationAsTimestampNanoseconds01() throws Exception .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "60"+NO_NANOSECS_SUFFIX, value); + assertNotNull(value); + assertEquals("60"+NO_NANOSECS_SUFFIX, value); } @Test @@ -45,8 +44,8 @@ public void testSerializationAsTimestampNanoseconds02() throws Exception .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "13498.000008374", value); + assertNotNull(value); + assertEquals("13498.000008374", value); } @Test @@ -58,8 +57,8 @@ public void testSerializationAsTimestampMilliseconds01() throws Exception .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "60000", value); + assertNotNull(value); + assertEquals("60000", value); } @Test @@ -71,8 +70,8 @@ public void testSerializationAsTimestampMilliseconds02() throws Exception .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "13498000", value); + assertNotNull(value); + assertEquals("13498000", value); } @Test @@ -84,8 +83,8 @@ public void testSerializationAsTimestampMilliseconds03() throws Exception .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "13498837", value); + assertNotNull(value); + assertEquals("13498837", value); } @Test @@ -96,8 +95,8 @@ public void testSerializationAsString01() throws Exception .without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) .writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + duration.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + duration.toString() + '"', value); } @Test @@ -108,8 +107,8 @@ public void testSerializationAsString02() throws Exception .without(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) .writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + duration.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + duration.toString() + '"', value); } @Test @@ -122,9 +121,8 @@ public void testSerializationWithTypeInfo01() throws Exception Duration duration = Duration.ofSeconds(13498L, 8374); String value = mapper.writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + Duration.class.getName() + "\",13498.000008374]", value); + assertNotNull(value); + assertEquals("[\"" + Duration.class.getName() + "\",13498.000008374]", value); } @Test @@ -137,9 +135,8 @@ public void testSerializationWithTypeInfo02() throws Exception Duration duration = Duration.ofSeconds(13498L, 837481723); String value = mapper.writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + Duration.class.getName() + "\",13498837]", value); + assertNotNull(value); + assertEquals("[\"" + Duration.class.getName() + "\",13498837]", value); } @Test @@ -151,8 +148,7 @@ public void testSerializationWithTypeInfo03() throws Exception Duration duration = Duration.ofSeconds(13498L, 8374); String value = mapper.writeValueAsString(duration); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + Duration.class.getName() + "\",\"" + duration.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + Duration.class.getName() + "\",\"" + duration.toString() + "\"]", value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java index 724806ae..3fa74700 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestFeatures.java @@ -16,32 +16,33 @@ package com.fasterxml.jackson.datatype.jsr310.old; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class TestFeatures { @Test public void testWriteDateTimestampsAsNanosecondsSettingEnabledByDefault() { - assertTrue("Write date timestamps as nanoseconds setting should be enabled by default.", - SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault()); + assertTrue(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault(), + "Write date timestamps as nanoseconds setting should be enabled by default."); } @Test public void testReadDateTimestampsAsNanosecondsSettingEnabledByDefault() { - assertTrue("Read date timestamps as nanoseconds setting should be enabled by default.", - DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault()); + assertTrue(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS.enabledByDefault(), + "Read date timestamps as nanoseconds setting should be enabled by default."); } @Test public void testAdjustDatesToContextTimeZoneSettingEnabledByDefault() { - assertTrue("Adjust dates to context time zone setting should be enabled by default.", - DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE.enabledByDefault()); + assertTrue(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE.enabledByDefault(), + "Adjust dates to context time zone setting should be enabled by default."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java index e11823ed..67a625e5 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantKeySerialization.java @@ -1,16 +1,16 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; - import java.time.Instant; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.*; public class TestInstantKeySerialization extends ModuleTestBase { @@ -24,7 +24,7 @@ public class TestInstantKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -40,7 +40,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(INSTANT_0_STRING, "test"), value); + assertEquals(map(INSTANT_0_STRING, "test"), value); } @Test @@ -49,7 +49,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - assertEquals("Value is incorrect", map(INSTANT_STRING, "test"), value); + assertEquals(map(INSTANT_STRING, "test"), value); } @Test @@ -57,7 +57,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(INSTANT_0_STRING, "test"), TYPE_REF); map.put(INSTANT_0, "test"); - assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -65,7 +65,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(INSTANT_STRING, "test"), TYPE_REF); map.put(INSTANT, "test"); - assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java index 6e3736f7..c420f4ec 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestInstantSerialization.java @@ -16,22 +16,20 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Instant; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestInstantSerialization extends ModuleTestBase { @@ -39,7 +37,7 @@ public class TestInstantSerialization extends ModuleTestBase private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -54,8 +52,8 @@ public void testSerializationAsTimestamp01Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "0.0", value); + assertNotNull(value); + assertEquals("0.0", value); } @Test @@ -67,8 +65,8 @@ public void testSerializationAsTimestamp01Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "0", value); + assertNotNull(value); + assertEquals("0", value); } @Test @@ -80,8 +78,8 @@ public void testSerializationAsTimestamp02Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "123456789.183917322", value); + assertNotNull(value); + assertEquals("123456789.183917322", value); } @Test @@ -93,8 +91,8 @@ public void testSerializationAsTimestamp02Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "123456789183", value); + assertNotNull(value); + assertEquals("123456789183", value); } @Test @@ -106,8 +104,8 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), value); + assertNotNull(value); + assertEquals(DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), value); } @Test @@ -119,8 +117,8 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Long.toString(date.toEpochMilli()), value); + assertNotNull(value); + assertEquals(Long.toString(date.toEpochMilli()), value); } @Test @@ -131,8 +129,8 @@ public void testSerializationAsString01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertNotNull(value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -143,8 +141,8 @@ public void testSerializationAsString02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertNotNull(value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -155,8 +153,8 @@ public void testSerializationAsString03() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertNotNull(value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -169,8 +167,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"" + Instant.class.getName() + "\",123456789.183917322]", value); + assertNotNull(value); + assertEquals("[\"" + Instant.class.getName() + "\",123456789.183917322]", value); } @Test @@ -183,8 +181,8 @@ public void testSerializationWithTypeInfo02() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"" + Instant.class.getName() + "\",123456789183]", value); + assertNotNull(value); + assertEquals("[\"" + Instant.class.getName() + "\",123456789183]", value); } @Test @@ -196,9 +194,8 @@ public void testSerializationWithTypeInfo03() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } @Test @@ -208,8 +205,8 @@ public void testDeserializationAsFloat01() throws Exception Instant value = this.mapper.readValue("0.000000000", Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -219,8 +216,8 @@ public void testDeserializationAsFloat02() throws Exception Instant value = this.mapper.readValue("123456789.183917322", Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -232,8 +229,8 @@ public void testDeserializationAsFloat03() throws Exception DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), Instant.class ); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -244,8 +241,8 @@ public void testDeserializationAsInt01Nanoseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); Instant value = this.mapper.readValue("0", Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -256,8 +253,8 @@ public void testDeserializationAsInt01Milliseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); Instant value = this.mapper.readValue("0", Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -268,8 +265,8 @@ public void testDeserializationAsInt02Nanoseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); Instant value = this.mapper.readValue("123456789", Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -280,8 +277,8 @@ public void testDeserializationAsInt02Milliseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); Instant value = this.mapper.readValue("123456789422", Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -293,8 +290,8 @@ public void testDeserializationAsInt03Nanoseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); Instant value = this.mapper.readValue(Long.toString(date.getEpochSecond()), Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -306,8 +303,8 @@ public void testDeserializationAsInt03Milliseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); Instant value = this.mapper.readValue(Long.toString(date.toEpochMilli()), Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -317,8 +314,8 @@ public void testDeserializationAsString01() throws Exception Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -328,8 +325,8 @@ public void testDeserializationAsString02() throws Exception Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -339,8 +336,8 @@ public void testDeserializationAsString03() throws Exception Instant value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', Instant.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -353,9 +350,9 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + Instant.class.getName() + "\",123456789.183917322]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an Instant.", value instanceof Instant); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertInstanceOf(Instant.class, value, "The value should be an Instant."); + assertEquals(date, value); } @Test @@ -369,9 +366,9 @@ public void testDeserializationWithTypeInfo02() throws Exception "[\"" + Instant.class.getName() + "\",123456789]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an Instant.", value instanceof Instant); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertInstanceOf(Instant.class, value, "The value should be an Instant."); + assertEquals(date, value); } @Test @@ -385,9 +382,9 @@ public void testDeserializationWithTypeInfo03() throws Exception "[\"" + Instant.class.getName() + "\",123456789422]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an Instant.", value instanceof Instant); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertInstanceOf(Instant.class, value, "The value should be an Instant."); + assertEquals(date, value); } @Test @@ -400,8 +397,8 @@ public void testDeserializationWithTypeInfo04() throws Exception "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an Instant.", value instanceof Instant); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertInstanceOf(Instant.class, value, "The value should be an Instant."); + assertEquals(date, value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java index 090401da..01e77945 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateKeySerialization.java @@ -1,16 +1,16 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; - import java.time.LocalDate; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestLocalDateKeySerialization extends ModuleTestBase { @@ -22,7 +22,7 @@ public class TestLocalDateKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -38,7 +38,7 @@ public void testSerialization() throws Exception { String value = om.writeValueAsString(map); - assertEquals("Incorrect value", map(DATE_STRING, "test"), value); + assertEquals(map(DATE_STRING, "test"), value); } @Test @@ -48,7 +48,7 @@ public void testDeserialization() throws Exception { TYPE_REF); map.put(DATE, "test"); - assertEquals("Incorrect value", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java index 7203b196..a72cd6cf 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateSerialization.java @@ -16,9 +16,8 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.time.LocalDate; import java.time.Month; @@ -27,14 +26,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestLocalDateSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -48,8 +46,8 @@ public void testSerializationAsTimestamp01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[1986,1,17]", value); + assertNotNull(value); + assertEquals("[1986,1,17]", value); } @Test @@ -60,8 +58,8 @@ public void testSerializationAsTimestamp02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2013,8,21]", value); + assertNotNull(value); + assertEquals("[2013,8,21]", value); } @Test @@ -72,8 +70,8 @@ public void testSerializationAsString01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + date.toString() + '"', value); } @Test @@ -84,8 +82,8 @@ public void testSerializationAsString02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + date.toString() + '"', value); } @Test @@ -97,9 +95,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", value); } @Test @@ -109,8 +106,8 @@ public void testDeserializationAsTimestamp01() throws Exception LocalDate value = this.mapper.readValue("[1986,1,17]", LocalDate.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -120,8 +117,8 @@ public void testDeserializationAsTimestamp02() throws Exception LocalDate value = this.mapper.readValue("[2013,8,21]", LocalDate.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -131,8 +128,8 @@ public void testDeserializationAsString01() throws Exception LocalDate value = this.mapper.readValue('"' + date.toString() + '"', LocalDate.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -142,8 +139,8 @@ public void testDeserializationAsString02() throws Exception LocalDate value = this.mapper.readValue('"' + date.toString() + '"', LocalDate.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertEquals(date, value); } @Test @@ -156,8 +153,8 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalDate.", value instanceof LocalDate); - assertEquals("The value is not correct.", date, value); + assertNotNull(value); + assertInstanceOf(LocalDate.class, value); + assertEquals(date, value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java index 0d8e5730..d6d64122 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeKeySerialization.java @@ -1,17 +1,17 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; - import java.time.LocalDateTime; import java.time.ZoneOffset; import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestLocalDateTimeKeySerialization extends ModuleTestBase { @@ -28,7 +28,7 @@ public class TestLocalDateTimeKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -44,7 +44,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - assertEquals("Value is incorrect", map(DATE_TIME_0_STRING, "test"), value); + assertEquals(map(DATE_TIME_0_STRING, "test"), value); } @Test @@ -53,7 +53,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - assertEquals("Value is incorrect", map(DATE_TIME_STRING, "test"), value); + assertEquals(map(DATE_TIME_STRING, "test"), value); } @Test @@ -63,7 +63,7 @@ public void testDeserialization0() throws Exception { TYPE_REF); map.put(DATE_TIME_0, "test"); - assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -73,7 +73,7 @@ public void testDeserialization1() throws Exception { TYPE_REF); map.put(DATE_TIME, "test"); - assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java index 544fd0ed..9f696a88 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalDateTimeSerialization.java @@ -16,26 +16,26 @@ package com.fasterxml.jackson.datatype.jsr310.old; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectReader; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Before; -import org.junit.Test; - import java.time.LocalDateTime; import java.time.Month; import java.time.temporal.Temporal; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class TestLocalDateTimeSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { mapper = newMapper(); @@ -49,8 +49,8 @@ public void testSerializationAsTimestamp01() throws Exception String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[1986,1,17,15,43]", value); + assertNotNull(value); + assertEquals("[1986,1,17,15,43]", value); } @Test @@ -59,8 +59,8 @@ public void testSerializationAsTimestamp02() throws Exception LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2013,8,21,9,22,57]", value); + assertNotNull(value); + assertEquals("[2013,8,21,9,22,57]", value); } @Test @@ -71,8 +71,8 @@ public void testSerializationAsTimestamp03Nanosecond() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2013,8,21,9,22,0,57]", value); + assertNotNull(value); + assertEquals("[2013,8,21,9,22,0,57]", value); } @Test @@ -83,8 +83,8 @@ public void testSerializationAsTimestamp03Millisecond() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2013,8,21,9,22,0,0]", value); + assertNotNull(value); + assertEquals("[2013,8,21,9,22,0,0]", value); } @Test @@ -95,8 +95,8 @@ public void testSerializationAsTimestamp04Nanosecond() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2005,11,5,22,31,5,829837]", value); + assertNotNull(value); + assertEquals("[2005,11,5,22,31,5,829837]", value); } @Test @@ -107,8 +107,8 @@ public void testSerializationAsTimestamp04Millisecond() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2005,11,5,22,31,5,422]", value); + assertNotNull(value); + assertEquals("[2005,11,5,22,31,5,422]", value); } @Test @@ -120,8 +120,8 @@ public void testSerializationAsString01() throws Exception m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"1986-01-17T15:43:06\"", value); + assertNotNull(value); + assertEquals("\"1986-01-17T15:43:06\"", value); } @Test @@ -133,8 +133,8 @@ public void testSerializationAsString02() throws Exception m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -146,8 +146,8 @@ public void testSerializationAsString03() throws Exception m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -161,9 +161,8 @@ public void testSerializationWithTypeInfo01() throws Exception m.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", value); + assertNotNull(value); + assertEquals("[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", value); } @Test @@ -177,9 +176,8 @@ public void testSerializationWithTypeInfo02() throws Exception LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422829837); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", value); + assertNotNull(value); + assertEquals("[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", value); } @Test @@ -191,9 +189,8 @@ public void testSerializationWithTypeInfo03() throws Exception LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", value); } @Test @@ -201,9 +198,9 @@ public void testDeserializationAsTimestamp01() throws Exception { LocalDateTime value = this.mapper.readValue("[1986,1,17,15,43]", LocalDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value); } @Test @@ -211,9 +208,9 @@ public void testDeserializationAsTimestamp02() throws Exception { LocalDateTime value = this.mapper.readValue("[2013,8,21,9,22,57]", LocalDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value); } @Test @@ -223,9 +220,9 @@ public void testDeserializationAsTimestamp03Nanoseconds() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2013,8,21,9,22,0,57]"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value); } @Test @@ -235,9 +232,9 @@ public void testDeserializationAsTimestamp03Milliseconds() throws Exception .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2013,8,21,9,22,0,57]"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 0, 57000000); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value); } @Test @@ -247,9 +244,9 @@ public void testDeserializationAsTimestamp04Nanoseconds() throws Exception .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829837]"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value); } @Test @@ -259,9 +256,9 @@ public void testDeserializationAsTimestamp04Milliseconds01() throws Exception .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829837]"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value); } @Test @@ -271,9 +268,9 @@ public void testDeserializationAsTimestamp04Milliseconds02() throws Exception .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS); LocalDateTime value = r.readValue("[2005,11,5,22,31,5,829]"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829000000); - assertEquals("The value is not correct.", time, value); + assertEquals(time, value); } @Test @@ -283,8 +280,8 @@ public void testDeserializationAsString01() throws Exception LocalDateTime value = this.mapper.readValue('"' + time.toString() + '"', LocalDateTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -294,8 +291,8 @@ public void testDeserializationAsString02() throws Exception LocalDateTime value = this.mapper.readValue('"' + time.toString() + '"', LocalDateTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -305,8 +302,8 @@ public void testDeserializationAsString03() throws Exception LocalDateTime value = this.mapper.readValue('"' + time.toString() + '"', LocalDateTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -321,9 +318,9 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertTrue(value instanceof LocalDateTime); + assertEquals(time, value); } @Test @@ -337,9 +334,9 @@ public void testDeserializationWithTypeInfo02() throws Exception "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertTrue(value instanceof LocalDateTime); + assertEquals(time, value); } @Test @@ -352,8 +349,8 @@ public void testDeserializationWithTypeInfo03() throws Exception "[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalDateTime.", value instanceof LocalDateTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertTrue(value instanceof LocalDateTime); + assertEquals(time, value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java index b7efe692..10f9d5ed 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestLocalTimeKeySerialization extends ModuleTestBase { @@ -26,7 +27,7 @@ public class TestLocalTimeKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -42,7 +43,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(TIME_0_STRING, "test"), value); + assertEquals(map(TIME_0_STRING, "test"), value); } @Test @@ -51,7 +52,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(TIME_STRING, "test"), value); + assertEquals(map(TIME_STRING, "test"), value); } @Test @@ -59,7 +60,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(TIME_0_STRING, "test"), TYPE_REF); map.put(TIME_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -67,7 +68,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(TIME_STRING, "test"), TYPE_REF); map.put(TIME, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java index 711d071f..b1f37486 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestLocalTimeSerialization.java @@ -16,37 +16,27 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.LocalTime; import java.time.temporal.Temporal; +import org.junit.jupiter.api.*; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestLocalTimeSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); } - @After - public void tearDown() - { - - } - @Test public void testSerializationAsTimestamp01() throws Exception { @@ -55,8 +45,8 @@ public void testSerializationAsTimestamp01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[15,43]", value); + assertNotNull(value); + assertEquals("[15,43]", value); } @Test @@ -67,8 +57,8 @@ public void testSerializationAsTimestamp02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[9,22,57]", value); + assertNotNull(value); + assertEquals("[9,22,57]", value); } @Test @@ -80,8 +70,8 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[9,22,0,57]", value); + assertNotNull(value); + assertEquals("[9,22,0,57]", value); } @Test @@ -93,8 +83,8 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[9,22,0,0]", value); + assertNotNull(value); + assertEquals("[9,22,0,0]", value); } @Test @@ -106,8 +96,8 @@ public void testSerializationAsTimestamp04Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[22,31,5,829837]", value); + assertNotNull(value); + assertEquals("[22,31,5,829837]", value); } @Test @@ -119,8 +109,8 @@ public void testSerializationAsTimestamp04Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[22,31,5,422]", value); + assertNotNull(value); + assertEquals("[22,31,5,422]", value); } @Test @@ -131,8 +121,8 @@ public void testSerializationAsString01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"15:43:20\"", value); + assertNotNull(value); + assertEquals("\"15:43:20\"", value); } @Test @@ -143,8 +133,8 @@ public void testSerializationAsString02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -155,8 +145,8 @@ public void testSerializationAsString03() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -169,8 +159,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", value); + assertNotNull(value); + assertEquals("[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", value); } @Test @@ -183,8 +173,8 @@ public void testSerializationWithTypeInfo02() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", value); + assertNotNull(value); + assertEquals("[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", value); } @Test @@ -196,9 +186,8 @@ public void testSerializationWithTypeInfo03() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", value); } @Test @@ -208,8 +197,8 @@ public void testDeserializationAsTimestamp01() throws Exception LocalTime value = this.mapper.readValue("[15,43]", LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -219,8 +208,8 @@ public void testDeserializationAsTimestamp02() throws Exception LocalTime value = this.mapper.readValue("[9,22,57]", LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -231,8 +220,8 @@ public void testDeserializationAsTimestamp03Nanoseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); LocalTime value = this.mapper.readValue("[9,22,0,57]", LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -243,8 +232,8 @@ public void testDeserializationAsTimestamp03Milliseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); LocalTime value = this.mapper.readValue("[9,22,0,57]", LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -255,8 +244,8 @@ public void testDeserializationAsTimestamp04Nanoseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); LocalTime value = this.mapper.readValue("[22,31,5,829837]", LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -267,8 +256,8 @@ public void testDeserializationAsTimestamp04Milliseconds01() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); LocalTime value = this.mapper.readValue("[22,31,5,829837]", LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -279,8 +268,8 @@ public void testDeserializationAsTimestamp04Milliseconds02() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); LocalTime value = this.mapper.readValue("[22,31,5,829]", LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -290,8 +279,8 @@ public void testDeserializationAsString01() throws Exception LocalTime value = this.mapper.readValue('"' + time.toString() + '"', LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -301,8 +290,8 @@ public void testDeserializationAsString02() throws Exception LocalTime value = this.mapper.readValue('"' + time.toString() + '"', LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -312,8 +301,8 @@ public void testDeserializationAsString03() throws Exception LocalTime value = this.mapper.readValue('"' + time.toString() + '"', LocalTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -327,9 +316,9 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalTime.", value instanceof LocalTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertInstanceOf(LocalTime.class, value); + assertEquals(time, value); } @Test @@ -343,9 +332,9 @@ public void testDeserializationWithTypeInfo02() throws Exception "[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalTime.", value instanceof LocalTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertInstanceOf(LocalTime.class, value); + assertEquals(time, value); } @Test @@ -358,8 +347,8 @@ public void testDeserializationWithTypeInfo03() throws Exception "[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a LocalTime.", value instanceof LocalTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertInstanceOf(LocalTime.class, value); + assertEquals(time, value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java index 9c8145ee..34550857 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDayKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestMonthDayKeySerialization extends ModuleTestBase { @@ -21,7 +22,7 @@ public class TestMonthDayKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -37,7 +38,7 @@ public void testSerialization() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(MONTH_DAY_STRING, "test"), value); + assertEquals(map(MONTH_DAY_STRING, "test"), value); } @Test @@ -45,7 +46,7 @@ public void testDeserialization() throws Exception { Map value = om.readValue(map(MONTH_DAY_STRING, "test"), TYPE_REF); map.put(MONTH_DAY, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java index 15cc2ea7..d7954e35 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestMonthDaySerialization.java @@ -16,33 +16,27 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Month; import java.time.MonthDay; import java.time.temporal.TemporalAccessor; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestMonthDaySerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); } - @After - public void tearDown() { } - @Test public void testSerialization01() throws Exception { @@ -50,8 +44,8 @@ public void testSerialization01() throws Exception String value = this.mapper.writeValueAsString(monthDay); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"--01-17\"", value); + assertNotNull(value); + assertEquals("\"--01-17\"", value); } @Test @@ -61,8 +55,8 @@ public void testSerialization02() throws Exception String value = this.mapper.writeValueAsString(monthDay); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"--08-21\"", value); + assertNotNull(value); + assertEquals("\"--08-21\"", value); } @Test @@ -73,8 +67,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(monthDay); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", value); + assertNotNull(value); + assertEquals("[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", value); } @Test @@ -84,8 +78,8 @@ public void testDeserialization01() throws Exception MonthDay value = this.mapper.readValue("\"--01-17\"", MonthDay.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", monthDay, value); + assertNotNull(value); + assertEquals(monthDay, value); } @Test @@ -95,8 +89,8 @@ public void testDeserialization02() throws Exception MonthDay value = this.mapper.readValue("\"--08-21\"", MonthDay.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", monthDay, value); + assertNotNull(value); + assertEquals(monthDay, value); } @Test @@ -107,8 +101,8 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); TemporalAccessor value = this.mapper.readValue("[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", TemporalAccessor.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a MonthDay.", value instanceof MonthDay); - assertEquals("The value is not correct.", monthDay, value); + assertNotNull(value); + assertInstanceOf(MonthDay.class, value); + assertEquals(monthDay, value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java index 586055ba..763c9f3c 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestNullKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestNullKeySerialization extends ModuleTestBase { @SuppressWarnings("deprecation") @@ -21,7 +22,7 @@ public class TestNullKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -35,7 +36,7 @@ public void testSerialization() throws Exception { map.put(null, "test"); String value = om.writeValueAsString(map); - Assert.assertEquals(map(NULL_KEY, "test"), value); + assertEquals(map(NULL_KEY, "test"), value); } @Test @@ -43,7 +44,7 @@ public void testDeserialization() throws Exception { Map value = om.readValue(map(NULL_KEY, "test"), TYPE_REF); map.put(null, "test"); - Assert.assertEquals(map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java index b3556ce2..bf062a0d 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeKeySerialization.java @@ -6,12 +6,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestOffsetDateTimeKeySerialization extends ModuleTestBase { @@ -27,7 +28,7 @@ public class TestOffsetDateTimeKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -43,7 +44,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(DATE_TIME_0_STRING, "test"), value); + assertEquals(map(DATE_TIME_0_STRING, "test"), value); } @Test @@ -52,7 +53,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(DATE_TIME_1_STRING, "test"), value); + assertEquals(map(DATE_TIME_1_STRING, "test"), value); } @Test @@ -61,7 +62,7 @@ public void testSerialization2() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(DATE_TIME_2_STRING, "test"), value); + assertEquals(map(DATE_TIME_2_STRING, "test"), value); } @Test @@ -69,7 +70,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(DATE_TIME_0_STRING, "test"), TYPE_REF); map.put(DATE_TIME_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -77,7 +78,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(DATE_TIME_1_STRING, "test"), TYPE_REF); map.put(DATE_TIME_1, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -85,7 +86,7 @@ public void testDeserialization2() throws Exception { Map value = om.readValue(map(DATE_TIME_2_STRING, "test"), TYPE_REF); map.put(DATE_TIME_2, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java index 89d2f478..35b12cfb 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetDateTimeSerialization.java @@ -16,10 +16,6 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId; @@ -30,13 +26,15 @@ import java.time.temporal.Temporal; import java.util.TimeZone; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestOffsetDateTimeSerialization extends ModuleTestBase { @@ -52,7 +50,7 @@ public class TestOffsetDateTimeSerialization extends ModuleTestBase private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -67,8 +65,8 @@ public void testSerializationAsTimestamp01Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "0.0", value); + assertNotNull(value); + assertEquals("0.0", value); } @Test @@ -80,8 +78,8 @@ public void testSerializationAsTimestamp01Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "0", value); + assertNotNull(value); + assertEquals("0", value); } @Test @@ -93,8 +91,8 @@ public void testSerializationAsTimestamp02Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "123456789.183917322", value); + assertNotNull(value); + assertEquals("123456789.183917322", value); } @Test @@ -106,8 +104,8 @@ public void testSerializationAsTimestamp02Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "123456789183", value); + assertNotNull(value); + assertEquals("123456789183", value); } @Test @@ -119,8 +117,8 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); + assertNotNull(value); + assertEquals(DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); } @Test @@ -131,7 +129,7 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", Long.toString(date.toInstant().toEpochMilli()), value); + assertEquals(Long.toString(date.toInstant().toEpochMilli()), value); } @Test @@ -141,8 +139,7 @@ public void testSerializationAsString01() throws Exception String value = mapper.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' - + FORMATTER.withZone(Z1).format(date) + '"', value); + assertEquals('"' + FORMATTER.withZone(Z1).format(date) + '"', value); } @Test @@ -152,8 +149,7 @@ public void testSerializationAsString02() throws Exception String value = mapper.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' - + FORMATTER.withZone(Z2).format(date) + '"', value); + assertEquals('"' + FORMATTER.withZone(Z2).format(date) + '"', value); } @Test @@ -163,8 +159,7 @@ public void testSerializationAsString03() throws Exception String value = mapper.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' - + FORMATTER.withZone(Z3).format(date) + '"', value); + assertEquals('"' + FORMATTER.withZone(Z3).format(date) + '"', value); } @Test @@ -175,7 +170,7 @@ public void testSerializationAsStringWithMapperTimeZone01() throws Exception .setTimeZone(TimeZone.getTimeZone(Z1)) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -186,7 +181,7 @@ public void testSerializationAsStringWithMapperTimeZone02() throws Exception .setTimeZone(TimeZone.getTimeZone(Z2)) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -197,7 +192,7 @@ public void testSerializationAsStringWithMapperTimeZone03() throws Exception .setTimeZone(TimeZone.getTimeZone(Z3)) .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -209,8 +204,7 @@ public void testSerializationWithTypeInfo01() throws Exception .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true) .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", value); + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", value); } @Test @@ -222,8 +216,7 @@ public void testSerializationWithTypeInfo02() throws Exception .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true) .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",123456789183]", value); + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",123456789183]", value); } @Test @@ -234,8 +227,7 @@ public void testSerializationWithTypeInfo03() throws Exception .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .addMixIn(Temporal.class, MockObjectConfiguration.class) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",\"" + + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER_UTC.withZone(Z3).format(date) + "\"]", value); } @@ -249,8 +241,7 @@ public void testSerializationWithTypeInfoAndMapperTimeZone() throws Exception .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .addMixIn(Temporal.class, MockObjectConfiguration.class) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } @Test @@ -260,9 +251,9 @@ public void testDeserializationAsFloat01WithoutTimeZone() throws Exception OffsetDateTime value = this.mapper.readValue("0.000000000", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -273,9 +264,9 @@ public void testDeserializationAsFloat01WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue("0.000000000", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -285,9 +276,9 @@ public void testDeserializationAsFloat02WithoutTimeZone() throws Exception OffsetDateTime value = this.mapper.readValue("123456789.183917322", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -298,9 +289,9 @@ public void testDeserializationAsFloat02WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue("123456789.183917322", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -312,9 +303,9 @@ public void testDeserializationAsFloat03WithoutTimeZone() throws Exception DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class ); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -327,9 +318,9 @@ public void testDeserializationAsFloat03WithTimeZone() throws Exception DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), OffsetDateTime.class ); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -340,9 +331,9 @@ public void testDeserializationAsInt01NanosecondsWithoutTimeZone() throws Except this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -354,9 +345,9 @@ public void testDeserializationAsInt01NanosecondsWithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -367,9 +358,9 @@ public void testDeserializationAsInt01MillisecondsWithoutTimeZone() throws Excep this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -381,9 +372,9 @@ public void testDeserializationAsInt01MillisecondsWithTimeZone() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue("0", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -394,9 +385,9 @@ public void testDeserializationAsInt02NanosecondsWithoutTimeZone() throws Except this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetDateTime value = this.mapper.readValue("123456789", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -408,9 +399,9 @@ public void testDeserializationAsInt02NanosecondsWithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue("123456789", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -421,9 +412,9 @@ public void testDeserializationAsInt02MillisecondsWithoutTimeZone() throws Excep this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); OffsetDateTime value = this.mapper.readValue("123456789422", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -435,9 +426,9 @@ public void testDeserializationAsInt02MillisecondsWithTimeZone() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue("123456789422", OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -449,9 +440,9 @@ public void testDeserializationAsInt03NanosecondsWithoutTimeZone() throws Except this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -464,9 +455,9 @@ public void testDeserializationAsInt03NanosecondsWithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -479,9 +470,9 @@ public void testDeserializationAsInt03MillisecondsWithoutTimeZone() throws Excep OffsetDateTime value = this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -495,9 +486,9 @@ public void testDeserializationAsInt03MillisecondsWithTimeZone() throws Exceptio OffsetDateTime value = this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -508,9 +499,9 @@ public void testDeserializationAsString01WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -522,9 +513,9 @@ public void testDeserializationAsString01WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -536,9 +527,9 @@ public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z1), value.getOffset()); + assertEquals(getOffset(value, Z1), value.getOffset(), "The time zone is not correct."); } @Test @@ -549,9 +540,9 @@ public void testDeserializationAsString02WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -563,9 +554,9 @@ public void testDeserializationAsString02WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -577,9 +568,9 @@ public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z2), value.getOffset()); + assertEquals(getOffset(value, Z2), value.getOffset(), "The time zone is not correct."); } @Test @@ -590,9 +581,9 @@ public void testDeserializationAsString03WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, value.getOffset()); + assertEquals(ZoneOffset.UTC, value.getOffset(), "The time zone is not correct."); } @Test @@ -604,9 +595,9 @@ public void testDeserializationAsString03WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), value.getOffset()); + assertEquals(getDefaultOffset(date), value.getOffset(), "The time zone is not correct."); } @Test @@ -618,9 +609,9 @@ public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); OffsetDateTime value = this.mapper.readValue('"' + FORMATTER.format(date) + '"', OffsetDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", getOffset(value, Z3), value.getOffset()); + assertEquals(getOffset(value, Z3), value.getOffset(), "The time zone is not correct."); } @Test @@ -633,10 +624,10 @@ public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -650,10 +641,10 @@ public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -667,10 +658,10 @@ public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -685,10 +676,10 @@ public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -702,10 +693,10 @@ public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -720,10 +711,10 @@ public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",123456789422]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -737,10 +728,10 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", ZoneOffset.UTC, ((OffsetDateTime) value).getOffset()); + assertEquals(ZoneOffset.UTC, ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -755,10 +746,10 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); assertIsEqual(date, (OffsetDateTime) value); - assertEquals("The time zone is not correct.", getDefaultOffset(date), ((OffsetDateTime) value).getOffset()); + assertEquals(getDefaultOffset(date), ((OffsetDateTime) value).getOffset(), "The time zone is not correct."); } @Test @@ -773,17 +764,17 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an OffsetDateTime.", value instanceof OffsetDateTime); + assertNotNull(value); + assertInstanceOf(OffsetDateTime.class, value, "The value should be an OffsetDateTime."); OffsetDateTime cast = (OffsetDateTime) value; assertIsEqual(date, cast); - assertEquals("The time zone is not correct.", getOffset(cast, Z3), cast.getOffset()); + assertEquals(getOffset(cast, Z3), cast.getOffset(), "The time zone is not correct."); } private static void assertIsEqual(OffsetDateTime expected, OffsetDateTime actual) { - assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.", - expected.isEqual(actual)); + assertTrue(expected.isEqual(actual), + "The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">."); } private static ZoneOffset getDefaultOffset(OffsetDateTime date) diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java index 577a6760..daed6941 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeKeySerialization.java @@ -5,12 +5,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestOffsetTimeKeySerialization extends ModuleTestBase { @@ -26,7 +27,7 @@ public class TestOffsetTimeKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -42,7 +43,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals(map(TIME_0_STRING, "test"), value); + assertEquals(map(TIME_0_STRING, "test"), value); } @Test @@ -51,7 +52,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(TIME_1_STRING, "test"), value); + assertEquals(map(TIME_1_STRING, "test"), value); } @Test @@ -60,7 +61,7 @@ public void testSerialization2() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(TIME_2_STRING, "test"), value); + assertEquals(map(TIME_2_STRING, "test"), value); } @Test @@ -68,7 +69,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(TIME_0_STRING, "test"), TYPE_REF); map.put(TIME_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -76,7 +77,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(TIME_1_STRING, "test"), TYPE_REF); map.put(TIME_1, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -84,7 +85,7 @@ public void testDeserialization2() throws Exception { Map value = om.readValue(map(TIME_2_STRING, "test"), TYPE_REF); map.put(TIME_2, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java index 169197ed..0b9a32ed 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestOffsetTimeSerialization.java @@ -16,26 +16,24 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.OffsetTime; import java.time.ZoneOffset; import java.time.temporal.Temporal; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestOffsetTimeSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -49,8 +47,8 @@ public void testSerializationAsTimestamp01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[15,43,\"+03:00\"]", value); + assertNotNull(value); + assertEquals("[15,43,\"+03:00\"]", value); } @Test @@ -61,8 +59,8 @@ public void testSerializationAsTimestamp02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[9,22,57,\"-06:30\"]", value); + assertNotNull(value); + assertEquals("[9,22,57,\"-06:30\"]", value); } @Test @@ -74,8 +72,8 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[9,22,0,57,\"-06:30\"]", value); + assertNotNull(value); + assertEquals("[9,22,0,57,\"-06:30\"]", value); } @Test @@ -87,8 +85,8 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[9,22,0,0,\"-06:30\"]", value); + assertNotNull(value); + assertEquals("[9,22,0,0,\"-06:30\"]", value); } @Test @@ -100,8 +98,8 @@ public void testSerializationAsTimestamp04Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[22,31,5,829837,\"+11:00\"]", value); + assertNotNull(value); + assertEquals("[22,31,5,829837,\"+11:00\"]", value); } @Test @@ -113,8 +111,8 @@ public void testSerializationAsTimestamp04Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[22,31,5,422,\"+11:00\"]", value); + assertNotNull(value); + assertEquals("[22,31,5,422,\"+11:00\"]", value); } @Test @@ -125,8 +123,8 @@ public void testSerializationAsString01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -137,8 +135,8 @@ public void testSerializationAsString02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -149,8 +147,8 @@ public void testSerializationAsString03() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -163,9 +161,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", value); + assertNotNull(value); + assertEquals("[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", value); } @Test @@ -178,9 +175,8 @@ public void testSerializationWithTypeInfo02() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", value); + assertNotNull(value); + assertEquals("[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", value); } @Test @@ -192,9 +188,8 @@ public void testSerializationWithTypeInfo03() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", value); } @Test @@ -204,8 +199,8 @@ public void testDeserializationAsTimestamp01() throws Exception OffsetTime value = this.mapper.readValue("[15,43,\"+0300\"]", OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -215,8 +210,8 @@ public void testDeserializationAsTimestamp02() throws Exception OffsetTime value = this.mapper.readValue("[9,22,57,\"-06:30\"]", OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -227,8 +222,8 @@ public void testDeserializationAsTimestamp03Nanoseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetTime value = this.mapper.readValue("[9,22,0,57,\"-06:30\"]", OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -239,8 +234,8 @@ public void testDeserializationAsTimestamp03Milliseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); OffsetTime value = this.mapper.readValue("[9,22,0,57,\"-06:30\"]", OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -251,8 +246,8 @@ public void testDeserializationAsTimestamp04Nanoseconds() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); OffsetTime value = this.mapper.readValue("[22,31,5,829837,\"+11:00\"]", OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -263,8 +258,8 @@ public void testDeserializationAsTimestamp04Milliseconds01() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); OffsetTime value = this.mapper.readValue("[22,31,5,829837,\"+11:00\"]", OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -275,8 +270,8 @@ public void testDeserializationAsTimestamp04Milliseconds02() throws Exception this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); OffsetTime value = this.mapper.readValue("[22,31,5,829,\"+11:00\"]", OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -286,8 +281,8 @@ public void testDeserializationAsString01() throws Exception OffsetTime value = this.mapper.readValue('"' + time.toString() + '"', OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -297,8 +292,8 @@ public void testDeserializationAsString02() throws Exception OffsetTime value = this.mapper.readValue('"' + time.toString() + '"', OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -308,8 +303,8 @@ public void testDeserializationAsString03() throws Exception OffsetTime value = this.mapper.readValue('"' + time.toString() + '"', OffsetTime.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertEquals(time, value); } @Test @@ -323,9 +318,9 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertInstanceOf(OffsetTime.class, value, "The value should be a OffsetTime."); + assertEquals(time, value); } @Test @@ -339,9 +334,9 @@ public void testDeserializationWithTypeInfo02() throws Exception "[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertInstanceOf(OffsetTime.class, value, "The value should be a OffsetTime."); + assertEquals(time, value); } @Test @@ -354,8 +349,8 @@ public void testDeserializationWithTypeInfo03() throws Exception "[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a OffsetTime.", value instanceof OffsetTime); - assertEquals("The value is not correct.", time, value); + assertNotNull(value); + assertInstanceOf(OffsetTime.class, value, "The value should be a OffsetTime."); + assertEquals(time, value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java index de5f9ec0..e6e0e567 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestPeriodKeySerialization extends ModuleTestBase { @@ -23,7 +24,7 @@ public class TestPeriodKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -39,7 +40,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(PERIOD_0_STRING, "test"), value); + assertEquals(map(PERIOD_0_STRING, "test"), value); } @Test @@ -48,7 +49,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(PERIOD_STRING, "test"), value); + assertEquals(map(PERIOD_STRING, "test"), value); } @Test @@ -56,7 +57,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(PERIOD_0_STRING, "test"), TYPE_REF); map.put(PERIOD_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -64,7 +65,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(PERIOD_STRING, "test"), TYPE_REF); map.put(PERIOD, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java index 4d362efd..5b77e414 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestPeriodSerialization.java @@ -16,32 +16,26 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Period; import java.time.temporal.TemporalAmount; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestPeriodSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); } - @After - public void tearDown() { } - @Test public void testSerialization01() throws Exception { @@ -49,8 +43,8 @@ public void testSerialization01() throws Exception String value = this.mapper.writeValueAsString(period); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + period.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + period.toString() + '"', value); } @Test @@ -60,8 +54,8 @@ public void testSerialization02() throws Exception String value = this.mapper.writeValueAsString(period); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + period.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + period.toString() + '"', value); } @Test @@ -72,9 +66,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(TemporalAmount.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(period); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + Period.class.getName() + "\",\"" + period.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + Period.class.getName() + "\",\"" + period.toString() + "\"]", value); } @Test @@ -84,8 +77,8 @@ public void testDeserialization01() throws Exception Period value = this.mapper.readValue('"' + period.toString() + '"', Period.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", period, value); + assertNotNull(value); + assertEquals(period, value); } @Test @@ -95,8 +88,8 @@ public void testDeserialization02() throws Exception Period value = this.mapper.readValue('"' + period.toString() + '"', Period.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", period, value); + assertNotNull(value); + assertEquals(period, value); } @Test @@ -109,8 +102,8 @@ public void testDeserializationWithTypeInfo01() throws Exception "[\"" + Period.class.getName() + "\",\"" + period.toString() + "\"]", TemporalAmount.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a Period.", value instanceof Period); - assertEquals("The value is not correct.", period, value); + assertNotNull(value); + assertInstanceOf(Period.class, value, "The value should be a Period."); + assertEquals(period, value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java index ee9f0919..8574d2d2 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestYearKeySerialization extends ModuleTestBase { @@ -18,7 +19,7 @@ public class TestYearKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -34,7 +35,7 @@ public void testSerialization() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map("3141", "test"), value); + assertEquals(map("3141", "test"), value); } @Test @@ -42,7 +43,7 @@ public void testDeserialization() throws Exception { Map value = om.readValue(map("3141", "test"), TYPE_REF); map.put(Year.of(3141), "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java index 00e16599..9dda0745 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestYearMonthKeySerialization extends ModuleTestBase { @@ -19,7 +20,7 @@ public class TestYearMonthKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -35,7 +36,7 @@ public void testSerialization() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map("3141-05", "test"), value); + assertEquals(map("3141-05", "test"), value); } @Test @@ -43,7 +44,7 @@ public void testDeserialization() throws Exception { Map value = om.readValue(map("3141-05", "test"), TYPE_REF); map.put(YearMonth.of(3141, 5), "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java index 110baec5..fd7c1996 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearMonthSerialization.java @@ -16,28 +16,26 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Month; import java.time.YearMonth; import java.time.temporal.Temporal; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestYearMonthSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -51,8 +49,8 @@ public void testSerializationAsTimestamp01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[1986,1]", value); + assertNotNull(value); + assertEquals("[1986,1]", value); } @Test @@ -63,8 +61,8 @@ public void testSerializationAsTmestamp02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); String value = this.mapper.writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2013,8]", value); + assertNotNull(value); + assertEquals("[2013,8]", value); } @Test @@ -75,8 +73,8 @@ public void testSerializationAsString01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + yearMonth.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + yearMonth.toString() + '"', value); } @Test @@ -87,8 +85,8 @@ public void testSerializationAsString02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + yearMonth.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + yearMonth.toString() + '"', value); } @Test @@ -100,9 +98,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", value); } @Test @@ -113,8 +110,8 @@ public void testDeserializationAsTimestamp01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); YearMonth value = this.mapper.readValue("[1986,1]", YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -125,8 +122,8 @@ public void testDeserializationAsTimestamp02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true); YearMonth value = this.mapper.readValue("[2013,8]", YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -137,8 +134,8 @@ public void testDeserializationAsString01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); YearMonth value = this.mapper.readValue('"' + yearMonth.toString() + '"', YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -149,8 +146,8 @@ public void testDeserializationAsString02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); YearMonth value = this.mapper.readValue('"' + yearMonth.toString() + '"', YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -161,9 +158,9 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue("[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", Temporal.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a YearMonth.", value instanceof YearMonth); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertInstanceOf(YearMonth.class, value, "The value should be a YearMonth."); + assertEquals(yearMonth, value); } private static class SimpleAggregate @@ -187,8 +184,8 @@ public void testSerializationWithPattern01() throws Exception String value = this.mapper.writeValueAsString(simpleAggregate); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "{\"yearMonth\":\"1308\"}", value); + assertNotNull(value); + assertEquals("{\"yearMonth\":\"1308\"}", value); } @Test @@ -199,7 +196,7 @@ public void testDeserializationWithPattern01() throws Exception SimpleAggregate value = this.mapper.readValue("{\"yearMonth\":\"1308\"}", SimpleAggregate.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", simpleAggregate.yearMonth, value.yearMonth); + assertNotNull(value); + assertEquals(simpleAggregate.yearMonth, value.yearMonth); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java index 371596ee..e493d60e 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestYearSerialization.java @@ -16,23 +16,21 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Year; import java.time.temporal.Temporal; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestYearSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -45,8 +43,8 @@ public void testSerialization01() throws Exception String value = this.mapper.writeValueAsString(year); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "1986", value); + assertNotNull(value); + assertEquals("1986", value); } @Test @@ -56,8 +54,8 @@ public void testSerialization02() throws Exception String value = this.mapper.writeValueAsString(year); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "2013", value); + assertNotNull(value); + assertEquals("2013", value); } @Test @@ -68,8 +66,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(year); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"" + Year.class.getName() + "\",2005]", value); + assertNotNull(value); + assertEquals("[\"" + Year.class.getName() + "\",2005]", value); } @Test @@ -77,8 +75,8 @@ public void testDeserialization01() throws Exception { Year value = this.mapper.readValue("1986", Year.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Year.of(1986), value); + assertNotNull(value); + assertEquals(Year.of(1986), value); } @Test @@ -86,8 +84,8 @@ public void testDeserialization02() throws Exception { Year value = this.mapper.readValue("2013", Year.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Year.of(2013), value); + assertNotNull(value); + assertEquals(Year.of(2013), value); } @Test @@ -96,8 +94,8 @@ public void testDeserializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue("[\"" + Year.class.getName() + "\",2005]", Temporal.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a Year.", value instanceof Year); - assertEquals("The value is not correct.", Year.of(2005), value); + assertNotNull(value); + assertInstanceOf(Year.class, value, "The value should be a Year."); + assertEquals(Year.of(2005), value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java index 289192b0..268149fb 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestZoneIdKeySerialization extends ModuleTestBase { @@ -25,7 +26,7 @@ public class TestZoneIdKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -41,7 +42,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(ZONE_0_STRING, "test"), value); + assertEquals(map(ZONE_0_STRING, "test"), value); } @Test @@ -50,7 +51,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(ZONE_1_STRING, "test"), value); + assertEquals(map(ZONE_1_STRING, "test"), value); } @Test @@ -59,7 +60,7 @@ public void testSerialization2() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(ZONE_2_STRING, "test"), value); + assertEquals(map(ZONE_2_STRING, "test"), value); } @Test @@ -67,7 +68,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(ZONE_0_STRING, "test"), TYPE_REF); map.put(ZONE_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -75,7 +76,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(ZONE_1_STRING, "test"), TYPE_REF); map.put(ZONE_1, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -83,7 +84,7 @@ public void testDeserialization2() throws Exception { Map value = om.readValue(map(ZONE_2_STRING, "test"), TYPE_REF); map.put(ZONE_2, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java index 8c6f1f1f..ce5c3056 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneIdSerialization.java @@ -16,21 +16,20 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.time.ZoneId; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestZoneIdSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -43,8 +42,8 @@ public void testSerialization01() throws Exception String value = this.mapper.writeValueAsString(id); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"America/Chicago\"", value); + assertNotNull(value); + assertEquals("\"America/Chicago\"", value); } @Test @@ -54,8 +53,8 @@ public void testSerialization02() throws Exception String value = this.mapper.writeValueAsString(id); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"America/Anchorage\"", value); + assertNotNull(value); + assertEquals("\"America/Anchorage\"", value); } @Test @@ -66,8 +65,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(id); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"java.time.ZoneId\",\"America/Denver\"]", value); + assertNotNull(value); + assertEquals("[\"java.time.ZoneId\",\"America/Denver\"]", value); } @Test @@ -75,8 +74,8 @@ public void testDeserialization01() throws Exception { ZoneId value = this.mapper.readValue("\"America/Chicago\"", ZoneId.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", ZoneId.of("America/Chicago"), value); + assertNotNull(value); + assertEquals(ZoneId.of("America/Chicago"), value); } @Test @@ -84,8 +83,8 @@ public void testDeserialization02() throws Exception { ZoneId value = this.mapper.readValue("\"America/Anchorage\"", ZoneId.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", ZoneId.of("America/Anchorage"), value); + assertNotNull(value); + assertEquals(ZoneId.of("America/Anchorage"), value); } @Test @@ -94,7 +93,7 @@ public void testDeserializationWithTypeInfo02() throws Exception this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); ZoneId value = this.mapper.readValue("[\"" + ZoneId.class.getName() + "\",\"America/Denver\"]", ZoneId.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", ZoneId.of("America/Denver"), value); + assertNotNull(value); + assertEquals(ZoneId.of("America/Denver"), value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java index b9e2d00e..b034056e 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetKeySerialization.java @@ -4,12 +4,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestZoneOffsetKeySerialization extends ModuleTestBase { @@ -23,7 +24,7 @@ public class TestZoneOffsetKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -39,7 +40,7 @@ public void testSerialization0() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(OFFSET_0_STRING, "test"), value); + assertEquals(map(OFFSET_0_STRING, "test"), value); } @Test @@ -48,7 +49,7 @@ public void testSerialization1() throws Exception { String value = om.writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(OFFSET_1_STRING, "test"), value); + assertEquals(map(OFFSET_1_STRING, "test"), value); } @Test @@ -56,7 +57,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(OFFSET_0_STRING, "test"), TYPE_REF); map.put(OFFSET_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -64,7 +65,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(OFFSET_1_STRING, "test"), TYPE_REF); map.put(OFFSET_1, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java index c526c66f..63596cd0 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZoneOffsetSerialization.java @@ -16,23 +16,21 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.ZoneId; import java.time.ZoneOffset; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestZoneOffsetSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); @@ -45,8 +43,8 @@ public void testSerialization01() throws Exception String value = this.mapper.writeValueAsString(offset); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"Z\"", value); + assertNotNull(value); + assertEquals("\"Z\"", value); } @Test @@ -56,8 +54,8 @@ public void testSerialization02() throws Exception String value = this.mapper.writeValueAsString(offset); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"+03:00\"", value); + assertNotNull(value); + assertEquals("\"+03:00\"", value); } @Test @@ -67,8 +65,8 @@ public void testSerialization03() throws Exception String value = this.mapper.writeValueAsString(offset); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"-06:30\"", value); + assertNotNull(value); + assertEquals("\"-06:30\"", value); } @Test @@ -79,8 +77,8 @@ public void testSerializationWithTypeInfo03() throws Exception this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(offset); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[\"" + ZoneOffset.class.getName() + "\",\"+04:15\"]", value); + assertNotNull(value); + assertEquals("[\"" + ZoneOffset.class.getName() + "\",\"+04:15\"]", value); } @Test @@ -88,8 +86,8 @@ public void testDeserialization01() throws Exception { ZoneOffset value = this.mapper.readValue("\"Z\"", ZoneOffset.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", ZoneOffset.of("Z"), value); + assertNotNull(value); + assertEquals(ZoneOffset.of("Z"), value); } @Test @@ -97,8 +95,8 @@ public void testDeserialization02() throws Exception { ZoneOffset value = this.mapper.readValue("\"+0300\"", ZoneOffset.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", ZoneOffset.of("+0300"), value); + assertNotNull(value); + assertEquals(ZoneOffset.of("+0300"), value); } @Test @@ -106,8 +104,8 @@ public void testDeserialization03() throws Exception { ZoneOffset value = this.mapper.readValue("\"-06:30\"", ZoneOffset.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", ZoneOffset.of("-0630"), value); + assertNotNull(value); + assertEquals(ZoneOffset.of("-0630"), value); } @Test @@ -116,8 +114,8 @@ public void testDeserializationWithTypeInfo03() throws Exception this.mapper.addMixIn(ZoneId.class, MockObjectConfiguration.class); ZoneId value = this.mapper.readValue("[\"" + ZoneOffset.class.getName() + "\",\"+0415\"]", ZoneId.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a ZoneOffset.", value instanceof ZoneOffset); - assertEquals("The value is not correct.", ZoneOffset.of("+0415"), value); + assertNotNull(value); + assertInstanceOf(ZoneOffset.class, value); + assertEquals(ZoneOffset.of("+0415"), value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java index 0c1e4cac..23c951c6 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeKeySerialization.java @@ -7,12 +7,13 @@ import java.util.HashMap; import java.util.Map; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestZonedDateTimeKeySerialization extends ModuleTestBase { @@ -37,7 +38,7 @@ public class TestZonedDateTimeKeySerialization extends ModuleTestBase { private ObjectMapper om; private Map map; - @Before + @BeforeEach public void setUp() { this.om = newMapper(); map = new HashMap<>(); @@ -53,7 +54,7 @@ public void testSerialization0() throws Exception { String value = om.writerFor(TYPE_REF).writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(DATE_TIME_0_STRING, "test"), value); + assertEquals(map(DATE_TIME_0_STRING, "test"), value); } @Test @@ -62,7 +63,7 @@ public void testSerialization1() throws Exception { String value = om.writerFor(TYPE_REF).writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(DATE_TIME_1_STRING, "test"), value); + assertEquals(map(DATE_TIME_1_STRING, "test"), value); } @Test @@ -71,7 +72,7 @@ public void testSerialization2() throws Exception { String value = om.writerFor(TYPE_REF).writeValueAsString(map); - Assert.assertEquals("Value is incorrect", map(DATE_TIME_2_STRING, "test"), value); + assertEquals(map(DATE_TIME_2_STRING, "test"), value); } @Test @@ -79,7 +80,7 @@ public void testDeserialization0() throws Exception { Map value = om.readValue(map(DATE_TIME_0_STRING, "test"), TYPE_REF); map.put(DATE_TIME_0, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -87,7 +88,7 @@ public void testDeserialization1() throws Exception { Map value = om.readValue(map(DATE_TIME_1_STRING, "test"), TYPE_REF); map.put(DATE_TIME_1, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } @Test @@ -95,7 +96,7 @@ public void testDeserialization2() throws Exception { Map value = om.readValue(map(DATE_TIME_2_STRING, "test"), TYPE_REF); map.put(DATE_TIME_2_OFFSET, "test"); - Assert.assertEquals("Value is incorrect", map, value); + assertEquals(map, value); } private String map(String key, String value) { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java index 188092fa..49c8a3c7 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/old/TestZonedDateTimeSerialization.java @@ -16,10 +16,6 @@ package com.fasterxml.jackson.datatype.jsr310.old; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; @@ -29,14 +25,15 @@ import java.time.temporal.Temporal; import java.util.TimeZone; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class TestZonedDateTimeSerialization extends ModuleTestBase { private static final ZoneId Z1 = ZoneId.of("America/Chicago"); @@ -51,18 +48,12 @@ public class TestZonedDateTimeSerialization extends ModuleTestBase { private ObjectMapper mapper; - @Before + @BeforeEach public void setUp() { this.mapper = newMapper(); } - @After - public void tearDown() - { - - } - @Test public void testSerializationAsTimestamp01Nanoseconds() throws Exception { @@ -72,8 +63,8 @@ public void testSerializationAsTimestamp01Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "0.0", value); + assertNotNull(value); + assertEquals("0.0", value); } @Test @@ -85,8 +76,8 @@ public void testSerializationAsTimestamp01Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "0", value); + assertNotNull(value); + assertEquals("0", value); } @Test @@ -98,8 +89,8 @@ public void testSerializationAsTimestamp02Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "123456789.183917322", value); + assertNotNull(value); + assertEquals("123456789.183917322", value); } @Test @@ -111,8 +102,8 @@ public void testSerializationAsTimestamp02Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "123456789183", value); + assertNotNull(value); + assertEquals("123456789183", value); } @Test @@ -124,8 +115,8 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); + assertNotNull(value); + assertEquals(DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); } @Test @@ -137,8 +128,8 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", Long.toString(date.toInstant().toEpochMilli()), value); + assertNotNull(value); + assertEquals(Long.toString(date.toInstant().toEpochMilli()), value); } @Test @@ -149,8 +140,8 @@ public void testSerializationAsString01() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + date.toString() + '"', value); } @Test @@ -161,8 +152,8 @@ public void testSerializationAsString02() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + date.toString() + '"', value); } @Test @@ -173,8 +164,8 @@ public void testSerializationAsString03() throws Exception this.mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + date.toString() + '"', value); } @Test @@ -186,7 +177,7 @@ public void testSerializationAsStringWithZoneIdOff() throws Exception { String value = mapper.writeValueAsString(date); - assertEquals("The value is incorrect", "\"" + date.toString() + "\"", value); + assertEquals("\"" + date.toString() + "\"", value); } @Test @@ -199,9 +190,8 @@ public void testSerializationWithTypeInfo01() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", value); + assertNotNull(value); + assertEquals("[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", value); } @Test @@ -214,9 +204,8 @@ public void testSerializationWithTypeInfo02() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",123456789183]", value); + assertNotNull(value); + assertEquals("[\"" + ZonedDateTime.class.getName() + "\",123456789183]", value); } @Test @@ -228,9 +217,8 @@ public void testSerializationWithTypeInfo03() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = this.mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", value); } @Test @@ -240,9 +228,9 @@ public void testDeserializationAsFloat01WithoutTimeZone() throws Exception ZonedDateTime value = this.mapper.readValue("0.000000000", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -253,9 +241,9 @@ public void testDeserializationAsFloat01WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue("0.000000000", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -265,9 +253,9 @@ public void testDeserializationAsFloat02WithoutTimeZone() throws Exception ZonedDateTime value = this.mapper.readValue("123456789.183917322", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -278,9 +266,9 @@ public void testDeserializationAsFloat02WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue("123456789.183917322", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -290,11 +278,11 @@ public void testDeserializationAsFloat03WithoutTimeZone() throws Exception ZonedDateTime value = this.mapper.readValue( DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), ZonedDateTime.class - ); + ); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -305,11 +293,11 @@ public void testDeserializationAsFloat03WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue( DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), ZonedDateTime.class - ); + ); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -320,9 +308,9 @@ public void testDeserializationAsInt01NanosecondsWithoutTimeZone() throws Except this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -334,9 +322,9 @@ public void testDeserializationAsInt01NanosecondsWithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -347,9 +335,9 @@ public void testDeserializationAsInt01MillisecondsWithoutTimeZone() throws Excep this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -361,9 +349,9 @@ public void testDeserializationAsInt01MillisecondsWithTimeZone() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue("0", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -374,9 +362,9 @@ public void testDeserializationAsInt02NanosecondsWithoutTimeZone() throws Except this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); ZonedDateTime value = this.mapper.readValue("123456789", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -388,9 +376,9 @@ public void testDeserializationAsInt02NanosecondsWithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue("123456789", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -401,9 +389,9 @@ public void testDeserializationAsInt02MillisecondsWithoutTimeZone() throws Excep this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false); ZonedDateTime value = this.mapper.readValue("123456789422", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -415,9 +403,9 @@ public void testDeserializationAsInt02MillisecondsWithTimeZone() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue("123456789422", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -429,9 +417,9 @@ public void testDeserializationAsInt03NanosecondsWithoutTimeZone() throws Except this.mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, true); ZonedDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -444,9 +432,9 @@ public void testDeserializationAsInt03NanosecondsWithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue(Long.toString(date.toEpochSecond()), ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -459,9 +447,9 @@ public void testDeserializationAsInt03MillisecondsWithoutTimeZone() throws Excep ZonedDateTime value = this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -475,9 +463,9 @@ public void testDeserializationAsInt03MillisecondsWithTimeZone() throws Exceptio ZonedDateTime value = this.mapper.readValue(Long.toString(date.toInstant().toEpochMilli()), ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -488,9 +476,9 @@ public void testDeserializationAsString01WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -502,9 +490,9 @@ public void testDeserializationAsString01WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -516,9 +504,9 @@ public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", Z1, value.getZone()); + assertEquals(Z1, value.getZone(), "The time zone is not correct."); } @Test @@ -529,9 +517,9 @@ public void testDeserializationAsString02WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -543,9 +531,9 @@ public void testDeserializationAsString02WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -557,9 +545,9 @@ public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", Z2, value.getZone()); + assertEquals(Z2, value.getZone(), "The time zone is not correct."); } @Test @@ -570,9 +558,9 @@ public void testDeserializationAsString03WithoutTimeZone() throws Exception this.mapper.configure(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE, true); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -584,9 +572,9 @@ public void testDeserializationAsString03WithTimeZone() throws Exception this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -598,9 +586,9 @@ public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exceptio this.mapper.setTimeZone(TimeZone.getDefault()); ZonedDateTime value = this.mapper.readValue('"' + date.toString() + '"', ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", Z3, value.getZone()); + assertEquals(Z3, value.getZone(), "The time zone is not correct."); } @Test @@ -611,12 +599,12 @@ public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -628,12 +616,12 @@ public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -645,12 +633,12 @@ public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -663,12 +651,12 @@ public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -680,12 +668,12 @@ public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789422]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -698,12 +686,12 @@ public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789422]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -715,12 +703,12 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -733,12 +721,12 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception this.mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = this.mapper.readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class - ); + ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -753,15 +741,16 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce "[\"" + ZonedDateTime.class.getName() + "\",\"" + date.toString() + "\"]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", Z3, ((ZonedDateTime) value).getZone()); + assertEquals(Z3, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } private static void assertIsEqual(ZonedDateTime expected, ZonedDateTime actual) { - assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.", - expected.isEqual(actual)); + assertTrue(expected.isEqual(actual), + "The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">."); } } + diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/AlwaysWriteSubSecondDigits76Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/AlwaysWriteSubSecondDigits76Test.java new file mode 100644 index 00000000..4ecd5698 --- /dev/null +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/AlwaysWriteSubSecondDigits76Test.java @@ -0,0 +1,203 @@ +package com.fasterxml.jackson.datatype.jsr310.ser; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.Locale; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.json.JsonMapper; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Tests for [modules-java8#76]: {@link JavaTimeFeature#ALWAYS_WRITE_SUBSECOND_DIGITS}. + */ +public class AlwaysWriteSubSecondDigits76Test extends ModuleTestBase +{ + static class Wrapper { + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss") + public OffsetDateTime value; + + Wrapper(OffsetDateTime v) { value = v; } + } + + static class ShapeOnlyWrapper { + @JsonFormat(shape = JsonFormat.Shape.STRING) + public Instant value; + + ShapeOnlyWrapper(Instant v) { value = v; } + } + + // NOTE: cannot use `ModuleTestBase.mapperBuilder()` here, since it already registers a + // plain `JavaTimeModule` and duplicate registrations of the same module are ignored + private static JsonMapper.Builder builderWithFeature() { + return JsonMapper.builder() + .defaultLocale(Locale.ENGLISH) + .addModule(new JavaTimeModule() + .enable(JavaTimeFeature.ALWAYS_WRITE_SUBSECOND_DIGITS)); + } + + private final ObjectMapper MAPPER = builderWithFeature() + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .build(); + + private final ObjectMapper DEFAULT_MAPPER = mapperBuilder() + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .build(); + + @Test + public void testInstantZeroSubSecond() throws Exception + { + Instant value = Instant.parse("2017-09-14T04:28:48Z"); + // Default: sub-second field omitted entirely + assertEquals(q("2017-09-14T04:28:48Z"), DEFAULT_MAPPER.writeValueAsString(value)); + // Enabled: zero-padded to millisecond precision + assertEquals(q("2017-09-14T04:28:48.000Z"), MAPPER.writeValueAsString(value)); + } + + @Test + public void testInstantHigherPrecisionNotTruncated() throws Exception + { + assertEquals(q("2017-09-14T04:28:48.100Z"), + MAPPER.writeValueAsString(Instant.parse("2017-09-14T04:28:48.100Z"))); + assertEquals(q("2017-09-14T04:28:48.123456Z"), + MAPPER.writeValueAsString(Instant.parse("2017-09-14T04:28:48.123456Z"))); + assertEquals(q("2017-09-14T04:28:48.123456789Z"), + MAPPER.writeValueAsString(Instant.parse("2017-09-14T04:28:48.123456789Z"))); + } + + @Test + public void testOffsetDateTime() throws Exception + { + OffsetDateTime value = OffsetDateTime.parse("2017-09-14T04:28:48+02:00"); + assertEquals(q("2017-09-14T04:28:48+02:00"), DEFAULT_MAPPER.writeValueAsString(value)); + assertEquals(q("2017-09-14T04:28:48.000+02:00"), MAPPER.writeValueAsString(value)); + + // Note: the JDK ISO formatter renders 100 msec as ".1"; with the feature on, + // width is stable at (at least) 3 digits + OffsetDateTime millis = OffsetDateTime.parse("2017-09-14T04:28:48.100+02:00"); + assertEquals(q("2017-09-14T04:28:48.1+02:00"), DEFAULT_MAPPER.writeValueAsString(millis)); + assertEquals(q("2017-09-14T04:28:48.100+02:00"), MAPPER.writeValueAsString(millis)); + } + + @Test + public void testZonedDateTime() throws Exception + { + ZonedDateTime value = ZonedDateTime.parse("2017-09-14T04:28:48+02:00[Europe/Budapest]"); + assertEquals(q("2017-09-14T04:28:48.000+02:00"), MAPPER.writeValueAsString(value)); + } + + @Test + public void testZonedDateTimeWithZoneId() throws Exception + { + ObjectMapper mapper = builderWithFeature() + .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .enable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) + .build(); + ZonedDateTime value = ZonedDateTime.parse("2017-09-14T04:28:48+02:00[Europe/Budapest]"); + assertEquals(q("2017-09-14T04:28:48.000+02:00[Europe/Budapest]"), + mapper.writeValueAsString(value)); + } + + @Test + public void testLocalDateTime() throws Exception + { + LocalDateTime value = LocalDateTime.parse("2017-09-14T04:28:48"); + assertEquals(q("2017-09-14T04:28:48"), DEFAULT_MAPPER.writeValueAsString(value)); + assertEquals(q("2017-09-14T04:28:48.000"), MAPPER.writeValueAsString(value)); + + // Seconds keep being written even when zero (as with the JDK ISO formatter) + LocalDateTime noSeconds = LocalDateTime.parse("2017-09-14T04:28"); + assertEquals(q("2017-09-14T04:28:00"), DEFAULT_MAPPER.writeValueAsString(noSeconds)); + assertEquals(q("2017-09-14T04:28:00.000"), MAPPER.writeValueAsString(noSeconds)); + } + + // Feature must not leak into numeric timestamp serialization + @Test + public void testTimestampsUnaffected() throws Exception + { + ObjectMapper mapper = builderWithFeature() + .enable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .build(); + assertEquals("1505363328.000000000", + mapper.writeValueAsString(Instant.parse("2017-09-14T04:28:48Z"))); + } + + // ... nor override an explicit `@JsonFormat` pattern + @Test + public void testExplicitPatternWins() throws Exception + { + assertEquals(a2q("{'value':'2017-09-14T04:28:48'}"), + MAPPER.writeValueAsString(new Wrapper(OffsetDateTime.parse("2017-09-14T04:28:48Z")))); + } + + // Values written with the feature on must still be readable + @Test + public void testRoundTrip() throws Exception + { + for (String raw : new String[] { + "2017-09-14T04:28:48Z", "2017-09-14T04:28:48.123456789Z", + "1970-01-01T00:00:00Z", "+10000-09-14T04:28:48Z", "-0100-09-14T04:28:48Z" }) { + Instant value = Instant.parse(raw); + String json = MAPPER.writeValueAsString(value); + assertEquals(value, MAPPER.readValue(json, Instant.class), + "Round-trip failed for " + raw + " (serialized as " + json + ")"); + } + } + + // The full `Instant` range must keep working: it is wider than that of + // `LocalDateTime`, so the feature must not route `Instant` through a + // zone-bound formatter + @Test + public void testInstantMinMax() throws Exception + { + // no sub-second part -> padded + assertEquals(q("-1000000000-01-01T00:00:00Z"), + DEFAULT_MAPPER.writeValueAsString(Instant.MIN)); + assertEquals(q("-1000000000-01-01T00:00:00.000Z"), + MAPPER.writeValueAsString(Instant.MIN)); + + // already at nanosecond precision -> unchanged + assertEquals(q("+1000000000-12-31T23:59:59.999999999Z"), + DEFAULT_MAPPER.writeValueAsString(Instant.MAX)); + assertEquals(q("+1000000000-12-31T23:59:59.999999999Z"), + MAPPER.writeValueAsString(Instant.MAX)); + + // and just inside the `LocalDateTime` boundary, either side + assertEquals(q("-999999999-01-01T00:00:00.000Z"), + MAPPER.writeValueAsString(LocalDateTime.MIN.toInstant(ZoneOffset.UTC))); + assertEquals(q("-1000000000-12-31T23:59:59.000Z"), + MAPPER.writeValueAsString(LocalDateTime.MIN.toInstant(ZoneOffset.UTC).minusSeconds(1))); + } + + // `Instant.toString()` writes 0, 3, 6 or 9 sub-second digits; only the + // zero case may be rewritten, the rest must be passed through untouched + @Test + public void testInstantSubSecondWidthsPreserved() throws Exception + { + assertEquals(q("1970-01-01T00:00:00.000000001Z"), + MAPPER.writeValueAsString(Instant.ofEpochSecond(0, 1))); + assertEquals(q("1970-01-01T00:00:00.000001Z"), + MAPPER.writeValueAsString(Instant.ofEpochSecond(0, 1000))); + assertEquals(q("1970-01-01T00:00:00.001Z"), + MAPPER.writeValueAsString(Instant.ofEpochSecond(0, 1000000))); + } + + // `@JsonFormat(shape=STRING)` without a pattern must not lose the padding + @Test + public void testShapeStringWithoutPattern() throws Exception + { + assertEquals(a2q("{'value':'2017-09-14T04:28:48.000Z'}"), + MAPPER.writeValueAsString(new ShapeOnlyWrapper(Instant.parse("2017-09-14T04:28:48Z")))); + } +} diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerTest.java index 9f645d5c..913c77ef 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerTest.java @@ -3,7 +3,7 @@ import java.time.Duration; import java.time.temporal.TemporalAmount; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; @@ -13,7 +13,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class DurationSerTest extends ModuleTestBase { @@ -146,8 +146,7 @@ public void testSerializationWithTypeInfo01() throws Exception Duration duration = Duration.ofSeconds(13498L, 8374); String value = mapper.writeValueAsString(duration); - assertEquals( - "[\"" + Duration.class.getName() + "\",13498.000008374]", value); + assertEquals("[\"" + Duration.class.getName() + "\",13498.000008374]", value); } @Test @@ -161,8 +160,7 @@ public void testSerializationWithTypeInfo02() throws Exception Duration duration = Duration.ofSeconds(13498L, 837481723); String value = mapper.writeValueAsString(duration); - assertEquals( - "[\"" + Duration.class.getName() + "\",13498837]", value); + assertEquals("[\"" + Duration.class.getName() + "\",13498837]", value); } @Test @@ -175,8 +173,7 @@ public void testSerializationWithTypeInfo03() throws Exception Duration duration = Duration.ofSeconds(13498L, 8374); String value = mapper.writeValueAsString(duration); - assertEquals( - "[\"" + Duration.class.getName() + "\",\"" + duration.toString() + "\"]", value); + assertEquals("[\"" + Duration.class.getName() + "\",\"" + duration.toString() + "\"]", value); } /* diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java index 343efd83..951f0eb9 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/InstantSerTest.java @@ -16,9 +16,6 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -29,7 +26,10 @@ import java.time.Instant; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; -import org.junit.Test; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; public class InstantSerTest extends ModuleTestBase { @@ -46,8 +46,8 @@ public void testSerializationAsTimestamp01Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", NO_NANOSECS_SER, value); + assertNotNull(value); + assertEquals(NO_NANOSECS_SER, value); } @Test @@ -58,7 +58,7 @@ public void testSerializationAsTimestamp01Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "0", value); + assertEquals("0", value); } @Test @@ -69,7 +69,7 @@ public void testSerializationAsTimestamp02Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "123456789.183917322", value); + assertEquals("123456789.183917322", value); } @Test @@ -80,7 +80,7 @@ public void testSerializationAsTimestamp02Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "123456789183", value); + assertEquals("123456789183", value); } @Test @@ -91,7 +91,7 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), value); + assertEquals(DecimalUtils.toDecimal(date.getEpochSecond(), date.getNano()), value); } @Test @@ -102,7 +102,7 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", Long.toString(date.toEpochMilli()), value); + assertEquals(Long.toString(date.toEpochMilli()), value); } @Test @@ -112,7 +112,7 @@ public void testSerializationAsString01() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -122,7 +122,7 @@ public void testSerializationAsString02() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -132,7 +132,7 @@ public void testSerializationAsString03() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -144,7 +144,7 @@ public void testSerializationWithTypeInfo01() throws Exception .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); m.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = m.writeValueAsString(date); - assertEquals("The value is not correct.", "[\"" + Instant.class.getName() + "\",123456789.183917322]", value); + assertEquals("[\"" + Instant.class.getName() + "\",123456789.183917322]", value); } @Test @@ -156,7 +156,7 @@ public void testSerializationWithTypeInfo02() throws Exception .configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); m.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = m.writeValueAsString(date); - assertEquals("The value is not correct.", "[\"" + Instant.class.getName() + "\",123456789183]", value); + assertEquals("[\"" + Instant.class.getName() + "\",123456789183]", value); } @Test @@ -167,8 +167,7 @@ public void testSerializationWithTypeInfo03() throws Exception .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); m.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = m.writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); + assertEquals("[\"" + Instant.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } static class Pojo1 { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerTest.java index 03703859..1ecee663 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateSerTest.java @@ -20,6 +20,8 @@ import java.time.Month; import java.time.temporal.Temporal; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonTypeInfo; @@ -28,10 +30,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.*; public class LocalDateSerTest extends ModuleTestBase @@ -74,8 +73,8 @@ public void testSerializationAsTimestamp01() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[1986,1,17]", value); + assertNotNull(value); + assertEquals("[1986,1,17]", value); } @Test @@ -86,8 +85,8 @@ public void testSerializationAsTimestamp02() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2013,8,21]", value); + assertNotNull(value); + assertEquals("[2013,8,21]", value); } @Test @@ -98,8 +97,8 @@ public void testSerializationAsString01() throws Exception .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + date.toString() + '"', value); } @Test @@ -109,8 +108,8 @@ public void testSerializationAsString02() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + date.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + date.toString() + '"', value); } @Test @@ -122,9 +121,8 @@ public void testSerializationWithTypeInfo01() throws Exception LocalDate date = LocalDate.of(2005, Month.NOVEMBER, 5); String value = mapper.writeValueAsString(date); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + LocalDate.class.getName() + "\",\"" + date.toString() + "\"]", value); } // [modules-java8#46] diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerTest.java index a34a71ec..f7e66009 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalDateTimeSerTest.java @@ -21,6 +21,8 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -29,9 +31,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.*; public class LocalDateTimeSerTest extends ModuleTestBase @@ -49,7 +49,7 @@ static class LDTWrapper { public void testSerializationAsTimestamp01() throws Exception { LocalDateTime time = LocalDateTime.of(1986, Month.JANUARY, 17, 15, 43); - assertEquals("The value is not correct.", "[1986,1,17,15,43]", + assertEquals("[1986,1,17,15,43]", MAPPER.writeValueAsString(time)); } @@ -59,7 +59,7 @@ public void testSerializationAsTimestamp02() throws Exception LocalDateTime time = LocalDateTime.of(2013, Month.AUGUST, 21, 9, 22, 57); String value = MAPPER.writeValueAsString(time); - assertEquals("The value is not correct.", "[2013,8,21,9,22,57]", value); + assertEquals("[2013,8,21,9,22,57]", value); } @Test @@ -70,7 +70,7 @@ public void testSerializationAsTimestamp03Nanosecond() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(time); - assertEquals("The value is not correct.", "[2013,8,21,9,22,0,57]", value); + assertEquals("[2013,8,21,9,22,0,57]", value); } @Test @@ -81,7 +81,7 @@ public void testSerializationAsTimestamp03Millisecond() throws Exception ObjectMapper m = newMapper().disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); String value = m.writeValueAsString(time); - assertEquals("The value is not correct.", "[2013,8,21,9,22,0,0]", value); + assertEquals("[2013,8,21,9,22,0,0]", value); } @Test @@ -93,7 +93,7 @@ public void testSerializationAsTimestamp04Nanosecond() throws Exception .enable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); String value = m.writeValueAsString(time); - assertEquals("The value is not correct.", "[2005,11,5,22,31,5,829837]", value); + assertEquals("[2005,11,5,22,31,5,829837]", value); } @Test @@ -105,7 +105,7 @@ public void testSerializationAsTimestamp04Millisecond() throws Exception .disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); String value = m.writeValueAsString(time); - assertEquals("The value is not correct.", "[2005,11,5,22,31,5,422]", value); + assertEquals("[2005,11,5,22,31,5,422]", value); } @Test @@ -117,8 +117,8 @@ public void testSerializationAsString01() throws Exception m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "\"1986-01-17T15:43:05\"", value); + assertNotNull(value); + assertEquals("\"1986-01-17T15:43:05\"", value); } @Test @@ -130,8 +130,8 @@ public void testSerializationAsString02() throws Exception m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -143,8 +143,8 @@ public void testSerializationAsString03() throws Exception m.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -171,8 +171,7 @@ public void testSerializationWithTypeInfo01() throws Exception m.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = m.writeValueAsString(time); - assertEquals("The value is not correct.", - "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", value); + assertEquals("[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,829837]]", value); } @Test @@ -186,8 +185,7 @@ public void testSerializationWithTypeInfo02() throws Exception LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 422829837); String value = m.writeValueAsString(time); - assertEquals("The value is not correct.", - "[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", value); + assertEquals("[\"" + LocalDateTime.class.getName() + "\",[2005,11,5,22,31,5,422]]", value); } @Test @@ -199,8 +197,7 @@ public void testSerializationWithTypeInfo03() throws Exception LocalDateTime time = LocalDateTime.of(2005, Month.NOVEMBER, 5, 22, 31, 5, 829837); String value = m.writeValueAsString(time); - assertEquals("The value is not correct.", - "[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + assertEquals("[\"" + LocalDateTime.class.getName() + "\",\"" + time.toString() + "\"]", value); } // [modules-java8#288] diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerTest.java index 91d7e8de..d582661e 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/LocalTimeSerTest.java @@ -20,8 +20,7 @@ import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; @@ -30,7 +29,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; @SuppressWarnings("serial") public class LocalTimeSerTest extends ModuleTestBase @@ -58,24 +57,24 @@ public void testSerializationAsTimestamp01() throws Exception { String json = writer.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(LocalTime.of(15, 43)); - assertEquals("The value is not correct.", "[15,43]", json); + assertEquals("[15,43]", json, "The value is not correct."); } @Test public void testSerializationAsTimestamp02() throws Exception { String json = writer.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) - .writeValueAsString( LocalTime.of(9, 22, 57)); - assertEquals("The value is not correct.", "[9,22,57]", json); + .writeValueAsString(LocalTime.of(9, 22, 57)); + assertEquals("[9,22,57]", json, "The value is not correct."); } @Test public void testSerializationAsTimestamp03Nanoseconds() throws Exception { String json = writer.with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, - SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) - .writeValueAsString(LocalTime.of(9, 22, 0, 57)); - assertEquals("The value is not correct.", "[9,22,0,57]", json); + SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) + .writeValueAsString(LocalTime.of(9, 22, 0, 57)); + assertEquals("[9,22,0,57]", json, "The value is not correct."); } @Test @@ -88,7 +87,7 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = mapper.writeValueAsString(time); - assertEquals("The value is not correct.", "[9,22,0,0]", value); + assertEquals("[9,22,0,0]", value, "The value is not correct."); } @Test @@ -101,7 +100,7 @@ public void testSerializationAsTimestamp04Nanoseconds() throws Exception mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); String value = mapper.writeValueAsString(time); - assertEquals("The value is not correct.", "[22,31,5,829837]", value); + assertEquals("[22,31,5,829837]", value, "The value is not correct."); } @Test @@ -114,8 +113,8 @@ public void testSerializationAsTimestamp04Milliseconds() throws Exception mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); String value = mapper.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[22,31,5,422]", value); + assertNotNull(value, "The value should not be null."); + assertEquals("[22,31,5,422]", value, "The value is not correct."); } @Test @@ -127,7 +126,7 @@ public void testSerializationAsString01() throws Exception mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = mapper.writeValueAsString(time); - assertEquals("The value is not correct.", "\"15:43:20\"", value); + assertEquals("\"15:43:20\"", value, "The value is not correct."); } @Test @@ -139,7 +138,7 @@ public void testSerializationAsString02() throws Exception mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = mapper.writeValueAsString(time); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertEquals('"' + time.toString() + '"', value, "The value is not correct."); } @Test @@ -151,8 +150,8 @@ public void testSerializationAsString03() throws Exception m.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); String value = m.writeValueAsString(time); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertNotNull(value, "The value should not be null."); + assertEquals('"' + time.toString() + '"', value, "The value is not correct."); } // [modules-java8#115] @@ -160,9 +159,9 @@ public void testSerializationAsString03() throws Exception public void testWithCustomSerializer() throws Exception { String json = MAPPER.writeValueAsString(new CustomWrapper(LocalTime.of(15, 43))); - assertEquals("The value is not correct.", "{\"value\":\"15/43\"}", json); + assertEquals("{\"value\":\"15/43\"}", json, "The value is not correct."); } - + @Test public void testSerializationWithTypeInfo01() throws Exception { @@ -173,9 +172,8 @@ public void testSerializationWithTypeInfo01() throws Exception m.addMixIn(Temporal.class, MockObjectConfiguration.class); String json = m.writeValueAsString(time); - assertEquals("The value is not correct.", - "[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", - json); + assertEquals("[\"" + LocalTime.class.getName() + "\",[22,31,5,829837]]", json, + "The value is not correct."); } @Test @@ -188,9 +186,8 @@ public void testSerializationWithTypeInfo02() throws Exception m.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS); m.addMixIn(Temporal.class, MockObjectConfiguration.class); String json = m.writeValueAsString(time); - assertEquals("The value is not correct.", - "[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", - json); + assertEquals("[\"" + LocalTime.class.getName() + "\",[22,31,5,422]]", json, + "The value is not correct."); } @Test @@ -202,7 +199,7 @@ public void testSerializationWithTypeInfo03() throws Exception m.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = m.writeValueAsString(time); - assertEquals("The value is not correct.", - "[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + assertEquals("[\"" + LocalTime.class.getName() + "\",\"" + time.toString() + "\"]", value, + "The value is not correct."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerTest.java index 8f73238f..5693bf40 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/MonthDaySerTest.java @@ -20,14 +20,14 @@ import java.time.MonthDay; import java.time.temporal.TemporalAccessor; -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class MonthDaySerTest extends ModuleTestBase @@ -37,14 +37,14 @@ public class MonthDaySerTest @Test public void testSerialization01() throws Exception { - assertEquals("The value is not correct.", "\"--01-17\"", + assertEquals("\"--01-17\"", MAPPER.writeValueAsString(MonthDay.of(Month.JANUARY, 17))); } @Test public void testSerialization02() throws Exception { - assertEquals("The value is not correct.", "\"--08-21\"", + assertEquals("\"--08-21\"", MAPPER.writeValueAsString(MonthDay.of(Month.AUGUST, 21))); } @@ -56,6 +56,6 @@ public void testSerializationWithTypeInfo01() throws Exception mapper.addMixIn(TemporalAccessor.class, MockObjectConfiguration.class); MonthDay monthDay = MonthDay.of(Month.NOVEMBER, 5); String value = mapper.writeValueAsString(monthDay); - assertEquals("The value is not correct.", "[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", value); + assertEquals("[\"" + MonthDay.class.getName() + "\",\"--11-05\"]", value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java index 77811bf3..689d8644 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetDateTimeSerTest.java @@ -1,24 +1,27 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.junit.Assert.assertEquals; - import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; import java.time.temporal.Temporal; import java.util.TimeZone; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.DecimalUtils; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + public class OffsetDateTimeSerTest extends ModuleTestBase { @@ -50,7 +53,7 @@ public void testSerializationAsTimestamp01Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "0.0", value); + assertEquals("0.0", value); } @Test @@ -61,7 +64,7 @@ public void testSerializationAsTimestamp01Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "0", value); + assertEquals("0", value); } @Test @@ -72,7 +75,7 @@ public void testSerializationAsTimestamp02Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "123456789.183917322", value); + assertEquals("123456789.183917322", value); } @Test @@ -83,7 +86,7 @@ public void testSerializationAsTimestamp02Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "123456789183", value); + assertEquals("123456789183", value); } @Test @@ -94,7 +97,7 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); + assertEquals(DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); } @Test @@ -105,7 +108,7 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", Long.toString(date.toInstant().toEpochMilli()), value); + assertEquals(Long.toString(date.toInstant().toEpochMilli()), value); } @Test @@ -115,7 +118,7 @@ public void testSerializationAsString01() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + assertEquals('"' + FORMATTER.withZone(Z1).format(date) + '"', value); } @@ -126,7 +129,7 @@ public void testSerializationAsString02() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + assertEquals('"' + FORMATTER.withZone(Z2).format(date) + '"', value); } @@ -137,7 +140,7 @@ public void testSerializationAsString03() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + assertEquals('"' + FORMATTER.withZone(Z3).format(date) + '"', value); } @@ -167,7 +170,7 @@ public void testSerializationAsStringWithMapperTimeZone01() throws Exception .writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -179,7 +182,7 @@ public void testSerializationAsStringWithMapperTimeZone02() throws Exception .writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -191,7 +194,7 @@ public void testSerializationAsStringWithMapperTimeZone03() throws Exception .writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -204,8 +207,7 @@ public void testSerializationWithTypeInfo01() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", value); + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",123456789.183917322]", value); } @Test @@ -218,8 +220,7 @@ public void testSerializationWithTypeInfo02() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",123456789183]", value); + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",123456789183]", value); } @Test @@ -230,8 +231,7 @@ public void testSerializationWithTypeInfo03() throws Exception .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .addMixIn(Temporal.class, MockObjectConfiguration.class) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",\"" + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.withZone(Z3).format(date) + "\"]", value); } @@ -245,8 +245,7 @@ public void testSerializationWithTypeInfoAndMapperTimeZone() throws Exception .addMixIn(Temporal.class, MockObjectConfiguration.class) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); + assertEquals("[\"" + OffsetDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } @Test @@ -260,7 +259,7 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOn() t .writeValueAsString(date); // We expect to have the date written with the ZoneId Z2 - assertEquals("The value is incorrect", "\"" + FORMATTER.format(date.atZoneSameInstant(Z2)) + "\"", value); + assertEquals("\"" + FORMATTER.format(date.atZoneSameInstant(Z2)) + "\"", value); } @Test @@ -274,7 +273,7 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff() .writeValueAsString(date); // We expect to have the date written with the ZoneId Z3 - assertEquals("The value is incorrect", "\"" + FORMATTER.format(date) + "\"", value); + assertEquals("\"" + FORMATTER.format(date) + "\"", value); } static class Pojo1 { @@ -288,4 +287,102 @@ public void testShapeInt() throws Exception { String json1 = newMapper().writeValueAsString(new Pojo1()); assertEquals("{\"t1\":1651053600000,\"t2\":1651053600.000000000}", json1); } + + /* + /********************************************************************** + /* Tests for custom formatter (modules-java8#376) + /********************************************************************** + */ + + @Test + public void testSerializationWithCustomFormatter() throws Exception + { + // Create a custom formatter that displays only 3 digits of nano-seconds instead of 9 + // Use ISO_LOCAL_DATE and ISO_LOCAL_TIME separately to control nanosecond precision + DateTimeFormatter customFormatter = new DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(java.time.temporal.ChronoField.MINUTE_OF_HOUR, 2) + .optionalStart() + .appendLiteral(':') + .appendValue(java.time.temporal.ChronoField.SECOND_OF_MINUTE, 2) + .optionalStart() + .appendFraction(java.time.temporal.ChronoField.NANO_OF_SECOND, 3, 3, true) + .optionalEnd() + .optionalEnd() + .appendOffsetId() + .toFormatter(); + + // Create a date with nanoseconds (123456789 nanos = .123456789 seconds) + OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.UTC); + String json = _mapper(customFormatter).writeValueAsString(date); + + // Should output with only 3 digits of nano precision (.123 instead of .123456789) + assertEquals(q("2025-01-01T22:01:05.123Z"), json); + } + + @Test + public void testSerializationWithCustomFormatterNoNanos() throws Exception + { + // Create a formatter without nanoseconds + DateTimeFormatter customFormatter = new DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(java.time.temporal.ChronoField.MINUTE_OF_HOUR, 2) + .optionalStart() + .appendLiteral(':') + .appendValue(java.time.temporal.ChronoField.SECOND_OF_MINUTE, 2) + .optionalEnd() + .appendOffsetId() + .toFormatter(); + + OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.UTC); + String json = _mapper(customFormatter).writeValueAsString(date); + + // Should output without nanoseconds + assertEquals(q("2025-01-01T22:01:05Z"), json); + } + + @Test + public void testSerializationWithCustomFormatterAndOffset() throws Exception + { + // Create a custom formatter that displays only 3 digits of nano-seconds + DateTimeFormatter customFormatter = new DateTimeFormatterBuilder() + .append(DateTimeFormatter.ISO_LOCAL_DATE) + .appendLiteral('T') + .appendValue(java.time.temporal.ChronoField.HOUR_OF_DAY, 2) + .appendLiteral(':') + .appendValue(java.time.temporal.ChronoField.MINUTE_OF_HOUR, 2) + .optionalStart() + .appendLiteral(':') + .appendValue(java.time.temporal.ChronoField.SECOND_OF_MINUTE, 2) + .optionalStart() + .appendFraction(java.time.temporal.ChronoField.NANO_OF_SECOND, 3, 3, true) + .optionalEnd() + .optionalEnd() + .appendOffsetId() + .toFormatter(); + + + // Create a date with a non-UTC offset + OffsetDateTime date = OffsetDateTime.of(2025, 1, 1, 22, 1, 5, 123456789, ZoneOffset.ofHours(5)); + String json = _mapper(customFormatter).writeValueAsString(date); + + // Should output with offset +05:00 and 3 digits of nano precision + assertEquals(q("2025-01-01T22:01:05.123+05:00"), json); + } + + private ObjectMapper _mapper(DateTimeFormatter dtf) { + OffsetDateTimeSerializer customSerializer = OffsetDateTimeSerializer.INSTANCE + .withFormatter(dtf); + SimpleModule customModule = new SimpleModule("CustomOffsetDateTimeModule") + .addSerializer(OffsetDateTime.class, customSerializer); + return mapperBuilder() + .addModule(customModule) + .build(); + } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerTest.java index c1e9e5ad..fa31d8a5 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OffsetTimeSerTest.java @@ -20,13 +20,13 @@ import java.time.ZoneOffset; import java.time.temporal.Temporal; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class OffsetTimeSerTest extends ModuleTestBase { @@ -39,7 +39,7 @@ public void testSerializationAsTimestamp01() throws Exception String value = MAPPER.writer() .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(time); - assertEquals("The value is not correct.", "[15,43,\"+03:00\"]", value); + assertEquals("[15,43,\"+03:00\"]", value); } @Test @@ -49,7 +49,7 @@ public void testSerializationAsTimestamp02() throws Exception String value = MAPPER.writer() .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(time); - assertEquals("The value is not correct.", "[9,22,57,\"-06:30\"]", value); + assertEquals("[9,22,57,\"-06:30\"]", value); } @Test @@ -60,7 +60,7 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(time); - assertEquals("The value is not correct.", "[9,22,0,57,\"-06:30\"]", value); + assertEquals("[9,22,0,57,\"-06:30\"]", value); } @Test @@ -71,7 +71,7 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(time); - assertEquals("The value is not correct.", "[9,22,0,0,\"-06:30\"]", value); + assertEquals("[9,22,0,0,\"-06:30\"]", value); } @Test @@ -82,7 +82,7 @@ public void testSerializationAsTimestamp04Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(time); - assertEquals("The value is not correct.", "[22,31,5,829837,\"+11:00\"]", value); + assertEquals("[22,31,5,829837,\"+11:00\"]", value); } @Test @@ -93,7 +93,7 @@ public void testSerializationAsTimestamp04Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(time); - assertEquals("The value is not correct.", "[22,31,5,422,\"+11:00\"]", value); + assertEquals("[22,31,5,422,\"+11:00\"]", value); } @Test @@ -103,7 +103,7 @@ public void testSerializationAsString01() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(time); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -113,7 +113,7 @@ public void testSerializationAsString02() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(time); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -123,7 +123,7 @@ public void testSerializationAsString03() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(time); - assertEquals("The value is not correct.", '"' + time.toString() + '"', value); + assertEquals('"' + time.toString() + '"', value); } @Test @@ -136,8 +136,7 @@ public void testSerializationWithTypeInfo01() throws Exception mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, true); mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); - assertEquals("The value is not correct.", - "[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", + assertEquals("[\"" + OffsetTime.class.getName() + "\",[22,31,5,829837,\"+11:00\"]]", mapper.writeValueAsString(time)); } @@ -151,8 +150,7 @@ public void testSerializationWithTypeInfo02() throws Exception mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false); mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); - assertEquals("The value is not correct.", - "[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", + assertEquals("[\"" + OffsetTime.class.getName() + "\",[22,31,5,422,\"+11:00\"]]", mapper.writeValueAsString(time)); } @@ -166,7 +164,6 @@ public void testSerializationWithTypeInfo03() throws Exception mapper.addMixIn(Temporal.class, MockObjectConfiguration.class); String value = mapper.writeValueAsString(time); - assertEquals("The value is not correct.", - "[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", value); + assertEquals("[\"" + OffsetTime.class.getName() + "\",\"" + time.toString() + "\"]", value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OneBasedMonthSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OneBasedMonthSerTest.java index 7c1a3a4a..fffe87f7 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OneBasedMonthSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/OneBasedMonthSerTest.java @@ -1,10 +1,8 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.junit.Assert.assertEquals; - import java.time.Month; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -13,6 +11,8 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + public class OneBasedMonthSerTest extends ModuleTestBase { static class Wrapper { @@ -47,7 +47,6 @@ public void testSerializationFromEnumWithPattern_zeroBased() throws Exception assertEquals( "{\"month\":0}" , mapper.writeValueAsString(new Wrapper(Month.JANUARY))); } - private JsonMapper mapperForZeroBased() { return JsonMapper.builder() .addModule(new JavaTimeModule().disable(JavaTimeFeature.ONE_BASED_MONTHS)) @@ -59,5 +58,4 @@ private JsonMapper mapperForOneBased() { .addModule(new JavaTimeModule().enable(JavaTimeFeature.ONE_BASED_MONTHS)) .build(); } - } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/PeriodSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/PeriodSerTest.java index 2a629abb..ebd8d6b3 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/PeriodSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/PeriodSerTest.java @@ -16,16 +16,16 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.junit.Assert.assertEquals; - import java.time.Period; import java.time.temporal.TemporalAmount; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class PeriodSerTest extends ModuleTestBase { @@ -51,8 +51,7 @@ public void testSerializationWithTypeInfo01() throws Exception .addMixIn(TemporalAmount.class, MockObjectConfiguration.class) .build(); String value = mapper.writeValueAsString(period); - assertEquals( - "[" + q(Period.class.getName()) + ",\"P5Y1M12D\"]", value); + assertEquals("[" + q(Period.class.getName()) + ",\"P5Y1M12D\"]", value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateSerializationWithCustomFormatter.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateSerializationWithCustomFormatter.java index 553f4abc..975b3cc0 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateSerializationWithCustomFormatter.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateSerializationWithCustomFormatter.java @@ -2,34 +2,25 @@ import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; +import java.util.stream.Stream; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.MatcherAssert.assertThat; - -import static org.hamcrest.core.StringContains.containsString; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -@RunWith(Parameterized.class) -public class TestLocalDateSerializationWithCustomFormatter { - private final DateTimeFormatter formatter; +import static org.junit.jupiter.api.Assertions.*; - public TestLocalDateSerializationWithCustomFormatter(DateTimeFormatter formatter) { - this.formatter = formatter; - } +public class TestLocalDateSerializationWithCustomFormatter { - @Test - public void testSerialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testSerialization(DateTimeFormatter formatter) throws Exception { LocalDate date = LocalDate.now(); - assertThat(serializeWith(date, formatter), containsString(date.format(formatter))); + assertTrue(serializeWith(date, formatter).contains(date.format(formatter)), + "Serialized value should contain the formatted date"); } private String serializeWith(LocalDate date, DateTimeFormatter f) throws Exception { @@ -38,10 +29,12 @@ private String serializeWith(LocalDate date, DateTimeFormatter f) throws Excepti return mapper.writeValueAsString(date); } - @Test - public void testDeserialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testDeserialization(DateTimeFormatter formatter) throws Exception { LocalDate date = LocalDate.now(); - assertThat(deserializeWith(date.format(formatter), formatter), equalTo(date)); + assertEquals(date, deserializeWith(date.format(formatter), formatter), + "Deserialized value should match the original date"); } private LocalDate deserializeWith(String json, DateTimeFormatter f) throws Exception { @@ -50,15 +43,14 @@ private LocalDate deserializeWith(String json, DateTimeFormatter f) throws Excep return mapper.readValue("\"" + json + "\"", LocalDate.class); } - @Parameters - public static Collection customFormatters() { - Collection formatters = new ArrayList<>(); - formatters.add(new Object[]{DateTimeFormatter.BASIC_ISO_DATE}); - formatters.add(new Object[]{DateTimeFormatter.ISO_DATE}); - formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE}); - formatters.add(new Object[]{DateTimeFormatter.ISO_ORDINAL_DATE}); - formatters.add(new Object[]{DateTimeFormatter.ISO_WEEK_DATE}); - formatters.add(new Object[]{DateTimeFormatter.ofPattern("MM/dd/yyyy")}); - return formatters; + static Stream customFormatters() { + return Stream.of( + DateTimeFormatter.BASIC_ISO_DATE, + DateTimeFormatter.ISO_DATE, + DateTimeFormatter.ISO_LOCAL_DATE, + DateTimeFormatter.ISO_ORDINAL_DATE, + DateTimeFormatter.ISO_WEEK_DATE, + DateTimeFormatter.ofPattern("MM/dd/yyyy") + ); } -} +} \ No newline at end of file diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateTimeSerializationWithCustomFormatter.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateTimeSerializationWithCustomFormatter.java index 27de3d8e..bcd1562e 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateTimeSerializationWithCustomFormatter.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalDateTimeSerializationWithCustomFormatter.java @@ -2,33 +2,24 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; +import java.util.stream.Stream; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.core.StringContains.containsString; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -@RunWith(Parameterized.class) -public class TestLocalDateTimeSerializationWithCustomFormatter { - private final DateTimeFormatter formatter; +import static org.junit.jupiter.api.Assertions.*; - public TestLocalDateTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) { - this.formatter = formatter; - } +public class TestLocalDateTimeSerializationWithCustomFormatter { - @Test - public void testSerialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testSerialization(DateTimeFormatter formatter) throws Exception { LocalDateTime dateTime = LocalDateTime.now(); - assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter))); + assertTrue(serializeWith(dateTime, formatter).contains(dateTime.format(formatter))); } private String serializeWith(LocalDateTime dateTime, DateTimeFormatter f) throws Exception { @@ -36,10 +27,11 @@ private String serializeWith(LocalDateTime dateTime, DateTimeFormatter f) throws return mapper.writeValueAsString(dateTime); } - @Test - public void testDeserialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testDeserialization(DateTimeFormatter formatter) throws Exception { LocalDateTime dateTime = LocalDateTime.now(); - assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime)); + assertEquals(dateTime, deserializeWith(dateTime.format(formatter), formatter)); } private LocalDateTime deserializeWith(String json, DateTimeFormatter f) throws Exception { @@ -47,11 +39,10 @@ private LocalDateTime deserializeWith(String json, DateTimeFormatter f) throws E return mapper.readValue("\"" + json + "\"", LocalDateTime.class); } - @Parameters - public static Collection customFormatters() { - Collection formatters = new ArrayList<>(); - formatters.add(new Object[]{DateTimeFormatter.ISO_DATE_TIME}); - formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE_TIME}); - return formatters; + static Stream customFormatters() { + return Stream.of( + DateTimeFormatter.ISO_DATE_TIME, + DateTimeFormatter.ISO_LOCAL_DATE_TIME + ); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalTimeSerializationWithCustomFormatter.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalTimeSerializationWithCustomFormatter.java index e5503c72..1cf70568 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalTimeSerializationWithCustomFormatter.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestLocalTimeSerializationWithCustomFormatter.java @@ -1,34 +1,26 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.core.StringContains.containsString; + +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import java.time.LocalTime; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; +import static org.junit.jupiter.api.Assertions.*; -@RunWith(Parameterized.class) public class TestLocalTimeSerializationWithCustomFormatter { - private final DateTimeFormatter formatter; - - public TestLocalTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) { - this.formatter = formatter; - } - @Test - public void testSerialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testSerialization(DateTimeFormatter formatter) throws Exception { LocalTime dateTime = LocalTime.now(); - assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter))); + assertTrue(serializeWith(dateTime, formatter).contains(dateTime.format(formatter))); } private String serializeWith(LocalTime dateTime, DateTimeFormatter f) throws Exception { @@ -37,10 +29,11 @@ private String serializeWith(LocalTime dateTime, DateTimeFormatter f) throws Exc return mapper.writeValueAsString(dateTime); } - @Test - public void testDeserialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testDeserialization(DateTimeFormatter formatter) throws Exception { LocalTime dateTime = LocalTime.now(); - assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime)); + assertEquals(dateTime, deserializeWith(dateTime.format(formatter), formatter)); } private LocalTime deserializeWith(String json, DateTimeFormatter f) throws Exception { @@ -49,11 +42,10 @@ private LocalTime deserializeWith(String json, DateTimeFormatter f) throws Excep return mapper.readValue("\"" + json + "\"", LocalTime.class); } - @Parameters - public static Collection customFormatters() { - Collection formatters = new ArrayList<>(); - formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_TIME}); - formatters.add(new Object[]{DateTimeFormatter.ISO_TIME}); - return formatters; + static Stream customFormatters() { + return Stream.of( + DateTimeFormatter.ISO_LOCAL_TIME, + DateTimeFormatter.ISO_TIME + ); } -} +} \ No newline at end of file diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearMonthSerializationWithCustomFormatter.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearMonthSerializationWithCustomFormatter.java index c4e658a4..f59a18e8 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearMonthSerializationWithCustomFormatter.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearMonthSerializationWithCustomFormatter.java @@ -1,34 +1,25 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.hamcrest.core.StringContains.containsString; +import java.time.YearMonth; +import java.time.format.DateTimeFormatter; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.YearMonthDeserializer; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import java.time.YearMonth; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; +import static org.junit.jupiter.api.Assertions.*; -@RunWith(Parameterized.class) public class TestYearMonthSerializationWithCustomFormatter { - private final DateTimeFormatter formatter; - - public TestYearMonthSerializationWithCustomFormatter(DateTimeFormatter formatter) { - this.formatter = formatter; - } - @Test - public void testSerialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testSerialization(DateTimeFormatter formatter) throws Exception { YearMonth dateTime = YearMonth.now(); - assertThat(serializeWith(dateTime, formatter), containsString(dateTime.format(formatter))); + assertTrue(serializeWith(dateTime, formatter).contains(dateTime.format(formatter))); } private String serializeWith(YearMonth dateTime, DateTimeFormatter f) throws Exception { @@ -37,10 +28,11 @@ private String serializeWith(YearMonth dateTime, DateTimeFormatter f) throws Exc return mapper.writeValueAsString(dateTime); } - @Test - public void testDeserialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testDeserialization(DateTimeFormatter formatter) throws Exception { YearMonth dateTime = YearMonth.now(); - assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime)); + assertEquals(dateTime, deserializeWith(dateTime.format(formatter), formatter)); } private YearMonth deserializeWith(String json, DateTimeFormatter f) throws Exception { @@ -49,11 +41,10 @@ private YearMonth deserializeWith(String json, DateTimeFormatter f) throws Excep return mapper.readValue("\"" + json + "\"", YearMonth.class); } - @Parameters - public static Collection customFormatters() { - Collection formatters = new ArrayList<>(); - formatters.add(new Object[]{DateTimeFormatter.ofPattern("uuuu-MM")}); - formatters.add(new Object[]{DateTimeFormatter.ofPattern("uu-M")}); - return formatters; + static Stream customFormatters() { + return Stream.of( + DateTimeFormatter.ofPattern("uuuu-MM"), + DateTimeFormatter.ofPattern("uu-M") + ); } -} +} \ No newline at end of file diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearSerializationWithCustomFormatter.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearSerializationWithCustomFormatter.java index 1bdd6c2b..93a1b130 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearSerializationWithCustomFormatter.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestYearSerializationWithCustomFormatter.java @@ -1,34 +1,27 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.IsEqual.equalTo; + +import java.time.Year; +import java.time.format.DateTimeFormatter; +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.datatype.jsr310.deser.YearDeserializer; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import java.time.Year; -import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; +import static org.junit.jupiter.api.Assertions.*; -@RunWith(Parameterized.class) public class TestYearSerializationWithCustomFormatter { - private final DateTimeFormatter formatter; - - public TestYearSerializationWithCustomFormatter(DateTimeFormatter formatter) { - this.formatter = formatter; - } - @Test - public void testSerialization() throws Exception { + @ParameterizedTest + @MethodSource("customFormatters") + void testSerialization(DateTimeFormatter formatter) throws Exception { Year year = Year.now(); String expected = "\"" + year.format(formatter) + "\""; - assertThat(serializeWith(year, formatter), equalTo(expected)); + assertEquals(expected, serializeWith(year, formatter)); } private String serializeWith(Year dateTime, DateTimeFormatter f) throws Exception { @@ -37,10 +30,11 @@ private String serializeWith(Year dateTime, DateTimeFormatter f) throws Exceptio return mapper.writeValueAsString(dateTime); } - @Test - public void testDeserialization() throws Exception { - Year dateTime = Year.now(); - assertThat(deserializeWith(dateTime.format(formatter), formatter), equalTo(dateTime)); + @ParameterizedTest + @MethodSource("customFormatters") + void testDeserialization(DateTimeFormatter formatter) throws Exception { + Year year = Year.now(); + assertEquals(year, deserializeWith(year.format(formatter), formatter)); } private Year deserializeWith(String json, DateTimeFormatter f) throws Exception { @@ -49,11 +43,10 @@ private Year deserializeWith(String json, DateTimeFormatter f) throws Exception return mapper.readValue("\"" + json + "\"", Year.class); } - @Parameters - public static Collection customFormatters() { - Collection formatters = new ArrayList<>(); - formatters.add(new Object[]{DateTimeFormatter.ofPattern("yyyy")}); - formatters.add(new Object[]{DateTimeFormatter.ofPattern("yy")}); - return formatters; + static Stream customFormatters() { + return Stream.of( + DateTimeFormatter.ofPattern("yyyy"), + DateTimeFormatter.ofPattern("yy") + ); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestZonedDateTimeSerializationWithCustomFormatter.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestZonedDateTimeSerializationWithCustomFormatter.java index 283172c0..ce09a051 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestZonedDateTimeSerializationWithCustomFormatter.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/TestZonedDateTimeSerializationWithCustomFormatter.java @@ -4,34 +4,26 @@ import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.module.SimpleModule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; import java.util.TimeZone; +import java.util.stream.Stream; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.StringContains.containsString; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -@RunWith(Parameterized.class) -public class TestZonedDateTimeSerializationWithCustomFormatter { - private final DateTimeFormatter formatter; +import static org.junit.jupiter.api.Assertions.assertTrue; - public TestZonedDateTimeSerializationWithCustomFormatter(DateTimeFormatter formatter) { - this.formatter = formatter; - } - @Test - public void testSerialization() throws Exception { +public class TestZonedDateTimeSerializationWithCustomFormatter { + + @MethodSource("customFormatters") + @ParameterizedTest + public void testSerialization(DateTimeFormatter formatter) throws Exception { ZonedDateTime zonedDateTime = ZonedDateTime.now(); - assertThat(serializeWith(zonedDateTime, formatter), - containsString(zonedDateTime.format(formatter.withZone(ZoneOffset.UTC)))); + assertTrue(serializeWith(zonedDateTime, formatter).contains(zonedDateTime.format(formatter.withZone(ZoneOffset.UTC)))); } private String serializeWith(ZonedDateTime zonedDateTime, DateTimeFormatter f) throws Exception { @@ -44,13 +36,12 @@ private String serializeWith(ZonedDateTime zonedDateTime, DateTimeFormatter f) t return mapper.writeValueAsString(zonedDateTime); } - @Parameters - public static Collection customFormatters() { - Collection formatters = new ArrayList<>(); - formatters.add(new Object[]{DateTimeFormatter.ISO_ZONED_DATE_TIME}); - formatters.add(new Object[]{DateTimeFormatter.ISO_OFFSET_DATE_TIME}); - formatters.add(new Object[]{DateTimeFormatter.ISO_LOCAL_DATE_TIME}); - formatters.add(new Object[]{DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")}); - return formatters; + public static Stream customFormatters() { + return Stream.of( + DateTimeFormatter.ISO_ZONED_DATE_TIME, + DateTimeFormatter.ISO_OFFSET_DATE_TIME, + DateTimeFormatter.ISO_LOCAL_DATE_TIME, + DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ") + ); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteNanosecondsTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteNanosecondsTest.java index c1080f5a..5768fd15 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteNanosecondsTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteNanosecondsTest.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; import java.time.Duration; import java.time.Instant; @@ -15,8 +14,9 @@ import java.time.ZoneOffset; import java.time.ZonedDateTime; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.CoreMatchers.containsString; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; public class WriteNanosecondsTest extends ModuleTestBase { public static final ZoneId UTC = ZoneId.of("UTC"); @@ -28,8 +28,8 @@ public void testSerializeDurationWithAndWithoutNanoseconds() throws Exception { String json = MAPPER.writeValueAsString(value); - assertThat(json, containsString("\"nanoseconds\":0.0")); - assertThat(json, containsString("\"notNanoseconds\":0")); + assertTrue(json.contains("\"nanoseconds\":0.0")); + assertTrue(json.contains("\"notNanoseconds\":0")); } @Test @@ -38,8 +38,8 @@ public void testSerializeInstantWithAndWithoutNanoseconds() throws Exception { String json = MAPPER.writeValueAsString(input); - assertThat(json, containsString("\"nanoseconds\":0.0")); - assertThat(json, containsString("\"notNanoseconds\":0")); + assertTrue(json.contains("\"nanoseconds\":0.0")); + assertTrue(json.contains("\"notNanoseconds\":0")); } @Test @@ -51,8 +51,8 @@ public void testSerializeLocalDateTimeWithAndWithoutNanoseconds() throws Excepti String json = MAPPER.writeValueAsString(input); - assertThat(json, containsString("\"nanoseconds\":[1970,1,1,0,0,0,1]")); - assertThat(json, containsString("\"notNanoseconds\":[1970,1,1,0,0,0,0]")); + assertTrue(json.contains("\"nanoseconds\":[1970,1,1,0,0,0,1]")); + assertTrue(json.contains("\"notNanoseconds\":[1970,1,1,0,0,0,0]")); } @Test @@ -64,8 +64,8 @@ public void testSerializeLocalTimeWithAndWithoutNanoseconds() throws Exception { String json = MAPPER.writeValueAsString(input); - assertThat(json, containsString("\"nanoseconds\":[0,0,0,1]")); - assertThat(json, containsString("\"notNanoseconds\":[0,0,0,0]")); + assertTrue(json.contains("\"nanoseconds\":[0,0,0,1]")); + assertTrue(json.contains("\"notNanoseconds\":[0,0,0,0]")); } @Test @@ -74,8 +74,8 @@ public void testSerializeOffsetDateTimeWithAndWithoutNanoseconds() throws Except String json = MAPPER.writeValueAsString(input); - assertThat(json, containsString("\"nanoseconds\":0.0")); - assertThat(json, containsString("\"notNanoseconds\":0")); + assertTrue(json.contains("\"nanoseconds\":0.0")); + assertTrue(json.contains("\"notNanoseconds\":0")); } @Test @@ -87,8 +87,8 @@ public void testSerializeOffsetTimeWithAndWithoutNanoseconds() throws Exception String json = MAPPER.writeValueAsString(input); - assertThat(json, containsString("\"nanoseconds\":[0,0,0,1,\"Z\"]")); - assertThat(json, containsString("\"notNanoseconds\":[0,0,0,0,\"Z\"]")); + assertTrue(json.contains("\"nanoseconds\":[0,0,0,1,\"Z\"]")); + assertTrue(json.contains("\"notNanoseconds\":[0,0,0,0,\"Z\"]")); } @Test @@ -97,8 +97,8 @@ public void testSerializeZonedDateTimeWithAndWithoutNanoseconds() throws Excepti String json = MAPPER.writeValueAsString(input); - assertThat(json, containsString("\"nanoseconds\":0.0")); - assertThat(json, containsString("\"notNanoseconds\":0")); + assertTrue(json.contains("\"nanoseconds\":0.0")); + assertTrue(json.contains("\"notNanoseconds\":0")); } private static class DummyClass { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteZoneIdTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteZoneIdTest.java index f9c2c0e1..957965eb 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteZoneIdTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/WriteZoneIdTest.java @@ -5,6 +5,8 @@ import java.time.ZonedDateTime; import java.util.HashMap; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; @@ -12,10 +14,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class WriteZoneIdTest extends ModuleTestBase { @@ -39,7 +38,7 @@ public void testSerialization01() throws Exception { ZoneId id = ZoneId.of("America/Chicago"); String value = MAPPER.writeValueAsString(id); - assertEquals("The value is not correct.", "\"America/Chicago\"", value); + assertEquals("\"America/Chicago\"", value); } @Test @@ -47,7 +46,7 @@ public void testSerialization02() throws Exception { ZoneId id = ZoneId.of("America/Anchorage"); String value = MAPPER.writeValueAsString(id); - assertEquals("The value is not correct.", "\"America/Anchorage\"", value); + assertEquals("\"America/Anchorage\"", value); } @Test @@ -59,7 +58,7 @@ public void testSerializationWithTypeInfo01() throws Exception .addModule(new JavaTimeModule()) .build(); String value = mapper.writeValueAsString(id); - assertEquals("The value is not correct.", "[\"java.time.ZoneId\",\"America/Denver\"]", value); + assertEquals("[\"java.time.ZoneId\",\"America/Denver\"]", value); } @Test @@ -74,11 +73,11 @@ public void testJacksonAnnotatedPOJOWithDateWithTimezoneToJson() throws Exceptio // just verify appending of timezone id itself: String json = MAPPER.writeValueAsString(input); if (!json.contains("\"01-01-1970T")) { - Assert.fail("Should contain time prefix, did not: "+json); + fail("Should contain time prefix, did not: "+json); } String match = String.format("[%s]", ZONE_ID_STR); if (!json.contains(match)) { - Assert.fail("Should contain zone id "+match+", does not: "+json); + fail("Should contain zone id "+match+", does not: "+json); } } @@ -91,6 +90,6 @@ public void testMapSerialization() throws Exception { String json = MAPPER.writer() .with(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) .writeValueAsString(map); - Assert.assertEquals("{\"2007-12-03T10:15:30+01:00[Europe/Warsaw]\":\"\"}", json); + assertEquals("{\"2007-12-03T10:15:30+01:00[Europe/Warsaw]\":\"\"}", json); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializationTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializationTest.java index 11984715..f7d48284 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializationTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearMonthSerializationTest.java @@ -16,14 +16,12 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import java.time.Month; import java.time.YearMonth; import java.time.temporal.Temporal; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonProperty; @@ -32,7 +30,7 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class YearMonthSerializationTest extends ModuleTestBase @@ -60,8 +58,8 @@ public void testSerializationAsTimestamp01() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[1986,1]", value); + assertNotNull(value); + assertEquals("[1986,1]", value); } @Test @@ -72,8 +70,8 @@ public void testSerializationAsTmestamp02() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "[2013,8]", value); + assertNotNull(value); + assertEquals("[2013,8]", value); } @Test @@ -84,8 +82,8 @@ public void testSerializationAsString01() throws Exception .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + yearMonth.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + yearMonth.toString() + '"', value); } @Test @@ -96,8 +94,8 @@ public void testSerializationAsString02() throws Exception .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", '"' + yearMonth.toString() + '"', value); + assertNotNull(value); + assertEquals('"' + yearMonth.toString() + '"', value); } @Test @@ -109,9 +107,8 @@ public void testSerializationWithTypeInfo01() throws Exception .addMixIn(Temporal.class, MockObjectConfiguration.class); String value = mapper.writeValueAsString(yearMonth); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", - "[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", value); + assertNotNull(value); + assertEquals("[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", value); } @Test @@ -120,8 +117,8 @@ public void testDeserializationAsTimestamp01() throws Exception YearMonth yearMonth = YearMonth.of(1986, Month.JANUARY); YearMonth value = MAPPER.readValue("[1986,1]", YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -130,8 +127,8 @@ public void testDeserializationAsTimestamp02() throws Exception YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); YearMonth value = MAPPER.readValue("[2013,8]", YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -140,8 +137,8 @@ public void testDeserializationAsString01() throws Exception YearMonth yearMonth = YearMonth.of(1986, Month.JANUARY); YearMonth value = MAPPER.readValue('"' + yearMonth.toString() + '"', YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -150,8 +147,8 @@ public void testDeserializationAsString02() throws Exception YearMonth yearMonth = YearMonth.of(2013, Month.AUGUST); YearMonth value = this.MAPPER.readValue('"' + yearMonth.toString() + '"', YearMonth.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertEquals(yearMonth, value); } @Test @@ -163,9 +160,9 @@ public void testDeserializationWithTypeInfo01() throws Exception .addMixIn(Temporal.class, MockObjectConfiguration.class); Temporal value = mapper.readValue("[\"" + YearMonth.class.getName() + "\",\"" + yearMonth.toString() + "\"]", Temporal.class); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be a YearMonth.", value instanceof YearMonth); - assertEquals("The value is not correct.", yearMonth, value); + assertNotNull(value); + assertInstanceOf(YearMonth.class, value, "The value should be a YearMonth."); + assertEquals(yearMonth, value); } @Test @@ -176,8 +173,8 @@ public void testSerializationWithPattern01() throws Exception String value = MAPPER.writeValueAsString(simpleAggregate); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", "{\"yearMonth\":\"1308\"}", value); + assertNotNull(value); + assertEquals("{\"yearMonth\":\"1308\"}", value); } @Test @@ -188,7 +185,7 @@ public void testDeserializationWithPattern01() throws Exception SimpleAggregate value = MAPPER.readValue("{\"yearMonth\":\"1308\"}", SimpleAggregate.class); - assertNotNull("The value should not be null.", value); - assertEquals("The value is not correct.", simpleAggregate.yearMonth, value.yearMonth); + assertNotNull(value); + assertEquals(simpleAggregate.yearMonth, value.yearMonth); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerTest.java index 23501ce4..ac67be3a 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/YearSerTest.java @@ -19,13 +19,13 @@ import java.time.Year; import java.time.temporal.Temporal; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class YearSerTest extends ModuleTestBase { @@ -34,9 +34,9 @@ public class YearSerTest extends ModuleTestBase @Test public void testDefaultSerialization() throws Exception { - assertEquals("The value is not correct.", "1986", + assertEquals("1986", MAPPER.writeValueAsString(Year.of(1986))); - assertEquals("The value is not correct.", "2013", + assertEquals("2013", MAPPER.writeValueAsString(Year.of(2013))); } @@ -46,6 +46,6 @@ public void testSerializationWithTypeInfo() throws Exception ObjectMapper mapper = newMapper() .addMixIn(Temporal.class, MockObjectConfiguration.class); String value = mapper.writeValueAsString(Year.of(2005)); - assertEquals("The value is not correct.", "[\"" + Year.class.getName() + "\",2005]", value); + assertEquals("[\"" + Year.class.getName() + "\",2005]", value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerTest.java index 6d74618f..b2a7c6d5 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerTest.java @@ -16,15 +16,15 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.junit.Assert.assertEquals; - import java.time.ZoneId; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class ZoneIdSerTest extends ModuleTestBase { @@ -38,20 +38,20 @@ public class ZoneIdSerTest extends ModuleTestBase public void testSerialization01() throws Exception { final String value = MAPPER.writeValueAsString(ZoneId.of("America/Chicago")); - assertEquals("The value is not correct.", "\"America/Chicago\"", value); + assertEquals("\"America/Chicago\"", value); } @Test public void testSerialization02() throws Exception { final String value = MAPPER.writeValueAsString(ZoneId.of("America/Anchorage")); - assertEquals("The value is not correct.", "\"America/Anchorage\"", value); + assertEquals("\"America/Anchorage\"", value); } @Test public void testSerializationWithTypeInfo01() throws Exception { String value = MOCK_OBJECT_MIXIN_MAPPER.writeValueAsString(ZoneId.of("America/Denver")); - assertEquals("The value is not correct.", "[\"java.time.ZoneId\",\"America/Denver\"]", value); + assertEquals("[\"java.time.ZoneId\",\"America/Denver\"]", value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneOffsetSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneOffsetSerTest.java index 11472215..97181f97 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneOffsetSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZoneOffsetSerTest.java @@ -19,13 +19,13 @@ import java.time.ZoneId; import java.time.ZoneOffset; -import static org.junit.Assert.assertEquals; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.*; public class ZoneOffsetSerTest extends ModuleTestBase { @@ -36,7 +36,7 @@ public void testSerialization01() throws Exception { ZoneOffset offset = ZoneOffset.of("Z"); String value = MAPPER.writeValueAsString(offset); - assertEquals("The value is not correct.", "\"Z\"", value); + assertEquals("\"Z\"", value); } @Test @@ -44,7 +44,7 @@ public void testSerialization02() throws Exception { ZoneOffset offset = ZoneOffset.of("+0300"); String value = MAPPER.writeValueAsString(offset); - assertEquals("The value is not correct.", "\"+03:00\"", value); + assertEquals("\"+03:00\"", value); } @Test @@ -52,7 +52,7 @@ public void testSerialization03() throws Exception { ZoneOffset offset = ZoneOffset.of("-0630"); String value = MAPPER.writeValueAsString(offset); - assertEquals("The value is not correct.", "\"-06:30\"", value); + assertEquals("\"-06:30\"", value); } @Test @@ -62,6 +62,6 @@ public void testSerializationWithTypeInfo03() throws Exception .addMixIn(ZoneId.class, MockObjectConfiguration.class); ZoneOffset offset = ZoneOffset.of("+0415"); String value = mapper.writeValueAsString(offset); - assertEquals("The value is not correct.", "[\"" + ZoneOffset.class.getName() + "\",\"+04:15\"]", value); + assertEquals("[\"" + ZoneOffset.class.getName() + "\",\"+04:15\"]", value); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java index 5c306770..614d9089 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerTest.java @@ -16,10 +16,6 @@ package com.fasterxml.jackson.datatype.jsr310.ser; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - import com.fasterxml.jackson.core.JsonProcessingException; import java.time.Instant; import java.time.ZoneId; @@ -32,10 +28,11 @@ import java.util.Locale; import java.util.TimeZone; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.datatype.jsr310.JavaTimeFeature; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; -import org.junit.Test; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -46,6 +43,8 @@ import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + public class ZonedDateTimeSerTest extends ModuleTestBase { @@ -95,7 +94,7 @@ public void testSerializationAsTimestamp01Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "0.0", value); + assertEquals("0.0", value); } @Test @@ -108,7 +107,7 @@ public void testSerializationAsTimestamp01NegativeSeconds() throws Exception .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); ZonedDateTime actual = MAPPER.readValue(serialized, ZonedDateTime.class); - assertEquals("The value is not correct.", date, actual); + assertEquals(date, actual); } @Test @@ -119,14 +118,14 @@ public void testSerializationAsTimestamp01NegativeSecondsWithDefaults() throws E ZonedDateTime original = ZonedDateTime.parse("Apr 13 1969 05:05:38.599 UTC", dtf); String serialized = MAPPER.writeValueAsString(original); ZonedDateTime deserialized = MAPPER.readValue(serialized, ZonedDateTime.class); - assertEquals("The day is not correct.", original.getDayOfMonth(), deserialized.getDayOfMonth()); - assertEquals("The month is not correct.", original.getMonthValue(), deserialized.getMonthValue()); - assertEquals("The year is not correct.", original.getYear(), deserialized.getYear()); - assertEquals("The hour is not correct.", original.getHour(), deserialized.getHour()); - assertEquals("The minute is not correct.", original.getMinute(), deserialized.getMinute()); - assertEquals("The second is not correct.", original.getSecond(), deserialized.getSecond()); - assertEquals("The nano is not correct.", original.getNano(), deserialized.getNano()); - assertEquals("The time zone is not correct.", ZoneId.of("UTC").getRules(), deserialized.getZone().getRules()); + assertEquals(original.getDayOfMonth(), deserialized.getDayOfMonth(), "The day is not correct."); + assertEquals(original.getMonthValue(), deserialized.getMonthValue(), "The month is not correct."); + assertEquals(original.getYear(), deserialized.getYear(), "The year is not correct."); + assertEquals(original.getHour(), deserialized.getHour(), "The hour is not correct."); + assertEquals(original.getMinute(), deserialized.getMinute(), "The minute is not correct."); + assertEquals(original.getSecond(), deserialized.getSecond(), "The second is not correct."); + assertEquals(original.getNano(), deserialized.getNano(), "The nano is not correct."); + assertEquals(ZoneId.of("UTC").getRules(), deserialized.getZone().getRules(), "The time zone is not correct."); } @Test @@ -137,7 +136,7 @@ public void testSerializationAsTimestamp01Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "0", value); + assertEquals("0", value); } @Test @@ -148,7 +147,7 @@ public void testSerializationAsTimestamp02Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "123456789.183917322", value); + assertEquals("123456789.183917322", value); } @Test @@ -159,7 +158,7 @@ public void testSerializationAsTimestamp02Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", "123456789183", value); + assertEquals("123456789183", value); } @Test @@ -170,7 +169,7 @@ public void testSerializationAsTimestamp03Nanoseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .with(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); + assertEquals(DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), value); } @Test @@ -181,7 +180,7 @@ public void testSerializationAsTimestamp03Milliseconds() throws Exception .with(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .without(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .writeValueAsString(date); - assertEquals("The value is not correct.", Long.toString(date.toInstant().toEpochMilli()), value); + assertEquals(Long.toString(date.toInstant().toEpochMilli()), value); } @Test @@ -191,7 +190,7 @@ public void testSerializationAsString01() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + assertEquals('"' + FORMATTER.withZone(Z1).format(date) + '"', value); } @@ -202,7 +201,7 @@ public void testSerializationAsString02() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + assertEquals('"' + FORMATTER.withZone(Z2).format(date) + '"', value); } @@ -213,7 +212,7 @@ public void testSerializationAsString03() throws Exception String value = MAPPER.writer() .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + assertEquals('"' + FORMATTER.withZone(Z3).format(date) + '"', value); } @@ -225,7 +224,7 @@ public void testSerializationAsStringWithMapperTimeZone01() throws Exception .with(TimeZone.getTimeZone(Z1)) .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -236,7 +235,7 @@ public void testSerializationAsStringWithMapperTimeZone02() throws Exception .with(TimeZone.getTimeZone(Z2)) .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -247,7 +246,7 @@ public void testSerializationAsStringWithMapperTimeZone03() throws Exception .with(TimeZone.getTimeZone(Z3)) .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .writeValueAsString(date); - assertEquals("The value is not correct.", '"' + FORMATTER.format(date) + '"', value); + assertEquals('"' + FORMATTER.format(date) + '"', value); } @Test @@ -258,7 +257,7 @@ public void testSerializationAsStringWithZoneIdOff() throws Exception { SerializationFeature.WRITE_DATES_WITH_ZONE_ID) .writeValueAsString(date); - assertEquals("The value is incorrect.", "\"" + assertEquals("\"" + FORMATTER.withZone(Z3).format(date) + "\"", value); } @@ -271,7 +270,7 @@ public void testSerializationAsStringWithZoneIdOffAndMapperTimeZone() throws Exc SerializationFeature.WRITE_DATES_WITH_ZONE_ID) .writeValueAsString(date); - assertEquals("The value is incorrect.", "\"" + FORMATTER.format(date) + "\"", value); + assertEquals("\"" + FORMATTER.format(date) + "\"", value); } @Test @@ -282,7 +281,7 @@ public void testSerializationAsStringWithZoneIdOn() throws Exception { .with(SerializationFeature.WRITE_DATES_WITH_ZONE_ID) .writeValueAsString(date); - assertEquals("The value is incorrect.", "\"" + DateTimeFormatter.ISO_ZONED_DATE_TIME.format(date) + "\"", value); + assertEquals("\"" + DateTimeFormatter.ISO_ZONED_DATE_TIME.format(date) + "\"", value); } @Test @@ -297,7 +296,7 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOnAndA .writeValueAsString(date); // We expect to have the date written with the datetime of ZoneId Z2 - assertEquals("The value is incorrect", "\"" + date.withZoneSameInstant(Z2).format(FORMATTER_WITHOUT_ZONEID) + "\"", value); + assertEquals("\"" + date.withZoneSameInstant(Z2).format(FORMATTER_WITHOUT_ZONEID) + "\"", value); } @Test @@ -312,7 +311,7 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOffAnd .writeValueAsString(date); // We expect to have the date written with the datetime of ZoneId Z3 - assertEquals("The value is incorrect", "\"" + date.format(FORMATTER_WITHOUT_ZONEID) + "\"", value); + assertEquals("\"" + date.format(FORMATTER_WITHOUT_ZONEID) + "\"", value); } @Test @@ -326,7 +325,7 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOn() t .writeValueAsString(date); // We expect to have the date written with the ZoneId Z2 - assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.withZoneSameInstant(Z2)) + "\"", value); + assertEquals("\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date.withZoneSameInstant(Z2)) + "\"", value); } @Test @@ -340,7 +339,7 @@ public void testSerializationAsStringWithDefaultTimeZoneAndContextTimeZoneOff() .writeValueAsString(date); // We expect to have the date written with the ZoneId Z3 - assertEquals("The value is incorrect", "\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date) + "\"", value); + assertEquals("\"" + DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(date) + "\"", value); } @Test @@ -353,8 +352,7 @@ public void testSerializationWithTypeInfo01() throws Exception .enable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .build() .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", value); + assertEquals("[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", value); } @Test @@ -367,8 +365,7 @@ public void testSerializationWithTypeInfo02() throws Exception .disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) .build() .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",123456789183]", value); + assertEquals("[\"" + ZonedDateTime.class.getName() + "\",123456789183]", value); } @Test @@ -380,8 +377,7 @@ public void testSerializationWithTypeInfo03() throws Exception .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build() .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",\"" + assertEquals("[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.withZone(Z3).format(date) + "\"]", value); } @@ -396,8 +392,7 @@ public void testSerializationWithTypeInfoAndMapperTimeZone() throws Exception .writer() .with(TimeZone.getTimeZone(Z3)) .writeValueAsString(date); - assertEquals("The value is not correct.", - "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); + assertEquals("[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]", value); } @Test @@ -406,9 +401,9 @@ public void testDeserializationAsFloat01WithoutTimeZone() throws Exception ZonedDateTime date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(0L), Z1); ZonedDateTime value = MAPPER.readValue("0.000000000", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -420,9 +415,9 @@ public void testDeserializationAsFloat01WithTimeZone() throws Exception .with(TimeZone.getDefault()) .readValue("0.000000000"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -432,9 +427,9 @@ public void testDeserializationAsFloat02WithoutTimeZone() throws Exception ZonedDateTime value = this.MAPPER.readValue("123456789.183917322", ZonedDateTime.class); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -446,9 +441,9 @@ public void testDeserializationAsFloat02WithTimeZone() throws Exception .with(TimeZone.getDefault()) .readValue("123456789.183917322"); - assertNotNull("The value should not be null.", value); + assertNotNull(value); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -459,7 +454,7 @@ public void testDeserializationAsFloat03WithoutTimeZone() throws Exception DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano()), ZonedDateTime.class ); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -471,7 +466,7 @@ public void testDeserializationAsFloat03WithTimeZone() throws Exception .readValue(DecimalUtils.toDecimal(date.toEpochSecond(), date.getNano())); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -483,7 +478,7 @@ public void testDeserializationAsInt01NanosecondsWithoutTimeZone() throws Except .readValue("0"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -496,7 +491,7 @@ public void testDeserializationAsInt01NanosecondsWithTimeZone() throws Exception .readValue("0"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -508,7 +503,7 @@ public void testDeserializationAsInt01MillisecondsWithoutTimeZone() throws Excep .readValue("0"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -521,7 +516,7 @@ public void testDeserializationAsInt01MillisecondsWithTimeZone() throws Exceptio .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("0"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -532,7 +527,7 @@ public void testDeserializationAsInt02NanosecondsWithoutTimeZone() throws Except .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("123456789"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -544,7 +539,7 @@ public void testDeserializationAsInt02NanosecondsWithTimeZone() throws Exception .with(TimeZone.getDefault()) .readValue("123456789"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -555,7 +550,7 @@ public void testDeserializationAsInt02MillisecondsWithoutTimeZone() throws Excep .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("123456789422"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -567,7 +562,7 @@ public void testDeserializationAsInt02MillisecondsWithTimeZone() throws Exceptio .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue("123456789422"); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -580,7 +575,7 @@ public void testDeserializationAsInt03NanosecondsWithoutTimeZone() throws Except .with(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue(Long.toString(date.toEpochSecond()), ZonedDateTime.class); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -594,7 +589,7 @@ public void testDeserializationAsInt03NanosecondsWithTimeZone() throws Exception .with(TimeZone.getDefault()) .readValue(Long.toString(date.toEpochSecond()), ZonedDateTime.class); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -607,7 +602,7 @@ public void testDeserializationAsInt03MillisecondsWithoutTimeZone() throws Excep .without(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS) .readValue(Long.toString(date.toInstant().toEpochMilli())); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -621,7 +616,7 @@ public void testDeserializationAsInt03MillisecondsWithTimeZone() throws Exceptio .with(TimeZone.getDefault()) .readValue(Long.toString(date.toInstant().toEpochMilli())); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -632,7 +627,7 @@ public void testDeserializationAsString01WithoutTimeZone() throws Exception .with(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -644,7 +639,7 @@ public void testDeserializationAsString01WithTimeZone() throws Exception .with(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -656,7 +651,7 @@ public void testDeserializationAsString01WithTimeZoneTurnedOff() throws Exceptio .with(TimeZone.getDefault()) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", FIX_OFFSET, value.getZone()); + assertEquals(FIX_OFFSET, value.getZone(), "The time zone is not correct."); } @Test @@ -674,7 +669,7 @@ public void testDeserializationAsString02WithoutTimeZone() throws Exception .with(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -686,7 +681,7 @@ public void testDeserializationAsString02WithTimeZone() throws Exception .with(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -698,7 +693,7 @@ public void testDeserializationAsString02WithTimeZoneTurnedOff() throws Exceptio .without(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", FIX_OFFSET, value.getZone()); + assertEquals(FIX_OFFSET, value.getZone(), "The time zone is not correct."); } @Test @@ -716,7 +711,7 @@ public void testDeserializationAsString03WithoutTimeZone() throws Exception .with(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, value.getZone()); + assertEquals(DEFAULT_TZ, value.getZone(), "The time zone is not correct."); } @Test @@ -728,7 +723,7 @@ public void testDeserializationAsString03WithTimeZone() throws Exception .with(TimeZone.getDefault()) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), value.getZone()); + assertEquals(ZoneId.systemDefault().normalized(), value.getZone(), "The time zone is not correct."); } @Test @@ -740,7 +735,7 @@ public void testDeserializationAsString03WithTimeZoneTurnedOff() throws Exceptio .without(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue('"' + FORMATTER.format(date) + '"'); assertIsEqual(date, value); - assertEquals("The time zone is not correct.", FIX_OFFSET, value.getZone()); + assertEquals(FIX_OFFSET, value.getZone(), "The time zone is not correct."); } @Test @@ -760,10 +755,10 @@ public void testDeserializationWithTypeInfo01WithoutTimeZone() throws Exception .readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789.183917322]", Temporal.class ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -780,10 +775,10 @@ public void testDeserializationWithTypeInfo01WithTimeZone() throws Exception ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -797,9 +792,9 @@ public void testDeserializationWithTypeInfo02WithoutTimeZone() throws Exception .readerFor(Temporal.class) .readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789]"); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -816,10 +811,10 @@ public void testDeserializationWithTypeInfo02WithTimeZone() throws Exception "[\"" + ZonedDateTime.class.getName() + "\",123456789]" ); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -834,10 +829,10 @@ public void testDeserializationWithTypeInfo03WithoutTimeZone() throws Exception .readValue( "[\"" + ZonedDateTime.class.getName() + "\",123456789422]"); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -852,10 +847,10 @@ public void testDeserializationWithTypeInfo03WithTimeZone() throws Exception .with(TimeZone.getDefault()) .readValue("[\"" + ZonedDateTime.class.getName() + "\",123456789422]"); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -870,10 +865,10 @@ public void testDeserializationWithTypeInfo04WithoutTimeZone() throws Exception .readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]"); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", DEFAULT_TZ, ((ZonedDateTime) value).getZone()); + assertEquals(DEFAULT_TZ, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -890,10 +885,10 @@ public void testDeserializationWithTypeInfo04WithTimeZone() throws Exception .readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]"); - assertNotNull("The value should not be null.", value); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertNotNull(value); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone()); + assertEquals(ZoneId.systemDefault().normalized(), ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -909,9 +904,9 @@ public void testDeserializationWithTypeInfo04WithTimeZoneTurnedOff() throws Exce .without(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .readValue( "[\"" + ZonedDateTime.class.getName() + "\",\"" + FORMATTER.format(date) + "\"]"); - assertTrue("The value should be an ZonedDateTime.", value instanceof ZonedDateTime); + assertInstanceOf(ZonedDateTime.class, value, "The value should be an ZonedDateTime."); assertIsEqual(date, (ZonedDateTime) value); - assertEquals("The time zone is not correct.", FIX_OFFSET, ((ZonedDateTime) value).getZone()); + assertEquals(FIX_OFFSET, ((ZonedDateTime) value).getZone(), "The time zone is not correct."); } @Test @@ -980,9 +975,33 @@ public void testShapeInt() throws JsonProcessingException { assertEquals("{\"t1\":1651053600000,\"t2\":1651053600.000000000}", json1); } + // [dataformat-joda#92] DateTime serialization result is not same as Java 8 ZonedDateTime + @Test + public void testSerializationWithZoneWithDefaultTimeZone() throws Exception + { + ZonedDateTime java8ZonedDateTime = ZonedDateTime.of(2023, 10, 1, 12, 0, 0, 0, + ZoneId.of("Asia/Shanghai")); + + // Without WRITE_DATES_WITH_CONTEXT_TIME_ZONE + assertEquals("\"2023-10-01T12:00:00+08:00\"", + MAPPER.writer() + .with(TimeZone.getTimeZone("UTC")) + .without(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .writeValueAsString(java8ZonedDateTime)); + + // With WRITE_DATES_WITH_CONTEXT_TIME_ZONE + assertEquals("\"2023-10-01T04:00:00Z\"", + MAPPER.writer() + .with(TimeZone.getTimeZone("UTC")) + .with(SerializationFeature.WRITE_DATES_WITH_CONTEXT_TIME_ZONE) + .without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) + .writeValueAsString(java8ZonedDateTime)); + } + private static void assertIsEqual(ZonedDateTime expected, ZonedDateTime actual) { - assertTrue("The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">.", - expected.isEqual(actual)); + assertTrue(expected.isEqual(actual), + "The value is not correct. Expected timezone-adjusted <" + expected + ">, actual <" + actual + ">."); } } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerWithJsonFormat333Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerWithJsonFormat333Test.java index a15055f7..5e1e8111 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerWithJsonFormat333Test.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/ser/ZonedDateTimeSerWithJsonFormat333Test.java @@ -2,14 +2,14 @@ import java.time.ZonedDateTime; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; // [module-java8#333]: ZonedDateTime serialization with @JsonFormat pattern never uses // while WRITE_DATES_WITH_ZONE_ID enabled #333 diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestFailureExpected.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestFailureExpected.java new file mode 100644 index 00000000..352ca9dd --- /dev/null +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestFailureExpected.java @@ -0,0 +1,37 @@ +package com.fasterxml.jackson.datatype.jsr310.testutil.failure; + +import java.lang.annotation.*; + +import org.junit.jupiter.api.extension.ExtendWith; + +/** + *

+ * Annotation used to indicate that a JUnit-5 based tests method is expected to fail. + * + *

+ * When a test method is annotated with {@code @JacksonTestFailureExpected}, the + * {@link JacksonTestFailureExpectedInterceptor} will intercept the test execution. + * If the test passes, which is an unexpected behavior, the interceptor will throw an exception to fail the test, + * indicating that the test was expected to fail but didn't. + *

+ * + *

Usage Example:

+ * + *

+ *
+ *     @Test
+ *     @JacksonTestFailureExpected
+ *     public void testFeatureNotYetImplemented() {
+ *         // Test code that is expected to fail
+ *     }
+ * }
+ * 
+ * + *

+ * + * @since 2.19 + */ +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(JacksonTestFailureExpectedInterceptor.class) +public @interface JacksonTestFailureExpected { } diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestFailureExpectedInterceptor.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestFailureExpectedInterceptor.java new file mode 100644 index 00000000..1de29814 --- /dev/null +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestFailureExpectedInterceptor.java @@ -0,0 +1,43 @@ +package com.fasterxml.jackson.datatype.jsr310.testutil.failure; + +import java.lang.reflect.Method; + +import org.junit.jupiter.api.extension.*; + +/** + * Custom {@link InvocationInterceptor} that intercepts test method invocation. + * To pass the test ***only if*** test fails with an exception, and fail the test otherwise. + * + * @since 2.19 + */ +public class JacksonTestFailureExpectedInterceptor + implements InvocationInterceptor +{ + @Override + public void interceptTestMethod(Invocation invocation, + ReflectiveInvocationContext invocationContext, ExtensionContext extensionContext) + throws Throwable + { + try { + invocation.proceed(); + } catch (Throwable t) { + // do-nothing, we do expect an exception + return; + } + handleUnexpectePassingTest(invocationContext); + } + + private void handleUnexpectePassingTest(ReflectiveInvocationContext invocationContext) { + // Collect information we need + Object targetClass = invocationContext.getTargetClass(); + Object testMethod = invocationContext.getExecutable().getName(); + //List arguments = invocationContext.getArguments(); + + // Create message + String message = String.format("Test method %s.%s() passed, but should have failed", targetClass, testMethod); + + // throw exception + throw new JacksonTestShouldFailException(message); + } + +} diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestShouldFailException.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestShouldFailException.java new file mode 100644 index 00000000..68790320 --- /dev/null +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/testutil/failure/JacksonTestShouldFailException.java @@ -0,0 +1,18 @@ +package com.fasterxml.jackson.datatype.jsr310.testutil.failure; + +/** + * Exception used to alert that a test is passing, but should be failing. + * + * WARNING : This only for test code, and should never be thrown from production code. + * + * @since 2.19 + */ +public class JacksonTestShouldFailException + extends RuntimeException +{ + private static final long serialVersionUID = 1L; + + public JacksonTestShouldFailException(String msg) { + super(msg); + } +} diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/JDKSerializabilityTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/tofix/JDKSerializabilityTest.java similarity index 85% rename from datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/JDKSerializabilityTest.java rename to datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/tofix/JDKSerializabilityTest.java index 6e2487b7..90d38e88 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/JDKSerializabilityTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/tofix/JDKSerializabilityTest.java @@ -1,18 +1,20 @@ -package com.fasterxml.jackson.datatype.jsr310.failing; +package com.fasterxml.jackson.datatype.jsr310.tofix; import java.io.*; import java.time.Year; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.datatype.jsr310.*; import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.datatype.jsr310.testutil.failure.JacksonTestFailureExpected; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class JDKSerializabilityTest extends ModuleTestBase { + @JacksonTestFailureExpected @Test public void testJDKSerializability() throws Exception { final Year input = Year.of(1986); diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/ZonedDateTimeIssue244Test.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/tofix/ZonedDateTimeIssue244Test.java similarity index 80% rename from datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/ZonedDateTimeIssue244Test.java rename to datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/tofix/ZonedDateTimeIssue244Test.java index eedb7699..81a79b0c 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/failing/ZonedDateTimeIssue244Test.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/tofix/ZonedDateTimeIssue244Test.java @@ -1,16 +1,20 @@ -package com.fasterxml.jackson.datatype.jsr310.failing; +package com.fasterxml.jackson.datatype.jsr310.tofix; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; -import org.junit.Test; +import com.fasterxml.jackson.datatype.jsr310.testutil.failure.JacksonTestFailureExpected; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; +// 17-Nov-2025, tatu: Issue closed as not planned to be fixed: unit test +// left in for now, likely to be removed in future. /** * Test case for https://github.com/FasterXML/jackson-modules-java8/issues/244 */ @@ -20,6 +24,7 @@ public class ZonedDateTimeIssue244Test extends ModuleTestBase .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build(); + @JacksonTestFailureExpected @Test public void zoneIdUTC() throws Exception { @@ -32,6 +37,7 @@ public void zoneOffsetUTC() throws Exception assertSerializeAndDeserialize(ZonedDateTime.now(ZoneOffset.UTC)); // fails! } + @JacksonTestFailureExpected @Test public void zoneOffsetNonUTC() throws Exception { diff --git a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/util/DurationUnitConverterTest.java b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/util/DurationUnitConverterTest.java index bf75bba7..7873baf8 100644 --- a/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/util/DurationUnitConverterTest.java +++ b/datetime/src/test/java/com/fasterxml/jackson/datatype/jsr310/util/DurationUnitConverterTest.java @@ -1,14 +1,13 @@ package com.fasterxml.jackson.datatype.jsr310.util; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; - import java.time.temporal.ChronoUnit; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase; +import static org.junit.jupiter.api.Assertions.*; + public class DurationUnitConverterTest extends ModuleTestBase { @@ -47,8 +46,8 @@ public void shouldNotMapToTemporalUnit() { // Not matching at all "DOESNOTMATCH", "", " " }) { - assertNull("Should not map pattern '"+invalid+"'", - DurationUnitConverter.from(invalid)); + assertNull(DurationUnitConverter.from(invalid), + "Should not map pattern '"+invalid+"'"); } } } diff --git a/parameter-names/README.md b/parameter-names/README.md index 43dbf65d..1404490b 100644 --- a/parameter-names/README.md +++ b/parameter-names/README.md @@ -1,5 +1,4 @@ -Jackson module -that adds support for accessing parameter names; a feature added in JDK 8. +Jackson module that adds support for accessing parameter names; a feature added in JDK 8. ## Usage @@ -12,6 +11,11 @@ For maven dependency definition, click on the second badge in the previous, Stat Like all standard Jackson modules (libraries that implement Module interface), registration is done as follows: ```java +ObjectMapper mapper = JsonMapper.builder() + .addModule(new ParameterNamesModule()) + .build(); + +// Or, older (pre-2.10): ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new ParameterNamesModule()); ``` @@ -26,9 +30,9 @@ To override this behavior use the `ParameterNamesModule` constructor with `JsonC For example, to use same behavior as for constructors with multiple parameters: ```java -ObjectMapper objectMapper = new ObjectMapper(); -objectMapper.registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)); - +ObjectMapper mapper = JsonMapper.builder() + .addModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)) + .build(); ``` ### Usage example @@ -76,14 +80,15 @@ class Person { Preconditions: - - class Person must be compiled with Java 8 compliant compiler with option to store formal parameter names turned on (`-parameters` option). For more information about Java 8 API for accessing parameter names at runtime see [this][2] - - if there are multiple visible constructors and there is no default constructor, `@JsonCreator` is required to select constructor for deserialization - - if used with `jackson-databind` lower than `2.6.0`, `@JsonCreator` is required. In practice, `@JsonCreator` is also often required with `2.6.0+` due to issues with constructor discovery that will be resolved in `2.7`. - - if class Person has a single argument constructor, its argument needs to be annotated with `@JsonProperty("propertyName")`. This is to preserve legacy behavior, see [FasterXML/jackson-databind/#1498][3] +- class Person must be compiled with Java 8 compliant compiler with option to store formal parameter names turned on (`-parameters` option). For more information about Java 8 API for accessing parameter names at runtime see [this][2] +- if there are multiple visible constructors and there is no default constructor, `@JsonCreator` is required to select constructor for deserialization +- if used with `jackson-databind` lower than `2.6.0`, `@JsonCreator` is required. In practice, `@JsonCreator` is also often required with `2.6.0+` due to issues with constructor discovery that will be resolved in `2.7`. +- if class Person has a single-argument constructor, its argument needs to be annotated with `@JsonProperty("propertyName")`. This is to preserve legacy behavior, see [FasterXML/jackson-databind/#1498][3] + ## More See [Wiki](../../../wiki) for more information (javadocs, downloads). -[1]: http://jackson.codehaus.org/1.1.2/javadoc/org/codehaus/jackson/annotate/JsonProperty.html +[1]: https://javadoc.io/static/com.fasterxml.jackson.core/jackson-annotations/2.20/com/fasterxml/jackson/annotation/JsonProperty.html [2]: http://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html [3]: https://github.com/FasterXML/jackson-databind/issues/1498 diff --git a/parameter-names/pom.xml b/parameter-names/pom.xml index 7f872178..7a5baf8b 100644 --- a/parameter-names/pom.xml +++ b/parameter-names/pom.xml @@ -9,7 +9,7 @@ com.fasterxml.jackson.module jackson-modules-java8 - 2.18.11-SNAPSHOT + 2.23.0-SNAPSHOT jackson-module-parameter-names Jackson-module-parameter-names @@ -74,7 +74,6 @@ introspection of method/constructor parameter names, without having to add expli ${javac.target.version} true true - true -Xlint -parameters @@ -85,22 +84,11 @@ introspection of method/constructor parameter names, without having to add expli com.google.code.maven-replacer-plugin replacer - - - process-packageVersion - generate-sources - - - - org.apache.maven.plugins - maven-surefire-plugin - ${version.plugin.surefire} - - - ${packageVersion.dir}/failing/*.java - - + + + org.cyclonedx + cyclonedx-maven-plugin + + + org.gradlex + gradle-module-metadata-maven-plugin + diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ConstructorDetectorConfigTest.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ConstructorDetectorConfigTest.java index bd9627f5..8309b1c6 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ConstructorDetectorConfigTest.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ConstructorDetectorConfigTest.java @@ -1,12 +1,12 @@ package com.fasterxml.jackson.module.paramnames; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.cfg.ConstructorDetector; +import static org.junit.jupiter.api.Assertions.*; + public class ConstructorDetectorConfigTest extends ModuleTestBase { diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/CreatorWithNamingStrategy74Test.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/CreatorWithNamingStrategy74Test.java index f1e81044..cb9aa78c 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/CreatorWithNamingStrategy74Test.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/CreatorWithNamingStrategy74Test.java @@ -1,12 +1,12 @@ package com.fasterxml.jackson.module.paramnames; -import static org.assertj.core.api.BDDAssertions.then; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; -import org.junit.Test; +import static org.assertj.core.api.BDDAssertions.then; // for [java8-modules#74] public class CreatorWithNamingStrategy74Test @@ -43,6 +43,6 @@ private void verifyObjectDeserializationWithNamingStrategy( // when Object actual = objectMapper.readValue(json, expected.getClass()); - then(actual).isEqualToComparingFieldByField(expected); + then(actual).usingRecursiveComparison().isEqualTo(expected); } } diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/DelegatingCreatorTest.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/DelegatingCreatorTest.java index d17f0444..ce40dce2 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/DelegatingCreatorTest.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/DelegatingCreatorTest.java @@ -1,14 +1,14 @@ package com.fasterxml.jackson.module.paramnames; +import java.io.*; +import java.util.*; + +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.type.*; import com.fasterxml.jackson.databind.*; -import org.junit.*; - -import java.io.*; -import java.util.*; - import static org.assertj.core.api.BDDAssertions.*; public class DelegatingCreatorTest extends ModuleTestBase @@ -68,7 +68,7 @@ public void shouldNotOverrideJsonCreatorAnnotationWithSpecifiedMode() throws IOE Map props = new HashMap<>(); props.put("value", "aValue"); ClassWithDelegatingCreator expected = new ClassWithDelegatingCreator(props); - then(actual).isEqualToComparingFieldByField(expected); + then(actual).usingRecursiveComparison().isEqualTo(expected); } @Test @@ -77,7 +77,7 @@ public void shouldDeserializeIntWrapper() throws Exception { IntWrapper actual = mapper.readValue ("{\"value\":13}", IntWrapper.class); - then(actual).isEqualToComparingFieldByField(new IntWrapper(13)); + then(actual).usingRecursiveComparison().isEqualTo(new IntWrapper(13)); } @Test @@ -86,6 +86,6 @@ public void shouldDeserializeGenericWrapper() throws Exception { GenericWrapper actual = mapper.readValue ("{\"value\":\"aValue\"}", new TypeReference>() { }); - then(actual).isEqualToComparingFieldByField(new GenericWrapper<>("aValue")); + then(actual).usingRecursiveComparison().isEqualTo(new GenericWrapper<>("aValue")); } } diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/EnumNamingTest.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/EnumNamingTest.java index 4b275c1b..9258f626 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/EnumNamingTest.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/EnumNamingTest.java @@ -1,13 +1,13 @@ package com.fasterxml.jackson.module.paramnames; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.databind.ObjectMapper; -import static org.junit.Assert.assertEquals; - -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; public class EnumNamingTest extends ModuleTestBase { diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JDKSerializabilityTest.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JDKSerializabilityTest.java index ef0ade22..ebb2b513 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JDKSerializabilityTest.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JDKSerializabilityTest.java @@ -2,11 +2,11 @@ import java.io.*; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.databind.*; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; public class JDKSerializabilityTest extends ModuleTestBase { diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JsonCreatorTest.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JsonCreatorTest.java index ba525c96..43413b9d 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JsonCreatorTest.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/JsonCreatorTest.java @@ -1,16 +1,15 @@ package com.fasterxml.jackson.module.paramnames; +import org.junit.jupiter.api.Test; + import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import org.junit.*; - import static org.assertj.core.api.BDDAssertions.*; +import static org.junit.jupiter.api.Assertions.*; + public class JsonCreatorTest extends ModuleTestBase { static class ClassWithJsonCreatorOnStaticMethod { @@ -62,7 +61,7 @@ public void testJsonCreatorOnStaticMethod() throws Exception String json = a2q("{'first':'1st','second':'2nd'}"); ClassWithJsonCreatorOnStaticMethod actual = MAPPER.readValue(json, ClassWithJsonCreatorOnStaticMethod.class); - then(actual).isEqualToComparingFieldByField(new ClassWithJsonCreatorOnStaticMethod("1st", "2nd")); + then(actual).usingRecursiveComparison().isEqualTo(new ClassWithJsonCreatorOnStaticMethod("1st", "2nd")); } // [modules-base#178] diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/MapKeyNames206Test.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/MapKeyNames206Test.java index e3336be2..9ed6fdcf 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/MapKeyNames206Test.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/MapKeyNames206Test.java @@ -2,7 +2,7 @@ import java.util.*; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonKey; import com.fasterxml.jackson.annotation.JsonPropertyOrder; @@ -10,7 +10,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.*; // [modules-java8#206] public class MapKeyNames206Test diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/NamingStrategy67Test.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/NamingStrategy67Test.java index 645af4b1..330707dc 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/NamingStrategy67Test.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/NamingStrategy67Test.java @@ -2,7 +2,7 @@ import static org.assertj.core.api.BDDAssertions.then; -import org.junit.Test; +import org.junit.jupiter.api.Test; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; @@ -43,7 +43,7 @@ public void testSnakeCaseNaming() throws Exception "{\"first_property\":\""+MSG+"\"}", // "{\"firstProperty\":\""+MSG+"\"}", ClassWithOneProperty.class); - then(actual).isEqualToComparingFieldByField(new ClassWithOneProperty(MSG)); + then(actual).usingRecursiveComparison().isEqualTo(new ClassWithOneProperty(MSG)); } @Test @@ -54,6 +54,6 @@ public void testPrivateConstructorWithPropertyAnnotations() throws Exception ClassWithTwoProperties actual = mapper.readValue("{\"a\":1, \"b\": 2}", ClassWithTwoProperties.class); - then(actual).isEqualToComparingFieldByField(new ClassWithTwoProperties(1, 2)); + then(actual).usingRecursiveComparison().isEqualTo(new ClassWithTwoProperties(1, 2)); } } diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ParameterNamesAnnotationIntrospectorTest.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ParameterNamesAnnotationIntrospectorTest.java index 433a5dd5..5f3a35e0 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ParameterNamesAnnotationIntrospectorTest.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/ParameterNamesAnnotationIntrospectorTest.java @@ -4,12 +4,11 @@ import com.fasterxml.jackson.databind.introspect.AnnotatedConstructor; import com.fasterxml.jackson.databind.introspect.AnnotatedMethod; import com.fasterxml.jackson.databind.introspect.AnnotatedParameter; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.BDDMockito; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.Mockito; import java.lang.reflect.Constructor; import java.lang.reflect.MalformedParametersException; @@ -19,20 +18,20 @@ import static org.assertj.core.api.BDDAssertions.then; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.when; /** * @author Lovro Pandzic */ -@RunWith(MockitoJUnitRunner.class) public class ParameterNamesAnnotationIntrospectorTest { - @Mock private ParameterExtractor parameterExtractor; private ParameterNamesAnnotationIntrospector introspector; - @Before + @BeforeEach public void setUp() throws Exception { + parameterExtractor = Mockito.mock(ParameterExtractor.class); introspector = new ParameterNamesAnnotationIntrospector(JsonCreator.Mode.DEFAULT, parameterExtractor); } @@ -44,7 +43,7 @@ public void shouldFindParameterNameFromConstructorForLegalIndex() throws Excepti Parameter[] givenParameters = givenConstructor.getParameters(); AnnotatedConstructor owner = new AnnotatedConstructor(null, givenConstructor, null, null); AnnotatedParameter annotatedParameter = new AnnotatedParameter(owner, null, null, null, 0); - given(parameterExtractor.getParameters(any())).willReturn(givenParameters); + when(parameterExtractor.getParameters(any())).thenReturn(givenParameters); // when String actual = introspector.findImplicitPropertyName(annotatedParameter); diff --git a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/PersonTest.java b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/PersonTest.java index 6dc184e5..58f45e20 100644 --- a/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/PersonTest.java +++ b/parameter-names/src/test/java/com/fasterxml/jackson/module/paramnames/PersonTest.java @@ -5,7 +5,7 @@ import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Test; +import org.junit.jupiter.api.Test; public class PersonTest { @@ -22,7 +22,7 @@ public void shouldBeAbleToDeserializePerson() throws IOException // then Person expected = new Person("joe", "smith"); expected.setNickname("joey"); - then(actual).isEqualToComparingFieldByField(expected); + then(actual).usingRecursiveComparison().isEqualTo(expected); } } diff --git a/pom.xml b/pom.xml index 9d404b11..209fb36d 100644 --- a/pom.xml +++ b/pom.xml @@ -3,12 +3,12 @@ com.fasterxml.jackson jackson-base - 2.18.11-SNAPSHOT + 2.23.0-SNAPSHOT com.fasterxml.jackson.module jackson-modules-java8 Jackson modules: Java 8 - 2.18.11-SNAPSHOT + 2.23.0-SNAPSHOT pom Parent pom for Jackson modules needed to support Java 8 features and types @@ -40,10 +40,15 @@ jackson-databind - + - junit - junit + org.junit.jupiter + junit-jupiter + test + + + org.junit.jupiter + junit-jupiter-api test @@ -63,37 +68,22 @@ - - - com.google.code.maven-replacer-plugin - replacer - + + + com.google.code.maven-replacer-plugin + replacer + process-packageVersion generate-sources - - - - + + + org.apache.maven.plugins maven-surefire-plugin - - - com/fasterxml/jackson/**/failing/*.java - - - + - - - - - de.jjohannes - gradle-module-metadata-maven-plugin - - - diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 1ab404f3..c8693d31 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -7,17 +7,13 @@ Tatu Saloranta (cowtowncoder@github): author (other modules) Michael O'Keeffe (kupci@github): co-author (since 2.10) Louis-Rémi Paquet (lrpg@github) - #15: Optional and OptionalLong deserialization not consistent when - deserializing from String - (2.8.8) - -* Reported #15: Optional and OptionalLong deserialization - not consistent when deserializing from String + * Reported #15: Optional and OptionalLong deserialization + not consistent when deserializing from String (2.8.8) Mikko Tiihonen (gmokki@github) -* Reported #33: `Jdk8Serializer.findReferenceSerializer()` leads to `StackOverflowError` - in 2.8.9 + * Reported #33: `Jdk8Serializer.findReferenceSerializer()` leads to `StackOverflowError` + in 2.8.9 (2.8.10) João Cabrita (kewne@github) @@ -170,6 +166,8 @@ Michał Ostrowski (karbi@github) David Schlosnagle (schlosna@github) * Contributed #266: Optimize `InstantDeserializer` method `replaceZeroOffsetAsZIfNecessary()` (2.15.0) + * Contributed #336: Optimize `InstantDeserializer` `addInColonToOffsetIfMissing()` + (2.19.0) Daniel Scalzi (dscalzi@github) * Contributed #267: Normalize zone id during ZonedDateTime deserialization @@ -207,8 +205,41 @@ Emanuel Trandafir (@etrandafir93) Joo Hyuk Kim (@JooHyukKim) * Fixed #308: Can't deserialize `OffsetDateTime.MIN`: Invalid value for EpochDay (2.18.2) + * Fixed #337: Negative `Duration` does not round-trip properly with + `WRITE_DURATIONS_AS_TIMESTAMPS` enabled + (2.19.0) Kevin Mahon (@Strongbeard) * Fixed #291: `InstantDeserializer` fails to parse negative numeric timestamp strings for pre-1970 values (2.18.4) + * Reported #359: `InstantDeserializer` deserializes the nanosecond portion of fractional + negative timestamps incorrectly + (2.21.0) + +Joey Muia (@jmuia) + * Reported #337: Negative `Duration` does not round-trip properly with + `WRITE_DURATIONS_AS_TIMESTAMPS` enabled + (2.19.0) + +Henning Pöttker (@hpoettker) + * Contributed #342: Lenient deserialization of `LocalDate` is not time-zone aware + (2.19.0) + +Boleslav Bobcik (@bbobcik) + * Reported, contributed fix for #364: Deserialization of Month in ONE_BASED_MONTHS + mode fails for value "12" + (2.19.0) + +Albert Lovers (@AlbertLovers) + * Reported, contributed fix for #381: Fix a potential problem in `JavaTimeModule._findFactory()` + (2.21.0) + +Ryan (@rycler) + * Reported #76: Missing milliseconds, when serializing Java 8 date-time, if they are zeros + (2.23.0) + +Seonwoo Jung (@seonwooj0810) + * Contributed fix for #76: Missing milliseconds, when serializing Java 8 date-time, + if they are zeros + (2.23.0) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 81390e95..6bcaa93e 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -8,6 +8,71 @@ Modules: === Releases === ------------------------------------------------------------------------ +2.23.0 (not yet released) + +#76: Missing milliseconds, when serializing Java 8 date-time if they are zeros (Add + `JavaTimeFeature.ALWAYS_WRITE_SUBSECOND_DIGITS`) + (reported by @rycler) + (fix contributed by Seonwoo J) + +2.22.2 (16-Aug-2026) +2.22.1 (07-Jul-2026) +2.22.0 (31-May-2026) + +No changes since 2.21 + +2.21.6 (14-Aug-2026) +2.21.5 (06-Jul-2026) +2.21.4 (28-May-2026) +2.21.3 (28-Apr-2026) +2.21.2 (20-Mar-2026) +2.21.1 (22-Feb-2026) + +No changes since 2.21.0 + +2.21.0 (18-Jan-2026) + +#359: `InstantDeserializer` deserializes the nanosecond portion of fractional + negative timestamps incorrectly + (reported by Kevin M) + (fix by @cowtowncoder, w/ Claude code) +#376: Allow specifying custom `DateTimeFormatter` for `OffsetDateTime` ser/deser + (new constructors?) + (requested by @ZIRAKrezovic) +#381: Fix a potential problem in `JavaTimeModule._findFactory()` + (reported, fix by, Albert L) + +2.20.2 (19-Jan-2025) +2.20.1 (30-Oct-2025) +2.20.0 (28-Aug-2025) + +No changes since 2.19 + +2.19.4 (29-Oct-2025) +2.19.3 (29-Oct-2025) +2.19.2 (18-Jul-2025) +2.19.1 (13-Jun-2025) + +No changes since 2.19.0 + +2.19.0 (24-Apr-2025) + +#307: Instant precision should be retained in ObjectMapper#readTree (add + `JsonNodeFeature.USE_BIG_DECIMAL_FOR_FLOATS` to force use of `BigDecimal` + for `JsonNode` reads) + (reported by @jzheaux) +#336: Optimize `InstantDeserializer` `addInColonToOffsetIfMissing()` + (contributed by David S) +#337: Negative `Duration` does not round-trip properly with + `WRITE_DURATIONS_AS_TIMESTAMPS` enabled + (reported by Joey M) + (fix by Joo-Hyuk K) +#342: Lenient deserialization of `LocalDate`, `LocalDateTime` + is not time-zone aware + (contributed by Henning P) +#364: Deserialization of Month in ONE_BASED_MONTHS mode fails for value "12" + (reported, fix contributed by Boleslav B) + 2.18.10 (15-Aug-2026) #387: Validate length of input in `InstantDeserializer`