Skip to content

Commit 66c34da

Browse files
committed
[CSV-248] Test CSVRecord deserialization from binary format.
The serialised form was created using version 1.6.
1 parent 8d6772a commit 66c34da

3 files changed

Lines changed: 80 additions & 17 deletions

File tree

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,23 +232,6 @@ public void testSerialization() throws IOException, ClassNotFoundException {
232232
}
233233
}
234234

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-
}
250-
}
251-
252235
@Test
253236
public void testToMap() {
254237
final Map<String, String> map = this.recordWithHeader.toMap();
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.commons.csv.issues;
19+
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertFalse;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
25+
import org.apache.commons.csv.CSVRecord;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.io.IOException;
29+
import java.io.InputStream;
30+
import java.io.ObjectInputStream;
31+
32+
public class JiraCsv248Test {
33+
/**
34+
* Test deserialisation of a CSVRecord create using version 1.6.
35+
*
36+
* <p>This test asserts that serialization from 1.8 onwards is consistent with
37+
* previous versions. Serialization was broken in version 1.7.
38+
*
39+
* @throws IOException Signals that an I/O exception has occurred.
40+
* @throws ClassNotFoundException If the CSVRecord cannot be deserialized
41+
*/
42+
@Test
43+
public void testJiraCsv248() throws IOException, ClassNotFoundException {
44+
// Record was originally created using CSV version 1.6 with the following code:
45+
//try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) {
46+
// CSVRecord rec = parser.iterator().next();
47+
//}
48+
try (InputStream in = getTestInput();
49+
ObjectInputStream ois = new ObjectInputStream(in)) {
50+
final Object object = ois.readObject();
51+
assertTrue(object instanceof CSVRecord);
52+
final CSVRecord rec = (CSVRecord) object;
53+
assertEquals(1L, rec.getRecordNumber());
54+
assertEquals("One", rec.get(0));
55+
assertEquals("Two", rec.get(1));
56+
assertEquals(2, rec.size());
57+
// The comment and whitespace are ignored so this is not 17 but 4
58+
assertEquals(4, rec.getCharacterPosition());
59+
assertEquals("my comment", rec.getComment());
60+
// The parser is not serialized
61+
assertNull(rec.getParser());
62+
// Check all header map functionality is absent
63+
assertTrue(rec.isConsistent());
64+
assertFalse(rec.isMapped("A"));
65+
assertFalse(rec.isSet("A"));
66+
assertEquals(0, rec.toMap().size());
67+
// This will throw
68+
try {
69+
rec.get("A");
70+
org.junit.jupiter.api.Assertions.fail("Access by name is not expected after deserialisation");
71+
} catch (IllegalStateException expected) {
72+
// OK
73+
}
74+
}
75+
}
76+
77+
private static InputStream getTestInput() {
78+
return ClassLoader.getSystemClassLoader().getResourceAsStream("CSV-248/csvRecord.bin");
79+
}
80+
}
485 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)