Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 848c148

Browse files
committed
2 parents 934f214 + 56d9c8d commit 848c148

3 files changed

Lines changed: 63 additions & 31 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
</properties>
4040
<body>
4141
<release version="1.9" date="2020-MM-DD" description="Feature and bug fix release (Java 8)">
42+
<!-- FIX -->
4243
<action type="add" dev="ggregory" due-to="dota17">Add test cases for CSVRecord with get(Enum) and toString. #54.</action>
4344
<action type="update" dev="ggregory" due-to="Amey Jadiye">Replace FindBugs with SpotBugs #56.</action>
4445
<action type="update" dev="ggregory" due-to="Chen">Javadoc typo in CSVFormat let's -> lets #57.</action>
@@ -57,6 +58,8 @@
5758
<action issue="CSV-267" type="fix" dev="ggregory" due-to="Arturo Bernal">Minor improvements #126, #127.</action>
5859
<action issue="CSV-123" type="fix" dev="ggregory" due-to="Emmanuel Bourg, Benedikt Ritter, shivakrishnaah, Gary Gregory">Add possibility to use ResultSet header meta data as CSV header #11.</action>
5960
<!-- ADD -->
61+
<action issue="CSV-275" type="add" dev="ggregory" due-to="Michael Wyraz, Gary Gregory">Make CSVRecord#toList() public.</action>
62+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add CSVRecord#toStream().</action>
6063
<!-- UPDATE -->
6164
<action type="update" dev="ggregory" due-to="Gary Gregory">Update org.junit.jupiter:junit-jupiter from 5.6.0 to 5.7.0, #84 #109</action>
6265
<action type="update" dev="ggregory" due-to="Gary Gregory">Update tests from Apache Commons Lang 3.9 to 3.12.0.</action>

src/main/java/org/apache/commons/csv/CSVRecord.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.List;
2525
import java.util.Map;
2626
import java.util.Map.Entry;
27+
import java.util.stream.Stream;
2728
import java.util.Objects;
2829

