Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Switch to using AntlrStringStream

...

Code Block
titleCSVTest.java
public class CSVTests {
    @Test
    public void testNewline() throws IOException, RecognitionException {
        CSVParser parser = createParser("\n");
        parser.line();
    }

    @Test
    public void testCRLF() throws IOException, RecognitionException {
        CSVParser parser = createParser("\r\n");
        parser.line();
    }
    
    private CSVParser createParser(String testString) throws IOException {
        byte[] byteArray = testString.getBytes("ISO-8859-1");
        InputStream 	CharStream stream = new ByteArrayInputStreamANTLRStringStream(byteArraytestString);
        ANTLRInputStream input = new ANTLRInputStream(stream);
        	CSVLexer lexer = new CSVLexer(inputstream);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CSVParser parser = new CSVParser(tokens);
        return parser;
    }

}

...

Code Block
titleCSVTests.java fragment
@Test
public void testSpaceRemoval() throws IOException, RecognitionException {
    CSVLexer lexer = createLexer("Red  ,   Green, ,Blue\n");
    CSVParser parser = createParser(lexer);
    List<String> result = parser.line();
    assert result.size() == 4 : "Expected 4 items";
    assert result.get(0).equals("Red") : "Expected Red";
    assert result.get(1).equals("Green") : "Expected Green";
    assert result.get(2).equals("") : "Expected empty";
    assert result.get(3).equals("Blue") : "Expected Blue";
    // Now make sure we didn't have any lexing errors, which were failing silently earlier
    // The parser drives the lexer, so check for exceptions after
    // parsing.
    List<RecognitionException> lexerExceptions = lexer.getExceptions();
    assert lexerExceptions.isEmpty() : "Lexer threw exceptions -- see output";
}

// and later...

private CSVParser createParser(CSVLexer lexer) throws IOException {
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CSVParser parser = new CSVParser(tokens);
    return parser;
}

private CSVParser createParser(String testString) throws IOException {
    CSVLexer lexer = createLexer(testString);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    CSVParser parser = new CSVParser(tokens);
    return parser;
}

private CSVLexer createLexer(String testString) throws IOException {
    byte[] byteArray = testString.getBytes("ISO-8859-1");
    InputStream 	CharStream stream = new ByteArrayInputStreamANTLRStringStream(byteArraytestString);
    ANTLRInputStream input = new ANTLRInputStream(stream);
    	CSVLexer lexer = new CSVLexer(inputstream);
    return lexer;
}

(minus) Of course, this doesn't compile: CSVLexer doesn't implement getExceptions.

...