|
1 | 1 | package com.fasterxml.jackson.databind.module;
|
2 | 2 |
|
| 3 | +import java.io.ByteArrayInputStream; |
3 | 4 | import java.io.IOException;
|
| 5 | +import java.io.StringReader; |
4 | 6 | import java.lang.reflect.Type;
|
5 | 7 | import java.util.*;
|
6 | 8 |
|
@@ -239,29 +241,68 @@ public void serialize(Test3787Bean value, JsonGenerator gen, SerializerProvider
|
239 | 241 | /**********************************************************
|
240 | 242 | */
|
241 | 243 |
|
| 244 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 245 | + |
| 246 | + @Test |
| 247 | + public void testDeserializationWithoutModule() throws Exception |
| 248 | + { |
| 249 | + final String DOC = "{\"str\":\"ab\",\"num\":2}"; |
| 250 | + |
| 251 | + try { |
| 252 | + MAPPER.readValue(DOC, CustomBean.class); |
| 253 | + fail("Should have caused an exception"); |
| 254 | + } catch (DatabindException e) { |
| 255 | + verifyException(e, "Cannot construct"); |
| 256 | + verifyException(e, "no creators"); |
| 257 | + } |
| 258 | + |
| 259 | + // And then other variations |
| 260 | + try { |
| 261 | + MAPPER.readValue(new StringReader(DOC), CustomBean.class); |
| 262 | + fail("Should have caused an exception"); |
| 263 | + } catch (DatabindException e) { |
| 264 | + verifyException(e, "Cannot construct"); |
| 265 | + verifyException(e, "no creators"); |
| 266 | + } |
| 267 | + |
| 268 | + try { |
| 269 | + MAPPER.readValue(utf8Bytes(DOC), CustomBean.class); |
| 270 | + fail("Should have caused an exception"); |
| 271 | + } catch (DatabindException e) { |
| 272 | + verifyException(e, "Cannot construct"); |
| 273 | + verifyException(e, "no creators"); |
| 274 | + } |
| 275 | + |
| 276 | + try { |
| 277 | + MAPPER.readValue(new ByteArrayInputStream(utf8Bytes(DOC)), CustomBean.class); |
| 278 | + fail("Should have caused an exception"); |
| 279 | + } catch (DatabindException e) { |
| 280 | + verifyException(e, "Cannot construct"); |
| 281 | + verifyException(e, "no creators"); |
| 282 | + } |
| 283 | + } |
| 284 | + |
242 | 285 | /**
|
243 | 286 | * Basic test to ensure we do not have functioning default
|
244 | 287 | * serializers for custom types used in tests.
|
245 | 288 | */
|
246 | 289 | @Test
|
247 |
| - public void testWithoutModule() |
| 290 | + public void testSerializationWithoutModule() throws Exception |
248 | 291 | {
|
249 |
| - ObjectMapper mapper = new ObjectMapper(); |
250 | 292 | // first: serialization failure:
|
251 | 293 | try {
|
252 |
| - mapper.writeValueAsString(new CustomBean("foo", 3)); |
| 294 | + MAPPER.writeValueAsString(new CustomBean("foo", 3)); |
253 | 295 | fail("Should have caused an exception");
|
254 |
| - } catch (IOException e) { |
| 296 | + } catch (DatabindException e) { |
255 | 297 | verifyException(e, "No serializer found");
|
256 | 298 | }
|
257 | 299 |
|
258 |
| - // then deserialization |
| 300 | + // and with another write call for test coverage |
259 | 301 | try {
|
260 |
| - mapper.readValue("{\"str\":\"ab\",\"num\":2}", CustomBean.class); |
| 302 | + MAPPER.writeValueAsBytes(new CustomBean("foo", 3)); |
261 | 303 | fail("Should have caused an exception");
|
262 |
| - } catch (IOException e) { |
263 |
| - verifyException(e, "Cannot construct"); |
264 |
| - verifyException(e, "no creators"); |
| 304 | + } catch (DatabindException e) { |
| 305 | + verifyException(e, "No serializer found"); |
265 | 306 | }
|
266 | 307 | }
|
267 | 308 |
|
|
0 commit comments