Skip to content

Commit fbe56f1

Browse files
committed
...
1 parent cf7ad0a commit fbe56f1

File tree

1 file changed

+53
-27
lines changed

1 file changed

+53
-27
lines changed

src/test/java/com/fasterxml/jackson/databind/convert/CoerceEnumTest.java

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,43 +89,49 @@ private void _testEnumFromEmptyGlobalConfig(final CoercionInputShape shape, fina
8989
ObjectMapper mapper;
9090

9191
// First, coerce to null
92-
mapper = newJsonMapper();
93-
mapper.coercionConfigDefaults().setCoercion(shape, CoercionAction.AsNull)
94-
.setAcceptBlankAsEmpty(allowEmpty);
92+
mapper = _globMapper(shape, CoercionAction.AsNull, allowEmpty);
9593
assertNull(_verifyFromEmptyPass(mapper, json));
9694

9795
// Then coerce as empty
98-
mapper = newJsonMapper();
99-
mapper.coercionConfigDefaults().setCoercion(shape, CoercionAction.AsEmpty)
100-
.setAcceptBlankAsEmpty(allowEmpty);
96+
mapper = _globMapper(shape, CoercionAction.AsEmpty, allowEmpty);
10197
EnumCoerce b = _verifyFromEmptyPass(mapper, json);
10298
assertEquals(ENUM_DEFAULT, b);
10399

104100
// and finally, "try convert", which for Enums is same as "empty" (default)
105-
mapper = newJsonMapper();
106-
mapper.coercionConfigDefaults().setCoercion(shape, CoercionAction.TryConvert)
107-
.setAcceptBlankAsEmpty(allowEmpty);
101+
mapper = _globMapper(shape, CoercionAction.TryConvert, allowEmpty);
108102
assertEquals(ENUM_DEFAULT, _verifyFromEmptyPass(mapper, json));
109103
}
110104

105+
private ObjectMapper _globMapper(CoercionInputShape shape, CoercionAction act,
106+
Boolean allowEmpty)
107+
{
108+
ObjectMapper mapper = newJsonMapper();
109+
mapper.coercionConfigDefaults().setCoercion(shape, act)
110+
.setAcceptBlankAsEmpty(allowEmpty);
111+
return mapper;
112+
}
113+
111114
private void _testEnumFromEmptyLogicalTypeConfig(final CoercionInputShape shape, final String json,
112115
Boolean allowEmpty)
113116
throws Exception
114117
{
115118
ObjectMapper mapper;
119+
EnumCoerce b;
116120

117121
// First, coerce to null
118-
mapper = newJsonMapper();
119-
mapper.coercionConfigFor(LogicalType.Enum).setCoercion(shape, CoercionAction.AsNull)
120-
.setAcceptBlankAsEmpty(allowEmpty);
121-
assertNull(_verifyFromEmptyPass(mapper, json));
122+
mapper = _logMapper(LogicalType.Enum, shape, CoercionAction.AsNull, allowEmpty);
123+
b = _verifyFromEmptyPass(mapper, json);
124+
assertNull(b);
122125

123126
// Then coerce as empty
124-
mapper = newJsonMapper();
125-
mapper.coercionConfigFor(LogicalType.Enum).setCoercion(shape, CoercionAction.AsEmpty)
126-
.setAcceptBlankAsEmpty(allowEmpty);
127-
EnumCoerce b = _verifyFromEmptyPass(mapper, json);
128-
assertNotNull(b);
127+
mapper = _logMapper(LogicalType.Enum, shape, CoercionAction.AsEmpty, allowEmpty);
128+
b = _verifyFromEmptyPass(mapper, json);
129+
assertEquals(ENUM_DEFAULT, b);
130+
131+
// and with TryConvert (for enums same as empty)
132+
mapper = _logMapper(LogicalType.Enum, shape, CoercionAction.TryConvert, allowEmpty);
133+
b = _verifyFromEmptyPass(mapper, json);
134+
assertEquals(ENUM_DEFAULT, b);
129135

130136
// But also make fail again with 2-level settings
131137
mapper = newJsonMapper();
@@ -136,24 +142,35 @@ private void _testEnumFromEmptyLogicalTypeConfig(final CoercionInputShape shape,
136142
_verifyFromEmptyFail(mapper, json);
137143
}
138144

145+
private ObjectMapper _logMapper(LogicalType type, CoercionInputShape shape, CoercionAction act,
146+
Boolean allowEmpty)
147+
{
148+
ObjectMapper mapper = newJsonMapper();
149+
mapper.coercionConfigFor(type).setCoercion(shape, act)
150+
.setAcceptBlankAsEmpty(allowEmpty);
151+
return mapper;
152+
}
153+
139154
private void _testEnumFromEmptyPhysicalTypeConfig(final CoercionInputShape shape, final String json,
140155
Boolean allowEmpty)
141156
throws Exception
142157
{
143158
ObjectMapper mapper;
159+
EnumCoerce b;
144160

145161
// First, coerce to null
146-
mapper = newJsonMapper();
147-
mapper.coercionConfigFor(EnumCoerce.class).setCoercion(shape, CoercionAction.AsNull)
148-
.setAcceptBlankAsEmpty(allowEmpty);
149-
assertNull(_verifyFromEmptyPass(mapper, json));
162+
mapper = _physMapper(EnumCoerce.class, shape, CoercionAction.AsNull, allowEmpty);
163+
b = _verifyFromEmptyPass(mapper, json);
164+
assertNull(b);
150165

151166
// Then coerce as empty
152-
mapper = newJsonMapper();
153-
mapper.coercionConfigFor(EnumCoerce.class).setCoercion(shape, CoercionAction.AsEmpty)
154-
.setAcceptBlankAsEmpty(allowEmpty);
155-
EnumCoerce b = _verifyFromEmptyPass(mapper, json);
156-
assertNotNull(b);
167+
mapper = _physMapper(EnumCoerce.class, shape, CoercionAction.AsEmpty, allowEmpty);
168+
b = _verifyFromEmptyPass(mapper, json);
169+
assertEquals(ENUM_DEFAULT, b);
170+
171+
mapper = _physMapper(EnumCoerce.class, shape, CoercionAction.TryConvert, allowEmpty);
172+
b = _verifyFromEmptyPass(mapper, json);
173+
assertEquals(ENUM_DEFAULT, b);
157174

158175
// But also make fail again with 2-level settings, with physical having precedence
159176
mapper = newJsonMapper();
@@ -163,6 +180,15 @@ private void _testEnumFromEmptyPhysicalTypeConfig(final CoercionInputShape shape
163180
_verifyFromEmptyFail(mapper, json);
164181
}
165182

183+
private ObjectMapper _physMapper(Class<?> type, CoercionInputShape shape, CoercionAction act,
184+
Boolean allowEmpty)
185+
{
186+
ObjectMapper mapper = newJsonMapper();
187+
mapper.coercionConfigFor(type).setCoercion(shape, act)
188+
.setAcceptBlankAsEmpty(allowEmpty);
189+
return mapper;
190+
}
191+
166192
/*
167193
/********************************************************
168194
/* Verification helper methods

0 commit comments

Comments
 (0)