Skip to content

Commit 18e2454

Browse files
committed
Add improved tests wrt #2635 fix
1 parent 36db662 commit 18e2454

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

src/test/java/com/fasterxml/jackson/databind/struct/ScalarCoercionTest.java

+62-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
import com.fasterxml.jackson.databind.*;
1212
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
1313

14-
// for [databind#1106]
1514
public class ScalarCoercionTest extends BaseMapTest
1615
{
16+
static class BooleanPOJO {
17+
public boolean value;
18+
}
19+
1720
private final ObjectMapper COERCING_MAPPER = jsonMapperBuilder()
1821
.enable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
1922
.build();
@@ -161,11 +164,25 @@ public void testStringCoercionFail() throws Exception
161164
_verifyRootStringCoerceFail("123.0", BigDecimal.class);
162165
}
163166

167+
public void testToBooleanCoercionFailBytes() throws Exception
168+
{
169+
final String beanDoc = aposToQuotes("{'value':1}");
170+
_verifyBooleanCoerceFail("1", true, JsonToken.VALUE_NUMBER_INT, "1", Boolean.TYPE);
171+
_verifyBooleanCoerceFail("1", true, JsonToken.VALUE_NUMBER_INT, "1", Boolean.class);
172+
_verifyBooleanCoerceFail(beanDoc, true, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class);
173+
}
174+
175+
public void testToBooleanCoercionFailChars() throws Exception
176+
{
177+
final String beanDoc = aposToQuotes("{'value':1}");
178+
_verifyBooleanCoerceFail("1", false, JsonToken.VALUE_NUMBER_INT, "1", Boolean.TYPE);
179+
_verifyBooleanCoerceFail("1", false, JsonToken.VALUE_NUMBER_INT, "1", Boolean.class);
180+
_verifyBooleanCoerceFail(beanDoc, false, JsonToken.VALUE_NUMBER_INT, "1", BooleanPOJO.class);
181+
}
182+
164183
public void testMiscCoercionFail() throws Exception
165184
{
166185
// And then we have coercions from more esoteric types too
167-
_verifyCoerceFail("1", Boolean.TYPE);
168-
_verifyCoerceFail("1", Boolean.class);
169186

170187
_verifyCoerceFail("65", Character.class);
171188
_verifyCoerceFail("65", Character.TYPE);
@@ -230,4 +247,46 @@ private void _verifyStringCoerceFail(JsonParser p,
230247
assertEquals(unquotedValue, p.getText());
231248
}
232249
}
250+
251+
private void _verifyBooleanCoerceFail(String doc, boolean useBytes,
252+
JsonToken tokenType, String tokenValue, Class<?> targetType) throws IOException
253+
{
254+
// Test failure for root value: for both byte- and char-backed sources.
255+
256+
// [databind#2635]: important, need to use `readValue()` that takes content and NOT
257+
// JsonParser, as this forces closing of underlying parser and exposes more issues.
258+
259+
final ObjectReader r = NOT_COERCING_MAPPER.readerFor(targetType);
260+
try {
261+
if (useBytes) {
262+
r.readValue(utf8Bytes(doc));
263+
} else {
264+
r.readValue(doc);
265+
}
266+
fail("Should not have allowed coercion");
267+
} catch (MismatchedInputException e) {
268+
_verifyBooleanCoerceFailReason(e, tokenType, tokenValue);
269+
}
270+
}
271+
272+
@SuppressWarnings("resource")
273+
private void _verifyBooleanCoerceFailReason(MismatchedInputException e,
274+
JsonToken tokenType, String tokenValue) throws IOException
275+
{
276+
verifyException(e, "Cannot coerce ");
277+
verifyException(e, " for type `");
278+
verifyException(e, "enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow");
279+
280+
JsonParser p = (JsonParser) e.getProcessor();
281+
282+
assertToken(tokenType, p.currentToken());
283+
284+
final String text = p.getText();
285+
286+
if (!tokenValue.equals(text)) {
287+
String textDesc = (text == null) ? "NULL" : quote(text);
288+
fail("Token text ("+textDesc+") via parser of type "+p.getClass().getName()
289+
+" not as expected ("+quote(tokenValue)+")");
290+
}
291+
}
233292
}

0 commit comments

Comments
 (0)