View Javadoc
1   /*
2    * Copyright 2017 Andrew Rucket 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  import java.lang.reflect.Field;
24  
25  import static org.junit.jupiter.api.Assertions.*;
26  
27  public class CsvRequiredFieldEmptyExceptionTest {
28      private static final String TEST_MESSAGE = "some test message";
29      
30      @Test
31      public void codeCoverageConstructors() throws NoSuchFieldException {
32          Class<TestCase80> c = TestCase80.class;
33          Field f = c.getField("test");
34          CsvRequiredFieldEmptyException e1 = new CsvRequiredFieldEmptyException(c, f);
35          assertEquals(TestCase80.class, e1.getBeanClass());
36          assertEquals(f, e1.getDestinationField());
37          
38          e1 = new CsvRequiredFieldEmptyException();
39          assertNull(e1.getBeanClass());
40          assertNull(e1.getDestinationField());
41          assertNull(e1.getCause());
42          assertNull(e1.getMessage());
43          assertEquals(-1, e1.getLineNumber());
44          assertNull(e1.getLine());
45  
46          String err = "Test";
47          e1 = new CsvRequiredFieldEmptyException(err);
48          assertNull(e1.getBeanClass());
49          assertNull(e1.getDestinationField());
50          assertNull(e1.getCause());
51          assertEquals(err, e1.getMessage());
52          assertEquals(-1, e1.getLineNumber());
53          assertNull(e1.getLine());
54          
55      }
56      
57      @Test
58      public void serializationDeserialization() throws IOException, ClassNotFoundException {
59          MockBean bean = new MockBean();
60          Field field = bean.getClass().getDeclaredFields()[0];
61          CsvRequiredFieldEmptyException orig = new CsvRequiredFieldEmptyException(bean.getClass(), field, TEST_MESSAGE);
62          orig.setLineNumber(Long.MAX_VALUE);
63          orig.setLine(new String[]{"abc", "def"});
64          assertNotNull(orig.getBeanClass());
65          assertNotNull(orig.getDestinationField());
66          assertEquals(Long.MAX_VALUE, orig.getLineNumber());
67          assertArrayEquals(new String[]{"abc", "def"}, orig.getLine());
68          assertNotNull(orig.getLocalizedMessage());
69          
70          ByteArrayOutputStream baos = new ByteArrayOutputStream();
71          try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
72              oos.writeObject(orig);
73          }
74          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
75          CsvRequiredFieldEmptyException deserialized = (CsvRequiredFieldEmptyException) ois.readObject();
76          assertEquals(orig.getBeanClass(), deserialized.getBeanClass());
77          assertNull(deserialized.getDestinationField());
78          assertEquals(orig.getLocalizedMessage(), deserialized.getLocalizedMessage());
79          assertEquals(orig.getLineNumber(), deserialized.getLineNumber());
80          assertArrayEquals(orig.getLine(), deserialized.getLine());
81      }
82  }