View Javadoc
1   package com.opencsv.bean;
2   
3   import com.opencsv.bean.exceptionhandler.CsvExceptionHandler;
4   import com.opencsv.bean.mocks.MockBean;
5   import com.opencsv.exceptions.CsvException;
6   import org.junit.jupiter.api.BeforeEach;
7   import org.junit.jupiter.api.DisplayName;
8   import org.junit.jupiter.api.Test;
9   
10  import java.io.StringReader;
11  
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  public class CsvToBeanBuilderTest {
15  
16      private static final String TEST_STRING = "Some string not really parsed but I needed a reader.";
17  
18      private CsvToBean<MockBean> throwsExceptionFirst;
19      private CsvToBean<MockBean> withExceptionHandlerFirst;
20  
21      private final CsvExceptionHandler exceptionHandler = new JunkExceptionHandler();
22  
23      @BeforeEach
24      public void setUp() {
25          HeaderColumnNameMappingStrategy<MockBean> strategy = new HeaderColumnNameMappingStrategy<>();
26          strategy.setType(MockBean.class);
27          throwsExceptionFirst = new CsvToBeanBuilder<MockBean>(new StringReader(TEST_STRING))
28                  .withMappingStrategy(strategy)
29                  .withThrowExceptions(true)
30                  .withExceptionHandler(exceptionHandler)
31                  .build();
32          withExceptionHandlerFirst = new CsvToBeanBuilder<MockBean>(new StringReader(TEST_STRING))
33                  .withMappingStrategy(strategy)
34                  .withExceptionHandler(exceptionHandler)
35                  .withThrowExceptions(true)
36                  .build();
37      }
38  
39      @DisplayName("If both withExceptionHandler and withThrowsException are called in the same builder then the withThrowsException is used.")
40      @Test
41      public void precedenceOfExceptionHandlers() {
42          assertTrue(throwsExceptionFirst.getExceptionHandler() instanceof JunkExceptionHandler);
43          assertTrue(withExceptionHandlerFirst.getExceptionHandler() instanceof JunkExceptionHandler);
44      }
45  
46      private static class JunkExceptionHandler implements CsvExceptionHandler {
47  
48          @Override
49          public CsvException handleException(CsvException e) {
50              return null;
51          }
52      }
53  }