How to do unit testing of Visitors in Jsqlparser?

843 Views Asked by At

I have implemented the Visitors of JSqlparser to parse SQL queries. The code is working absolutely fine. I am not sure how to do unit testing for those visitors. For example I have the following sql statement:

Select a.a1, a.a2 from foo as a

To parse it I have implemented StatementVisitor interface and my code goes like this:

public class StmntVisitorImpl implements StatementVisitor {


@Override
public void visit(Select select) {
    LOG.debug("Begin Select visit : "+select.toString());
    SelectVisitor selectVisitor = new SelectVisitorImpl();      
    select.getSelectBody().accept(selectVisitor);
    if(select.getWithItemsList()!=null && !select.getWithItemsList().isEmpty()){
        select.getWithItemsList().stream()
        .forEach(wth -> wth.accept(selectVisitor));             
    }               
}
@Override
public void visit(Delete delete) {}
@Override
public void visit(Update update) {}
@Override
public void visit(Insert insert) {}
@Override
public void visit(Replace replace) {}
@Override
public void visit(Drop drop) {}
}

Then I am calling it in another class as follows:

String sql = "Select a.a1, a.a2 from foo as a";
CCJSqlParserManager parserManager = new CCJSqlParserManager();
StatementVisitor statementVisitor = new StmntVisitorImpl ();
Statement sqlStatement =  parserManager.parse(new StringReader(sql));
sqlStatement.accept(statementVisitor);

I want to write unit test cases for my StmntVisitorImpl, SelectVisitorImpl and other classes implementing the Visitor interfaces given by JSqlparser. How should I do it?

1

There are 1 best solutions below

0
On BEST ANSWER

In addition to my comment have a look at this link. You can figure out the solution.

The buffer variable can be passed to the overloaded constructor of ExpressionDeParser from the parent method where it is being used and then any detail that we want to log can be done using buffer.