コード例 #1
0
        //-------------------------------------------------------------------------
        public virtual void test_of_empty_no_header_reader()
        {
            CsvFile csvFile = CsvFile.of(new StringReader(""), false, ',');

            assertEquals(csvFile.headers().size(), 0);
            assertEquals(csvFile.rowCount(), 0);
        }
コード例 #2
0
        public virtual void test_of_simple_no_header_tabs()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1T), false, '\t');

            assertEquals(csvFile.headers().size(), 0);
            assertEquals(csvFile.containsHeader("Foo"), false);
            assertEquals(csvFile.containsHeader(Pattern.compile("Foo")), false);
            assertEquals(csvFile.rowCount(), 3);
            assertEquals(csvFile.row(0).lineNumber(), 1);
            assertEquals(csvFile.row(1).lineNumber(), 2);
            assertEquals(csvFile.row(2).lineNumber(), 3);

            assertEquals(csvFile.row(0).headers().size(), 0);
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "h1");
            assertEquals(csvFile.row(0).field(1), "h2");
            assertEquals(csvFile.row(1).headers().size(), 0);
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r11");
            assertEquals(csvFile.row(1).field(1), "r12");
            assertEquals(csvFile.row(2).headers().size(), 0);
            assertEquals(csvFile.row(2).fieldCount(), 2);
            assertEquals(csvFile.row(2).field(0), "r21");
            assertEquals(csvFile.row(2).field(1), "r22");
        }
コード例 #3
0
        public virtual void test_of_empty_no_header()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(""), false);

            assertEquals(csvFile.headers().size(), 0);
            assertEquals(csvFile.rowCount(), 0);
            assertEquals(csvFile.containsHeader("Foo"), false);
            assertEquals(csvFile.containsHeader(Pattern.compile("Foo")), false);
        }
コード例 #4
0
        public virtual void test_of_headerComment()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV7), true);

            assertEquals(csvFile.rowCount(), 1);
            assertEquals(csvFile.row(0).lineNumber(), 3);

            assertEquals(csvFile.headers().size(), 2);
            assertEquals(csvFile.headers().get(0), "h1");
            assertEquals(csvFile.headers().get(1), "h2");
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r1");
            assertEquals(csvFile.row(0).field(1), "r2");
        }
コード例 #5
0
        public virtual void test_of_quotingWithEquals()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV4B), false);

            assertEquals(csvFile.rowCount(), 3);
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "alpha");
            assertEquals(csvFile.row(0).field(1), "be, \"at\", one");
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "alpha\",\"be\"");
            assertEquals(csvFile.row(1).field(1), "\"at\", one");
            assertEquals(csvFile.row(2).fieldCount(), 2);
            assertEquals(csvFile.row(2).field(0), "r21");
            assertEquals(csvFile.row(2).field(1), " r22 ");
        }
コード例 #6
0
        public virtual void test_of_blank_row()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV3), false);

            assertEquals(csvFile.rowCount(), 2);
            assertEquals(csvFile.row(0).lineNumber(), 1);
            assertEquals(csvFile.row(1).lineNumber(), 3);

            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r11");
            assertEquals(csvFile.row(0).field(1), "r12");
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r21");
            assertEquals(csvFile.row(1).field(1), "r22");
        }
コード例 #7
0
        public virtual void test_of_comment_blank_no_header()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV2), false);

            assertEquals(csvFile.headers().size(), 0);
            assertEquals(csvFile.rowCount(), 2);
            assertEquals(csvFile.row(0).lineNumber(), 1);
            assertEquals(csvFile.row(1).lineNumber(), 5);

            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "h1");
            assertEquals(csvFile.row(0).field(1), "h2");
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r21");
            assertEquals(csvFile.row(1).field(1), "r22");
        }
コード例 #8
0
        public virtual void test_of_comment_blank_with_header()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV2), true);
            ImmutableList <string> headers = csvFile.headers();

            assertEquals(headers.size(), 2);
            assertEquals(headers.get(0), "h1");
            assertEquals(headers.get(1), "h2");
            assertEquals(csvFile.rows().size(), 1);
            assertEquals(csvFile.rowCount(), 1);
            assertEquals(csvFile.row(0).lineNumber(), 5);

            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r21");
            assertEquals(csvFile.row(0).field(1), "r22");
            assertEquals(csvFile.rows().get(0), csvFile.row(0));
        }
