jdt.core Java模型的JUnit测试 [英] JUnit test for jdt.core Java Models

查看:91
本文介绍了jdt.core Java模型的JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的代码进行一些JUnit测试。但问题是,我使用像ICompilationUnit,IPackageFragment,ITypes等Java模型。我没有得到如何创建一些ICompilationUnit然后测试。我搜索了google和stackoverflow以获取信息,但没有找到任何东西。



我的问题是,如何使Junit测试与jdt.core的类...可以有人给我可能是一些代码示例。



谢谢



这里是一个编码的方法:

  private void updateLists(){

if(!getCompilationUnit()。isEmpty()){
(int i = 0; i< getCompilationUnit()。size(); i ++){

try {

Document doc = new Document(getCompilationUnit ()获得(ⅰ).getSource());

int totalNumberOfCode = doc.getNumberOfLines();

IType type = getCompilationUnit()。get(i).findPrimaryType();

IType [] types = getCompilationUnit()。get(i).getTypes();

updateListPrimaryType(type,totalNumberOfCode,types);

updateListIMethod(type);
updateListMember(type,types);


} catch(JavaModelException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
}


解决方案

这段代码显示了如何使用Java源代码(在变量javaSource中),并通过使用Java AST解析器从中获取ICompilationUnit。您可以使用插件org.eclipse.jdt.core作为依赖项来获取这些类。

  import org.eclipse.jdt.core .dom.ASTParser; 

String javaSource =some java source'

//查看org.eclipse.jdt.core.dom.AST的完整
//列表支持水平。
ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setSource(javaSource.toCharArray());
CompilationUnit unit =(CompilationUnit)parser.createAST(null);


I am trying to make some JUnit tests for my code. But the problem is, I make use of the Java Models like ICompilationUnit, IPackageFragment, ITypes and etc. I did not get how to create some ICompilationUnit and then test. I searched google and stackoverflow for information but did not find something.

My question is, how can I make Junit test with classes of the jdt.core...can somebody give me may be some code examples.

Thanks

Here is a Method I coded:

private void updateLists() {

    if(!getCompilationUnit().isEmpty()){

        for(int i = 0;  i < getCompilationUnit().size(); i++){

        try {

                Document doc = new Document(getCompilationUnit().get(i).getSource());

                int totalNumberOfCode = doc.getNumberOfLines();

                IType type = getCompilationUnit().get(i).findPrimaryType();                 

                                    IType[] types = getCompilationUnit().get(i).getTypes();

                updateListPrimaryType(type, totalNumberOfCode, types);

                updateListIMethod(type);
                updateListMember(type,types);


            } catch (JavaModelException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

解决方案

This snippet of code shows how to take some Java source (in variable javaSource) and get an ICompilationUnit from it by using the Java AST parser. You get these classes by using plugin org.eclipse.jdt.core as a dependency.

import org.eclipse.jdt.core.dom.ASTParser;

String javaSource = "some java source"'

    // see org.eclipse.jdt.core.dom.AST for a complete
    // list of supported levels.
ASTParser parser = ASTParser.newParser(AST.JLS2);
parser.setSource(javaSource.toCharArray());
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

这篇关于jdt.core Java模型的JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