You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/10/14 23:02:55 UTC

svn commit: r1398135 - /commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Author: ggregory
Date: Sun Oct 14 21:02:55 2012
New Revision: 1398135

URL: http://svn.apache.org/viewvc?rev=1398135&view=rev
Log:
Sort members.

Modified:
    commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Modified: commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java?rev=1398135&r1=1398134&r2=1398135&view=diff
==============================================================================
--- commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java (original)
+++ commons/proper/csv/trunk/src/test/java/org/apache/commons/csv/CSVPrinterTest.java Sun Oct 14 21:02:55 2012
@@ -34,62 +34,164 @@ import org.junit.Test;
 
 public class CSVPrinterTest {
 
+    public static String printable(final String s) {
+        final StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < s.length(); i++) {
+            final char ch = s.charAt(i);
+            if (ch <= ' ' || ch >= 128) {
+                sb.append("(").append((int) ch).append(")");
+            } else {
+                sb.append(ch);
+            }
+        }
+        return sb.toString();
+    }
+
     String lineSeparator = CSVFormat.DEFAULT.getLineSeparator();
 
+    public void doOneRandom(final CSVFormat format) throws Exception {
+        final Random r = new Random();
+
+        final int nLines = r.nextInt(4) + 1;
+        final int nCol = r.nextInt(3) + 1;
+        // nLines=1;nCol=2;
+        final String[][] lines = new String[nLines][];
+        for (int i = 0; i < nLines; i++) {
+            final String[] line = new String[nCol];
+            lines[i] = line;
+            for (int j = 0; j < nCol; j++) {
+                line[j] = randStr();
+            }
+        }
+
+        final StringWriter sw = new StringWriter();
+        final CSVPrinter printer = new CSVPrinter(sw, format);
+
+        for (int i = 0; i < nLines; i++) {
+            // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
+            printer.printRecord(lines[i]);
+        }
+
+        printer.flush();
+        final String result = sw.toString();
+        // System.out.println("### :" + printable(result));
+
+        final CSVParser parser = new CSVParser(result, format);
+        final List<CSVRecord> parseResult = parser.getRecords();
+
+        Utils.compare("Printer output :" + printable(result), lines, parseResult);
+    }
+
+    public void doRandom(final CSVFormat format, final int iter) throws Exception {
+        for (int i = 0; i < iter; i++) {
+            doOneRandom(format);
+        }
+    }
+
+    public String randStr() {
+        final Random r = new Random();
+
+        final int sz = r.nextInt(20);
+        // sz = r.nextInt(3);
+        final char[] buf = new char[sz];
+        for (int i = 0; i < sz; i++) {
+            // stick in special chars with greater frequency
+            char ch;
+            final int what = r.nextInt(20);
+            switch (what) {
+                case 0:
+                    ch = '\r';
+                    break;
+                case 1:
+                    ch = '\n';
+                    break;
+                case 2:
+                    ch = '\t';
+                    break;
+                case 3:
+                    ch = '\f';
+                    break;
+                case 4:
+                    ch = ' ';
+                    break;
+                case 5:
+                    ch = ',';
+                    break;
+                case 6:
+                    ch = '"';
+                    break;
+                case 7:
+                    ch = '\'';
+                    break;
+                case 8:
+                    ch = '\\';
+                    break;
+                default:
+                    ch = (char) r.nextInt(300);
+                    break;
+                // default: ch = 'a'; break;
+            }
+            buf[i] = ch;
+        }
+        return new String(buf);
+    }
+
     @Test
-    public void testPrinter1() throws IOException {
+    public void testDisabledComment() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a", "b");
-        assertEquals("a,b" + lineSeparator, sw.toString());
+        printer.printComment("This is a comment");
+
+        assertEquals("", sw.toString());
     }
 
     @Test
