Test cases for CSVPrinter.printComment()
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297075 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7bd9d1d970
commit
fa5dca3be1
|
@ -61,6 +61,11 @@ public class CSVPrinter {
|
|||
newLine = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush the underlying stream.
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
public void flush() throws IOException {
|
||||
out.flush();
|
||||
}
|
||||
|
@ -82,22 +87,23 @@ public class CSVPrinter {
|
|||
|
||||
|
||||
/**
|
||||
* Put a comment among the comma separated values.
|
||||
* Comments will always begin on a new line and occupy a
|
||||
* least one full line. The character specified to star
|
||||
* comments and a space will be inserted at the beginning of
|
||||
* each new line in the comment.
|
||||
* Put a comment on a new line among the comma separated values. Comments
|
||||
* will always begin on a new line and occupy a least one full line. The
|
||||
* character specified to start comments and a space will be inserted at
|
||||
* the beginning of each new line in the comment.
|
||||
* <p/>
|
||||
* If comments are disabled in the current CSV format this method does nothing.
|
||||
*
|
||||
* @param comment the comment to output
|
||||
*/
|
||||
public void printlnComment(String comment) throws IOException {
|
||||
public void printComment(String comment) throws IOException {
|
||||
if (this.format.isCommentingDisabled()) {
|
||||
return;
|
||||
}
|
||||
if (!newLine) {
|
||||
println();
|
||||
}
|
||||
out.write(this.format.getCommentStart());
|
||||
out.write(format.getCommentStart());
|
||||
out.write(' ');
|
||||
for (int i = 0; i < comment.length(); i++) {
|
||||
char c = comment.charAt(i);
|
||||
|
@ -109,7 +115,7 @@ public class CSVPrinter {
|
|||
// break intentionally excluded.
|
||||
case '\n':
|
||||
println();
|
||||
out.write(this.format.getCommentStart());
|
||||
out.write(format.getCommentStart());
|
||||
out.write(' ');
|
||||
break;
|
||||
default:
|
||||
|
|
|
@ -28,7 +28,7 @@ import junit.framework.TestCase;
|
|||
*/
|
||||
public class CSVPrinterTest extends TestCase {
|
||||
|
||||
String lineSeparator = "\r\n";
|
||||
String lineSeparator = CSVFormat.DEFAULT.getLineSeparator();
|
||||
|
||||
public void testPrinter1() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
|
@ -102,6 +102,29 @@ public class CSVPrinterTest extends TestCase {
|
|||
assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testDisabledComment() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
|
||||
printer.printComment("This is a comment");
|
||||
|
||||
assertEquals("", sw.toString());
|
||||
}
|
||||
|
||||
public void testSingleLineComment() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
||||
printer.printComment("This is a comment");
|
||||
|
||||
assertEquals("# This is a comment" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testMultiLineComment() throws IOException {
|
||||
StringWriter sw = new StringWriter();
|
||||
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
|
||||
printer.printComment("This is a comment\non multiple lines");
|
||||
|
||||
assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
|
||||
}
|
||||
|
||||
public void testRandom() throws Exception {
|
||||
int iter = 10000;
|
||||
|
@ -184,11 +207,11 @@ public class CSVPrinterTest extends TestCase {
|
|||
}
|
||||
|
||||
public static String printable(String s) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < s.length(); i++) {
|
||||
char ch = s.charAt(i);
|
||||
if (ch <= ' ' || ch >= 128) {
|
||||
sb.append("(" + (int) ch + ")");
|
||||
sb.append("(").append((int) ch).append(")");
|
||||
} else {
|
||||
sb.append(ch);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue