Remove TestObjectMapper in favor of DefaultObjectMapper. (#15769)

Remove dilemma on what object mapper class to use in tests since the
DefaultObjectMapper class provides all the same settings and goodies.
This commit is contained in:
Abhishek Radhakrishnan 2024-01-26 16:35:12 -08:00 committed by GitHub
parent ba07965580
commit f58fd5b75f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 43 additions and 156 deletions

View File

@ -46,12 +46,12 @@ public class DefaultObjectMapper extends ObjectMapper
public DefaultObjectMapper()
{
this((JsonFactory) null, null);
this(null, null);
}
public DefaultObjectMapper(String serviceName)
{
this((JsonFactory) null, serviceName);
this(null, serviceName);
}
public DefaultObjectMapper(DefaultObjectMapper mapper)

View File

@ -1,70 +0,0 @@
/*
* 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.druid;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.apache.druid.java.util.common.Intervals;
import org.joda.time.Interval;
import java.io.IOException;
/**
*/
public class TestObjectMapper extends ObjectMapper
{
public TestObjectMapper()
{
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
configure(MapperFeature.AUTO_DETECT_GETTERS, false);
configure(MapperFeature.AUTO_DETECT_FIELDS, false);
configure(MapperFeature.AUTO_DETECT_IS_GETTERS, false);
configure(MapperFeature.AUTO_DETECT_SETTERS, false);
configure(SerializationFeature.INDENT_OUTPUT, false);
registerModule(new TestModule());
}
public static class TestModule extends SimpleModule
{
TestModule()
{
addSerializer(Interval.class, ToStringSerializer.instance);
addDeserializer(
Interval.class,
new StdDeserializer<Interval>(Interval.class)
{
@Override
public Interval deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException
{
return Intervals.of(jsonParser.getText());
}
}
);
}
}
}

View File

@ -20,7 +20,7 @@
package org.apache.druid.data.input.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Test;
@ -30,7 +30,7 @@ import java.util.Collections;
public class DelimitedParseSpecTest
{
private final ObjectMapper jsonMapper = new TestObjectMapper();
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Test
public void testSerde() throws IOException

View File

@ -23,9 +23,9 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.data.input.ByteBufferInputRowParser;
import org.apache.druid.data.input.InputRow;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.parsers.JSONPathFieldSpec;
@ -42,7 +42,7 @@ import java.util.List;
public class InputRowParserSerdeTest
{
private final ObjectMapper jsonMapper = new TestObjectMapper();
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Test
public void testStringInputRowParserSerde() throws Exception

View File

@ -22,7 +22,7 @@ package org.apache.druid.data.input.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.parsers.JSONPathFieldSpec;
import org.apache.druid.java.util.common.parsers.JSONPathFieldType;
import org.apache.druid.java.util.common.parsers.JSONPathSpec;
@ -39,7 +39,7 @@ import java.util.Map;
public class JSONParseSpecTest
{
private final ObjectMapper jsonMapper = new TestObjectMapper();
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Test
public void testParseRow()

View File

@ -20,7 +20,7 @@
package org.apache.druid.data.input.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.parsers.JSONPathFieldSpec;
import org.apache.druid.java.util.common.parsers.JSONPathFieldType;
import org.apache.druid.java.util.common.parsers.JSONPathSpec;
@ -33,7 +33,7 @@ import java.util.List;
public class JSONPathSpecTest
{
private final ObjectMapper jsonMapper = new TestObjectMapper();
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Test
public void testSerde() throws IOException

View File

@ -22,7 +22,7 @@ package org.apache.druid.data.input.impl;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.parsers.Parser;
import org.apache.druid.js.JavaScriptConfig;
import org.junit.Assert;
@ -38,7 +38,7 @@ import java.util.Map;
*/
public class JavaScriptParseSpecTest
{
private final ObjectMapper jsonMapper = new TestObjectMapper();
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Rule
public ExpectedException expectedException = ExpectedException.none();

View File

@ -20,7 +20,7 @@
package org.apache.druid.data.input.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Test;
@ -31,7 +31,7 @@ import java.util.Collections;
*/
public class RegexParseSpecTest
{
private final ObjectMapper jsonMapper = new TestObjectMapper();
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Test
public void testSerde() throws IOException

View File

@ -24,7 +24,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
@ -43,7 +43,7 @@ import java.util.Set;
public class JsonConfiguratorTest
{
private static final String PROP_PREFIX = "test.property.prefix.";
private final ObjectMapper mapper = new TestObjectMapper();
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
private final Properties properties = new Properties();
@Rule
@ -55,7 +55,7 @@ public class JsonConfiguratorTest
@Before
public void setUp()
{
mapper.registerSubtypes(MappableObject.class);
jsonMapper.registerSubtypes(MappableObject.class);
}
final Validator validator = new Validator()
@ -118,7 +118,7 @@ public class JsonConfiguratorTest
@Test
public void testSimpleConfigurate()
{
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1", "prop1");
properties.setProperty(PROP_PREFIX + "prop1List", "[\"prop2\"]");
final MappableObject obj = configurator.configurate(properties, PROP_PREFIX, MappableObject.class);
@ -129,7 +129,7 @@ public class JsonConfiguratorTest
@Test
public void testMissingConfigList()
{
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1", "prop1");
final MappableObject obj = configurator.configurate(properties, PROP_PREFIX, MappableObject.class);
Assert.assertEquals("prop1", obj.prop1);
@ -139,7 +139,7 @@ public class JsonConfiguratorTest
@Test
public void testMissingConfig()
{
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1List", "[\"prop2\"]");
final MappableObject obj = configurator.configurate(properties, PROP_PREFIX, MappableObject.class);
Assert.assertNull(obj.prop1);
@ -149,7 +149,7 @@ public class JsonConfiguratorTest
@Test
public void testQuotedConfig()
{
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1", "testing \"prop1\"");
final MappableObject obj = configurator.configurate(properties, PROP_PREFIX, MappableObject.class);
Assert.assertEquals("testing \"prop1\"", obj.prop1);
@ -159,7 +159,7 @@ public class JsonConfiguratorTest
@Test
public void testPropertyWithDot()
{
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop2.prop.2", "testing");
properties.setProperty(PROP_PREFIX + "prop1", "prop1");
final MappableObject obj = configurator.configurate(properties, PROP_PREFIX, MappableObject.class);
@ -176,7 +176,7 @@ public class JsonConfiguratorTest
List<String> list = ImmutableList.of("list", "of", "strings");
environmentVariables.set("MY_VAR", "value2");
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1", "${sys:my.property}");
properties.setProperty(PROP_PREFIX + "prop1List", "${file:UTF-8:src/test/resources/list.json}");
properties.setProperty(PROP_PREFIX + "prop2.prop.2", "${env:MY_VAR}");
@ -197,7 +197,7 @@ public class JsonConfiguratorTest
System.setProperty("json.path", "src/test/resources/list.json");
environmentVariables.set("PROP2_NAME", "MY_VAR");
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1", "${sys:${env:SYS_PROP}}");
properties.setProperty(PROP_PREFIX + "prop1List", "${file:UTF-8:${sys:json.path}}");
properties.setProperty(PROP_PREFIX + "prop2.prop.2", "${env:${env:PROP2_NAME}}");
@ -212,7 +212,7 @@ public class JsonConfiguratorTest
{
List<String> list = ImmutableList.of("list", "of", "strings");
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1", "${sys:my.property:-value1}");
properties.setProperty(PROP_PREFIX + "prop1List", "${unknown:-[\"list\", \"of\", \"strings\"]}");
properties.setProperty(PROP_PREFIX + "prop2.prop.2", "${MY_VAR:-value2}");
@ -225,7 +225,7 @@ public class JsonConfiguratorTest
@Test
public void testPropertyInterpolationUndefinedException()
{
final JsonConfigurator configurator = new JsonConfigurator(mapper, validator);
final JsonConfigurator configurator = new JsonConfigurator(jsonMapper, validator);
properties.setProperty(PROP_PREFIX + "prop1", "${sys:my.property}");
Assert.assertThrows(

View File

@ -19,15 +19,9 @@
package org.apache.druid.indexer;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.DateTimes;
import org.joda.time.DateTime;
import org.junit.Assert;
import org.junit.Test;
@ -35,15 +29,11 @@ import java.io.IOException;
public class TaskStatusPlusTest
{
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Test
public void testSerde() throws IOException
{
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(
new SimpleModule()
.addDeserializer(DateTime.class, new DateTimeDeserializer())
.addSerializer(DateTime.class, ToStringSerializer.instance)
);
final TaskStatusPlus status = new TaskStatusPlus(
"testId",
"testGroupId",
@ -57,19 +47,13 @@ public class TaskStatusPlusTest
"ds_test",
null
);
final String json = mapper.writeValueAsString(status);
Assert.assertEquals(status, mapper.readValue(json, TaskStatusPlus.class));
final String json = jsonMapper.writeValueAsString(status);
Assert.assertEquals(status, jsonMapper.readValue(json, TaskStatusPlus.class));
}
@Test
public void testJsonAttributes() throws IOException
{
final ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(
new SimpleModule()
.addDeserializer(DateTime.class, new DateTimeDeserializer())
.addSerializer(DateTime.class, ToStringSerializer.instance)
);
final String json = "{\n"
+ "\"id\": \"testId\",\n"
+ "\"groupId\": \"testGroupId\",\n"
@ -88,43 +72,16 @@ public class TaskStatusPlusTest
+ "\"dataSource\": \"ds_test\",\n"
+ "\"errorMsg\": null\n"
+ "}";
TaskStatusPlus taskStatusPlus = mapper.readValue(json, TaskStatusPlus.class);
TaskStatusPlus taskStatusPlus = jsonMapper.readValue(json, TaskStatusPlus.class);
Assert.assertNotNull(taskStatusPlus);
Assert.assertNotNull(taskStatusPlus.getStatusCode());
Assert.assertTrue(taskStatusPlus.getStatusCode().isRunnable());
Assert.assertNotNull(taskStatusPlus.getRunnerStatusCode());
String serialized = mapper.writeValueAsString(taskStatusPlus);
String serialized = jsonMapper.writeValueAsString(taskStatusPlus);
Assert.assertTrue(serialized.contains("\"status\":"));
Assert.assertTrue(serialized.contains("\"statusCode\":"));
Assert.assertTrue(serialized.contains("\"runnerStatusCode\":"));
}
// Copied from org.apache.druid.jackson.JodaStuff
private static class DateTimeDeserializer extends StdDeserializer<DateTime>
{
public DateTimeDeserializer()
{
super(DateTime.class);
}
@Override
public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
{
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NUMBER_INT) {
return DateTimes.utc(jp.getLongValue());
}
if (t == JsonToken.VALUE_STRING) {
String str = jp.getText().trim();
if (str.length() == 0) { // [JACKSON-360]
return null;
}
// make sure to preserve time zone information when parsing timestamps
return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str);
}
throw ctxt.mappingException(getValueClass());
}
}
}

View File

@ -24,10 +24,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.RangeSet;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.data.input.impl.DimensionsSpec;
import org.apache.druid.indexer.partitions.DynamicPartitionsSpec;
import org.apache.druid.indexer.partitions.HashedPartitionsSpec;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.DateTimes;
import org.apache.druid.java.util.common.Intervals;
import org.apache.druid.java.util.common.jackson.JacksonUtils;
@ -52,7 +52,7 @@ import java.util.Map;
*/
public class DataSegmentTest
{
private static final ObjectMapper MAPPER = new TestObjectMapper();
private static final ObjectMapper MAPPER = new DefaultObjectMapper();
private static final int TEST_VERSION = 0x9;
private static ShardSpec getShardSpec(final int partitionNum)

View File

@ -25,8 +25,8 @@ import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.collect.ImmutableMap;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.CommaListJoinDeserializer;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.apache.druid.java.util.common.Intervals;
import org.apache.druid.java.util.common.jackson.JacksonUtils;
import org.apache.druid.timeline.DataSegment.PruneSpecsHolder;
@ -56,7 +56,7 @@ public class SegmentStatusInClusterTest
private static ObjectMapper createObjectMapper()
{
ObjectMapper objectMapper = new TestObjectMapper();
ObjectMapper objectMapper = new DefaultObjectMapper();
InjectableValues.Std injectableValues = new InjectableValues.Std();
injectableValues.addValue(PruneSpecsHolder.class, PruneSpecsHolder.DEFAULT);
objectMapper.setInjectableValues(injectableValues);

View File

@ -20,7 +20,7 @@
package org.apache.druid.timeline.partition;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.druid.TestObjectMapper;
import org.apache.druid.jackson.DefaultObjectMapper;
import org.junit.Assert;
import org.junit.Test;
@ -28,6 +28,8 @@ import java.io.IOException;
public class NoneShardSpecTest
{
private final ObjectMapper jsonMapper = new DefaultObjectMapper();
@Test
public void testEqualsAndHashCode()
{
@ -41,9 +43,8 @@ public class NoneShardSpecTest
public void testSerde() throws Exception
{
final NoneShardSpec one = NoneShardSpec.instance();
ObjectMapper mapper = new TestObjectMapper();
NoneShardSpec serde1 = mapper.readValue(mapper.writeValueAsString(one), NoneShardSpec.class);
NoneShardSpec serde2 = mapper.readValue(mapper.writeValueAsString(one), NoneShardSpec.class);
NoneShardSpec serde1 = jsonMapper.readValue(jsonMapper.writeValueAsString(one), NoneShardSpec.class);
NoneShardSpec serde2 = jsonMapper.readValue(jsonMapper.writeValueAsString(one), NoneShardSpec.class);
// Serde should return same object instead of creating new one every time.
Assert.assertTrue(serde1 == serde2);
@ -55,8 +56,7 @@ public class NoneShardSpecTest
public void testPartitionFieldIgnored() throws IOException
{
final String jsonStr = "{\"type\": \"none\",\"partitionNum\": 2}";
ObjectMapper mapper = new TestObjectMapper();
final ShardSpec noneShardSpec = mapper.readValue(jsonStr, ShardSpec.class);
final ShardSpec noneShardSpec = jsonMapper.readValue(jsonStr, ShardSpec.class);
Assert.assertEquals(NoneShardSpec.instance(), noneShardSpec);
}
}