|
1 | 1 | package com.fasterxml.jackson.databind.struct;
|
2 | 2 |
|
| 3 | +import java.io.ByteArrayInputStream; |
3 | 4 | import java.io.IOException;
|
| 5 | +import java.io.StringReader; |
4 | 6 | import java.math.BigDecimal;
|
5 | 7 | import java.math.BigInteger;
|
6 | 8 |
|
| 9 | +import com.fasterxml.jackson.core.JsonParser; |
| 10 | +import com.fasterxml.jackson.core.JsonToken; |
7 | 11 | import com.fasterxml.jackson.databind.*;
|
8 | 12 | import com.fasterxml.jackson.databind.exc.MismatchedInputException;
|
9 | 13 |
|
@@ -138,23 +142,23 @@ public void testStringCoercionOk() throws Exception
|
138 | 142 |
|
139 | 143 | public void testStringCoercionFail() throws Exception
|
140 | 144 | {
|
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); |
158 | 162 | }
|
159 | 163 |
|
160 | 164 | public void testMiscCoercionFail() throws Exception
|
@@ -192,4 +196,38 @@ private void _verifyCoerceFail(String input, Class<?> type) throws IOException
|
192 | 196 | verifyException(e, "enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow");
|
193 | 197 | }
|
194 | 198 | }
|
| 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 | + } |
195 | 233 | }
|
0 commit comments