@Test
public void replaceDatasetClause() throws Exception {

    InputStream query = getClass().getResourceAsStream("/example2.rq");
    Sparql11Parser parser = Query.getParser(streamToString(query));
    parser.addErrorListener(new BaseErrorListener() {
        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
            throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
        }
    });
    Sparql11Parser.QueryContext queryContext = parser.query();
    // Rewrite the dataset clause
    TokenStreamRewriter rewriter = new TokenStreamRewriter(parser.getTokenStream());
    ParseTreeWalker walker = new ParseTreeWalker();
    DatasetListener listener = new DatasetListener(rewriter);
    walker.walk(listener, queryContext);
    // Test result
    String newText = rewriter.getText();
    parser = Query.getParser(newText);
    String datasetText = parser.query().selectQuery().datasetClause().get(0).getText();
    assertEquals(DATASET_REPLACEMENT, datasetText);
}