View Javadoc
1   /*
2    * Copyright 2017 Andrew Rucker Jones.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.opencsv.exceptions;
17  
18  import com.opencsv.bean.mocks.MockBean;
19  import org.junit.jupiter.api.Test;
20  
21  import java.io.*;
22  import java.lang.reflect.Field;
23  
24  import static org.junit.jupiter.api.Assertions.*;
25  
26  public class CsvDataTypeMismatchExceptionTest {
27      private static final String TEST_MESSAGE = "some test message";
28      
29      @Test
30      public void codeCoverageConstructors() {
31          CsvDataTypeMismatchException e2 = new CsvDataTypeMismatchException();
32          assertNull(e2.getDestinationClass());
33          assertNull(e2.getSourceObject());
34          e2 = new CsvDataTypeMismatchException(TEST_MESSAGE);
35          assertEquals(TEST_MESSAGE, e2.getMessage());
36      }
37      
38      @Test
39      public void serializationDeserialization() throws IOException, ClassNotFoundException {
40          MockBean bean = new MockBean();
41          Field field = bean.getClass().getDeclaredFields()[0];
42          CsvDataTypeMismatchException orig = new CsvDataTypeMismatchException(field, bean.getClass(), TEST_MESSAGE);
43          orig.setLineNumber(Long.MAX_VALUE);
44          orig.setLine(new String[]{"abc", "def"});
45          assertNotNull(orig.getDestinationClass());
46          assertNotNull(orig.getSourceObject());
47          assertEquals(Long.MAX_VALUE, orig.getLineNumber());
48          assertArrayEquals(new String[]{"abc", "def"}, orig.getLine());
49          assertNotNull(orig.getLocalizedMessage());
50          
51          ByteArrayOutputStream baos = new ByteArrayOutputStream();
52          try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
53              oos.writeObject(orig);
54          }
55          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
56          CsvDataTypeMismatchException deserialized = (CsvDataTypeMismatchException) ois.readObject();
57          assertEquals(orig.getDestinationClass(), deserialized.getDestinationClass());
58          assertNull(deserialized.getSourceObject());
59          assertEquals(orig.getLocalizedMessage(), deserialized.getLocalizedMessage());
60          assertEquals(orig.getLineNumber(), deserialized.getLineNumber());
61          assertArrayEquals(orig.getLine(), deserialized.getLine());
62      }
63  }