• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.seq;
2 
3 import com.fasterxml.jackson.databind.*;
4 
5 /**
6  * Tests to verify aspects of error recover for reading using
7  * iterator.
8  */
9 public class ReadRecoveryTest extends BaseMapTest
10 {
11     static class Bean {
12         public int a, b;
13 
toString()14         @Override public String toString() { return "{Bean, a="+a+", b="+b+"}"; }
15     }
16 
17     /*
18     /**********************************************************
19     /* Unit tests; root-level value sequences via Mapper
20     /**********************************************************
21      */
22 
23     private final ObjectMapper MAPPER = new ObjectMapper();
24 
testRootBeans()25     public void testRootBeans() throws Exception
26     {
27         final String JSON = aposToQuotes("{'a':3} {'x':5}");
28         MappingIterator<Bean> it = MAPPER.readerFor(Bean.class).readValues(JSON);
29         // First one should be fine
30         assertTrue(it.hasNextValue());
31         Bean bean = it.nextValue();
32         assertEquals(3, bean.a);
33         // but second one not
34         try {
35             bean = it.nextValue();
36             fail("Should not have succeeded");
37         } catch (JsonMappingException e) {
38             verifyException(e, "Unrecognized field \"x\"");
39         }
40         // 21-May-2015, tatu: With [databind#734], recovery, we now know there's no more data!
41         assertFalse(it.hasNextValue());
42 
43         it.close();
44     }
45 
46     // for [databind#734]
47     // Simple test for verifying that basic recover works for a case of
48     // unknown structured value
testSimpleRootRecovery()49     public void testSimpleRootRecovery() throws Exception
50     {
51         final String JSON = aposToQuotes("{'a':3}{'a':27,'foo':[1,2],'b':{'x':3}}  {'a':1,'b':2} ");
52 
53         MappingIterator<Bean> it = MAPPER.readerFor(Bean.class).readValues(JSON);
54         Bean bean = it.nextValue();
55 
56         assertNotNull(bean);
57         assertEquals(3, bean.a);
58 
59         // second one problematic
60         try {
61             it.nextValue();
62         } catch (JsonMappingException e) {
63             verifyException(e, "Unrecognized field \"foo\"");
64         }
65 
66         // but should recover nicely
67         bean = it.nextValue();
68         assertNotNull(bean);
69         assertEquals(1, bean.a);
70         assertEquals(2, bean.b);
71 
72         assertFalse(it.hasNextValue());
73 
74         it.close();
75     }
76 
77     // Similar to "raw" root-level Object sequence, but in array
testSimpleArrayRecovery()78     public void testSimpleArrayRecovery() throws Exception
79     {
80         final String JSON = aposToQuotes("[{'a':3},{'a':27,'foo':[1,2],'b':{'x':3}}  ,{'a':1,'b':2}  ]");
81 
82         MappingIterator<Bean> it = MAPPER.readerFor(Bean.class).readValues(JSON);
83         Bean bean = it.nextValue();
84 
85         assertNotNull(bean);
86         assertEquals(3, bean.a);
87 
88         // second one problematic
89         try {
90             it.nextValue();
91         } catch (JsonMappingException e) {
92             verifyException(e, "Unrecognized field \"foo\"");
93         }
94 
95         // but should recover nicely
96         bean = it.nextValue();
97         assertNotNull(bean);
98         assertEquals(1, bean.a);
99         assertEquals(2, bean.b);
100 
101         assertFalse(it.hasNextValue());
102 
103         it.close();
104     }
105 }
106