1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.cellprocessor.constraint;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.supercsv.SuperCsvTestUtils.ANONYMOUS_CSVCONTEXT;
20 import static org.supercsv.cellprocessor.constraint.LMinMax.MAX_INTEGER;
21 import static org.supercsv.cellprocessor.constraint.LMinMax.MIN_INTEGER;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.supercsv.cellprocessor.StrReplace;
26 import org.supercsv.cellprocessor.ift.CellProcessor;
27 import org.supercsv.exception.SuperCsvCellProcessorException;
28 import org.supercsv.exception.SuperCsvConstraintViolationException;
29 import org.supercsv.mock.IdentityTransform;
30
31
32
33
34
35
36
37 public class LMinMaxTest {
38
39 private CellProcessor processor;
40 private CellProcessor processorChain;
41
42
43
44
45 @Before
46 public void setUp() {
47 processor = new LMinMax(MIN_INTEGER, MAX_INTEGER);
48 processorChain = new LMinMax(MIN_INTEGER, MAX_INTEGER, new IdentityTransform());
49 }
50
51
52
53
54 @Test
55 public void testValidLong() {
56 long input = 123L;
57 assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
58 assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
59 }
60
61
62
63
64 @Test
65 public void testValidLongString() {
66 String input = "123";
67 Long expected = 123L;
68 assertEquals(expected, processor.execute(input, ANONYMOUS_CSVCONTEXT));
69 assertEquals(expected, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
70 }
71
72
73
74
75 @Test
76 public void testMinBoundary() {
77 long input = MIN_INTEGER;
78 assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
79 assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
80 }
81
82
83
84
85 @Test
86 public void testMaxBoundary() {
87 long input = MAX_INTEGER;
88 assertEquals(input, processor.execute(input, ANONYMOUS_CSVCONTEXT));
89 assertEquals(input, processorChain.execute(input, ANONYMOUS_CSVCONTEXT));
90 }
91
92
93
94
95 @Test
96 public void testChainedAfterStringCellProcessor() {
97 final CellProcessor chain = new StrReplace("zero", "0", new LMinMax(MIN_INTEGER, MAX_INTEGER));
98 assertEquals(0L, chain.execute("zero", ANONYMOUS_CSVCONTEXT));
99 }
100
101
102
103
104 @Test(expected = SuperCsvConstraintViolationException.class)
105 public void testLessThanMin() {
106 long lessThanMin = MIN_INTEGER - 1L;
107 processor.execute(lessThanMin, ANONYMOUS_CSVCONTEXT);
108 }
109
110
111
112
113 @Test(expected = SuperCsvConstraintViolationException.class)
114 public void testGreaterThanMax() {
115 long greaterThanMax = MAX_INTEGER + 1L;
116 processor.execute(greaterThanMax, ANONYMOUS_CSVCONTEXT);
117 }
118
119
120
121
122 @Test(expected = SuperCsvCellProcessorException.class)
123 public void testWithNonLongString() {
124 processor.execute("not long!", ANONYMOUS_CSVCONTEXT);
125 }
126
127
128
129
130 @Test(expected = IllegalArgumentException.class)
131 public void testWithInvalidMaxMin() {
132 new LMinMax(MAX_INTEGER, MIN_INTEGER);
133 }
134
135
136
137
138 @Test(expected = SuperCsvCellProcessorException.class)
139 public void testWithNull() {
140 processor.execute(null, ANONYMOUS_CSVCONTEXT);
141 }
142 }