cobertura 和tomcat如何集成使用

注意点

  1. 需要Copy cobertura.jar及其LIB到TOMCAT/COMMON/LIB:
  2. 只有当TOMCAT关闭的时候,才会写cobertura.ser文件

步骤

  1. start up tomcat
  2. build war
  3. deploy web application
  4. web test
  5. undeploy
  6. shut down
  7. coverage report

  附build.xml

<project name="KSystem" default="smoketest" basedir=".">

    
<property environment="env"/>
    
<property name="checkstyle.dir" value="${env.CHECKSTYLE_HOME}" />
    
<path id="checkstyle.classpath">
        
<fileset dir="${checkstyle.dir}">
            
<include name="checkstyle-all-4.1.jar" />
        
</fileset>
    
</path>
    
<property name="cobertura.dir" value="${env.COBERTURA_HOME}" />
    
<path id="cobertura.classpath">
        
<fileset dir="${cobertura.dir}">
            
<include name="cobertura.jar" />
        
</fileset>
    
</path>
    
    
<taskdef name="jcsc" classname="rj.tools.jcsc.ant.JCSCTask" classpath="${env.JCSC_HOME}/lib/jcsc.jar" />
    
<taskdef resource="checkstyletask.properties" classpathref="checkstyle.classpath" />
    
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
    
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask" classpath="${env.PMD_HOME}/lib/pmd-3.7.jar"/>

    
<!-- master build -->
    
<property name="project.dir" value="${basedir}" />
    
<property name="project.lib.dir" value="${project.dir}/WEB-INF/lib" />
    
<property name="build.dir" value="${basedir}/build" />
    
<property name="out.dir" value="${build.dir}" />
    
<property name="out.file.name" value="${ant.project.name}" />
    
<property name="classes.dir" value="${out.dir}/classes" />
    
<property name="src.dir" value="${project.dir}/src" />
    
<property name="webtests.dir" value="${project.dir}/webtests" />
    
<property name="instrumented.dir" value="${out.dir}/instrumented" />

    
<property name="cvs.repository" value=":pserver:ci:ci@192.168.126.10:/KSystem"/>
    
<property name="cvs.package" value="${ant.project.name}"/>
    
<target name="checkout">
        
<cvs command="update -d -P"/>
    
</target>    

    
<target name="qa.build" description="build java file including cobertura for coverage test">
        
<javac target="1.4" 
                source
="1.4" 
                destdir
="${classes.dir}" 
                debug
="yes"
                encoding
="utf-8">
            
<src path="${src.dir}" />
            
<src path="${tests.dir}" />
            
<include name="**/*.java" />
            
<classpath refid="compile.classpath"/>
            
<include name="${cobertura.path}" />
            
            
<!-- when i need do QA, the classpath is need, otherwise, donot include the classpath -->
            
<classpath refid="cobertura.classpath" />
        
</javac>
        
<copy todir="${classes.dir}">
            
<fileset dir="${src.dir}">
                
<exclude name="**/*.java"/>
            
</fileset>
        
</copy>
    
</target>
    
    
<target name="initdir">

        
<delete dir="${reports.jcsc.dir}" />
        
<delete dir="${reports.jcsc.dir}/xsl" />
        
        
<delete dir="${reports.checkstyle.dir}" />
        
<delete dir="${reports.cobertura.dir}" />
        
<delete dir="${reports.cobertura.xml.dir}" />
        
<delete dir="${reports.cobertura.html.dir}" />
        
        
<delete dir="${reports.junit.dir}" />
        
<delete dir="${reports.junit.html.dir}" />
        
<delete dir="${reports.junit.xml.dir}" />
        
        
<delete dir="${reports.pmd.dir}" />
        
<delete dir="${reports.pmd.xml.dir}" />
        
        
<delete dir="${reports.dir}"/>
        
        
<delete dir="${out.dir}" />
        
<delete dir="${classes.dir}" />
        
<mkdir dir="${out.dir}" />
        
<mkdir dir="${classes.dir}" />
        
        
<mkdir dir="${reports.dir}" />
        
<mkdir dir="${reports.jcsc.dir}" />
        
<mkdir dir="${reports.jcsc.dir}/xsl" />
        
<copy todir="${reports.jcsc.dir}/xsl">
            
<fileset dir="${env.JCSC_HOME}/html/xml/xsl" />
        
</copy>
        
        
<mkdir dir="${reports.checkstyle.dir}" />
        
<mkdir dir="${reports.cobertura.dir}" />
        
<mkdir dir="${reports.cobertura.xml.dir}" />
        
<mkdir dir="${reports.cobertura.html.dir}" />
        
        
<mkdir dir="${reports.junit.dir}" />
        
<mkdir dir="${reports.junit.html.dir}" />
        
<mkdir dir="${reports.junit.xml.dir}" />
        
        
<mkdir dir="${reports.pmd.dir}" />
        
<mkdir dir="${reports.pmd.xml.dir}" />
    
