1 package com.fasterxml.jackson.databind.exc; 2 3 import java.io.*; 4 import java.util.*; 5 6 import com.fasterxml.jackson.core.*; 7 import com.fasterxml.jackson.databind.*; 8 import com.fasterxml.jackson.databind.module.SimpleModule; 9 import com.fasterxml.jackson.databind.testutil.BrokenStringWriter; 10 11 /** 12 * Unit test for verifying that exceptions are properly handled (caught, 13 * re-thrown or wrapped, depending) 14 * with Object serialization. 15 */ 16 public class TestExceptionsDuringWriting 17 extends BaseMapTest 18 { 19 /* 20 /********************************************************** 21 /* Helper classes 22 /********************************************************** 23 */ 24 25 static class Bean { 26 // no methods, we'll use our custom serializer 27 } 28 29 static class SerializerWithErrors 30 extends JsonSerializer<Bean> 31 { 32 @Override serialize(Bean value, JsonGenerator jgen, SerializerProvider provider)33 public void serialize(Bean value, JsonGenerator jgen, SerializerProvider provider) 34 { 35 throw new IllegalArgumentException("test string"); 36 } 37 } 38 39 /* 40 /********************************************************** 41 /* Tests 42 /********************************************************** 43 */ 44 45 /** 46 * Unit test that verifies that by default all exceptions except for 47 * JsonMappingException are caught and wrapped. 48 */ testCatchAndRethrow()49 public void testCatchAndRethrow() 50 throws Exception 51 { 52 ObjectMapper mapper = new ObjectMapper(); 53 SimpleModule module = new SimpleModule("test-exceptions", Version.unknownVersion()); 54 module.addSerializer(Bean.class, new SerializerWithErrors()); 55 mapper.registerModule(module); 56 try { 57 StringWriter sw = new StringWriter(); 58 /* And just to make things more interesting, let's create 59 * a nested data struct... 60 */ 61 Bean[] b = { new Bean() }; 62 List<Bean[]> l = new ArrayList<Bean[]>(); 63 l.add(b); 64 mapper.writeValue(sw, l); 65 fail("Should have gotten an exception"); 66 } catch (IOException e) { 67 // should contain original message somewhere 68 verifyException(e, "test string"); 69 Throwable root = e.getCause(); 70 assertNotNull(root); 71 72 if (!(root instanceof IllegalArgumentException)) { 73 fail("Wrapped exception not IAE, but "+root.getClass()); 74 } 75 } 76 } 77 78 /** 79 * Unit test for verifying that regular IOExceptions are not wrapped 80 * but are passed through as is. 81 */ 82 @SuppressWarnings("resource") testExceptionWithSimpleMapper()83 public void testExceptionWithSimpleMapper() 84 throws Exception 85 { 86 ObjectMapper mapper = new ObjectMapper(); 87 try { 88 BrokenStringWriter sw = new BrokenStringWriter("TEST"); 89 mapper.writeValue(sw, createLongObject()); 90 fail("Should have gotten an exception"); 91 } catch (IOException e) { 92 verifyException(e, IOException.class, "TEST"); 93 } 94 } 95 96 @SuppressWarnings("resource") testExceptionWithMapperAndGenerator()97 public void testExceptionWithMapperAndGenerator() 98 throws Exception 99 { 100 ObjectMapper mapper = new ObjectMapper(); 101 JsonFactory f = new MappingJsonFactory(); 102 BrokenStringWriter sw = new BrokenStringWriter("TEST"); 103 JsonGenerator jg = f.createGenerator(sw); 104 105 try { 106 mapper.writeValue(jg, createLongObject()); 107 fail("Should have gotten an exception"); 108 } catch (IOException e) { 109 verifyException(e, IOException.class, "TEST"); 110 } 111 } 112 113 @SuppressWarnings("resource") testExceptionWithGeneratorMapping()114 public void testExceptionWithGeneratorMapping() 115 throws Exception 116 { 117 JsonFactory f = new MappingJsonFactory(); 118 JsonGenerator jg = f.createGenerator(new BrokenStringWriter("TEST")); 119 try { 120 jg.writeObject(createLongObject()); 121 fail("Should have gotten an exception"); 122 } catch (Exception e) { 123 verifyException(e, IOException.class, "TEST"); 124 } 125 } 126 127 /* 128 /********************************************************** 129 /* Helper methods 130 /********************************************************** 131 */ 132 verifyException(Exception e, Class<?> expType, String expMsg)133 void verifyException(Exception e, Class<?> expType, String expMsg) 134 throws Exception 135 { 136 if (e.getClass() != expType) { 137 fail("Expected exception of type "+expType.getName()+", got "+e.getClass().getName()); 138 } 139 if (expMsg != null) { 140 verifyException(e, expMsg); 141 } 142 } 143 createLongObject()144 Object createLongObject() 145 { 146 List<Object> leaf = new ArrayList<Object>(); 147 for (int i = 0; i < 256; ++i) { 148 leaf.add(Integer.valueOf(i)); 149 } 150 List<Object> root = new ArrayList<Object>(256); 151 for (int i = 0; i < 256; ++i) { 152 root.add(leaf); 153 } 154 return root; 155 } 156 } 157 158