Repository: johnzon Updated Branches: refs/heads/master 2ffe0f697 -> 092848f20
JOHNZON-144 add a unit test to make the problem clear Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/6b67b885 Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/6b67b885 Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/6b67b885 Branch: refs/heads/master Commit: 6b67b8850cf8c9c7f25f9908faecf51f699f38b3 Parents: 2ffe0f6 Author: Mark Struberg <[email protected]> Authored: Mon Nov 6 17:57:57 2017 +0100 Committer: Mark Struberg <[email protected]> Committed: Mon Nov 6 17:57:57 2017 +0100 ---------------------------------------------------------------------- .../jsonb/JohnzonConverterInJsonbTest.java | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/johnzon/blob/6b67b885/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonConverterInJsonbTest.java ---------------------------------------------------------------------- diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonConverterInJsonbTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonConverterInJsonbTest.java new file mode 100644 index 0000000..75023d3 --- /dev/null +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/JohnzonConverterInJsonbTest.java @@ -0,0 +1,79 @@ +/* + * 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 + * + * http://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. + */ +package org.apache.johnzon.jsonb; + +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; + +import javax.json.bind.Jsonb; +import javax.json.bind.spi.JsonbProvider; + +import org.apache.johnzon.mapper.Converter; +import org.apache.johnzon.mapper.JohnzonConverter; +import org.junit.Test; +import static org.junit.Assert.assertNotNull; + +/** + * Test that a Johnzon Converter works even in JsonB mode + */ +public class JohnzonConverterInJsonbTest { + + @Test + public void testJohnzonConverter() { + TestDTO dto = new TestDTO(); + dto.setInstant(Instant.now()); + + Jsonb jsonb = JsonbProvider.provider().create().build(); + String json = jsonb.toJson(dto); + assertNotNull(json); + } + + public static class TestDTO { + @JohnzonConverter(TestInstantConverter.class) + private Instant instant; + + public Instant getInstant() { + return instant; + } + + public void setInstant(Instant instant) { + this.instant = instant; + } + } + + + public static class TestInstantConverter implements Converter<Instant> { + public static final DateTimeFormatter FORMAT = DateTimeFormatter.ofPattern( + "yyyy-MM-dd HH:mm:ss.SSS"); + + @Override + public String toString(Instant date) { + return FORMAT.withZone(ZoneId.of("UTC")).format(date); + } + + @Override + public Instant fromString(String dateStr) { + return LocalDateTime.parse(dateStr, FORMAT) + .atZone(ZoneId.of("UTC")) + .toInstant(); + } + + } + +}