</target>
    
    
<property name="tomcat.manager.url" value="http://localhost:8080/manager" />
    
<property name="tomcat.username" value="admin" />
    
<property name="tomcat.password" value="admin" />
    
<property name="webapp.name" value="${ant.project.name}" />
    
<property name="tomcat.home" value="${env.CATALINA_HOME}" />
    
<target name="tomcat.startup">
        
<exec executable="${tomcat.home}/bin/startup.bat" />
    
</target>
    
<target name= "tomcat.shutdown">
        
<exec executable="${tomcat.home}/bin/shutdown.bat" />
    
</target>
    
<property file="tomcatTasks.properties" />
    
<taskdef file="tomcat.properties">
        
<classpath>
            
<pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
        
</classpath>
    
</taskdef>
    
<target name="tomcat.deploy" description="Install application in Tomcat">
        
<deploy url="${tomcat.manager.url}"
            username
="${tomcat.username}"
            password
="${tomcat.password}"
            path
="/${webapp.name}"
            war
="file:${out.dir}/${out.file.name}.war"/>
    
</target>

    
<target name="tomcat.undeploy" description="Remove application in Tomcat">
        
<undeploy url="${tomcat.manager.url}"
            username
="${tomcat.username}"
            password
="${tomcat.password}"
            path
="/${webapp.name}"/>
    
</target>
    
    
<!-- make report-->
    
<property name="reports.dir" value="${out.dir}/reports" />
    
<property name="reports.jcsc.dir" value="${reports.dir}/jcsc" />
    
<property name="jcscrules.path" value="${env.JCSC_HOME}/rules/genie.jcsc.xml" />
    
<property name="reports.checkstyle.dir" value="${reports.dir}/checkstyle" />
    
<property name="checksytlerule.path" value="${env.CHECKSTYLE_HOME}/sun_checks.xml" />
    
<property name="reports.cobertura.dir" value="${reports.dir}/coverage" /> 
    
<property name="reports.cobertura.xml.dir" value="${reports.cobertura.dir}/xml" /> 
    
<property name="reports.cobertura.html.dir" value="${reports.cobertura.dir}/html" /> 
    
<property name="reports.junit.dir" value="${reports.dir}/junit" />
    
<property name="reports.junit.xml.dir" value="${reports.junit.dir}/xml" />
    
<property name="reports.junit.html.dir" value="${reports.junit.dir}/html" />
    
<property name="reports.pmd.dir" value="${reports.dir}/pmd" />
    
<property name="reports.pmd.xml.dir" value="${reports.pmd.dir}/xml" />
    
<property name="out.junit.textType" value="xml" />

    
<target name="junitreport" description="generate reports of junit">
        
<junitreport todir="${reports.junit.xml.dir}">
            
<fileset dir="${reports.junit.xml.dir}">
                
<include name="TEST-*.xml" />
            
</fileset>
            
<report format="frames" todir="${reports.junit.html.dir}" />
        
</junitreport>
    
</target>
    
    
<target name="jcscreport">
        
<jcsc rules="${jcscrules.path}" destdir="${reports.jcsc.dir}">
            
<fileset dir="${src.dir}" includes="**/*.java"/>
        
</jcsc>
    
</target>
    
    
<target name="checkstylereport" >
        
<property name="checkstylereport.path" value="${reports.checkstyle.dir}/checkstyle_report.xml" />

        
<checkstyle config="${checksytlerule.path}"
            failureProperty
="checkstyle.failure"
            failOnViolation
="false">
            
<formatter type="xml" tofile="${checkstylereport.path}"/>
            
<fileset dir="src" includes="**/*.java"/>
        
</checkstyle>
    
</target>
    
    
<target name="pmd">
        
<pmd shortFilenames="true">
            
<ruleset>rulesets/favorites.xml</ruleset>
            
<ruleset>basic</ruleset>
            
<formatter type="xml" toFile="${reports.pmd.xml.dir}/pmd.xml" />
            
<fileset dir="${src.dir}">
                
<include name="**/*.java"/>
            
</fileset>
        
</pmd>
    
</target>
    
    
<target name="instrument">
        
<delete file="cobertura.ser"/>
        
<delete dir="${instrumented.dir}" />
        
<mkdir dir="${instrumented.dir}" />
        
<cobertura-instrument todir="${instrumented.dir}">
            
<ignore regex="org.apache.log4j.*" />

            
<fileset dir="${classes.dir}">
                
<include name="**/*.class" />
                
<exclude name="**/*Test.class" />
                
<exclude name="**/AllTests.class" />
            
</fileset>
        
</cobertura-instrument>
            
<copy todir="${instrumented.dir}">
                
<fileset dir="${classes.dir}">
                    
<exclude name="**/*.class"/>
                
</fileset>
            
</copy>

    
</target>

    
<target name="coverage-check">
        
<cobertura-check branchrate="34" totallinerate="100" />
    
</target>
    
    
<target name="coverage-report">
        