2930
/**
@@ -291,11 +292,10 @@ public int size() {
291292
/**
292293
* Converts the values to a List.
293294
*
294-
* TODO: Maybe make this public?
295-
*
296295
* @return a new List
296+
* @since 1.9.0
297297
*/
298-
private List<String> toList() {
298+
public List<String> toList() {
299299
return Arrays.asList(values);
300300
}
301301

@@ -308,6 +308,16 @@ public Map<String, String> toMap() {
308308
return putIn(new LinkedHashMap<String, String>(values.length));
309309
}
310310

311+
/**
312+
* Returns a sequential ordered stream whose elements are the values.
313+
*
314+
* @return the new stream.
315+
* @since 1.9.0
316+
*/
317+
public Stream<String> toStream() {
318+
return Arrays.stream(values);
319+
}
320+
311321
/**
312322
* Returns a string representation of the contents of this record. The result is constructed by comment, mapping,
313323
* recordNumber and by passing the internal values array to {@link Arrays#toString(Object[])}.

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

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.Map;
3535
import java.util.TreeMap;
3636
import java.util.concurrent.ConcurrentHashMap;
37+
import java.util.concurrent.atomic.AtomicInteger;
3738

3839
import org.apache.commons.lang3.StringUtils;
3940
import org.junit.jupiter.api.BeforeEach;
@@ -80,13 +81,26 @@ record = parser.iterator().next();
8081
}
8182
}
8283

84+
@Test
85+
public void testCSVRecordNULLValues() throws IOException {
86+
final CSVParser parser = CSVParser.parse("A,B\r\nONE,TWO", CSVFormat.DEFAULT.withHeader());
87+
final CSVRecord csvRecord = new CSVRecord(parser, null, null, 0L, 0L);
88+
assertEquals(0, csvRecord.size());
89+
assertThrows(IllegalArgumentException.class, () -> csvRecord.get("B"));
90+
}
91+
8392
@Test
8493
public void testGetInt() {
8594
assertEquals(values[0], record.get(0));
8695
assertEquals(values[1], record.get(1));
8796
assertEquals(values[2], record.get(2));
8897
}
8998

99+
@Test
100+
public void testGetNullEnum() {
101+
assertThrows(IllegalArgumentException.class, () -> recordWithHeader.get((Enum<?>) null));
102+
}
103+
90104
@Test
91105
public void testGetString() {
92106
assertEquals(values[0], recordWithHeader.get("first"));
@@ -110,11 +124,6 @@ public void testGetUnmappedEnum() {
110124
assertThrows(IllegalArgumentException.class, () -> recordWithHeader.get(EnumFixture.UNKNOWN_COLUMN));
111125
}
112126

113-
@Test
114-
public void testGetNullEnum() {
115-
assertThrows(IllegalArgumentException.class, () -> recordWithHeader.get((Enum<?>) null));
116-
}
117-
118127
@Test
119128
public void testGetUnmappedName() {
120129
assertThrows(IllegalArgumentException.class, () -> assertNull(recordWithHeader.get("fourth")));
@@ -130,6 +139,13 @@ public void testGetUnmappedPositiveInt() {
130139
assertThrows(ArrayIndexOutOfBoundsException.class, () -> recordWithHeader.get(Integer.MAX_VALUE));
131140
}
132141

142+
@Test
143+
public void testGetWithEnum() {
144+
assertEquals(recordWithHeader.get("first"), recordWithHeader.get(EnumHeader.FIRST));
145+
assertEquals(recordWithHeader.get("second"), recordWithHeader.get(EnumHeader.SECOND));
146+
assertThrows(IllegalArgumentException.class, () -> recordWithHeader.get(EnumFixture.UNKNOWN_COLUMN));
147+
}
148+
133149
@Test
134150
public void testIsConsistent() {
135151
assertTrue(record.isConsistent());
@@ -248,6 +264,15 @@ public void testSerialization() throws IOException, ClassNotFoundException {
248264
}
249265
}
250266

267+
@Test
268+
public void testToList() {
269+
int i = 0;
270+
for (final String value : record.toList()) {
271+
assertEquals(values[i], value);
272+
i++;
273+
}
274+
}
275+
251276
@Test
252277
public void testToMap() {
253278
final Map<String, String> map = this.recordWithHeader.toMap();
@@ -272,6 +297,23 @@ public void testToMapWithShortRecord() throws Exception {
272297
}
273298
}
274299

300+
@Test
301+
public void testToStream() {
302+
final AtomicInteger i = new AtomicInteger();
303+
record.toStream().forEach(value -> {
304+
assertEquals(values[i.get()], value);
305+
i.incrementAndGet();
306+
});
307+
}
308+
309+
@Test
310+
public void testToString() {
311+
assertNotNull(recordWithHeader.toString());
312+
assertTrue(recordWithHeader.toString().contains("comment="));
313+
assertTrue(recordWithHeader.toString().contains("recordNumber="));
314+
assertTrue(recordWithHeader.toString().contains("values="));
315+
}
316+
275317
private void validateMap(final Map<String, String> map, final boolean allowsNulls) {
276318
assertTrue(map.containsKey("first"));
277319
assertTrue(map.containsKey("second"));
@@ -285,27 +327,4 @@ private void validateMap(final Map<String, String> map, final boolean allowsNull
285327
assertEquals("C", map.get("third"));
286328
assertEquals(null, map.get("fourth"));
287329
}
288-
289-
@Test
290-
public void testToString() {
291-
assertNotNull(recordWithHeader.toString());
292-
assertTrue(recordWithHeader.toString().contains("comment="));
293-
assertTrue(recordWithHeader.toString().contains("recordNumber="));
294-
assertTrue(recordWithHeader.toString().contains("values="));
295-
}
296-
297-
@Test
298-
public void testGetWithEnum() {
299-
assertEquals(recordWithHeader.get("first"), recordWithHeader.get(EnumHeader.FIRST));
300-
assertEquals(recordWithHeader.get("second"), recordWithHeader.get(EnumHeader.SECOND));
301-
assertThrows(IllegalArgumentException.class, () -> recordWithHeader.get(EnumFixture.UNKNOWN_COLUMN));
302-
}
303-
304-
@Test
305-
public void testCSVRecordNULLValues() throws IOException {
306-
final CSVParser parser = CSVParser.parse("A,B\r\nONE,TWO", CSVFormat.DEFAULT.withHeader());
307-
final CSVRecord csvRecord = new CSVRecord(parser, null, null, 0L, 0L);
308-
assertEquals(0, csvRecord.size());
309-
assertThrows(IllegalArgumentException.class, () -> csvRecord.get("B"));
310-
}
311330
}

0 commit comments

Comments
 (0)