View Javadoc
1   package com.opencsv.exceptions;
2   
3   import com.opencsv.bean.mocks.MockBean;
4   import org.junit.jupiter.api.Test;
5   
6   import java.io.*;
7   import java.lang.reflect.Field;
8   
9   import static org.junit.jupiter.api.Assertions.*;
10  
11  public class CsvBeanIntrospectionExceptionTest {
12      private static final String TEST_MESSAGE = "some test message";
13  
14      @Test
15      public void defaultExceptionHasNoMessage() {
16          CsvBeanIntrospectionException exception = new CsvBeanIntrospectionException();
17          assertNull(exception.getMessage());
18          assertNull(exception.getBean());
19          assertNull(exception.getField());
20      }
21  
22      @Test
23      public void exceptionWithOnlyAMessage() {
24          CsvBeanIntrospectionException exception = new CsvBeanIntrospectionException(TEST_MESSAGE);
25          assertEquals(TEST_MESSAGE, exception.getMessage());
26          assertNull(exception.getBean());
27          assertNull(exception.getField());
28      }
29  
30      @Test
31      public void exceptionWithNoMessageButHasBeanAndField() {
32          MockBean bean = new MockBean();
33          Field field = bean.getClass().getDeclaredFields()[0];
34  
35          assertNotNull(bean);
36          assertNotNull(field);
37  
38          CsvBeanIntrospectionException exception = new CsvBeanIntrospectionException(bean, field);
39          String message = exception.getMessage();
40          System.out.println(message);
41          assertTrue(message.contains(bean.getClass().getCanonicalName()));
42          assertTrue(message.contains(field.getName()));
43  
44          assertEquals(bean, exception.getBean());
45          assertEquals(field, exception.getField());
46      }
47  
48      @Test
49      public void exceptionWithMessageBeanAndFieldWillReturnMessage() {
50          MockBean bean = new MockBean();
51          Field field = bean.getClass().getDeclaredFields()[0];
52  
53          assertNotNull(bean);
54          assertNotNull(field);
55  
56          CsvBeanIntrospectionException exception = new CsvBeanIntrospectionException(bean, field, TEST_MESSAGE);
57          assertEquals(bean, exception.getBean());
58          assertEquals(field, exception.getField());
59          assertEquals(TEST_MESSAGE, exception.getMessage());
60      }
61      
62      @Test
63      public void serializationDeserialization() throws IOException, ClassNotFoundException {
64          MockBean bean = new MockBean();
65          Field field = bean.getClass().getDeclaredFields()[0];
66          CsvBeanIntrospectionException orig = new CsvBeanIntrospectionException(bean, field, TEST_MESSAGE);
67          assertNotNull(orig.getBean());
68          assertNotNull(orig.getField());
69          assertNotNull(orig.getLocalizedMessage());
70          
71          ByteArrayOutputStream baos = new ByteArrayOutputStream();
72          try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
73              oos.writeObject(orig);
74          }
75          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
76          CsvBeanIntrospectionException deserialized = (CsvBeanIntrospectionException) ois.readObject();
77          assertNull(deserialized.getBean());
78          assertNull(deserialized.getField());
79          assertEquals(orig.getLocalizedMessage(), deserialized.getLocalizedMessage());
80      }
81  }