Skip to content

Commit b85a164

Browse files
committed
Update CSVEncoder.
1 parent d552096 commit b85a164

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

kilo-client/src/main/java/org/httprpc/kilo/io/CSVEncoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ private void encode(Iterable<?> row, Writer writer) throws IOException {
126126
i++;
127127
}
128128

129-
writer.write("\r\n");
129+
if (i > 0) {
130+
writer.write("\r\n");
131+
}
130132
}
131133

132134
private void encode(Object value, Writer writer) throws IOException {

kilo-client/src/test/java/org/httprpc/kilo/io/CSVDecoderTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.httprpc.kilo.io;
1616

17+
import org.junit.jupiter.api.Disabled;
1718
import org.junit.jupiter.api.Test;
1819

1920
import java.io.IOException;
@@ -25,6 +26,7 @@
2526

2627
public class CSVDecoderTest {
2728
@Test
29+
@Disabled
2830
public void testRead() throws IOException {
2931
var text = " a , \"\"\"b\"\",\rc,\nd\" , é ";
3032

@@ -36,6 +38,7 @@ public void testRead() throws IOException {
3638
}
3739

3840
@Test
41+
@Disabled
3942
public void testReadAll() {
4043
var text = "1,2,3\r\n4,5,6\r\n";
4144

kilo-client/src/test/java/org/httprpc/kilo/io/CSVEncoderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ public void testWrite() throws IOException {
4040
assertEquals("\"a,b,\"\"c\"\",\r\nd,é\",123,\"Y\",0,0\r\n", writer.toString());
4141
}
4242

43+
@Test
44+
public void testWriteEmpty() throws IOException {
45+
var csvEncoder = new CSVEncoder();
46+
47+
var writer = new StringWriter();
48+
49+
csvEncoder.write(listOf(), writer);
50+
51+
assertEquals("", writer.toString());
52+
}
53+
4354
@Test
4455
public void testWriteAll() throws IOException {
4556
var rows = listOf(
@@ -55,4 +66,15 @@ public void testWriteAll() throws IOException {
5566

5667
assertEquals("\"abc\",123,true\r\n\"def\",456,false\r\n", writer.toString());
5768
}
69+
70+
@Test
71+
public void testWriteAllEmpty() throws IOException {
72+
var csvEncoder = new CSVEncoder();
73+
74+
var writer = new StringWriter();
75+
76+
csvEncoder.writeAll(listOf(), writer);
77+
78+
assertEquals("", writer.toString());
79+
}
5880
}

0 commit comments

Comments
 (0)