通过代码方式使用Jacoco远程统计tomcat服务的代码覆盖率

1)修改远程tomcat下的bin/catalina.sh中JAVA_OPTS的配置
----------------------------------------------------------------
# -javaagent: 的后面跟jacoco的安装路径
# includes= 选项,选择你要覆盖率的服务
# port= 选项,选择你要打开的端口
# address= 选项,tomcat服务所在机器的ip地址(如果想在跟tomcat服务同一台机器上执行ant任务的话,需要改为127.0.0.1)
JAVA_OPTS="-javaagent:/path/to/your/jacoco_0.8.5/lib/jacocoagent.jar=includes=com.*,output=tcpserver,port=8893,address=10.81.14.77"

2)在开发环境pom.xml中引入jacoco的jar包

<!--使用jacoco对web工程生成全部的覆盖率报告-->
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.core</artifactId>
            <version>0.8.5</version>
        </dependency>

        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.report</artifactId>
            <version>0.8.5</version>
        </dependency>

3)调用代码实现

package com.wangfg.jacoco;

import org.jacoco.core.data.ExecutionDataWriter;
import org.jacoco.core.runtime.RemoteControlReader;
import org.jacoco.core.runtime.RemoteControlWriter;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;

/**
 * 用于生成xxx.exec数据的程序
 */
public class ExecutionDataClient {
    private static final String DESTFILE = "jacoco-client.exec";

    private static final String ADDRESS = "127.0.0.1";

    private static final int PORT = 8044;

    /**
     * Starts the execution data request.
     *
     * @throws IOException
     */
    @Test
    public  void test() throws IOException {
        final FileOutputStream localFile = new FileOutputStream(DESTFILE);
        final ExecutionDataWriter localWriter = new ExecutionDataWriter(
                localFile);

        // Open a socket to the coverage agent:
        final Socket socket = new Socket(InetAddress.getByName(ADDRESS), PORT);
        final RemoteControlWriter writer = new RemoteControlWriter(
                socket.getOutputStream());
        final RemoteControlReader reader = new RemoteControlReader(
                socket.getInputStream());
        reader.setSessionInfoVisitor(localWriter);
        reader.setExecutionDataVisitor(localWriter);

        // Send a dump command and read the response:
        writer.visitDumpCommand(true, false);
        reader.read();

        socket.close();
        localFile.close();
    }

}

  

package com.wangfg.jacoco;

import org.jacoco.core.analysis.Analyzer;
import org.jacoco.core.analysis.CoverageBuilder;
import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.tools.ExecFileLoader;
import org.jacoco.report.DirectorySourceFileLocator;
import org.jacoco.report.FileMultiReportOutput;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.html.HTMLFormatter;
import org.junit.Test;

import java.io.File;
import java.io.IOException;

public class ReportGenerator {

    private final String title = "jacoco-demo";
    private final File executionDataFile = new File("C:\\Users\\Administrator\\Downloads\\jacoco\\jacoco-client.exec");
    private final File classesDirectory = new File("C:\\Users\\Administrator\\Downloads\\jacoco\\target\\classes\\com\\wangfg\\jacoco\\controller");
    private final File sourceDirectory= new File("C:\\Users\\Administrator\\Downloads\\jacoco\\src");
    private final File reportDirectory = new File("C:\\Users\\Administrator\\Downloads\\jacoco\\coveragereport");

    private ExecFileLoader execFileLoader;

    /**
     * Create the report.
     *
     * @throws IOException
     */
    @Test
    public void test() throws IOException {
        // Read the jacoco.exec file. Multiple data files could be merged
        // at this point
        loadExecutionData();

        // Run the structure analyzer on a single class folder to build up
        // the coverage model. The process would be similar if your classes
        // were in a jar file. Typically you would create a bundle for each
        // class folder and each jar you want in your report. If you have
        // more than one bundle you will need to add a grouping node to your
        // report
        final IBundleCoverage bundleCoverage = analyzeStructure();
        createReport(bundleCoverage);

    }

    private void createReport(IBundleCoverage bundleCoverage)
            throws IOException {

        // Create a concrete report visitor based on some supplied
        // configuration. In this case we use the defaults
         HTMLFormatter htmlFormatter = new HTMLFormatter();
         IReportVisitor visitor = htmlFormatter
                .createVisitor(new FileMultiReportOutput(reportDirectory));

        // Initialize the report with all of the execution and session
        // information. At this point the report doesn‘t know about the
        // structure of the report being created
        visitor.visitInfo(execFileLoader.getSessionInfoStore().getInfos(),
                execFileLoader.getExecutionDataStore().getContents());

        // Populate the report structure with the bundle coverage information.
        // Call visitGroup if you need groups in your report.
        visitor.visitBundle(bundleCoverage, new DirectorySourceFileLocator(
                sourceDirectory, "utf-8", 4));

        // Signal end of structure information to allow report to write all
        // information out
        visitor.visitEnd();

    }

    private void loadExecutionData() throws IOException {
        execFileLoader = new ExecFileLoader();
        execFileLoader.load(executionDataFile);
    }

    private IBundleCoverage analyzeStructure() throws IOException {
         CoverageBuilder coverageBuilder = new CoverageBuilder();
         Analyzer analyzer = new Analyzer(
                execFileLoader.getExecutionDataStore(), coverageBuilder);
        analyzer.analyzeAll(classesDirectory);
        return coverageBuilder.getBundle(title);
    }

}

 4)分别执行以上代码即可得到远程代码的覆盖率。

