test refactoring
diff --git a/src/test/java/com/fasterxml/jackson/databind/creators/EnumCreatorTest.java b/src/test/java/com/fasterxml/jackson/databind/creators/EnumCreatorTest.java
index f5237d4..eecb300 100644
--- a/src/test/java/com/fasterxml/jackson/databind/creators/EnumCreatorTest.java
+++ b/src/test/java/com/fasterxml/jackson/databind/creators/EnumCreatorTest.java
@@ -1,10 +1,131 @@
package com.fasterxml.jackson.databind.creators;
+import java.math.BigDecimal;
+import java.util.*;
+
import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonGetter;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.deser.Deserializers;
+import com.fasterxml.jackson.databind.deser.std.EnumDeserializer;
+import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
+import com.fasterxml.jackson.databind.module.SimpleModule;
public class EnumCreatorTest extends BaseMapTest
{
+ protected enum EnumWithCreator {
+ A, B;
+
+ @JsonCreator
+ public static EnumWithCreator fromEnum(String str) {
+ if ("enumA".equals(str)) return A;
+ if ("enumB".equals(str)) return B;
+ return null;
+ }
+ }
+
+ protected enum EnumWithBDCreator {
+ E5, E8;
+
+ @JsonCreator
+ public static EnumWithBDCreator create(BigDecimal bd) {
+ if (bd.longValue() == 5L) return E5;
+ if (bd.longValue() == 8L) return E8;
+ return null;
+ }
+ }
+
+ protected enum TestEnumFromInt
+ {
+ ENUM_A(1), ENUM_B(2), ENUM_C(3);
+
+ private final int id;
+
+ private TestEnumFromInt(int id) {
+ this.id = id;
+ }
+
+ @JsonCreator public static TestEnumFromInt fromId(int id) {
+ for (TestEnumFromInt e: values()) {
+ if (e.id == id) return e;
+ }
+ return null;
+ }
+ }
+
+ static enum EnumWithPropertiesModeJsonCreator {
+ TEST1,
+ TEST2,
+ TEST3;
+
+ @JsonGetter("name")
+ public String getName() {
+ return name();
+ }
+
+ @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
+ public static EnumWithPropertiesModeJsonCreator create(@JsonProperty("name") String name) {
+ return EnumWithPropertiesModeJsonCreator.valueOf(name);
+ }
+ }
+
+ static enum EnumWithDelegateModeJsonCreator {
+ TEST1,
+ TEST2,
+ TEST3;
+
+ @JsonGetter("name")
+ public String getName() {
+ return name();
+ }
+
+ @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
+ public static EnumWithDelegateModeJsonCreator create(JsonNode json) {
+ return EnumWithDelegateModeJsonCreator.valueOf(json.get("name").asText());
+ }
+ }
+
+ // [databind#324]: exception from creator method
+ protected enum TestEnum324
+ {
+ A, B;
+
+ @JsonCreator public static TestEnum324 creator(String arg) {
+ throw new RuntimeException("Foobar!");
+ }
+ }
+
+ // [databind#745]
+ static class DelegatingDeserializers extends Deserializers.Base
+ {
+ @Override
+ public JsonDeserializer<?> findEnumDeserializer(final Class<?> type, final DeserializationConfig config, final BeanDescription beanDesc) throws JsonMappingException {
+ final Collection<AnnotatedMethod> factoryMethods = beanDesc.getFactoryMethods();
+ if (factoryMethods != null) {
+ for (AnnotatedMethod am : factoryMethods) {
+ final JsonCreator creator = am.getAnnotation(JsonCreator.class);
+ if (creator != null) {
+ return EnumDeserializer.deserializerForCreator(config, type, am, null, null);
+ }
+ }
+ }
+ return null;
+ }
+ }
+
+ // [databind#745]
+ static class DelegatingDeserializersModule extends SimpleModule
+ {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public void setupModule(final SetupContext context) {
+ context.addDeserializers(new DelegatingDeserializers());
+ }
+ }
+
static enum MyEnum960
{
VALUE, BOGUS;
@@ -18,8 +139,91 @@
static class MyEnum960Wrapper {
public MyEnum960 value;
}
+
+ /*
+ /**********************************************************
+ /* Test methods
+ /**********************************************************
+ */
+
+ protected final ObjectMapper MAPPER = new ObjectMapper();
+
+ public void testCreatorEnums() throws Exception {
+ EnumWithCreator value = MAPPER.readValue("\"enumA\"", EnumWithCreator.class);
+ assertEquals(EnumWithCreator.A, value);
+ }
+
+ public void testCreatorEnumsFromBigDecimal() throws Exception {
+ EnumWithBDCreator value = MAPPER.readValue("\"8.0\"", EnumWithBDCreator.class);
+ assertEquals(EnumWithBDCreator.E8, value);
+ }
+
+ public void testEnumWithCreatorEnumMaps() throws Exception {
+ EnumMap<EnumWithCreator,String> value = MAPPER.readValue("{\"enumA\":\"value\"}",
+ new TypeReference<EnumMap<EnumWithCreator,String>>() {});
+ assertEquals("value", value.get(EnumWithCreator.A));
+ }
+
+ public void testEnumWithCreatorMaps() throws Exception {
+ HashMap<EnumWithCreator,String> value = MAPPER.readValue("{\"enumA\":\"value\"}",
+ new TypeReference<java.util.HashMap<EnumWithCreator,String>>() {});
+ assertEquals("value", value.get(EnumWithCreator.A));
+ }
+
+ public void testEnumWithCreatorEnumSets() throws Exception {
+ EnumSet<EnumWithCreator> value = MAPPER.readValue("[\"enumA\"]",
+ new TypeReference<EnumSet<EnumWithCreator>>() {});
+ assertTrue(value.contains(EnumWithCreator.A));
+ }
+
+ public void testJsonCreatorPropertiesWithEnum() throws Exception
+ {
+ EnumWithPropertiesModeJsonCreator type1 = MAPPER.readValue("{\"name\":\"TEST1\", \"description\":\"TEST\"}", EnumWithPropertiesModeJsonCreator.class);
+ assertSame(EnumWithPropertiesModeJsonCreator.TEST1, type1);
+
+ EnumWithPropertiesModeJsonCreator type2 = MAPPER.readValue("{\"name\":\"TEST3\", \"description\":\"TEST\"}", EnumWithPropertiesModeJsonCreator.class);
+ assertSame(EnumWithPropertiesModeJsonCreator.TEST3, type2);
+
+ }
- private final ObjectMapper MAPPER = new ObjectMapper();
+ public void testJsonCreatorDelagateWithEnum() throws Exception {
+ final ObjectMapper mapper = new ObjectMapper();
+
+ EnumWithDelegateModeJsonCreator type1 = mapper.readValue("{\"name\":\"TEST1\", \"description\":\"TEST\"}", EnumWithDelegateModeJsonCreator.class);
+ assertSame(EnumWithDelegateModeJsonCreator.TEST1, type1);
+
+ EnumWithDelegateModeJsonCreator type2 = mapper.readValue("{\"name\":\"TEST3\", \"description\":\"TEST\"}", EnumWithDelegateModeJsonCreator.class);
+ assertSame(EnumWithDelegateModeJsonCreator.TEST3, type2);
+
+ }
+
+ public void testEnumsFromInts() throws Exception
+ {
+ Object ob = MAPPER.readValue("1 ", TestEnumFromInt.class);
+ assertEquals(TestEnumFromInt.class, ob.getClass());
+ assertSame(TestEnumFromInt.ENUM_A, ob);
+ }
+
+ // [databind#324]
+ public void testExceptionFromCreator() throws Exception
+ {
+ try {
+ /*TestEnum324 e =*/ MAPPER.readValue(quote("xyz"), TestEnum324.class);
+ fail("Should throw exception");
+ } catch (JsonMappingException e) {
+ verifyException(e, "foobar");
+ }
+ }
+
+ // [databind#745]
+ public void testDeserializerForCreatorWithEnumMaps() throws Exception
+ {
+ final ObjectMapper mapper = new ObjectMapper();
+ mapper.registerModule(new DelegatingDeserializersModule());
+ EnumMap<EnumWithCreator,String> value = mapper.readValue("{\"enumA\":\"value\"}",
+ new TypeReference<EnumMap<EnumWithCreator,String>>() {});
+ assertEquals("value", value.get(EnumWithCreator.A));
+ }
// for [databind#960]
public void testNoArgEnumCreator() throws Exception
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/deser/EnumDeserializationTest.java
similarity index 67%
rename from src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java
rename to src/test/java/com/fasterxml/jackson/databind/deser/EnumDeserializationTest.java
index 9ce5d09..11d81c3 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/EnumDeserializationTest.java
@@ -1,7 +1,6 @@
package com.fasterxml.jackson.databind.deser;
import java.io.IOException;
-import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -10,13 +9,11 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
-import com.fasterxml.jackson.databind.deser.std.EnumDeserializer;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
-import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.module.SimpleModule;
@SuppressWarnings("serial")
-public class TestEnumDeserialization
+public class EnumDeserializationTest
extends BaseMapTest
{
enum TestEnum { JACKSON, RULES, OK; }
@@ -48,28 +45,6 @@
return TestEnum.valueOf(jp.getText().toUpperCase());
}
}
-
- protected enum EnumWithCreator {
- A, B;
-
- @JsonCreator
- public static EnumWithCreator fromEnum(String str) {
- if ("enumA".equals(str)) return A;
- if ("enumB".equals(str)) return B;
- return null;
- }
- }
-
- protected enum EnumWithBDCreator {
- E5, E8;
-
- @JsonCreator
- public static EnumWithBDCreator create(BigDecimal bd) {
- if (bd.longValue() == 5L) return E5;
- if (bd.longValue() == 8L) return E8;
- return null;
- }
- }
protected enum LowerCaseEnum {
A, B, C;
@@ -93,61 +68,6 @@
@JsonProperty Map<TestEnum, String> map;
}
- protected enum TestEnumFor834
- {
- ENUM_A(1), ENUM_B(2), ENUM_C(3);
-
- private final int id;
-
- private TestEnumFor834(int id) {
- this.id = id;
- }
-
- @JsonCreator public static TestEnumFor834 fromId(int id) {
- for (TestEnumFor834 e: values()) {
- if (e.id == id) return e;
- }
- return null;
- }
- }
-
- // [Issue#324]: exception from creator method
- protected enum TestEnum324
- {
- A, B;
-
- @JsonCreator public static TestEnum324 creator(String arg) {
- throw new RuntimeException("Foobar!");
- }
- }
-
- // [databind#745]
- static class DelegatingDeserializers extends Deserializers.Base
- {
- @Override
- public JsonDeserializer<?> findEnumDeserializer(final Class<?> type, final DeserializationConfig config, final BeanDescription beanDesc) throws JsonMappingException {
- final Collection<AnnotatedMethod> factoryMethods = beanDesc.getFactoryMethods();
- if (factoryMethods != null) {
- for (AnnotatedMethod am : factoryMethods) {
- final JsonCreator creator = am.getAnnotation(JsonCreator.class);
- if (creator != null) {
- return EnumDeserializer.deserializerForCreator(config, type, am, null, null);
- }
- }
- }
- return null;
- }
- }
-
- // [databind#745]
- static class DelegatingDeserializersModule extends SimpleModule
- {
- @Override
- public void setupModule(final SetupContext context) {
- context.addDeserializers(new DelegatingDeserializers());
- }
- }
-
// [databind#677]
static enum EnumWithPropertyAnno {
@JsonProperty("a")
@@ -194,45 +114,10 @@
return null;
}
}
-
- static enum EnumWithPropertiesModeJsonCreator {
- TEST1,
- TEST2,
- TEST3;
-
- @JsonGetter("name")
- public String getName() {
- return name();
- }
-
- @JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
- public static EnumWithPropertiesModeJsonCreator create(@JsonProperty("name") String name) {
- return EnumWithPropertiesModeJsonCreator.valueOf(name);
- }
-
- }
-
- static enum EnumWithDelegateModeJsonCreator {
- TEST1,
- TEST2,
- TEST3;
-
- @JsonGetter("name")
- public String getName() {
- return name();
- }
-
- @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
- public static EnumWithDelegateModeJsonCreator create(JsonNode json) {
- return EnumWithDelegateModeJsonCreator.valueOf(json.get("name").asText());
- }
-
- }
-
/*
/**********************************************************
- /* Tests
+ /* Test methods
/**********************************************************
*/
@@ -256,12 +141,10 @@
// and no more content beyond that...
assertFalse(jp.hasCurrentToken());
- /* Then alternative with index (0 means first entry)
- */
+ // Then alternative with index (0 means first entry)
assertEquals(TestEnum.JACKSON, MAPPER.readValue(" 0 ", TestEnum.class));
- /* Then error case: unrecognized value
- */
+ // Then error case: unrecognized value
try {
/*Object result =*/ MAPPER.readValue("\"NO-SUCH-VALUE\"", TestEnum.class);
fail("Expected an exception for bogus enum value...");
@@ -308,16 +191,6 @@
assertEquals(EnumWithSubClass.A, value);
}
- public void testCreatorEnums() throws Exception {
- EnumWithCreator value = MAPPER.readValue("\"enumA\"", EnumWithCreator.class);
- assertEquals(EnumWithCreator.A, value);
- }
-
- public void testCreatorEnumsFromBigDecimal() throws Exception {
- EnumWithBDCreator value = MAPPER.readValue("\"8.0\"", EnumWithBDCreator.class);
- assertEquals(EnumWithBDCreator.E8, value);
- }
-
public void testToStringEnums() throws Exception
{
// can't reuse global one due to reconfig
@@ -397,25 +270,7 @@
assertEquals(Integer.valueOf(13), map.get(EnumWithJsonValue.A));
}
- public void testEnumWithCreatorEnumMaps() throws Exception {
- EnumMap<EnumWithCreator,String> value = MAPPER.readValue("{\"enumA\":\"value\"}",
- new TypeReference<EnumMap<EnumWithCreator,String>>() {});
- assertEquals("value", value.get(EnumWithCreator.A));
- }
-
- public void testEnumWithCreatorMaps() throws Exception {
- java.util.HashMap<EnumWithCreator,String> value = MAPPER.readValue("{\"enumA\":\"value\"}",
- new TypeReference<java.util.HashMap<EnumWithCreator,String>>() {});
- assertEquals("value", value.get(EnumWithCreator.A));
- }
-
- public void testEnumWithCreatorEnumSets() throws Exception {
- EnumSet<EnumWithCreator> value = MAPPER.readValue("[\"enumA\"]",
- new TypeReference<EnumSet<EnumWithCreator>>() {});
- assertTrue(value.contains(EnumWithCreator.A));
- }
-
- // [JACKSON-810], ability to ignore unknown Enum values:
+ // Ability to ignore unknown Enum values:
public void testAllowUnknownEnumValuesReadAsNull() throws Exception
{
@@ -452,13 +307,6 @@
}
}
- public void testEnumsFromInts() throws Exception
- {
- Object ob = MAPPER.readValue("1 ", TestEnumFor834.class);
- assertEquals(TestEnumFor834.class, ob.getClass());
- assertSame(TestEnumFor834.ENUM_A, ob);
- }
-
// [databind#141]: allow mapping of empty String into null
public void testEnumsWithEmpty() throws Exception
{
@@ -477,18 +325,7 @@
// not sure this is totally safe but...
assertEquals(TestEnum.JACKSON, mapper.readValue(quote("jackson"), TestEnum.class));
}
-
- // [databind#324]
- public void testExceptionFromCreator() throws Exception
- {
- try {
- /*TestEnum324 e =*/ MAPPER.readValue(quote("xyz"), TestEnum324.class);
- fail("Should throw exception");
- } catch (JsonMappingException e) {
- verifyException(e, "foobar");
- }
- }
-
+
// [databind#381]
public void testUnwrappedEnum() throws Exception {
final ObjectMapper mapper = new ObjectMapper();
@@ -520,16 +357,6 @@
assertSame(TestEnum.values()[1], en);
}
- // [databind#745]
- public void testDeserializerForCreatorWithEnumMaps() throws Exception
- {
- final ObjectMapper mapper = new ObjectMapper();
- mapper.registerModule(new DelegatingDeserializersModule());
- EnumMap<EnumWithCreator,String> value = mapper.readValue("{\"enumA\":\"value\"}",
- new TypeReference<EnumMap<EnumWithCreator,String>>() {});
- assertEquals("value", value.get(EnumWithCreator.A));
- }
-
public void testEnumWithJsonPropertyRename() throws Exception
{
String json = MAPPER.writeValueAsString(new EnumWithPropertyAnno[] {
@@ -587,27 +414,4 @@
EnumWithDefaultAnnoAndConstructor myEnum = mapper.readValue("\"foo\"", EnumWithDefaultAnnoAndConstructor.class);
assertNull("When using a constructor, the default value annotation shouldn't be used.", myEnum);
}
-
- public void testJsonCreatorPropertiesWithEnum() throws Exception {
- final ObjectMapper mapper = new ObjectMapper();
-
- EnumWithPropertiesModeJsonCreator type1 = mapper.readValue("{\"name\":\"TEST1\", \"description\":\"TEST\"}", EnumWithPropertiesModeJsonCreator.class);
- assertSame(EnumWithPropertiesModeJsonCreator.TEST1, type1);
-
- EnumWithPropertiesModeJsonCreator type2 = mapper.readValue("{\"name\":\"TEST3\", \"description\":\"TEST\"}", EnumWithPropertiesModeJsonCreator.class);
- assertSame(EnumWithPropertiesModeJsonCreator.TEST3, type2);
-
- }
-
- public void testJsonCreatorDelagateWithEnum() throws Exception {
- final ObjectMapper mapper = new ObjectMapper();
-
- EnumWithDelegateModeJsonCreator type1 = mapper.readValue("{\"name\":\"TEST1\", \"description\":\"TEST\"}", EnumWithDelegateModeJsonCreator.class);
- assertSame(EnumWithDelegateModeJsonCreator.TEST1, type1);
-
- EnumWithDelegateModeJsonCreator type2 = mapper.readValue("{\"name\":\"TEST3\", \"description\":\"TEST\"}", EnumWithDelegateModeJsonCreator.class);
- assertSame(EnumWithDelegateModeJsonCreator.TEST3, type2);
-
- }
-
}