Jacoco Java Agent Jboss6实践练习(on-the fly)

1, 下载Jacoco核心包. (https://www.jacoco.org/jacoco/), 此处使用jacoco-0.8.1.

aea3548eded3c08ca98327214d20e749ae5.jpg

2, 添加Jboss6 VM配置:

-javaagent:{Your path}/jacoco-0.8.1/jacocoagent.jar=output=tcpserver,port=6300,address=*,includes=com.xxx.*

这里设置address=*是为了确保远程客户端能成功连接Jacoco服务器。

3, 启动Jboss6,正常情况下,会看到6300端口处于监听状态。

4, 在Jboss6中完成代码的测试工作。

5, 在测试过程中可以使用Dump命令实时查看代码覆盖测试情况。注意:如果需要重启或者关闭Jboss6,则必须先使用Dump命令将测试结果保存。

java -version
pause
java -jar jacococli.jar dump --address localhost --destfile dump/test.exec
pause

6, 使用Merge合并多个*.exec。如果在整个测试过程中产生了多个.exec文件,则需要在生成报表前将其合并。

::Merges multiple exec files into a new one.
::set path=D:\Java\jdk1.6.0_34\bin
set path=D:\Java\jdk1.8.0_131\bin
java -jar jacococli.jar merge dump/*.exec --destfile merge/all.exec
pause

7, 生成测试报表

pause
java -jar jacococli.jar report merge/all.exec --classfiles D:\IDE\content_env_jboss6\jboss-eap-6.2-Spring2.5\standalone\deployments\content-productimport.war\WEB-INF\classes --html report.html --sourcefiles Z:\git\content-productimport\src\main\java
pause

 

 

Jacoco Doc Reference (https://www.jacoco.org/jacoco/):

Java Agent

JaCoCo uses class file instrumentation to record execution coverage data. Class files are instrumented on-the-fly using a so called Java agent. This mechanism allows in-memory pre-processing of all class files during class loading independent of the application framework.

If you use the JaCoCo Ant tasks or JaCoCo Maven plug-in you don't have to care about the agent and its options directly. This is transparently handled by the them.

The JaCoCo agent collects execution information and dumps it on request or when the JVM exits. There are three different modes for execution data output:

  • File System: At JVM termination execution data is written to a local file.
  • TCP Socket Server: External tools can connect to the JVM and retrieve execution data over the socket connection. Optional execution data reset and execution data dump on VM exit is possible.
  • TCP Socket Client: At startup the JaCoCo agent connects to a given TCP endpoint. Execution data is written to the socket connection on request. Optional execution data reset and execution data dump on VM exit is possible.

The agent jacocoagent.jar is part of the JaCoCo distribution and includes all required dependencies. A Java agent can be activated with the following JVM option:

-javaagent:[yourpath/]jacocoagent.jar=[option1]=[value1],[option2]=[value2]

The JaCoCo agent accepts the following options:

OptionDescriptionDefault
destfilePath to the output file for execution data.jacoco.exec
appendIf set to true and the execution data file already exists, coverage data is appended to the existing file. If set to false, an existing execution data file will be replaced.true
includesA list of class names that should be included in execution analysis. The list entries are separated by a colon (:) and may use wildcard characters (*and ?). Except for performance optimization or technical corner cases this option is normally not required.* (all classes)
excludesA list of class names that should be excluded from execution analysis. The list entries are separated by a colon (:) and may use wildcard characters (* and ?). Except for performance optimization or technical corner cases this option is normally not required.empty (no excluded classes)
exclclassloaderA list of class loader names that should be excluded from execution analysis. The list entries are separated by a colon (:) and may use wildcard characters (* and ?). This option might be required in case of special frameworks that conflict with JaCoCo code instrumentation, in particular class loaders that do not have access to the Java runtime classes.sun.reflect.DelegatingClassLoader
inclbootstrapclassesSpecifies whether also classes from the bootstrap classloader should be instrumented. Use this feature with caution, it needs heavy includes/excludes tuning.false
inclnolocationclassesSpecifies whether also classes without a source location should be instrumented. Normally such classes are generated at runtime e.g. by mocking frameworks and are therefore excluded by default.false
sessionidA session identifier that is written with the execution data. Without this parameter a random identifier is created by the agent.auto-generated
dumponexitIf set to true coverage data will be written on VM shutdown. The dump can only be written if either file is specified or the output is tcpserver/tcpclient and a connection is open at the time when the VM terminates.true
outputOutput method to use for writing coverage data. Valid options are:
  • file: At VM termination execution data is written to the file specified in the destfile attribute.
  • tcpserver: The agent listens for incoming connections on the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
  • tcpclient: At startup the agent connects to the TCP port specified by the address and port attribute. Execution data is written to this TCP connection.
  • none: Do not produce any output.
Please see the security considerations below.
file
addressIP address or hostname to bind to when the output method is tcpserver or connect to when the output method is tcpclient. In tcpserver mode the value "*" causes the agent to accept connections on any local address.loopback interface
portPort to bind to when the output method is tcpserver or connect to when the output method is tcpclient. In tcpserver mode the port must be available, which means that if multiple JaCoCo agents should run on the same machine, different ports have to be specified.6300
classdumpdirLocation relative to the working directory where all class files seen by the agent are dumped to. This can be useful for debugging purposes or in case of dynamically created classes for example when scripting engines are used.no dumps
jmxIf set to true the agent exposes functionality via JMX under the name org.jacoco:type=Runtime. Please see the security considerations below.false

Security Consideration for Remote Agent Control

The ports and connections opened in tcpserver and tcpclient mode and the JMX interface do not provide any authentication mechanism. If you run JaCoCo on production systems make sure that no untrusted sources have access to the TCP server port, or JaCoCo TCP clients only connect to trusted targets. Otherwise internal information of the application might be revealed or DOS attacks are possible.

转载于:https://my.oschina.net/jerval/blog/1827613

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值