原文地址:https://www.cnblogs.com/wangfg/p/12308308.html

时间: 2024-07-29 17:53:07

通过代码方式使用Jacoco远程统计tomcat服务的代码覆盖率的相关文章

通过ant方式使用Jacoco远程统计tomcat服务的代码覆盖率

1)修改远程tomcat下的bin/catalina.sh中JAVA_OPTS的配置----------------------------------------------------------------# -javaagent: 的后面跟jacoco的安装路径# includes= 选项,选择你要覆盖率的服务# port= 选项,选择你要打开的端口# address= 选项,tomcat服务所在机器的ip地址(如果想在跟tomcat服务同一台机器上执行ant任务的话,需要改为127.0

如何使用Jacoco远程统计tomcat服务的代码覆盖率

如何使用Jacoco远程统计tomcat服务的覆盖率 在做软件开发的时候经常会遇到做各种测试,这里介绍一种本人遇到的:代码覆盖率测试, 这个测试非常实用,能发现许多无效的模块和代码.强烈推荐!!!!! 网上好多资料都不全,而且没有详细的配置流程 本文将简单介绍如何使用Jacoco生成远程tomcat服务的覆盖率报告. (注:使用jacoco打开远程服务端口,有一定安全风险.) 软件安装 Ant Jacoco 远程Tomcat服务配置 1:sh shutdown.sh先关闭tomcat服务. 2:

【原】用IDEA远程Debug Tomcat服务

[环境参数] Web容器:Tomcat 8.0 IDE:IDEA 2018.1.5 [具体步骤] 1.配置Tomcat容器参数 编辑$CATALINA_HOME/bin/catalina.sh脚本,加入如下参数: -agentlib:jdwp=transport=dt_socket,address=8000,suspend=n,server=y 如下图所示. 注意:监听的端口可以为任意没有被占用的端口号,在此为8000. 可以通过linux命令"# netstat –anp | grep 800

Maven工程配置代码覆盖工具Jacoco

本篇博文我们将给出示例理解如何在Maven工程中配置Jacoco和如何使用Jacoco查看代码覆盖报告~ Jacoco是一个开源的Java代码覆盖率工具,Jacoco可以嵌入到Ant .Maven中,并提供了EclEmma Eclipse插件,也可以使用JavaAgent技术监控Java程序.很多第三方的工具提供了对Jacoco的集成,如sonar.Jenkins等. Maven工程 创建Maven工程 打开Eclipse,File->New->Project->Maven Projec

Eclipse远程调试Tomcat

最近,一直在研究Tomcat的工作内幕,主要的方法就是参考<How Tomcat Works> 这本书和Tomcat 5.5.26的源代码. Tomcat的代码结构还是比较清晰的,注释也比较全.但是代码毕竟是静态的,难以彻底弄清类与类之间的协作关系,以及运行时对象的交互关系. 如果能对Tomcat的启动.处理请求和停止的过程进行断点调试,看清Tomcat的每一步行踪,那么就能解决上面的问题了. 于是,又一个问题出来了:如何使用Eclipse远程调试Tomcat ? 上网查了一些资料,相关的文章

idea14远程调试tomcat项目

场景如下:本地开发好代码之后,到qa那边提测,qa把同一份代码部署到自己的linux测试机. 远程调试的步骤如下: 1. 首先在IDEA中打开项目代码,并保证远程调试机器,也是同一份代码. 2. 因为我们用的是Tomcat,所以在IDEA中点击右上角那个"Edit Configurations"按钮,然后在弹出的界面中点击左上角的加号,选择tomcat server->remote 3. 在弹出的的界面中填写服务器的ip和工程的端口. 4. 然后点击那个弹出框的Starup/Co

如何用myeclispe远程调试tomcat

在工作开发中,通常用本机进行代码编写,然后将编好的工程部署到测试服务器进行测试.往往测试服务器并不是自己的本机,因此对调试带来众多不便.今天学习可以用myeclispe远程调试tomcat. 步骤:1.将工程部署到远程测试服务器. 2.远程登陆测试服务器,启动tomcat远程调试.要用到的相关命令 sudo su - ps -ef | grep tomcat kill -9 进程号 sh catalina.sh jpda start     //以debug模式启动Tomcat(进入tomcat

远程debug tomcat

如何用eclispe远程调试tomcat tomcat是一种非常常见的java web应用服务器,有时候服务器可能并不是部署在本地,而是部署在远程其他的机器上,我们用eclispe该如何进行debug调试呢?下面小编就和大家分享一下解决的办法. 方法/步骤 1.在eclispe中新建web应用,项目名字叫web.里面只有一个Servlet类,文件名为info.java.Web.xml配置如下. info.java内容如下, 2.将该项目打成war包,名称为web.war. 3.将web.war复

java-jvisualvm远程监控tomcat

一.修改要访问的远程主机(Linux)相关文件,本文档只介绍了java-jvisualvm的JMX方式: 1.打开$CATALINA_HOME/bin/startup.sh, 找到倒数第二行(也就是exec "$PRGDIR"/"$EXECUTABLE" start "[email protected]"一行上边)加上如下内容: export CATALINA_OPTS="$CATALINA_OPTS-Dcom.sun.manageme