Skip to content

Commit dc9ecc1

Browse files
khatwaniNikhilpivovarit
authored andcommitted
Added test cases for BAEL-897 (eugenp#2436)
1 parent 0de69c3 commit dc9ecc1

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

‎jackson/src/test/java/com/baeldung/jackson/test/JacksonDeserializationUnitTest.java‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
package com.baeldung.jackson.test;
22

3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.hamcrest.CoreMatchers.not;
35
import static org.hamcrest.Matchers.equalTo;
46
import static org.hamcrest.Matchers.notNullValue;
57
import static org.junit.Assert.assertNotNull;
68
import static org.junit.Assert.assertThat;
79

810
import java.io.IOException;
11+
import java.time.ZoneId;
12+
import java.time.ZonedDateTime;
13+
14+
import org.junit.Test;
915

1016
import com.baeldung.jackson.deserialization.ItemDeserializer;
1117
import com.baeldung.jackson.dtos.Item;
1218
import com.baeldung.jackson.dtos.ItemWithSerializer;
1319
import com.baeldung.jackson.dtos.MyDto;
1420
import com.baeldung.jackson.dtos.ignore.MyDtoIgnoreUnknown;
15-
import org.junit.Test;
16-
1721
import com.fasterxml.jackson.core.JsonFactory;
1822
import com.fasterxml.jackson.core.JsonParseException;
1923
import com.fasterxml.jackson.core.JsonParser;
2024
import com.fasterxml.jackson.databind.DeserializationFeature;
2125
import com.fasterxml.jackson.databind.JsonMappingException;
2226
import com.fasterxml.jackson.databind.JsonNode;
2327
import com.fasterxml.jackson.databind.ObjectMapper;
28+
import com.fasterxml.jackson.databind.SerializationFeature;
2429
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
2530
import com.fasterxml.jackson.databind.module.SimpleModule;
2631

@@ -165,4 +170,35 @@ public final void givenDeserializerIsOnClass_whenDeserializingCustomRepresentati
165170
assertThat(readValue, notNullValue());
166171
}
167172

173+
@Test
174+
public void whenDeserialisingZonedDateTimeWithDefaults_thenTimeZoneIsNotPreserved() throws IOException {
175+
ObjectMapper objectMapper = new ObjectMapper();
176+
objectMapper.findAndRegisterModules();
177+
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
178+
// construct a new instance of ZonedDateTime
179+
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
180+
String converted = objectMapper.writeValueAsString(now);
181+
// restore an instance of ZonedDateTime from String
182+
ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
183+
System.out.println("serialized: " + now);
184+
System.out.println("restored: " + restored);
185+
assertThat(now, is(not(restored)));
186+
}
187+
188+
@Test
189+
public void whenDeserialisingZonedDateTimeWithFeaturesDisabled_thenTimeZoneIsPreserved() throws IOException {
190+
ObjectMapper objectMapper = new ObjectMapper();
191+
objectMapper.findAndRegisterModules();
192+
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
193+
objectMapper.disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE);
194+
// construct a new instance of ZonedDateTime
195+
ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
196+
String converted = objectMapper.writeValueAsString(now);
197+
// restore an instance of ZonedDateTime from String
198+
ZonedDateTime restored = objectMapper.readValue(converted, ZonedDateTime.class);
199+
System.out.println("serialized: " + now);
200+
System.out.println("restored: " + restored);
201+
assertThat(now, is(restored));
202+
}
203+
168204
}

0 commit comments

Comments
 (0)