-    public void testPrinter2() throws IOException {
+    public void testExcelPrintAllArrayOfArrays() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a,b", "b");
-        assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testPrinter3() throws IOException {
+    public void testExcelPrintAllArrayOfLists() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a, b", "b ");
-        assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }), Arrays.asList(new String[] { "r2c1", "r2c2" }) });
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testPrinter4() throws IOException {
+    public void testExcelPrintAllIterableOfArrays() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a", "b\"c");
-        assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testPrinter5() throws IOException {
+    public void testExcelPrintAllIterableOfLists() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a", "b\nc");
-        assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecords(Arrays.asList(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }),
+                Arrays.asList(new String[] { "r2c1", "r2c2" }) }));
+        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testQuoteAll() throws IOException {
+    public void testExcelPrinter1() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL));
-        printer.printRecord("a", "b\nc", "d");
-        assertEquals("\"a\",\"b\nc\",\"d\"" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecord("a", "b");
+        assertEquals("a,b" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testPrinter6() throws IOException {
+    public void testExcelPrinter2() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a", "b\r\nc");
-        assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
+        printer.printRecord("a,b", "b");
+        assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
     }
 
     @Test
@@ -111,95 +213,84 @@ public class CSVPrinterTest {
     }
 
     @Test
-    public void testPrinter7() throws IOException {
+    public void testMultiLineComment() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a", "b\\c");
-        assertEquals("a,b\\c" + lineSeparator, sw.toString());
-    }
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
+        printer.printComment("This is a comment\non multiple lines");
 
-    @Test
-    public void testExcelPrintAllArrayOfArrays() throws IOException {
-        final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.printRecords(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } });
-        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+        assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testExcelPrintAllArrayOfLists() throws IOException {
+    public void testPrinter1() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.printRecords(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }), Arrays.asList(new String[] { "r2c1", "r2c2" }) });
-        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+        printer.printRecord("a", "b");
+        assertEquals("a,b" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testExcelPrintAllIterableOfLists() throws IOException {
+    public void testPrinter2() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.printRecords(Arrays.asList(new List[] { Arrays.asList(new String[] { "r1c1", "r1c2" }),
-                Arrays.asList(new String[] { "r2c1", "r2c2" }) }));
-        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+        printer.printRecord("a,b", "b");
+        assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testExcelPrintAllIterableOfArrays() throws IOException {
+    public void testPrinter3() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.printRecords(Arrays.asList(new String[][] { { "r1c1", "r1c2" }, { "r2c1", "r2c2" } }));
-        assertEquals("r1c1,r1c2" + lineSeparator + "r2c1,r2c2" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+        printer.printRecord("a, b", "b ");
+        assertEquals("\"a, b\",\"b \"" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testExcelPrinter1() throws IOException {
+    public void testPrinter4() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.printRecord("a", "b");
-        assertEquals("a,b" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+        printer.printRecord("a", "b\"c");
+        assertEquals("a,\"b\"\"c\"" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testExcelPrinter2() throws IOException {
+    public void testPrinter5() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.EXCEL);
-        printer.printRecord("a,b", "b");
-        assertEquals("\"a,b\",b" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+        printer.printRecord("a", "b\nc");
+        assertEquals("a,\"b\nc\"" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testPrintNullValues() throws IOException {
+    public void testPrinter6() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printRecord("a", null, "b");
-        assertEquals("a,,b" + lineSeparator, sw.toString());
+        printer.printRecord("a", "b\r\nc");
+        assertEquals("a,\"b\r\nc\"" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testDisabledComment() throws IOException {
+    public void testPrinter7() throws IOException {
         final StringWriter sw = new StringWriter();
         final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
-        printer.printComment("This is a comment");
-
-        assertEquals("", sw.toString());
+        printer.printRecord("a", "b\\c");
+        assertEquals("a,b\\c" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testSingleLineComment() throws IOException {
+    public void testPrintNullValues() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
-        printer.printComment("This is a comment");
-
-        assertEquals("# This is a comment" + lineSeparator, sw.toString());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
+        printer.printRecord("a", null, "b");
+        assertEquals("a,,b" + lineSeparator, sw.toString());
     }
 
     @Test
-    public void testMultiLineComment() throws IOException {
+    public void testQuoteAll() throws IOException {
         final StringWriter sw = new StringWriter();
-        final 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());
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuotePolicy(Quote.ALL));
+        printer.printRecord("a", "b\nc", "d");
+        assertEquals("\"a\",\"b\nc\",\"d\"" + lineSeparator, sw.toString());
     }
 
     @Test
@@ -210,104 +301,13 @@ public class CSVPrinterTest {
         doRandom(CSVFormat.MYSQL, iter);
     }
 
-    public void doRandom(final CSVFormat format, final int iter) throws Exception {
-        for (int i = 0; i < iter; i++) {
-            doOneRandom(format);
-        }
-    }
-
-    public void doOneRandom(final CSVFormat format) throws Exception {
-        final Random r = new Random();
-
-        final int nLines = r.nextInt(4) + 1;
-        final int nCol = r.nextInt(3) + 1;
-        // nLines=1;nCol=2;
-        final String[][] lines = new String[nLines][];
-        for (int i = 0; i < nLines; i++) {
-            final String[] line = new String[nCol];
-            lines[i] = line;
-            for (int j = 0; j < nCol; j++) {
-                line[j] = randStr();
-            }
-        }
-
+    @Test
+    public void testSingleLineComment() throws IOException {
         final StringWriter sw = new StringWriter();
-        final CSVPrinter printer = new CSVPrinter(sw, format);
-
-        for (int i = 0; i < nLines; i++) {
-            // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j]));
-            printer.printRecord(lines[i]);
-        }
-
-        printer.flush();
-        final String result = sw.toString();
-        // System.out.println("### :" + printable(result));
-
-        final CSVParser parser = new CSVParser(result, format);
-        final List<CSVRecord> parseResult = parser.getRecords();
-
-        Utils.compare("Printer output :" + printable(result), lines, parseResult);
-    }
-
-    public static String printable(final String s) {
-        final StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < s.length(); i++) {
-            final char ch = s.charAt(i);
-            if (ch <= ' ' || ch >= 128) {
-                sb.append("(").append((int) ch).append(")");
-            } else {
-                sb.append(ch);
-            }
-        }
-        return sb.toString();
-    }
-
-    public String randStr() {
-        final Random r = new Random();
+        final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
+        printer.printComment("This is a comment");
 
-        final int sz = r.nextInt(20);
-        // sz = r.nextInt(3);
-        final char[] buf = new char[sz];
-        for (int i = 0; i < sz; i++) {
-            // stick in special chars with greater frequency
-            char ch;
-            final int what = r.nextInt(20);
-            switch (what) {
-                case 0:
-                    ch = '\r';
-                    break;
-                case 1:
-                    ch = '\n';
-                    break;
-                case 2:
-                    ch = '\t';
-                    break;
-                case 3:
-                    ch = '\f';
-                    break;
-                case 4:
-                    ch = ' ';
-                    break;
-                case 5:
-                    ch = ',';
-                    break;
-                case 6:
-                    ch = '"';
-                    break;
-                case 7:
-                    ch = '\'';
-                    break;
-                case 8:
-                    ch = '\\';
-                    break;
-                default:
-                    ch = (char) r.nextInt(300);
-                    break;
-                // default: ch = 'a'; break;
-            }
-            buf[i] = ch;
-        }
-        return new String(buf);
+        assertEquals("# This is a comment" + lineSeparator, sw.toString());
     }
 
 }