4
4
import java .util .*;
5
5
6
6
import com .fasterxml .jackson .annotation .JsonPropertyOrder ;
7
+ import com .fasterxml .jackson .core .JsonProcessingException ;
8
+ import com .fasterxml .jackson .databind .JsonMappingException ;
7
9
import com .fasterxml .jackson .databind .MappingIterator ;
8
10
import com .fasterxml .jackson .databind .ObjectWriter ;
9
11
import com .fasterxml .jackson .dataformat .csv .*;
12
14
* Tests for verifying behavior of enclosing input stream as
13
15
* a logical array.
14
16
*/
15
- public class TestParserSequences extends ModuleTestBase
17
+ public class ReadSequencesTest extends ModuleTestBase
16
18
{
17
19
@ JsonPropertyOrder ({"x" , "y" })
18
20
protected static class Entry {
@@ -65,6 +67,62 @@ public void testAsSequence() throws Exception
65
67
it .close ();
66
68
}
67
69
70
+ public void testSequenceRecovery () throws Exception
71
+ {
72
+ CsvMapper mapper = mapperForCsv ();
73
+ mapper .disable (CsvParser .Feature .WRAP_AS_ARRAY );
74
+ MappingIterator <Entry > it = mapper .readerWithSchemaFor (Entry .class ).readValues (
75
+ "1,2\n 3,invalid\n 5,6\n 1,2,3,5\n 13,-4\n garbage\n " );
76
+ Entry entry ;
77
+
78
+ assertTrue (it .hasNext ());
79
+ assertNotNull (entry = it .nextValue ());
80
+ assertEquals (1 , entry .x );
81
+ assertEquals (2 , entry .y );
82
+ assertTrue (it .hasNext ());
83
+
84
+ // second row, invalid:
85
+ try {
86
+ it .nextValue ();
87
+ fail ("Shouldn't have passed" );
88
+ } catch (JsonMappingException e ) {
89
+ verifyException (e , "'invalid': not a valid" );
90
+ }
91
+
92
+ // but third is fine again
93
+ assertNotNull (entry = it .nextValue ());
94
+ assertEquals (5 , entry .x );
95
+ assertEquals (6 , entry .y );
96
+
97
+ // fourth not
98
+ assertTrue (it .hasNext ());
99
+ try {
100
+ it .nextValue ();
101
+ fail ("Shouldn't have passed" );
102
+ } catch (JsonProcessingException e ) {
103
+ // !!! TODO, maybe: Would be nicer to get a JsonMappingException?
104
+ verifyException (e , "Too many entries" );
105
+ }
106
+
107
+ // fifth ok
108
+ assertTrue (it .hasNext ());
109
+ assertNotNull (entry = it .nextValue ());
110
+ assertEquals (13 , entry .x );
111
+ assertEquals (-4 , entry .y );
112
+
113
+ // and sixth busted again
114
+ assertTrue (it .hasNext ());
115
+ try {
116
+ it .nextValue ();
117
+ fail ("Shouldn't have passed" );
118
+ } catch (JsonMappingException e ) {
119
+ verifyException (e , "String value 'garbage'" );
120
+ }
121
+ assertFalse (it .hasNext ());
122
+ it .close ();
123
+ }
124
+
125
+
68
126
// Test using sequence of entries wrapped in a logical array.
69
127
public void testAsWrappedArray () throws Exception
70
128
{
@@ -97,7 +155,7 @@ public void testLongerUnwrapped() throws Exception
97
155
final int EXPECTED_BYTES = 97640 ;
98
156
assertEquals (EXPECTED_BYTES , bytes .length );
99
157
100
- MappingIterator <Entry > it = mapper .reader (Entry .class ).with (schema ).readValues (bytes , 0 , bytes .length );
158
+ MappingIterator <Entry > it = mapper .readerFor (Entry .class ).with (schema ).readValues (bytes , 0 , bytes .length );
101
159
verifySame (it , entries );
102
160
bytes = null ;
103
161
@@ -106,7 +164,7 @@ public void testLongerUnwrapped() throws Exception
106
164
assertEquals (EXPECTED_BYTES , text .length ());
107
165
it .close ();
108
166
109
- it = mapper .reader (Entry .class ).with (schema ).readValues (text );
167
+ it = mapper .readerFor (Entry .class ).with (schema ).readValues (text );
110
168
verifySame (it , entries );
111
169
it .close ();
112
170
@@ -118,7 +176,7 @@ public void testRawObjectArrays() throws Exception
118
176
CsvMapper mapper = new CsvMapper ();
119
177
mapper .enable (CsvParser .Feature .WRAP_AS_ARRAY );
120
178
final String CSV = "a,b\n c,d\n e,f\n " ;
121
- MappingIterator <Object []> it = mapper .reader (Object [].class ).readValues (CSV );
179
+ MappingIterator <Object []> it = mapper .readerFor (Object [].class ).readValues (CSV );
122
180
123
181
assertTrue (it .hasNext ());
124
182
Object [] row = it .next ();
0 commit comments