|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertThrows; |
24 | 24 | import static org.junit.jupiter.api.Assertions.assertTrue; |
25 | 25 |
|
| 26 | +import java.io.ByteArrayInputStream; |
26 | 27 | import java.io.ByteArrayOutputStream; |
27 | 28 | import java.io.IOException; |
| 29 | +import java.io.ObjectInputStream; |
28 | 30 | import java.io.ObjectOutputStream; |
29 | 31 | import java.io.StringReader; |
30 | 32 | import java.util.ArrayList; |
31 | 33 | import java.util.Collections; |
| 34 | +import java.util.HashMap; |
32 | 35 | import java.util.Map; |
33 | 36 | import java.util.TreeMap; |
34 | 37 | import java.util.concurrent.ConcurrentHashMap; |
@@ -192,15 +195,58 @@ public void testRemoveAndAddColumns() throws IOException { |
192 | 195 | } |
193 | 196 |
|
194 | 197 | @Test |
195 | | - public void testSerialization() throws IOException { |
| 198 | + public void testSerialization() throws IOException, ClassNotFoundException { |
196 | 199 | CSVRecord shortRec; |
197 | | - try (final CSVParser parser = CSVParser.parse("a,b", CSVFormat.newFormat(','))) { |
| 200 | + try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) { |
198 | 201 | shortRec = parser.iterator().next(); |
199 | 202 | } |
200 | 203 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
201 | 204 | try (ObjectOutputStream oos = new ObjectOutputStream(out)) { |
202 | 205 | oos.writeObject(shortRec); |
203 | 206 | } |
| 207 | + final ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); |
| 208 | + try (ObjectInputStream ois = new ObjectInputStream(in)) { |
| 209 | + final Object object = ois.readObject(); |
| 210 | + assertTrue(object instanceof CSVRecord); |
| 211 | + final CSVRecord rec = (CSVRecord) object; |
| 212 | + assertEquals(1L, rec.getRecordNumber()); |
| 213 | + assertEquals("One", rec.get(0)); |
| 214 | + assertEquals("Two", rec.get(1)); |
| 215 | + assertEquals(2, rec.size()); |
| 216 | + assertEquals(shortRec.getCharacterPosition(), rec.getCharacterPosition()); |
| 217 | + assertEquals("my comment", rec.getComment()); |
| 218 | + // The parser is not serialized |
| 219 | + assertNull(rec.getParser()); |
| 220 | + // Check all header map functionality is absent |
| 221 | + assertTrue(rec.isConsistent()); |
| 222 | + assertFalse(rec.isMapped("A")); |
| 223 | + assertFalse(rec.isSet("A")); |
| 224 | + assertEquals(0, rec.toMap().size()); |
| 225 | + // This will throw |
| 226 | + try { |
| 227 | + rec.get("A"); |
| 228 | + org.junit.jupiter.api.Assertions.fail("Access by name is not expected after deserialisation"); |
| 229 | + } catch (IllegalStateException expected) { |
| 230 | + // OK |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + /** |
| 236 | + * Test deserialisation of a record created using version 1.6. |
| 237 | + * |
| 238 | + * @throws IOException Signals that an I/O exception has occurred. |
| 239 | + */ |
| 240 | + @Test |
| 241 | + public void testDeserialisation() throws IOException { |
| 242 | + CSVRecord shortRec; |
| 243 | + try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) { |
| 244 | + shortRec = parser.iterator().next(); |
| 245 | + } |
| 246 | + try (FileOutputStream out = new FileOutputStream("/tmp/csvRecord.ser"); |
| 247 | + ObjectOutputStream oos = new ObjectOutputStream(out)) { |
| 248 | + oos.writeObject(shortRec); |
| 249 | + } |
204 | 250 | } |
205 | 251 |
|
206 | 252 | @Test |
|
0 commit comments