1 package com.fasterxml.jackson.databind.exc; 2 3 import java.io.StringWriter; 4 import java.util.ArrayList; 5 import java.util.Collection; 6 import java.util.Collections; 7 8 import com.fasterxml.jackson.core.*; 9 import com.fasterxml.jackson.databind.*; 10 import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition; 11 import com.fasterxml.jackson.databind.type.TypeFactory; 12 13 public class BasicExceptionTest extends BaseMapTest 14 { 15 private final ObjectMapper MAPPER = newJsonMapper(); 16 private final JsonFactory JSON_F = MAPPER.getFactory(); 17 testBadDefinition()18 public void testBadDefinition() throws Exception 19 { 20 JavaType t = TypeFactory.defaultInstance().constructType(String.class); 21 JsonParser p = JSON_F.createParser("[]"); 22 InvalidDefinitionException e = new InvalidDefinitionException(p, 23 "Testing", t); 24 assertEquals("Testing", e.getOriginalMessage()); 25 assertEquals(String.class, e.getType().getRawClass()); 26 assertNull(e.getBeanDescription()); 27 assertNull(e.getProperty()); 28 assertSame(p, e.getProcessor()); 29 p.close(); 30 31 // and via factory method: 32 BeanDescription beanDef = MAPPER.getSerializationConfig().introspectClassAnnotations(getClass()); 33 e = InvalidDefinitionException.from(p, "Testing", 34 beanDef, (BeanPropertyDefinition) null); 35 assertEquals(beanDef.getType(), e.getType()); 36 assertNotNull(e); 37 38 // and the other constructor too 39 JsonGenerator g = JSON_F.createGenerator(new StringWriter()); 40 e = new InvalidDefinitionException(p, 41 "Testing", t); 42 assertEquals("Testing", e.getOriginalMessage()); 43 assertEquals(String.class, e.getType().getRawClass()); 44 45 // and factory 46 e = InvalidDefinitionException.from(g, "Testing", 47 beanDef, (BeanPropertyDefinition) null); 48 assertEquals(beanDef.getType(), e.getType()); 49 assertNotNull(e); 50 51 g.close(); 52 } 53 54 @SuppressWarnings("deprecation") testInvalidFormat()55 public void testInvalidFormat() throws Exception 56 { 57 // deprecated methods should still work: 58 InvalidFormatException e = new InvalidFormatException("Testing", Boolean.TRUE, 59 String.class); 60 assertSame(Boolean.TRUE, e.getValue()); 61 assertNull(e.getProcessor()); 62 assertNotNull(e); 63 64 e = new InvalidFormatException("Testing", JsonLocation.NA, 65 Boolean.TRUE, String.class); 66 assertSame(Boolean.TRUE, e.getValue()); 67 assertNull(e.getProcessor()); 68 assertNotNull(e); 69 } 70 testIgnoredProperty()71 public void testIgnoredProperty() throws Exception 72 { 73 // first just construct valid instance with some variations 74 JsonParser p = JSON_F.createParser("{ }"); 75 IgnoredPropertyException e = IgnoredPropertyException.from(p, 76 this, // to get class from 77 "testProp", Collections.<Object>singletonList("x")); 78 assertNotNull(e); 79 80 e = IgnoredPropertyException.from(p, 81 getClass(), 82 "testProp", null); 83 assertNotNull(e); 84 assertNull(e.getKnownPropertyIds()); 85 p.close(); 86 87 // also, verify failure if null passed for "value" 88 try { 89 IgnoredPropertyException.from(p, null, 90 "testProp", Collections.<Object>singletonList("x")); 91 fail("Should not pass"); 92 } catch (NullPointerException e2) { 93 } 94 } 95 testUnrecognizedProperty()96 public void testUnrecognizedProperty() throws Exception 97 { 98 JsonParser p = JSON_F.createParser("{ }"); 99 UnrecognizedPropertyException e = UnrecognizedPropertyException.from(p, this, 100 "testProp", Collections.<Object>singletonList("y")); 101 assertNotNull(e); 102 assertEquals(getClass(), e.getReferringClass()); 103 Collection<Object> ids = e.getKnownPropertyIds(); 104 assertNotNull(ids); 105 assertEquals(1, ids.size()); 106 assertTrue(ids.contains("y")); 107 108 e = UnrecognizedPropertyException.from(p, getClass(), 109 "testProp", Collections.<Object>singletonList("y")); 110 111 assertEquals(getClass(), e.getReferringClass()); 112 p.close(); 113 } 114 115 // [databind#2128]: ensure Location added once and only once 116 // [databind#2482]: ensure Location is the original one testLocationAddition()117 public void testLocationAddition() throws Exception 118 { 119 String problemJson = "{\n\t\"userList\" : [\n\t{\n\t user : \"1\"\n\t},\n\t{\n\t \"user\" : \"2\"\n\t}\n\t]\n}"; 120 try { 121 MAPPER.readValue(problemJson, Users.class); 122 fail("Should not pass"); 123 } catch (JsonMappingException e) { // becomes "generic" due to wrapping for passing path info 124 String msg = e.getMessage(); 125 String[] str = msg.split(" at \\["); 126 if (str.length != 2) { 127 fail("Should only get one 'at [' marker, got "+(str.length-1)+", source: "+msg); 128 } 129 JsonLocation loc = e.getLocation(); 130 // String expectedLocation = "line: 4, column: 4"; 131 assertEquals(4, loc.getLineNr()); 132 assertEquals(4, loc.getColumnNr()); 133 } 134 } 135 static class User { 136 public String user; 137 } 138 139 static class Users { 140 public ArrayList<User> userList; 141 } 142 } 143