如何在Jsqlparser中对访问者进行单元测试? [英] How to do unit testing of Visitors in Jsqlparser?
问题描述
我已经实现了JSqlparser的Visitors来解析SQL查询.该代码工作正常.我不确定如何为那些访客进行单元测试.例如,我有以下sql语句:
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
要解析它,我实现了StatementVisitor接口,我的代码如下所示:
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);
我想为我的StmntVisitorImpl,SelectVisitorImpl和其他实现JSqlparser给定的Visitor接口的类编写单元测试用例.我该怎么办?
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?
推荐答案
In addition to my comment have a look at this link. You can figure out the solution.
可以将buffer
变量从使用它的父方法传递给ExpressionDeParser
的重载构造函数,然后可以使用buffer
完成我们要记录的任何详细信息.
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
.
这篇关于如何在Jsqlparser中对访问者进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!