You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by eb...@apache.org on 2012/03/05 15:42:14 UTC

svn commit: r1297075 - in /commons/sandbox/csv/trunk/src: main/java/org/apache/commons/csv/CSVPrinter.java test/java/org/apache/commons/csv/CSVPrinterTest.java

Author: ebourg
Date: Mon Mar  5 14:42:14 2012
New Revision: 1297075

URL: http://svn.apache.org/viewvc?rev=1297075&view=rev
Log:
Test cases for CSVPrinter.printComment()

Modified:
    commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
    commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Modified: commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java?rev=1297075&r1=1297074&r2=1297075&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java (original)
+++ commons/sandbox/csv/trunk/src/main/java/org/apache/commons/csv/CSVPrinter.java Mon Mar  5 14:42:14 2012
@@ -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:

Modified: commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1297075&r1=1297074&r2=1297075&view=diff
==============================================================================
--- commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/sandbox/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Mon Mar  5 14:42:14 2012
@@ -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 Test
         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 Test
     }
 
     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);
             }