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+ }
0 commit comments