This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git

commit 66c34da258f068046536d84e712a90a5f7f6e2a0
Author: aherbert <aherb...@apache.org>
AuthorDate: Tue Jan 21 12:42:58 2020 +0000

    [CSV-248] Test CSVRecord deserialization from binary format.
    
    The serialised form was created using version 1.6.
---
 .../java/org/apache/commons/csv/CSVRecordTest.java |  17 -----
 .../apache/commons/csv/issues/JiraCsv248Test.java  |  80 +++++++++++++++++++++
 src/test/resources/CSV-248/csvRecord.bin           | Bin 0 -> 485 bytes
 3 files changed, 80 insertions(+), 17 deletions(-)

diff --git a/src/test/java/org/apache/commons/csv/CSVRecordTest.java 
b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
index 8d11d53..d6fedb0 100644
--- a/src/test/java/org/apache/commons/csv/CSVRecordTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVRecordTest.java
@@ -232,23 +232,6 @@ public class CSVRecordTest {
         }
     }
 
-    /**
-     * Test deserialisation of a record created using version 1.6.
-     *
-     * @throws IOException Signals that an I/O exception has occurred.
-     */
-    @Test
-    public void testDeserialisation() throws IOException {
-        CSVRecord shortRec;
-        try (final CSVParser parser = CSVParser.parse("A,B\n#my 
comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) {
-            shortRec = parser.iterator().next();
-        }
-        try (FileOutputStream out = new FileOutputStream("/tmp/csvRecord.ser");
-            ObjectOutputStream oos = new ObjectOutputStream(out)) {
-            oos.writeObject(shortRec);
-        }
-    }
-
     @Test
     public void testToMap() {
         final Map<String, String> map = this.recordWithHeader.toMap();
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java 
b/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java
new file mode 100644
index 0000000..c39842f
--- /dev/null
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv248Test.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.csv.issues;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.commons.csv.CSVRecord;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+
+public class JiraCsv248Test {
+    /**
+     * Test deserialisation of a CSVRecord create using version 1.6.
+     *
+     * <p>This test asserts that serialization from 1.8 onwards is consistent 
with
+     * previous versions. Serialization was broken in version 1.7.
+     *
+     * @throws IOException Signals that an I/O exception has occurred.
+     * @throws ClassNotFoundException If the CSVRecord cannot be deserialized
+     */
+    @Test
+    public void testJiraCsv248() throws IOException, ClassNotFoundException {
+        // Record was originally created using CSV version 1.6 with the 
following code:
+        //try (final CSVParser parser = CSVParser.parse("A,B\n#my 
comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) {
+        //    CSVRecord rec = parser.iterator().next();
+        //}
+        try (InputStream in = getTestInput();
+            ObjectInputStream ois = new ObjectInputStream(in)) {
+            final Object object = ois.readObject();
+            assertTrue(object instanceof CSVRecord);
+            final CSVRecord rec = (CSVRecord) object;
+            assertEquals(1L, rec.getRecordNumber());
+            assertEquals("One", rec.get(0));
+            assertEquals("Two", rec.get(1));
+            assertEquals(2, rec.size());
+            // The comment and whitespace are ignored so this is not 17 but 4
+            assertEquals(4, rec.getCharacterPosition());
+            assertEquals("my comment", rec.getComment());
+            // The parser is not serialized
+            assertNull(rec.getParser());
+            // Check all header map functionality is absent
+            assertTrue(rec.isConsistent());
+            assertFalse(rec.isMapped("A"));
+            assertFalse(rec.isSet("A"));
+            assertEquals(0, rec.toMap().size());
+            // This will throw
+            try {
+                rec.get("A");
+                org.junit.jupiter.api.Assertions.fail("Access by name is not 
expected after deserialisation");
+            } catch (IllegalStateException expected) {
+                // OK
+            }
+        }
+    }
+
+    private static InputStream getTestInput() {
+        return 
ClassLoader.getSystemClassLoader().getResourceAsStream("CSV-248/csvRecord.bin");
+    }
+}
\ No newline at end of file
diff --git a/src/test/resources/CSV-248/csvRecord.bin 
b/src/test/resources/CSV-248/csvRecord.bin
new file mode 100644
index 0000000..36047d5
Binary files /dev/null and b/src/test/resources/CSV-248/csvRecord.bin differ

Reply via email to