ZoneIdDeserTest.java
/*
* Copyright 2013 FasterXML.com
*
* Licensed 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 com.fasterxml.jackson.datatype.jsr310.deser;
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;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.fasterxml.jackson.datatype.jsr310.MockObjectConfiguration;
import com.fasterxml.jackson.datatype.jsr310.ModuleTestBase;
import static org.junit.jupiter.api.Assertions.*;
public class ZoneIdDeserTest extends ModuleTestBase
{
private ObjectMapper MAPPER = newMapper();
private final TypeReference<Map<String, ZoneId>> MAP_TYPE_REF = new TypeReference<Map<String, ZoneId>>() { };
private final ObjectMapper MOCK_OBJECT_MIXIN_MAPPER = mapperBuilder()
.addMixIn(ZoneId.class, MockObjectConfiguration.class)
.build();
@Test
public void testDeserialization01() throws Exception
{
assertEquals(ZoneId.of("America/Chicago"),
MAPPER.readValue("\"America/Chicago\"", ZoneId.class),
"The value is not correct.");
}
@Test
public void testDeserialization02() throws Exception
{
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(ZoneId.of("America/Denver"), value,
"The value is not correct.");
}
/*
/**********************************************************
/* Tests for empty string handling
/**********************************************************
*/
@Test
public void testLenientDeserializeFromEmptyString() throws Exception {
String key = "zoneId";
ObjectMapper mapper = newMapper();
ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String valueFromNullStr = mapper.writeValueAsString(asMap(key, null));
Map<String, ZoneId> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
ZoneId actualDateFromNullStr = actualMapFromNullStr.get(key);
assertNull(actualDateFromNullStr);
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, ""));
Map<String, ZoneId> actualMapFromEmptyStr = objectReader.readValue(valueFromEmptyStr);
ZoneId actualDateFromEmptyStr = actualMapFromEmptyStr.get(key);
assertEquals(null, actualDateFromEmptyStr, "empty string failed to deserialize to null with lenient setting");
}
@Test
public void testStrictDeserializeFromEmptyString() throws Exception {
final String key = "zoneId";
final ObjectMapper mapper = mapperBuilder().build();
mapper.configOverride(ZoneId.class)
.setFormat(JsonFormat.Value.forLeniency(false));
final ObjectReader objectReader = mapper.readerFor(MAP_TYPE_REF);
String valueFromNullStr = mapper.writeValueAsString(asMap(key, null));
Map<String, ZoneId> actualMapFromNullStr = objectReader.readValue(valueFromNullStr);
assertNull(actualMapFromNullStr.get(key));
String valueFromEmptyStr = mapper.writeValueAsString(asMap(key, ""));
assertThrows(MismatchedInputException.class, () -> objectReader.readValue(valueFromEmptyStr));
}
}