[
https://issues.apache.org/jira/browse/CSV-302?focusedWorklogId=818584&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-818584
]
ASF GitHub Bot logged work on CSV-302:
--------------------------------------
Author: ASF GitHub Bot
Created on: 19/Oct/22 19:35
Start Date: 19/Oct/22 19:35
Worklog Time Spent: 10m
Work Description: garydgregory commented on code in PR #276:
URL: https://github.com/apache/commons-csv/pull/276#discussion_r999860555
##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2280,6 +2279,27 @@ private void validate() throws IllegalArgumentException {
}
}
+ private Object readResolve() throws ObjectStreamException {
+ // check whether field duplicateHeaderMode (as of 1.10.0) was found in
serialized form
+ // if not, set from boolean allowDuplicateHeaderNames
+ if (duplicateHeaderMode == null) {
+ setFieldValue("duplicateHeaderMode",
+ allowDuplicateHeaderNames ? DuplicateHeaderMode.ALLOW_ALL
: DuplicateHeaderMode.DISALLOW);
+ }
+ return this;
+ }
+
+ private void setFieldValue(final String fieldName, final Object value) {
+ try {
+ Field fld = getClass().getDeclaredField(fieldName);
Review Comment:
Use final where you can.
##########
src/main/java/org/apache/commons/csv/CSVFormat.java:
##########
@@ -2280,6 +2279,27 @@ private void validate() throws IllegalArgumentException {
}
}
+ private Object readResolve() throws ObjectStreamException {
+ // check whether field duplicateHeaderMode (as of 1.10.0) was found in
serialized form
+ // if not, set from boolean allowDuplicateHeaderNames
+ if (duplicateHeaderMode == null) {
+ setFieldValue("duplicateHeaderMode",
+ allowDuplicateHeaderNames ? DuplicateHeaderMode.ALLOW_ALL
: DuplicateHeaderMode.DISALLOW);
+ }
+ return this;
+ }
+
+ private void setFieldValue(final String fieldName, final Object value) {
+ try {
+ Field fld = getClass().getDeclaredField(fieldName);
+ fld.setAccessible(true); // make field non-final
+ fld.set(this, value);
+ fld.setAccessible(false);
+ } catch (Exception ex) {
Review Comment:
Unlikely to need to catch `Exception`, catching
`ReflectiveOperationException` is cleaner IMO. But where is this used from
anyway? If it's only called from `readResolve()` why not turn it into an
`ObjectStreamException`?
##########
src/test/java/org/apache/commons/csv/CSVFormatSerializationTest.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Test deserialization of class {@link CSVFormat}.
+ */
+class CSVFormatSerializationTest {
+
+ @ParameterizedTest(name = "[{index}] {0} ({1})")
+ @CsvSource(delimiterString = ";", value = {
+ "1.1.9;CSVFormat-Excel-1.9.0.ser"
+ //BROKEN ,"1.8;CSVFormat-Excel-1.8.ser" // field
CSVFormat.delimiter was of type char (now String)
+ //BROKEN ,"1.7;CSVFormat-Excel-1.7.ser" // field
CSVFormat.delimiter was of type char (now String)
+ })
+ void testDeserializeCsvFormat(String version, String fileName) throws
IOException, ClassNotFoundException {
+
+ InputStream resourceAsStream =
getClass().getResourceAsStream("/org/apache/commons/csv/serialization/" +
fileName);
Review Comment:
Use final where you can.
##########
src/test/java/org/apache/commons/csv/CSVFormatSerializationTest.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+
+/**
+ * Test deserialization of class {@link CSVFormat}.
+ */
+class CSVFormatSerializationTest {
Review Comment:
Note that the convention in Commons CSV is that test classes and methods are
public.
Issue Time Tracking
-------------------
Worklog Id: (was: 818584)
Time Spent: 1h (was: 50m)
> CSVFormat.duplicateHeaderMode requires default for backward compatibility
> -------------------------------------------------------------------------
>
> Key: CSV-302
> URL: https://issues.apache.org/jira/browse/CSV-302
> Project: Commons CSV
> Issue Type: Bug
> Components: Parser
> Affects Versions: 1.10.0
> Reporter: Markus Spann
> Priority: Minor
> Time Spent: 1h
> Remaining Estimate: 0h
>
> The member in class {{CSVFormat}}
> {code:java}
> boolean allowDuplicateHeaderNames{code}
> was recently replaced by
> {code:java}
> DuplicateHeaderMode duplicateHeaderMode{code}
> The boolean defaults to {{{}false{}}}, while the member of type
> {{DuplicateHeaderMode}} defaults to {{{}null{}}}.
> {{duplicateHeaderMode}} must be initialized with {{DISALLOW}} for backward
> compatibility.
> The change is also problematic with regards to serialization. The class is
> serializable and {{serialVersionUID}} is unchanged between versions. The
> boolean setting {{allowDuplicateHeaderNames}} in an object serialized in
> version 1.9.0 or earlier would always be de-serialized to
> {{duplicateHeaderMode = null}} in the current head.
> To handle the code changes correctly, customized de-serialization would need
> to be implemented.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)