View Javadoc
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 java.io.IOException;
26  import java.io.InputStream;
27  import java.io.ObjectInputStream;
28  
29  import org.apache.commons.csv.CSVRecord;
30  import org.junit.jupiter.api.Test;
31  
32  public class JiraCsv248Test {
33      private static InputStream getTestInput() {
34          return ClassLoader.getSystemClassLoader().getResourceAsStream("org/apache/commons/csv/CSV-248/csvRecord.bin");
35      }
36  
37      /**
38       * Test deserialisation of a CSVRecord created using version 1.6.
39       *
40       * <p>
41       * This test asserts that serialization from 1.8 onwards is consistent with previous versions. Serialization was
42       * broken in version 1.7.
43       *
44       * @throws IOException Signals that an I/O exception has occurred.
45       * @throws ClassNotFoundException If the CSVRecord cannot be deserialized
46       */
47      @Test
48      public void testJiraCsv248() throws IOException, ClassNotFoundException {
49          // Record was originally created using CSV version 1.6 with the following code:
50          // try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two",
51          // CSVFormat.DEFAULT.builder().setHeader().setCommentMarker('#'))) {
52          // CSVRecord rec = parser.iterator().next();
53          // }
54          try (InputStream in = getTestInput(); final ObjectInputStream ois = new ObjectInputStream(in)) {
55              final Object object = ois.readObject();
56              assertTrue(object instanceof CSVRecord);
57              final CSVRecord rec = (CSVRecord) object;
58              assertEquals(1L, rec.getRecordNumber());
59              assertEquals("One", rec.get(0));
60              assertEquals("Two", rec.get(1));
61              assertEquals(2, rec.size());
62              // The comment and whitespace are ignored so this is not 17 but 4
63              assertEquals(4, rec.getCharacterPosition());
64              assertEquals("my comment", rec.getComment());
65              // The parser is not serialized
66              assertNull(rec.getParser());
67              // Check all header map functionality is absent
68              assertTrue(rec.isConsistent());
69              assertFalse(rec.isMapped("A"));
70              assertFalse(rec.isSet("A"));
71              assertEquals(0, rec.toMap().size());
72              // This will throw
73              try {
74                  rec.get("A");
75                  org.junit.jupiter.api.Assertions.fail("Access by name is not expected after deserialisation");
76              } catch (final IllegalStateException expected) {
77                  // OK
78              }
79          }
80      }
81  }