|
23 | 23 | import static org.junit.jupiter.api.Assertions.assertNull; |
24 | 24 | import static org.junit.jupiter.api.Assertions.assertThrows; |
25 | 25 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertAll; |
26 | 27 |
|
27 | 28 | import java.io.ByteArrayInputStream; |
28 | 29 | import java.io.ByteArrayOutputStream; |
@@ -341,6 +342,37 @@ public void testToString() { |
341 | 342 | assertTrue(recordWithHeader.toString().contains("values=")); |
342 | 343 | } |
343 | 344 |
|
| 345 | + @Test |
| 346 | + public void testDuplicateHeaderGet() throws IOException { |
| 347 | + final String csv = "A,A,B,B\n1,2,5,6\n"; |
| 348 | + final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build(); |
| 349 | + |
| 350 | + try (final CSVParser parser = CSVParser.parse(csv, format)) { |
| 351 | + final CSVRecord record = parser.nextRecord(); |
| 352 | + |
| 353 | + assertAll("Test that it gets the last instance of a column when there are duplicate headings", |
| 354 | + () -> assertEquals("2", record.get("A")), |
| 355 | + () -> assertEquals("6", record.get("B")) |
| 356 | + ); |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + @Test |
| 361 | + public void testDuplicateHeaderToMap() throws IOException { |
| 362 | + final String csv = "A,A,B,B\n1,2,5,6\n"; |
| 363 | + final CSVFormat format = CSVFormat.DEFAULT.builder().setHeader().build(); |
| 364 | + |
| 365 | + try (final CSVParser parser = CSVParser.parse(csv, format)) { |
| 366 | + final CSVRecord record = parser.nextRecord(); |
| 367 | + final Map<String, String> map = record.toMap(); |
| 368 | + |
| 369 | + assertAll("Test that it gets the last instance of a column when there are duplicate headings", |
| 370 | + () -> assertEquals("2", map.get("A")), |
| 371 | + () -> assertEquals("6", map.get("B")) |
| 372 | + ); |
| 373 | + } |
| 374 | + } |
| 375 | + |
344 | 376 | private void validateMap(final Map<String, String> map, final boolean allowsNulls) { |
345 | 377 | assertTrue(map.containsKey(EnumHeader.FIRST.name())); |
346 | 378 | assertTrue(map.containsKey(EnumHeader.SECOND.name())); |
|
0 commit comments