14
14
15
15
package com .fasterxml .jackson .dataformat .ion ;
16
16
17
- import static org .junit .Assert .assertEquals ;
18
- import static org .junit .Assert .assertNotNull ;
19
-
20
- import java .io .ByteArrayOutputStream ;
21
- import java .io .IOException ;
22
-
23
- import org .junit .Before ;
24
- import org .junit .Test ;
25
-
26
- import com .fasterxml .jackson .dataformat .ion .IonFactory ;
27
- import com .fasterxml .jackson .dataformat .ion .IonObjectMapper ;
28
-
29
17
import com .amazon .ion .IonDatagram ;
30
18
import com .amazon .ion .IonList ;
19
+ import com .amazon .ion .IonReader ;
31
20
import com .amazon .ion .IonStruct ;
32
21
import com .amazon .ion .IonSystem ;
22
+ import com .amazon .ion .IonType ;
33
23
import com .amazon .ion .IonWriter ;
24
+ import com .amazon .ion .system .IonBinaryWriterBuilder ;
25
+ import com .amazon .ion .system .IonReaderBuilder ;
34
26
import com .amazon .ion .system .IonSystemBuilder ;
27
+ import com .amazon .ion .system .IonTextWriterBuilder ;
28
+ import com .amazon .ion .system .IonWriterBuilder ;
29
+ import org .junit .Before ;
30
+ import org .junit .Test ;
31
+
32
+ import java .io .ByteArrayOutputStream ;
33
+ import java .io .IOException ;
34
+
35
+ import static org .junit .Assert .assertEquals ;
36
+ import static org .junit .Assert .assertNotNull ;
35
37
36
38
public class DataBindWriteTest {
37
39
@@ -132,7 +134,19 @@ public void testIntArrayWriteBinary() throws Exception
132
134
IonDatagram loadedDatagram = ion .newLoader ().load (data );
133
135
assertEquals (expectedArray , loadedDatagram );
134
136
}
135
-
137
+
138
+ @ Test
139
+ public void testReusingBinaryIonWriter () throws Exception
140
+ {
141
+ _testIonWriterReuse (IonBinaryWriterBuilder .standard ());
142
+ }
143
+
144
+ @ Test
145
+ public void testReusingTextIonWriter () throws Exception
146
+ {
147
+ _testIonWriterReuse (IonTextWriterBuilder .standard ());
148
+ }
149
+
136
150
// // Helper methods
137
151
138
152
private byte [] _writeAsBytes (Object ob ) throws IOException
@@ -143,4 +157,26 @@ private byte[] _writeAsBytes(Object ob) throws IOException
143
157
m .writeValue (out , ob );
144
158
return out .toByteArray ();
145
159
}
160
+
161
+ private void _testIonWriterReuse (IonWriterBuilder ionWriterBuilder ) throws IOException {
162
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream (1024 );
163
+ IonWriter ionWriter = ionWriterBuilder .build (byteArrayOutputStream );
164
+
165
+ IonObjectMapper ionObjectMapper = new IonObjectMapper ();
166
+ ionObjectMapper .writeValue (ionWriter , "Animal" );
167
+ ionObjectMapper .writeValue (ionWriter , "Vegetable" );
168
+ ionObjectMapper .writeValue (ionWriter , "Mineral" );
169
+ ionWriter .close ();
170
+
171
+ byte [] data = byteArrayOutputStream .toByteArray ();
172
+ assertNotNull (data );
173
+
174
+ IonReader ionReader = IonReaderBuilder .standard ().build (data );
175
+ assertEquals (IonType .STRING , ionReader .next ());
176
+ assertEquals ("Animal" , ionReader .stringValue ());
177
+ assertEquals (IonType .STRING , ionReader .next ());
178
+ assertEquals ("Vegetable" , ionReader .stringValue ());
179
+ assertEquals (IonType .STRING , ionReader .next ());
180
+ assertEquals ("Mineral" , ionReader .stringValue ());
181
+ }
146
182
}
0 commit comments