服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

检测代码是否有junit test class的小功能

    去年为了公司的junit需求做了一个检测我们的代码是否有junit test class的一个小tool, 今天又翻出来看了下,觉得里面的有个小功能可能对大家有用,如果你也觉得有用,我觉得很荣幸。:)

1.用途:
指定一个类, 分析出类中所有方法的精确位置- 也就是某个方法在哪两行之间。
(大家可以考虑一下怎么做,然后再看看我的方法是不是太笨了。-_-!!)

2.用法:
新建一个类,将这几行代码加到mail方法,例如:


检测代码是否有junit test class的小功能(图一) 检测代码是否有junit test class的小功能(图二) 示例使用代码

code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/

--> 1 检测代码是否有junit test class的小功能(图二) 检测代码是否有junit test class的小功能(图一)     public   static   void  main(string[] args)  检测代码是否有junit test class的小功能(图三) {
 2 检测代码是否有junit test class的小功能(图四)         //  cvsparsemethod
 3 检测代码是否有junit test class的小功能(图四)         //  c:/documents and
 4 检测代码是否有junit test class的小功能(图四)         //  settings/hut/workspace/junitcheck/src/branding/src/java/com/webex/webapp/branding/common/brandingtools.java
 5 检测代码是否有junit test class的小功能(图四)         cvsparsemethod x  =   new  cvsparsemethod( " c://documents and settings//hut//workspace//accountmethodlocation// " ,
 6 检测代码是否有junit test class的小功能(图四)                 " net.blogjava.xmp123.cvsclass " );
 7 检测代码是否有junit test class的小功能(图四)
 8 检测代码是否有junit test class的小功能(图四)         // src/java/com/webex/webapp/urlapi/biz/urlapimgrimpl.java
 9 检测代码是否有junit test class的小功能(图四)         // c:/documents and settings/hut/workspace/junitcheck/src/meetingcenter/src/java/com/webex/webapp/meetingcenter/action/common/quickstartinviteaction.java
10 检测代码是否有junit test class的小功能(图四)         system.out.println(x.parseaction());
11 检测代码是否有junit test class的小功能(图五)    }

[补充]: cvsparsemethod的构造函数是cvsparsemethod(string string1,string string2), string1是目标的java文件在文件系统的位置,string2是类的全名..如net.blogjava.xmp123.cvsclass.
btw:不要忘记将后面download的jar导过来。

结果会打在console里,象这样:

检测代码是否有junit test class的小功能(图六) loading net.blogjava.xmp123.cvsclass
检测代码是否有junit test class的小功能(图六)loaded net.blogjava.xmp123.cvsclass
检测代码是否有junit test class的小功能(图六)found java source file in c:/documents and settings/hut/workspace/accountmethodlocation / net / blogjava / xmp123 / cvsclass.java
检测代码是否有junit test class的小功能(图六)parsing net.blogjava.xmp123.cvsclass检测代码是否有junit test class的小功能(图三)检测代码是否有junit test class的小功能(图三)检测代码是否有junit test class的小功能(图三)
检测代码是否有junit test class的小功能(图六)parsed a common  class  and brackets can matched the methods correctly
检测代码是否有junit test class的小功能(图六)methodparseengine have searched  10  methods(including constructors).
检测代码是否有junit test class的小功能(图六)completed :spend  0.187  seconds to parse  this  file.
检测代码是否有junit test class的小功能(图六)[ public  cvsclass(string) locate between  52  and  123
检测代码是否有junit test class的小功能(图六),  public  cvsmethod[] getcvsmethodarray() locate between  128  and  130
检测代码是否有junit test class的小功能(图六),  public  cvsconstructor[] getcvsconstructorarray() locate between  135  and  137
检测代码是否有junit test class的小功能(图六),  public  set getmethodset() locate between  142  and  144
检测代码是否有junit test class的小功能(图六),  public  set getconstructorset() locate between  149  and  151
检测代码是否有junit test class的小功能(图六),  public  string getclasspath() locate between  156  and  158
检测代码是否有junit test class的小功能(图六),  public  list getmethodlist() locate between  163  and  165
检测代码是否有junit test class的小功能(图六),  public  string getclassname() locate between  174  and  176
检测代码是否有junit test class的小功能(图六),  public   boolean  equals(object) locate between  183  and  188
检测代码是否有junit test class的小功能(图六),  public   int  hashcode() locate between  193  and  196
检测代码是否有junit test class的小功能(图六)]
检测代码是否有junit test class的小功能(图六)

3. 我是怎么做的。

.用java反射拿出所有的的类的构造函数和方法。
.读java源文件,通过匹配左右括号来匹配方法并记录出合适的位置。

4. 存在的问题:
到目前为止,这个小程序至少不能分析它自己的主类:cvsparsemethod(很多exceptions), 因为这个类里面定义的一些string中含用 { }而导致匹配的时候出错。呵呵。但是我保证,它应该在>90%的概率下正常工作。:) 

5. 下载的地址:
http://www.blogjava.net/files/xmp123/countmethodlocation.zip

扫描关注微信公众号