Skip to content

Commit 36db662

Browse files
committed
test cleanup
1 parent cdae672 commit 36db662

File tree

1 file changed

+55
-17
lines changed

1 file changed

+55
-17
lines changed

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

+55-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.fasterxml.jackson.databind.struct;
22

3+
import java.io.ByteArrayInputStream;
34
import java.io.IOException;
5+
import java.io.StringReader;
46
import java.math.BigDecimal;
57
import java.math.BigInteger;
68

9+
import com.fasterxml.jackson.core.JsonParser;
10+
import com.fasterxml.jackson.core.JsonToken;
711
import com.fasterxml.jackson.databind.*;
812
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
913

@@ -138,23 +142,23 @@ public void testStringCoercionOk() throws Exception
138142

139143
public void testStringCoercionFail() throws Exception
140144
{
141-
_verifyCoerceFail(quote("true"), Boolean.TYPE);
142-
_verifyCoerceFail(quote("true"), Boolean.class);
143-
_verifyCoerceFail(quote("123"), Byte.TYPE);
144-
_verifyCoerceFail(quote("123"), Byte.class);
145-
_verifyCoerceFail(quote("123"), Short.TYPE);
146-
_verifyCoerceFail(quote("123"), Short.class);
147-
_verifyCoerceFail(quote("123"), Integer.TYPE);
148-
_verifyCoerceFail(quote("123"), Integer.class);
149-
_verifyCoerceFail(quote("123"), Long.TYPE);
150-
_verifyCoerceFail(quote("123"), Long.class);
151-
_verifyCoerceFail(quote("123.5"), Float.TYPE);
152-
_verifyCoerceFail(quote("123.5"), Float.class);
153-
_verifyCoerceFail(quote("123.5"), Double.TYPE);
154-
_verifyCoerceFail(quote("123.5"), Double.class);
155-
156-
_verifyCoerceFail(quote("123"), BigInteger.class);
157-
_verifyCoerceFail(quote("123.0"), BigDecimal.class);
145+
_verifyRootStringCoerceFail("true", Boolean.TYPE);
146+
_verifyRootStringCoerceFail("true", Boolean.class);
147+
_verifyRootStringCoerceFail("123", Byte.TYPE);
148+
_verifyRootStringCoerceFail("123", Byte.class);
149+
_verifyRootStringCoerceFail("123", Short.TYPE);
150+
_verifyRootStringCoerceFail("123", Short.class);
151+
_verifyRootStringCoerceFail("123", Integer.TYPE);
152+
_verifyRootStringCoerceFail("123", Integer.class);
153+
_verifyRootStringCoerceFail("123", Long.TYPE);
154+
_verifyRootStringCoerceFail("123", Long.class);
155+
_verifyRootStringCoerceFail("123.5", Float.TYPE);
156+
_verifyRootStringCoerceFail("123.5", Float.class);
157+
_verifyRootStringCoerceFail("123.5", Double.TYPE);
158+
_verifyRootStringCoerceFail("123.5", Double.class);
159+
160+
_verifyRootStringCoerceFail("123", BigInteger.class);
161+
_verifyRootStringCoerceFail("123.0", BigDecimal.class);
158162
}
159163

160164
public void testMiscCoercionFail() throws Exception
@@ -192,4 +196,38 @@ private void _verifyCoerceFail(String input, Class<?> type) throws IOException
192196
verifyException(e, "enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow");
193197
}
194198
}
199+
200+
private void _verifyRootStringCoerceFail(String unquotedValue, Class<?> type) throws IOException
201+
{
202+
// Test failure for root value: for both byte- and char-backed sources:
203+
204+
final String input = quote(unquotedValue);
205+
try (JsonParser p = NOT_COERCING_MAPPER.getFactory().createParser(new StringReader(input))) {
206+
_verifyStringCoerceFail(p, unquotedValue, type);
207+
}
208+
final byte[] inputBytes = utf8Bytes(input);
209+
try (JsonParser p = NOT_COERCING_MAPPER.getFactory().createParser(new ByteArrayInputStream(inputBytes))) {
210+
_verifyStringCoerceFail(p, unquotedValue, type);
211+
}
212+
}
213+
214+
private void _verifyStringCoerceFail(JsonParser p,
215+
String unquotedValue, Class<?> type) throws IOException
216+
{
217+
try {
218+
NOT_COERCING_MAPPER.readerFor(type)
219+
.readValue(p);
220+
fail("Should not have allowed coercion");
221+
} catch (MismatchedInputException e) {
222+
verifyException(e, "Cannot coerce ");
223+
verifyException(e, " for type `");
224+
verifyException(e, "enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow");
225+
226+
assertNotNull(e.getProcessor());
227+
assertSame(p, e.getProcessor());
228+
229+
assertToken(JsonToken.VALUE_STRING, p.currentToken());
230+
assertEquals(unquotedValue, p.getText());
231+
}
232+
}
195233
}

0 commit comments

Comments
 (0)