|
1 | 1 | package com.fasterxml.jackson.datatype.jsr310.ser;
|
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.annotation.JsonFormat; |
3 | 4 | import com.fasterxml.jackson.databind.ObjectMapper;
|
4 | 5 | import com.fasterxml.jackson.databind.ObjectWriter;
|
5 | 6 | import com.fasterxml.jackson.databind.SerializationFeature;
|
@@ -161,4 +162,185 @@ public void testSerializationWithTypeInfo03() throws Exception
|
161 | 162 | assertEquals("The value is not correct.",
|
162 | 163 | "[\"" + Duration.class.getName() + "\",\"" + duration.toString() + "\"]", value);
|
163 | 164 | }
|
| 165 | + |
| 166 | + /* |
| 167 | + /********************************************************** |
| 168 | + /* Tests for custom patterns (modules-java8#189) |
| 169 | + /********************************************************** |
| 170 | + */ |
| 171 | + |
| 172 | + @Test |
| 173 | + public void shouldSerializeInNanos_whenSetAsPattern() throws Exception |
| 174 | + { |
| 175 | + ObjectMapper mapper = _mapperForPatternOverride("NANOS"); |
| 176 | + |
| 177 | + Duration duration = Duration.ofHours(1); |
| 178 | + String value = mapper.writeValueAsString(duration); |
| 179 | + |
| 180 | + assertEquals("The value is not correct.", "3600000000000", value); |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + public void shouldSerializeInMicros_whenSetAsPattern() throws Exception |
| 185 | + { |
| 186 | + ObjectMapper mapper = _mapperForPatternOverride("MICROS"); |
| 187 | + |
| 188 | + Duration duration = Duration.ofMillis(1); |
| 189 | + String value = mapper.writeValueAsString(duration); |
| 190 | + |
| 191 | + assertEquals("The value is not correct.", "1000", value); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + public void shouldSerializeInMicrosDiscardingFractions_whenSetAsPattern() throws Exception |
| 196 | + { |
| 197 | + ObjectMapper mapper = _mapperForPatternOverride("MICROS"); |
| 198 | + |
| 199 | + Duration duration = Duration.ofNanos(1500); |
| 200 | + String value = mapper.writeValueAsString(duration); |
| 201 | + |
| 202 | + assertEquals("The value is not correct.", "1", value); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void shouldSerializeInMillis_whenSetAsPattern() throws Exception |
| 207 | + { |
| 208 | + ObjectMapper mapper = _mapperForPatternOverride("MILLIS"); |
| 209 | + |
| 210 | + Duration duration = Duration.ofSeconds(1); |
| 211 | + String value = mapper.writeValueAsString(duration); |
| 212 | + |
| 213 | + assertEquals("The value is not correct.", "1000", value); |
| 214 | + } |
| 215 | + |
| 216 | + @Test |
| 217 | + public void shouldSerializeInMillisDiscardingFractions_whenSetAsPattern() throws Exception |
| 218 | + { |
| 219 | + ObjectMapper mapper = _mapperForPatternOverride("MILLIS"); |
| 220 | + |
| 221 | + Duration duration = Duration.ofNanos(1500000); |
| 222 | + String value = mapper.writeValueAsString(duration); |
| 223 | + |
| 224 | + assertEquals("The value is not correct.", "1", value); |
| 225 | + } |
| 226 | + |
| 227 | + @Test |
| 228 | + public void shouldSerializeInSeconds_whenSetAsPattern() throws Exception |
| 229 | + { |
| 230 | + ObjectMapper mapper = _mapperForPatternOverride("SECONDS"); |
| 231 | + |
| 232 | + Duration duration = Duration.ofMinutes(1); |
| 233 | + String value = mapper.writeValueAsString(duration); |
| 234 | + |
| 235 | + assertEquals("The value is not correct.", "60", value); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void shouldSerializeInSecondsDiscardingFractions_whenSetAsPattern() throws Exception |
| 240 | + { |
| 241 | + ObjectMapper mapper = _mapperForPatternOverride("SECONDS"); |
| 242 | + |
| 243 | + Duration duration = Duration.ofMillis(1500); |
| 244 | + String value = mapper.writeValueAsString(duration); |
| 245 | + |
| 246 | + assertEquals("The value is not correct.", "1", value); |
| 247 | + } |
| 248 | + |
| 249 | + @Test |
| 250 | + public void shouldSerializeInMinutes_whenSetAsPattern() throws Exception |
| 251 | + { |
| 252 | + ObjectMapper mapper = _mapperForPatternOverride("MINUTES"); |
| 253 | + |
| 254 | + Duration duration = Duration.ofHours(1); |
| 255 | + String value = mapper.writeValueAsString(duration); |
| 256 | + |
| 257 | + assertEquals("The value is not correct.", "60", value); |
| 258 | + } |
| 259 | + |
| 260 | + @Test |
| 261 | + public void shouldSerializeInMinutesDiscardingFractions_whenSetAsPattern() throws Exception |
| 262 | + { |
| 263 | + ObjectMapper mapper = _mapperForPatternOverride("MINUTES"); |
| 264 | + |
| 265 | + Duration duration = Duration.ofSeconds(90); |
| 266 | + String value = mapper.writeValueAsString(duration); |
| 267 | + |
| 268 | + assertEquals("The value is not correct.", "1", value); |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + public void shouldSerializeInHours_whenSetAsPattern() throws Exception |
| 273 | + { |
| 274 | + ObjectMapper mapper = _mapperForPatternOverride("HOURS"); |
| 275 | + |
| 276 | + Duration duration = Duration.ofDays(1); |
| 277 | + String value = mapper.writeValueAsString(duration); |
| 278 | + |
| 279 | + assertEquals("The value is not correct.", "24", value); |
| 280 | + } |
| 281 | + |
| 282 | + @Test |
| 283 | + public void shouldSerializeInHoursDiscardingFractions_whenSetAsPattern() throws Exception |
| 284 | + { |
| 285 | + ObjectMapper mapper = _mapperForPatternOverride("HOURS"); |
| 286 | + |
| 287 | + Duration duration = Duration.ofMinutes(90); |
| 288 | + String value = mapper.writeValueAsString(duration); |
| 289 | + |
| 290 | + assertEquals("The value is not correct.", "1", value); |
| 291 | + } |
| 292 | + |
| 293 | + @Test |
| 294 | + public void shouldSerializeInHalfDays_whenSetAsPattern() throws Exception |
| 295 | + { |
| 296 | + ObjectMapper mapper = _mapperForPatternOverride("HALF_DAYS"); |
| 297 | + |
| 298 | + Duration duration = Duration.ofDays(1); |
| 299 | + String value = mapper.writeValueAsString(duration); |
| 300 | + |
| 301 | + assertEquals("The value is not correct.", "2", value); |
| 302 | + } |
| 303 | + |
| 304 | + @Test |
| 305 | + public void shouldSerializeInHalfDaysDiscardingFractions_whenSetAsPattern() throws Exception |
| 306 | + { |
| 307 | + ObjectMapper mapper = _mapperForPatternOverride("DAYS"); |
| 308 | + |
| 309 | + Duration duration = Duration.ofHours(30); |
| 310 | + String value = mapper.writeValueAsString(duration); |
| 311 | + |
| 312 | + assertEquals("The value is not correct.", "1", value); |
| 313 | + } |
| 314 | + |
| 315 | + @Test |
| 316 | + public void shouldSerializeInDays_whenSetAsPattern() throws Exception |
| 317 | + { |
| 318 | + ObjectMapper mapper = _mapperForPatternOverride("DAYS"); |
| 319 | + |
| 320 | + Duration duration = Duration.ofDays(1); |
| 321 | + String value = mapper.writeValueAsString(duration); |
| 322 | + |
| 323 | + assertEquals("The value is not correct.", "1", value); |
| 324 | + } |
| 325 | + |
| 326 | + @Test |
| 327 | + public void shouldSerializeInDaysDiscardingFractions_whenSetAsPattern() throws Exception |
| 328 | + { |
| 329 | + ObjectMapper mapper = _mapperForPatternOverride("DAYS"); |
| 330 | + |
| 331 | + Duration duration = Duration.ofHours(36); |
| 332 | + String value = mapper.writeValueAsString(duration); |
| 333 | + |
| 334 | + assertEquals("The value is not correct.", "1", value); |
| 335 | + } |
| 336 | + |
| 337 | + protected ObjectMapper _mapperForPatternOverride(String pattern) { |
| 338 | + ObjectMapper mapper = mapperBuilder() |
| 339 | + .enable(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS) |
| 340 | + .disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS) |
| 341 | + .build(); |
| 342 | + mapper.configOverride(Duration.class) |
| 343 | + .setFormat(JsonFormat.Value.forPattern(pattern)); |
| 344 | + return mapper; |
| 345 | + } |
164 | 346 | }
|
0 commit comments