Skip to content

Commit c66ea98

Browse files
committed
CSV-216 test mutators
1 parent 6eebeb0 commit c66ea98

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

src/test/java/org/apache/commons/csv/CSVMutableRecordTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package org.apache.commons.csv;
22

3+
import static org.junit.Assert.assertTrue;
4+
35
import org.junit.Assert;
6+
import org.junit.Test;
47

58
public class CSVMutableRecordTest extends CSVRecordTest {
69

10+
@Override
11+
@Test
12+
public void isMutable() {
13+
assertTrue(record.isMutable());
14+
assertTrue(recordWithHeader.isMutable());
15+
}
16+
717
@Override
818
protected CSVFormat createCommaFormat() {
919
return super.createCommaFormat().withMutableRecords(true);

src/test/java/org/apache/commons/csv/CSVRecordTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertNotSame;
2223
import static org.junit.Assert.assertNull;
24+
import static org.junit.Assert.assertSame;
2325
import static org.junit.Assert.assertTrue;
2426

2527
import java.io.IOException;
@@ -187,6 +189,62 @@ public void testToMapWithShortRecord() throws Exception {
187189
}
188190
}
189191

192+
@Test
193+
public void isMutable() {
194+
assertFalse(record.isMutable());
195+
assertFalse(recordWithHeader.isMutable());
196+
}
197+
198+
@Test
199+
public void testMutable() throws Exception {
200+
CSVRecord mutable = record.mutable();
201+
assertTrue(mutable.isMutable());
202+
if (record.isMutable()) {
203+
assertSame(record, mutable);
204+
} else {
205+
assertNotSame(record, mutable);
206+
}
207+
}
208+
209+
@Test
210+
public void testImmutable() throws Exception {
211+
CSVRecord immutable = record.immutable();
212+
assertFalse(immutable.isMutable());
213+
assertSame(immutable, immutable.immutable());
214+
assertNotSame(immutable, immutable.mutable());
215+
if (record.isMutable()) {
216+
assertNotSame(record, immutable);
217+
} else {
218+
assertSame(record, immutable);
219+
}
220+
}
221+
222+
@Test
223+
public void testWithValue() throws Exception {
224+
assertEquals("A", record.get(0));
225+
CSVRecord newR = record.withValue(0, "X");
226+
assertEquals("X", newR.get(0));
227+
if (record.isMutable()) {
228+
assertSame(record, newR);
229+
} else {
230+
// unchanged
231+
assertEquals("A", record.get(0));
232+
}
233+
}
234+
235+
@Test
236+
public void testWithValueName() throws Exception {
237+
assertEquals("B", recordWithHeader.get("second"));
238+
CSVRecord newR = recordWithHeader.withValue("second", "Y");
239+
assertEquals("Y", newR.get("second"));
240+
if (record.isMutable()) {
241+
assertSame(recordWithHeader, newR);
242+
} else {
243+
// unchanged
244+
assertEquals("B", recordWithHeader.get("second"));
245+
}
246+
}
247+
190248
@Test
191249
public void testToMapWithNoHeader() throws Exception {
192250
try (final CSVParser parser = CSVParser.parse("a,b", createCommaFormat())) {

0 commit comments

Comments
 (0)