1 package com.fasterxml.jackson.databind.interop; 2 3 import java.io.*; 4 import java.util.List; 5 6 import com.fasterxml.jackson.databind.*; 7 8 public class ExceptionSerializableTest1195 extends BaseMapTest 9 { 10 static class ClassToRead { 11 public int x; 12 } 13 14 static class ContainerClassToRead { 15 public ClassToRead classToRead; 16 } 17 18 static class ContainerClassesToRead { 19 public List<ClassToRead> classesToRead; 20 } 21 22 final ObjectMapper MAPPER = new ObjectMapper(); 23 testExceptionSerializabilitySimple()24 public void testExceptionSerializabilitySimple() throws Exception 25 { 26 try { 27 MAPPER.readValue("{\"x\": \"B\"}", ClassToRead.class); 28 fail("Should not have passed"); 29 } catch (JsonMappingException e) { 30 verifyException(e, "not a valid Integer"); 31 _testSerializability(e); 32 } 33 try { 34 MAPPER.readValue("{\"classToRead\": {\"x\": \"B\"}}", ContainerClassToRead.class); 35 fail("Should not have passed"); 36 } catch (JsonMappingException e) { 37 verifyException(e, "not a valid Integer"); 38 _testSerializability(e); 39 } 40 } 41 testExceptionSerializabilityStructured()42 public void testExceptionSerializabilityStructured() throws Exception 43 { 44 try { 45 MAPPER.readValue("{\"classesToRead\": [{\"x\": 1}, {\"x\": \"B\"}]}", 46 ContainerClassesToRead.class); 47 fail("Should not have passed"); 48 } catch (JsonMappingException e) { 49 verifyException(e, "not a valid Integer"); 50 _testSerializability(e); 51 } 52 } 53 54 /* 55 /********************************************************** 56 /* Helper methods 57 /********************************************************** 58 */ 59 _testSerializability(Exception e)60 private void _testSerializability(Exception e) throws IOException 61 { 62 ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000); 63 ObjectOutputStream stream = new ObjectOutputStream(bytes); 64 try { 65 stream.writeObject(e); 66 stream.close(); 67 } catch (Exception e2) { 68 fail("Failed to JDK serialize "+e.getClass().getName()+": "+e2); 69 } 70 // and then back... 71 byte[] b = bytes.toByteArray(); 72 ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(b)); 73 Object result = null; 74 try { 75 result = objIn.readObject(); 76 } catch (Exception e2) { 77 fail("Failed to JDK deserialize "+e.getClass().getName()+": "+e2); 78 } 79 objIn.close(); 80 assertNotNull(result); 81 } 82 } 83