1 package com.fasterxml.jackson.core; 2 3 import java.io.StringWriter; 4 5 import com.fasterxml.jackson.core.io.JsonEOFException; 6 7 public class TestExceptions extends BaseTest 8 { 9 private final JsonFactory JSON_F = new JsonFactory(); 10 11 // For [core#10] testOriginalMesssage()12 public void testOriginalMesssage() 13 { 14 JsonProcessingException exc = new JsonParseException(null, "Foobar", JsonLocation.NA); 15 String msg = exc.getMessage(); 16 String orig = exc.getOriginalMessage(); 17 assertEquals("Foobar", orig); 18 assertTrue(msg.length() > orig.length()); 19 20 // and another 21 JsonProcessingException exc2 = new JsonProcessingException("Second", 22 JsonLocation.NA, exc); 23 assertSame(exc, exc2.getCause()); 24 exc2.clearLocation(); 25 assertNull(exc2.getLocation()); 26 27 // and yet with null 28 JsonProcessingException exc3 = new JsonProcessingException(exc); 29 assertNull(exc3.getOriginalMessage()); 30 assertEquals("N/A", exc3.getMessage()); 31 32 assertEquals("com.fasterxml.jackson.core.JsonProcessingException: N/A", exc3.toString()); 33 } 34 35 // [core#198] testAccessToParser()36 public void testAccessToParser() throws Exception 37 { 38 JsonParser p = JSON_F.createParser("{}"); 39 assertToken(JsonToken.START_OBJECT, p.nextToken()); 40 JsonParseException e = new JsonParseException(p, "Test!"); 41 assertSame(p, e.getProcessor()); 42 assertEquals("Test!", e.getOriginalMessage()); 43 JsonLocation loc = e.getLocation(); 44 assertNotNull(loc); 45 assertEquals(2, loc.getColumnNr()); 46 assertEquals(1, loc.getLineNr()); 47 p.close(); 48 } 49 50 // [core#198] testAccessToGenerator()51 public void testAccessToGenerator() throws Exception 52 { 53 StringWriter w = new StringWriter(); 54 JsonGenerator g = JSON_F.createGenerator(w); 55 g.writeStartObject(); 56 JsonGenerationException e = new JsonGenerationException("Test!", g); 57 assertSame(g, e.getProcessor()); 58 assertEquals("Test!", e.getOriginalMessage()); 59 g.close(); 60 } 61 62 // [core#281]: new eof exception testEofExceptionsBytes()63 public void testEofExceptionsBytes() throws Exception { 64 _testEofExceptions(MODE_INPUT_STREAM); 65 } 66 67 // [core#281]: new eof exception testEofExceptionsChars()68 public void testEofExceptionsChars() throws Exception { 69 _testEofExceptions(MODE_READER); 70 } 71 _testEofExceptions(int mode)72 private void _testEofExceptions(int mode) throws Exception 73 { 74 JsonParser p = createParser(mode, "[ "); 75 assertToken(JsonToken.START_ARRAY, p.nextToken()); 76 try { 77 p.nextToken(); 78 fail("Should get exception"); 79 } catch (JsonEOFException e) { 80 verifyException(e, "close marker for Array"); 81 } 82 p.close(); 83 84 p = createParser(mode, "{ \"foo\" : [ ] "); 85 assertToken(JsonToken.START_OBJECT, p.nextToken()); 86 assertToken(JsonToken.FIELD_NAME, p.nextToken()); 87 assertToken(JsonToken.START_ARRAY, p.nextToken()); 88 assertToken(JsonToken.END_ARRAY, p.nextToken()); 89 try { 90 p.nextToken(); 91 fail("Should get exception"); 92 } catch (JsonEOFException e) { 93 verifyException(e, "close marker for Object"); 94 } 95 p.close(); 96 97 p = createParser(mode, "{ \"fo"); 98 assertToken(JsonToken.START_OBJECT, p.nextToken()); 99 try { 100 p.nextToken(); 101 fail("Should get exception"); 102 } catch (JsonEOFException e) { 103 104 verifyException(e, "in field name"); 105 assertEquals(JsonToken.FIELD_NAME, e.getTokenBeingDecoded()); 106 } 107 p.close(); 108 109 p = createParser(mode, "{ \"field\" : "); 110 assertToken(JsonToken.START_OBJECT, p.nextToken()); 111 try { 112 p.nextToken(); 113 fail("Should get exception"); 114 } catch (JsonEOFException e) { 115 verifyException(e, "unexpected end-of-input"); 116 verifyException(e, "Object entries"); 117 } 118 p.close(); 119 120 // any other cases we'd like to test? 121 } 122 } 123