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