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  import java.lang.reflect.Field;
24  
25  import static org.junit.jupiter.api.Assertions.*;
26  
27  public class CsvConstraintViolationExceptionTest {
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          
35          CsvConstraintViolationException e4 = new CsvConstraintViolationException();
36          assertNull(e4.getSourceObject());
37          e4 = new CsvConstraintViolationException(f);
38          assertEquals(f, e4.getSourceObject());
39          e4 = new CsvConstraintViolationException(TEST_MESSAGE);
40          assertEquals(TEST_MESSAGE, e4.getMessage());
41          e4 = new CsvConstraintViolationException(f, TEST_MESSAGE);
42          assertEquals(f, e4.getSourceObject());
43          assertEquals(TEST_MESSAGE, e4.getMessage());
44      }
45      
46      @Test
47      public void serializationDeserialization() throws IOException, ClassNotFoundException {
48          MockBean bean = new MockBean();
49          CsvConstraintViolationException orig = new CsvConstraintViolationException(bean, TEST_MESSAGE);
50          orig.setLineNumber(Long.MAX_VALUE);
51          orig.setLine(new String[]{"abc", "def"});
52          assertNotNull(orig.getSourceObject());
53          assertEquals(Long.MAX_VALUE, orig.getLineNumber());
54          assertArrayEquals(new String[]{"abc", "def"}, orig.getLine());
55          assertNotNull(orig.getLocalizedMessage());
56          
57          ByteArrayOutputStream baos = new ByteArrayOutputStream();
58          try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
59              oos.writeObject(orig);
60          }
61          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
62          CsvConstraintViolationException deserialized = (CsvConstraintViolationException) ois.readObject();
63          assertNull(deserialized.getSourceObject());
64          assertEquals(orig.getLocalizedMessage(), deserialized.getLocalizedMessage());
65          assertEquals(orig.getLineNumber(), deserialized.getLineNumber());
66          assertArrayEquals(orig.getLine(), deserialized.getLine());
67      }
68  }