<cobertura-report srcdir="${src.dir}" destdir="${reports.cobertura.xml.dir}" format="xml" />
    
</target>
    
    
<target name="alternate-coverage-report">
        
<cobertura-report destdir="${reports.cobertura.html.dir}">
            
<fileset dir="${src.dir}">
                
<include name="**/*.java"/>
            
</fileset>
        
</cobertura-report>
    
</target>

    
<!-- for this project -->
    
<path id="compile.classpath">
        
<fileset dir="${project.lib.dir}">
            
<include name="**/*.jar" />
        
</fileset>
    
</path>
    
<path id="web.compile.classpath">
        
<fileset dir="${env.TOMCAT_HOME}/webapps/${ant.project.name}/WEB-INF/classes">
            
<include name="**/*.class" />
        
</fileset>
    
</path>

    
<!-- smoke test -->
    
<target name="unittest" depends="qa.build" description="unit test using nunit">
        
<!-- target unittest.overide means the a project can selfdefine itself's unit target, the test depends some special conditions. 
             that a project, which would build the unittest project, defines a unittest.override, so, i will call the unittest.override target,
             instead of unittest 
-->
        
<junit fork="yes" dir="${basedir}" failureProperty="test.failed" >
            
<classpath location="${instrumented.dir}" />
            
<classpath location="${classes.dir}" />
            
            
<classpath refid="compile.classpath" />

            
<classpath refid="cobertura.classpath" />

            
<formatter type="${out.junit.textType}"/>
            
<batchtest fork="yes" todir="${reports.junit.xml.dir}" >
                
<fileset dir="${tests.dir}">
                    
<include name="**/*Test.java"/>
                
</fileset>
            
</batchtest>
        
</junit>
    
</target>

    
<target name="smoketest" >
        
<property name="tests.dir" value="${project.dir}/tests" />
        
<antcall target="checkout"></antcall>
        
<antcall target="initdir"></antcall>
        
<antcall target="qa.build"></antcall>
        
<antcall target="instrument"></antcall>
        
<antcall target="unittest"></antcall>
        
<antcall target="junitreport"></antcall>
        
<antcall target="coverage-report"></antcall>
        
<antcall target="alternate-coverage-report"></antcall>
        
<antcall target="jcscreport"></antcall>
        
<antcall target="checkstylereport"></antcall>
        
<antcall target="pmd"></antcall>
    
</target>
    
<!-- system test -->
    
<target name="webtest">
        
<junit fork="yes" dir="${basedir}" failureProperty="test.failed" >
            
<classpath location="${classes.dir}" />
            
<classpath location="${tomcat.home}/webapps/${ant.project.name}/WEB-INF/classes" />
            
<classpath refid="compile.classpath" />
            
<classpath refid="cobertura.classpath" />
            
<formatter type="${out.junit.textType}"/>
            
<batchtest fork="yes" todir="${reports.junit.xml.dir}" >
                
<fileset dir="${tests.dir}">
                    
<include name="**/*Test.java"/>
                
</fileset>
            
</batchtest>
        
</junit>
    
</target>
    
    
<target name="buildwar">
        
<property name="jsp.dir" value="${project.dir}/jsp" />
        
<war destfile="${out.dir}/${out.file.name}.war" webxml="${project.dir}\WEB-INF\web.xml">
            
<fileset dir="${jsp.dir}">
                
<include name="**/*.jsp" />
                
<include name="**/*.html" />
                
<include name="**/*.htm"/>
            
</fileset>
            
<webinf dir="${project.dir}\WEB-INF">
                
<exclude name="${project.dir}\WEB-INF\web.xml" />
            
</webinf>
            
<classes dir="${out.dir}/classes" />
        
</war>
    
</target>

    
<target name="systemtest">
        
<property name="tests.dir" value="${project.dir}/webtests" />
        
<antcall target="checkout"></antcall>
        
<parallel threadCount="2">
            
<ant target="tomcat.startup" />
            
<sequential>
                
<sleep seconds="4"/>
                
<antcall target="initdir"></antcall>
                
<antcall target="qa.build"></antcall>
                
<antcall target="instrument"></antcall>
                
<antcall target="buildwar"></antcall>
                
<antcall target="tomcat.deploy"></antcall>
                
<antcall target="webtest"></antcall>
                
<antcall target="tomcat.undeploy"></antcall>
                
<antcall target="tomcat.shutdown"></antcall>
                
<antcall target="junitreport"></antcall>
                
<antcall target="coverage-report"></antcall>
                
<antcall target="alternate-coverage-report"></antcall>
                
<antcall target="jcscreport"></antcall>
                
<antcall target="checkstylereport"></antcall>
                
<antcall target="pmd"></antcall>
            
</sequential>
        
</parallel>
        
<delete dir="${tomcat.home}/webapps/${webapp.name}" />
    
</target>
</project>

posted on 2006-08-10 11:40  muddle  阅读(1099)  评论(2编辑  收藏  举报

导航