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 com.opencsv.bean.mocks.TestCase80;
20  import org.junit.jupiter.api.Test;
21  
22  import java.io.*;
23  
24  import static org.junit.jupiter.api.Assertions.*;
25  
26  public class CsvBadConverterExceptionTest {
27      private static final String TEST_MESSAGE = "some test message";
28      
29      @Test
30      public void codeCoverageConstructors() {
31          Class<TestCase80> c = TestCase80.class;
32  
33          CsvBadConverterException e3 = new CsvBadConverterException();
34          assertNull(e3.getConverterClass());
35          e3 = new CsvBadConverterException(c);
36          assertEquals(c, e3.getConverterClass());
37          e3 = new CsvBadConverterException(TEST_MESSAGE);
38          assertEquals(TEST_MESSAGE, e3.getMessage());
39      }
40      
41      @Test
42      public void serializationDeserialization() throws IOException, ClassNotFoundException {
43          MockBean bean = new MockBean();
44          CsvBadConverterException orig = new CsvBadConverterException(bean.getClass(), TEST_MESSAGE);
45          assertNotNull(orig.getConverterClass());
46          assertNotNull(orig.getLocalizedMessage());
47          
48          ByteArrayOutputStream baos = new ByteArrayOutputStream();
49          try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
50              oos.writeObject(orig);
51          }
52          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
53          CsvBadConverterException deserialized = (CsvBadConverterException) ois.readObject();
54          assertEquals(orig.getConverterClass(), deserialized.getConverterClass());
55          assertEquals(orig.getLocalizedMessage(), deserialized.getLocalizedMessage());
56      }
57  }