|
| 1 | +// |
| 2 | +// MessagePack for Java |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | +package org.msgpack.jackson.dataformat; |
| 17 | + |
| 18 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import org.msgpack.core.MessagePack; |
| 22 | +import org.msgpack.core.MessagePacker; |
| 23 | +import org.msgpack.core.MessageUnpacker; |
| 24 | + |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.time.Instant; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | + |
| 31 | +public class TimestampExtensionModuleTest |
| 32 | +{ |
| 33 | + private final ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory()); |
| 34 | + private final SingleInstant singleInstant = new SingleInstant(); |
| 35 | + private final TripleInstants tripleInstants = new TripleInstants(); |
| 36 | + |
| 37 | + private static class SingleInstant |
| 38 | + { |
| 39 | + public Instant instant; |
| 40 | + } |
| 41 | + |
| 42 | + private static class TripleInstants |
| 43 | + { |
| 44 | + public Instant a; |
| 45 | + public Instant b; |
| 46 | + public Instant c; |
| 47 | + } |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setUp() |
| 51 | + throws Exception |
| 52 | + { |
| 53 | + objectMapper.registerModule(TimestampExtensionModule.INSTANCE); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void testSingleInstantPojo() |
| 58 | + throws IOException |
| 59 | + { |
| 60 | + singleInstant.instant = Instant.now(); |
| 61 | + byte[] bytes = objectMapper.writeValueAsBytes(singleInstant); |
| 62 | + SingleInstant deserialized = objectMapper.readValue(bytes, SingleInstant.class); |
| 63 | + assertEquals(singleInstant.instant, deserialized.instant); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testTripleInstantsPojo() |
| 68 | + throws IOException |
| 69 | + { |
| 70 | + Instant now = Instant.now(); |
| 71 | + tripleInstants.a = now.minusSeconds(1); |
| 72 | + tripleInstants.b = now; |
| 73 | + tripleInstants.c = now.plusSeconds(1); |
| 74 | + byte[] bytes = objectMapper.writeValueAsBytes(tripleInstants); |
| 75 | + TripleInstants deserialized = objectMapper.readValue(bytes, TripleInstants.class); |
| 76 | + assertEquals(now.minusSeconds(1), deserialized.a); |
| 77 | + assertEquals(now, deserialized.b); |
| 78 | + assertEquals(now.plusSeconds(1), deserialized.c); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void serialize32BitFormat() |
| 83 | + throws IOException |
| 84 | + { |
| 85 | + singleInstant.instant = Instant.ofEpochSecond(Instant.now().getEpochSecond()); |
| 86 | + |
| 87 | + byte[] bytes = objectMapper.writeValueAsBytes(singleInstant); |
| 88 | + |
| 89 | + // Check the size of serialized data first |
| 90 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 91 | + unpacker.unpackMapHeader(); |
| 92 | + assertEquals("instant", unpacker.unpackString()); |
| 93 | + assertEquals(4, unpacker.unpackExtensionTypeHeader().getLength()); |
| 94 | + } |
| 95 | + |
| 96 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 97 | + unpacker.unpackMapHeader(); |
| 98 | + unpacker.unpackString(); |
| 99 | + assertEquals(singleInstant.instant, unpacker.unpackTimestamp()); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void serialize64BitFormat() |
| 105 | + throws IOException |
| 106 | + { |
| 107 | + singleInstant.instant = Instant.ofEpochSecond(Instant.now().getEpochSecond(), 1234); |
| 108 | + |
| 109 | + byte[] bytes = objectMapper.writeValueAsBytes(singleInstant); |
| 110 | + |
| 111 | + // Check the size of serialized data first |
| 112 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 113 | + unpacker.unpackMapHeader(); |
| 114 | + assertEquals("instant", unpacker.unpackString()); |
| 115 | + assertEquals(8, unpacker.unpackExtensionTypeHeader().getLength()); |
| 116 | + } |
| 117 | + |
| 118 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 119 | + unpacker.unpackMapHeader(); |
| 120 | + unpacker.unpackString(); |
| 121 | + assertEquals(singleInstant.instant, unpacker.unpackTimestamp()); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void serialize96BitFormat() |
| 127 | + throws IOException |
| 128 | + { |
| 129 | + singleInstant.instant = Instant.ofEpochSecond(19880866800L /* 2600-01-01 */, 1234); |
| 130 | + |
| 131 | + byte[] bytes = objectMapper.writeValueAsBytes(singleInstant); |
| 132 | + |
| 133 | + // Check the size of serialized data first |
| 134 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 135 | + unpacker.unpackMapHeader(); |
| 136 | + assertEquals("instant", unpacker.unpackString()); |
| 137 | + assertEquals(12, unpacker.unpackExtensionTypeHeader().getLength()); |
| 138 | + } |
| 139 | + |
| 140 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 141 | + unpacker.unpackMapHeader(); |
| 142 | + unpacker.unpackString(); |
| 143 | + assertEquals(singleInstant.instant, unpacker.unpackTimestamp()); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void deserialize32BitFormat() |
| 149 | + throws IOException |
| 150 | + { |
| 151 | + Instant instant = Instant.ofEpochSecond(Instant.now().getEpochSecond()); |
| 152 | + |
| 153 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 154 | + try (MessagePacker packer = MessagePack.newDefaultPacker(os)) { |
| 155 | + packer.packMapHeader(1) |
| 156 | + .packString("instant") |
| 157 | + .packTimestamp(instant); |
| 158 | + } |
| 159 | + |
| 160 | + byte[] bytes = os.toByteArray(); |
| 161 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 162 | + unpacker.unpackMapHeader(); |
| 163 | + unpacker.unpackString(); |
| 164 | + assertEquals(4, unpacker.unpackExtensionTypeHeader().getLength()); |
| 165 | + } |
| 166 | + |
| 167 | + SingleInstant deserialized = objectMapper.readValue(bytes, SingleInstant.class); |
| 168 | + assertEquals(instant, deserialized.instant); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void deserialize64BitFormat() |
| 173 | + throws IOException |
| 174 | + { |
| 175 | + Instant instant = Instant.ofEpochSecond(Instant.now().getEpochSecond(), 1234); |
| 176 | + |
| 177 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 178 | + try (MessagePacker packer = MessagePack.newDefaultPacker(os)) { |
| 179 | + packer.packMapHeader(1) |
| 180 | + .packString("instant") |
| 181 | + .packTimestamp(instant); |
| 182 | + } |
| 183 | + |
| 184 | + byte[] bytes = os.toByteArray(); |
| 185 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 186 | + unpacker.unpackMapHeader(); |
| 187 | + unpacker.unpackString(); |
| 188 | + assertEquals(8, unpacker.unpackExtensionTypeHeader().getLength()); |
| 189 | + } |
| 190 | + |
| 191 | + SingleInstant deserialized = objectMapper.readValue(bytes, SingleInstant.class); |
| 192 | + assertEquals(instant, deserialized.instant); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + public void deserialize96BitFormat() |
| 197 | + throws IOException |
| 198 | + { |
| 199 | + Instant instant = Instant.ofEpochSecond(19880866800L /* 2600-01-01 */, 1234); |
| 200 | + |
| 201 | + ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 202 | + try (MessagePacker packer = MessagePack.newDefaultPacker(os)) { |
| 203 | + packer.packMapHeader(1) |
| 204 | + .packString("instant") |
| 205 | + .packTimestamp(instant); |
| 206 | + } |
| 207 | + |
| 208 | + byte[] bytes = os.toByteArray(); |
| 209 | + try (MessageUnpacker unpacker = MessagePack.newDefaultUnpacker(bytes)) { |
| 210 | + unpacker.unpackMapHeader(); |
| 211 | + unpacker.unpackString(); |
| 212 | + assertEquals(12, unpacker.unpackExtensionTypeHeader().getLength()); |
| 213 | + } |
| 214 | + |
| 215 | + SingleInstant deserialized = objectMapper.readValue(bytes, SingleInstant.class); |
| 216 | + assertEquals(instant, deserialized.instant); |
| 217 | + } |
| 218 | +} |
0 commit comments