Skip to content

Commit fa5dca3

Browse files
committed
Test cases for CSVPrinter.printComment()
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297075 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7bd9d1d commit fa5dca3

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ public void println() throws IOException {
6161
newLine = true;
6262
}
6363

64+
/**
65+
* Flush the underlying stream.
66+
*
67+
* @throws IOException
68+
*/
6469
public void flush() throws IOException {
6570
out.flush();
6671
}
@@ -82,22 +87,23 @@ public void println(String[] values) throws IOException {
8287

8388

8489
/**
85-
* Put a comment among the comma separated values.
86-
* Comments will always begin on a new line and occupy a
87-
* least one full line. The character specified to star
88-
* comments and a space will be inserted at the beginning of
89-
* each new line in the comment.
90+
* Put a comment on a new line among the comma separated values. Comments
91+
* will always begin on a new line and occupy a least one full line. The
92+
* character specified to start comments and a space will be inserted at
93+
* the beginning of each new line in the comment.
94+
* <p/>
95+
* If comments are disabled in the current CSV format this method does nothing.
9096
*
9197
* @param comment the comment to output
9298
*/
93-
public void printlnComment(String comment) throws IOException {
99+
public void printComment(String comment) throws IOException {
94100
if (this.format.isCommentingDisabled()) {
95101
return;
96102
}
97103
if (!newLine) {
98104
println();
99105
}
100-
out.write(this.format.getCommentStart());
106+
out.write(format.getCommentStart());
101107
out.write(' ');
102108
for (int i = 0; i < comment.length(); i++) {
103109
char c = comment.charAt(i);
@@ -109,7 +115,7 @@ public void printlnComment(String comment) throws IOException {
109115
// break intentionally excluded.
110116
case '\n':
111117
println();
112-
out.write(this.format.getCommentStart());
118+
out.write(format.getCommentStart());
113119
out.write(' ');
114120
break;
115121
default:

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
public class CSVPrinterTest extends TestCase {
3030

31-
String lineSeparator = "\r\n";
31+
String lineSeparator = CSVFormat.DEFAULT.getLineSeparator();
3232

3333
public void testPrinter1() throws IOException {
3434
StringWriter sw = new StringWriter();
@@ -102,6 +102,29 @@ public void testExcelPrinter2() throws IOException {
102102
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
103103
}
104104

105+
public void testDisabledComment() throws IOException {
106+
StringWriter sw = new StringWriter();
107+
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
108+
printer.printComment("This is a comment");
109+
110+
assertEquals("", sw.toString());
111+
}
112+
113+
public void testSingleLineComment() throws IOException {
114+
StringWriter sw = new StringWriter();
115+
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
116+
printer.printComment("This is a comment");
117+
118+
assertEquals("# This is a comment" + lineSeparator, sw.toString());
119+
}
120+
121+
public void testMultiLineComment() throws IOException {
122+
StringWriter sw = new StringWriter();
123+
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
124+
printer.printComment("This is a comment\non multiple lines");
125+
126+
assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
127+
}
105128

106129
public void testRandom() throws Exception {
107130
int iter = 10000;
@@ -184,11 +207,11 @@ public static boolean equals(String[][] a, String[][] b) {
184207
}
185208

186209
public static String printable(String s) {
187-
StringBuffer sb = new StringBuffer();
210+
StringBuilder sb = new StringBuilder();
188211
for (int i = 0; i < s.length(); i++) {
189212
char ch = s.charAt(i);
190213
if (ch <= ' ' || ch >= 128) {
191-
sb.append("(" + (int) ch + ")");
214+
sb.append("(").append((int) ch).append(")");
192215
} else {
193216
sb.append(ch);
194217
}

0 commit comments

Comments
 (0)