How to do unit testing of Visitors in Jsqlparser?

徘徊边缘 提交于 2019-12-12 00:49:05

问题


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:


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.



来源:https://stackoverflow.com/questions/41887920/how-to-do-unit-testing-of-visitors-in-jsqlparser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!