コード例 #9
0
        public virtual void test_of_simple_with_header()
        {
            CsvFile csvFile = CsvFile.of(CharSource.wrap(CSV1), true);

            assertEquals(csvFile.containsHeader("Foo"), false);
            assertEquals(csvFile.containsHeader("h1"), true);
            assertEquals(csvFile.containsHeader(Pattern.compile("Foo")), false);
            assertEquals(csvFile.containsHeader(Pattern.compile("h[0-9]")), true);
            ImmutableList <string> headers = csvFile.headers();

            assertEquals(headers.size(), 2);
            assertEquals(headers.get(0), "h1");
            assertEquals(headers.get(1), "h2");
            assertEquals(csvFile.rowCount(), 3);
            assertEquals(csvFile.row(0).lineNumber(), 2);
            assertEquals(csvFile.row(1).lineNumber(), 3);
            assertEquals(csvFile.row(2).lineNumber(), 4);

            assertEquals(csvFile.row(0).headers(), headers);
            assertEquals(csvFile.row(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).field(0), "r11");
            assertEquals(csvFile.row(0).field(1), "r12");
            assertEquals(csvFile.row(1).headers(), headers);
            assertEquals(csvFile.row(1).fieldCount(), 2);
            assertEquals(csvFile.row(1).field(0), "r21");
            assertEquals(csvFile.row(1).field(1), "r22");

            assertEquals(csvFile.row(0).getField("h1"), "r11");
            assertEquals(csvFile.row(0).getField("h2"), "r12");
            assertEquals(csvFile.row(1).getField("h1"), "r21");
            assertEquals(csvFile.row(1).getField("h2"), "r22");
            assertThrowsIllegalArg(() => csvFile.row(0).getField("zzz"));

            assertEquals(csvFile.row(0).getValue("h1"), "r11");
            assertEquals(csvFile.row(0).getValue("h2"), "r12");
            assertEquals(csvFile.row(1).getValue("h1"), "r21");
            assertEquals(csvFile.row(1).getValue("h2"), "r22");
            assertThrowsIllegalArg(() => csvFile.row(0).getValue("zzz"));
            assertThrowsIllegalArg(() => csvFile.row(2).getValue("h2"));

            assertEquals(csvFile.row(0).findField("h1"), ("r11"));
            assertEquals(csvFile.row(0).findField("h2"), ("r12"));
            assertEquals(csvFile.row(1).findField("h1"), ("r21"));
            assertEquals(csvFile.row(1).findField("h2"), ("r22"));
            assertEquals(csvFile.row(0).findField("zzz"), null);

            assertEquals(csvFile.row(0).findValue("h1"), ("r11"));
            assertEquals(csvFile.row(0).findValue("h2"), ("r12"));
            assertEquals(csvFile.row(1).findValue("h1"), ("r21"));
            assertEquals(csvFile.row(1).findValue("h2"), ("r22"));
            assertEquals(csvFile.row(0).findValue("zzz"), null);
            assertEquals(csvFile.row(2).findValue("h2"), null);

            assertEquals(csvFile.row(0).getField(Pattern.compile("h[13]")), "r11");
            assertEquals(csvFile.row(0).getField(Pattern.compile("h[24]")), "r12");
            assertThrowsIllegalArg(() => csvFile.row(0).getField(Pattern.compile("zzz")));

            assertEquals(csvFile.row(0).getValue(Pattern.compile("h[13]")), "r11");
            assertEquals(csvFile.row(0).getValue(Pattern.compile("h[24]")), "r12");
            assertThrowsIllegalArg(() => csvFile.row(0).getValue(Pattern.compile("zzz")));
            assertThrowsIllegalArg(() => csvFile.row(2).getValue(Pattern.compile("h2")));

            assertEquals(csvFile.row(0).findField(Pattern.compile("h[13]")), ("r11"));
            assertEquals(csvFile.row(0).findField(Pattern.compile("h[24]")), ("r12"));
            assertEquals(csvFile.row(0).findField(Pattern.compile("zzz")), null);

            assertEquals(csvFile.row(0).findValue(Pattern.compile("h[13]")), ("r11"));
            assertEquals(csvFile.row(0).findValue(Pattern.compile("h[24]")), ("r12"));
            assertEquals(csvFile.row(0).findValue(Pattern.compile("zzz")), null);
            assertEquals(csvFile.row(2).findValue(Pattern.compile("h2")), null);

            assertEquals(csvFile.row(0).subRow(0).fieldCount(), 2);
            assertEquals(csvFile.row(0).subRow(1).fieldCount(), 1);
            assertEquals(csvFile.row(0).subRow(2).fieldCount(), 0);

            assertEquals(csvFile.row(0).subRow(0, 0).fieldCount(), 0);
            assertEquals(csvFile.row(0).subRow(0, 1).fieldCount(), 1);
            assertEquals(csvFile.row(0).subRow(2, 2).fieldCount(), 0);
        }