1 package com.opencsv.bean;
2
3 import com.opencsv.CSVReader;
4 import org.junit.jupiter.api.Test;
5
6 import java.io.StringReader;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11
12 import static org.junit.jupiter.api.Assertions.*;
13
14 public class CsvToBeanFilterTest {
15
16 private static final String TEST_STRING =
17 "FEATURE_NAME,STATE,USER_COUNT\n" +
18 "hello world,production,3228\n" +
19 "calc age,beta,74\n" +
20 "wash dishes,alpha,3";
21
22 private static final String TEST_EMPTY_STRING =
23 "FEATURE_NAME,STATE,USER_COUNT\n" +
24 "hello world, ,3228\n" +
25 "calc age,,74\n" +
26 "wash dishes, ,3";
27
28 private CSVReader createReader() {
29 StringReader reader = new StringReader(TEST_STRING);
30 return new CSVReader(reader);
31 }
32
33 private MappingStrategy<Feature> createMappingStrategy() {
34 HeaderColumnNameTranslateMappingStrategy<Feature> strategy = new HeaderColumnNameTranslateMappingStrategy<>();
35 Map<String, String> columnMap = new HashMap<>();
36 columnMap.put("FEATURE_NAME", "name");
37 columnMap.put("STATE", "state");
38 strategy.setColumnMapping(columnMap);
39 strategy.setType(Feature.class);
40 return strategy;
41 }
42
43 public static class Feature {
44
45 @CsvBindByName(column = "FEATURE_NAME")
46 private String name;
47
48 @CsvBindByName(required = true)
49 private String state;
50
51 public void setName(String name) {
52 this.name = name;
53 }
54
55 public void setState(String state) {
56 this.state = state;
57 }
58
59 public String getName() {
60 return name;
61 }
62
63 public String getState() {
64 return state;
65 }
66 }
67
68
69 private static class NonProductionFilter implements CsvToBeanFilter {
70
71 @Override
72 public boolean allowLine(String[] line) {
73 String value = line[1];
74 return !"production".equals(value);
75 }
76
77 }
78
79 @Test
80 public void testColumnNameTranslationWithLineFiltering() {
81 CsvToBean<Feature> csvToBean = new CsvToBeanBuilder<Feature>(createReader())
82 .withMappingStrategy(createMappingStrategy())
83 .withFilter(new NonProductionFilter())
84 .build();
85 List<Feature> list = csvToBean.parse();
86 assertEquals(2, list.size(), "Parsing resulted in the wrong number of items.");
87 assertEquals("calc age", list.get(0).getName(), "The first item has the wrong name.");
88 assertEquals("beta", list.get(0).getState(), "The first item has the wrong state.");
89 assertEquals("wash dishes", list.get(1).getName(), "The second item has the wrong name.");
90 assertEquals("alpha", list.get(1).getState(), "The second item has the wrong state.");
91 }
92
93 @Test
94 public void testColumnNameTranslationWithLineFilteringAndEmptyState() {
95 CsvToBean<Feature> csvToBean = new CsvToBeanBuilder<Feature>(new StringReader(TEST_EMPTY_STRING))
96 .withMappingStrategy(createMappingStrategy())
97 .withFilter(new NonProductionFilter())
98 .build();
99 List<Feature> list = csvToBean.parse();
100 assertEquals(" ", list.get(0).getState());
101 assertTrue(list.get(1).getState().isEmpty());
102 assertEquals(" ", list.get(2).getState());
103 }
104
105
106 @Test
107 public void testFilterWithParallelParsing() {
108 MappingStrategy<Feature> strategy = new HeaderColumnNameMappingStrategy<>();
109 strategy.setType(Feature.class);
110 List<Feature> list = new CsvToBeanBuilder<Feature>(new StringReader(TEST_STRING))
111 .withMappingStrategy(strategy)
112 .withFilter(new NonProductionFilter())
113 .build().parse();
114 assertEquals(2, list.size(), "Parsing resulted in the wrong number of items.");
115 assertEquals("calc age", list.get(0).getName(), "The first item has the wrong name.");
116 assertEquals("beta", list.get(0).getState(), "The first item has the wrong state.");
117 assertEquals("wash dishes", list.get(1).getName(), "The second item has the wrong name.");
118 assertEquals("alpha", list.get(1).getState(), "The second item has the wrong state.");
119 }
120
121 @Test
122 public void testFilterWithIteratorParsing() {
123 MappingStrategy<Feature> strategy = new HeaderColumnNameMappingStrategy<>();
124 strategy.setType(Feature.class);
125 CsvToBean<Feature> ctb = new CsvToBeanBuilder<Feature>(new StringReader(TEST_STRING))
126 .withMappingStrategy(strategy)
127 .withFilter(new NonProductionFilter())
128 .build();
129 List<Feature> list = new ArrayList<>(2);
130 for(Feature f : ctb) { list.add(f); }
131 assertEquals(2, list.size(), "Parsing resulted in the wrong number of items.");
132 assertEquals("calc age", list.get(0).getName(), "The first item has the wrong name.");
133 assertEquals("beta", list.get(0).getState(), "The first item has the wrong state.");
134 assertEquals("wash dishes", list.get(1).getName(), "The second item has the wrong name.");
135 assertEquals("alpha", list.get(1).getState(), "The second item has the wrong state.");
136 }
137 }