网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  java应用利器组合:ant+junit+cobertura     
  文章作者:未知  文章来源:水木森林  
  查看:99次  录入:管理员--2007-11-17  
 

    看标题就知道,这个是开发一个java应用的利器组合,使用ant完成工程的构建(build),使用junit完成单元测试,使用cobertura完成代码覆盖测试,也可以辅助查找性能瓶颈一些类型的bug,下面是一个完整的build.xml范例,可以完全拿来用,不需任何修改,只要你的目录和这里的目录一致(应该也是很通用的):

下载下面的build.xml文件

文件内容:
<project default="all">
    <!?c =================================================================== ?c>
    <!?c                               definitions                           ?c>
    <!?c =================================================================== ?c>   
    <property name="base-dir" location="." />
    <property name="lib-dir" location="${base-dir}/lib" />
    <property name="src-dir" location="${base-dir}/src" />
    <property name="testcase-dir" location="${base-dir}/test" />
    <property name="out-dir" location="${base-dir}/classes" />
    <property name="report-dir" location="${base-dir}/report" />
    <property name="junit-dir" location="${report-dir}/junit" />
    <property name="junit-xml-dir" location="${junit-dir}/xml" />
    <property name="junit-html-dir" location="${junit-dir}/html" />
    <property name="cobertura-dir" location="${report-dir}/cobertura" />
    <property name="cobertura-xml-dir" location="${cobertura-dir}/xml" />
    <property name="cobertura-html-dir" location="${cobertura-dir}/html" />
    <property name="instrumented-dir" location="${report-dir}/instrumented" />
    <property name="instrument-file" location="${instrumented-dir}/cobertura.ser" />
    <property name="cobertura.dir" value="${instrumented-dir}" />
   
   
    <path id="classpath.all">
        <pathelement location="${out-dir}" />
        <fileset id="alljars" dir="${lib-dir}">
            <include name="**/*.jar" />
        </fileset>
    </path>

    <!?c include the cobertura building jars ?c> 
    <path id="cobertura.classpath">
        <path refid="classpath.all" />
    </path>   
   
    <!?c define the cobertura property file ?c> 
    <taskdef classpathref="cobertura.classpath" resource="tasks.properties"/>
   
    <!?c =================================================================== ?c>
    <!?c                            project targets                          ?c>
    <!?c =================================================================== ?c>
   
    <target name="init" description="initialize the project env.">
        <!?c create the output folder ?c> 
        <mkdir dir="${out-dir}" />
        <mkdir dir="${report-dir}" />
        <copy todir="${out-dir}">
            <fileset dir="${src-dir}" includes="**/*.properties" />
        </copy>
    </target>

     <target name="compile" depends="init">
        <javac srcdir="${src-dir}" destdir="${out-dir}">
            <classpath refid="classpath.all" />
        </javac>
    </target>
  
    <!?c =================================================================== ?c>
    <!?c                            unit test targets                        ?c>
    <!?c =================================================================== ?c>
   
    <!?c - - - - - - - - - - - - - - - - -
      target: init
      initialize the build env            
    - - - - - - - - - - - - - - - - - ?c>   
    <target name="init-test" description="initialize the test env.">
        <!?c create the output folder ?c> 
        <mkdir dir="${junit-dir}" />
        <mkdir dir="${junit-xml-dir}" />
        <mkdir dir="${junit-html-dir}" />
    </target>

    <!?c - - - - - - - - - - - - - - - - -
      target: compile-test
      compile the test cases                
    - - - - - - - - - - - - - - - - - ?c>    
    <target name="compile-test" depends="compile">
        <javac srcdir="${testcase-dir}" destdir="${out-dir}">      
            <classpath refid="classpath.all" />
        </javac>
    </target>

    <!?c =================================
      target: test
      run the unit test
     ================================= ?c>   
    <target name="test" depends="init-test">
        <junit fork="yes" printsummary="on" maxmemory="100m">
            <sysproperty key="net.sourceforge.cobertura.datafile"
                file="${instrument-file}" />

            <classpath location="${instrumented-dir}" />
            <classpath refid="cobertura.classpath" />
                  
            <formatter type="xml" />
            <batchtest todir="${junit-xml-dir}">
                <fileset dir="${out-dir}">
                    <include name="**/test*.class" />
                    <exclude name="**/*$*.class" />
                </fileset>
            </batchtest>
        </junit>
        <junitreport todir="${junit-xml-dir}">
            <fileset dir="${junit-xml-dir}">
                <include name="test-*.xml" />
            </fileset>
            <report format="frames" todir="${junit-html-dir}" />
        </junitreport>
    </target>
   
    <!?c =================================================================== ?c>
    <!?c                      code coverage targets                          ?c>
    <!?c =================================================================== ?c>
   

    <!?c - - - - - - - - - - - - - - - - -
      target: init
      initialize the build env. for code coverage                     
    - - - - - - - - - - - - - - - - - ?c>   
    <target name="init-coverage" description="initialize the build env.">
        <!?c create the output folder ?c> 
        <mkdir dir="${instrumented-dir}" />
        <mkdir dir="${cobertura-dir}" />
        <mkdir dir="${cobertura-xml-dir}" />
        <mkdir dir="${cobertura-html-dir}" />
    </target>
   
    <target name="compile-coverage" depends="init">
        <javac srcdir="${src-dir}:${testcase-dir}" destdir="${out-dir}" debug="true" > 
            <classpath refid="cobertura.classpath" />
        </javac>
    </target>

    <!?c =================================
      target: instrument
      instrument into the class files, but
      exclude test classes
     ================================= ?c>    
    <target name="instrument" depends="init-coverage,compile-coverage" description="instrument into the class files">
        <!?c
          instrument the application classes, writing the
          instrumented classes into ${instrumented.dir}.
        ?c>
        <cobertura-instrument todir="${instrumented-dir}" datafile="${instrument-file}">
            <!?c
                the following line causes instrument to ignore any
                source line containing a reference to log4j, for the
                purposes of coverage reporting.
            ?c>
            <ignore regex="org.apache.log4j.*" />
            <fileset dir="${out-dir}">
                <!?c
                  instrument all the application classes, but
                  don’t instrument the test classes.
                ?c>
                <include name="**/*.class" />
                <exclude name="**/test*.class" />
            </fileset>
        </cobertura-instrument>
    </target>       
   
    <!?c =================================
      target: coverage-check
      check the code coverage by given rates.
     ================================= ?c> 
    <target name="coverage-check" description="check the code coverage by given rates">
      <cobertura-check branchrate="34" totallinerate="100" />
    </target>
   
    <!?c =================================
      target: coverage-report-xml
      generate code coverage report by xml format
     ================================= ?c>     
    <target name="coverage-report-xml" description="generate xml report">
      <!?c generate an xml file containing the coverage data using the "srcdir" attribute. ?c>
      <cobertura-report srcdir="${src-dir}" destdir="${cobertura-xml-dir}" format="xml" datafile="${instrument-file}"/>
    </target>
   
    <!?c =================================
      target: coverage-report-html
      generate code coverage report by html format
     ================================= ?c>   
    <target name="coverage-report-html">
    <!?c
        generate a series of html files containing the coverage
        data in a user-readable form using nested source filesets.
    ?c>
      <cobertura-report destdir="${cobertura-html-dir}" datafile="${instrument-file}">
        <fileset dir="${src-dir}">
          <include name="**/*.java"/>
        </fileset>
      </cobertura-report>
    </target>
   
    <!?c run the code coverage individual ?c>
    <target name="coverage" depends="compile-coverage,instrument,test,coverage-report-html"
        description="compile, instrument ourself, run the tests and generate junit and code coverage reports."/>
   
    <!?c =================================================================== ?c>
    <!?c                           public targets                            ?c>
    <!?c =================================================================== ?c>
   
    <target name="clean">
        <delete quiet="true" includeemptydirs="true">
            <fileset dir="${report-dir}">
                <exclude name=".cvsignore" />
                <exclude name="cvs" />
            </fileset>
            <fileset dir="${out-dir}">
            </fileset>
        </delete>
    </target>
   
   
    <!?c =================================================================== ?c>
    <!?c                           global targets                            ?c>
    <!?c =================================================================== ?c>
   
    <target name="all" depends="compile" />
   
    <target name="junit" depends="clean, compile-test, test" />

    <target name="full" depends="clean, coverage" />

</project>


作者: cherami
原载: ant+junit+cobertura
版权所有。转载时必须以链接形式注明作者和原始出处及本声明。

 
 
上一篇: java规则引擎-drools    下一篇: ojb中的多表查询和更新
  相关文档
nhibernate性能之二级缓存篇 11-17
jsp与javabean组合循序渐进教程(2) 11-17
使用midp2.0开发游戏(1)gamecanvas基础 11-17
有效和正确定义hashcode()和equals() 11-16
使用java实现数据报通讯过程 11-16
java高级开发:使用axis开发web service 04-29
在java调试中如何获得被装载类的实际路径 11-17
如何用java拷贝本地文件夹 11-17
java的double类型探索. 11-17
利用Digester把XML转换成为Java对象 08-06
设置java的安全策略 11-17
j2me 实现可伸展目录树treelist(图) 11-17
file 对象 11-16
java socket编程(1) 11-16
对《java与模式》中工厂方法模式的异议 01-22
eclipse+myeclipse+lomboz图解安装jsp 11-17
初学者入门java的xml编程实例解析 11-17
java规则 基本篇 11-17
jdk-1_5_0_04-nb-4_1-win简体中文版 11-17
【java】命令行参数的获取及判断 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息