Instrumentation, test and coverage analysis of several projects

72 views
Skip to first unread message

marce...@gmail.com

unread,
Apr 9, 2015, 10:56:50 AM4/9/15
to jac...@googlegroups.com
Hello, folks.
I need to instrument several java projects, run their test cases, and get coverage information. I do not have access to the source code of this projects, and I do not want to use a separate ant or maven configuration for each project in order to use jacocoagent.

I want to build a code in which I can instrument each class of the target project and replace the original version by the instrumented version. Then I want to run the JUnit test cases to execute the instrumented classes and, somehow, use the execution data to perform coverage analysis of each class.
As I need to do that for more than 100 projects, I'd like to automate the process instead of creating a configuration file for each project using ant or maven.

I adapted the CoreTutorial example to perform a task such as described above, but the data dumpped are always empty. Here is my code:

String current = new java.io.File( "." ).getCanonicalPath();
String targetClassName = "SampleClass.class";
String path = current+ "bin\\sample\\SampleClass.class";
final String targetName = SampleClass.class.getName();

// I kept the configuration of CoreTutorial
final IRuntime runtime = new LoggerRuntime();
final RuntimeData data = new RuntimeData();
runtime.startup(data);

//I tried to use the Java Agent
AgentOptions options = new AgentOptions();
options.setIncludes("*");
options.setAppend(false);
options.setDumpOnExit(true);
options.setDestfile(current+"bin\\sample\\data.trace");
OutputMode outputmode = OutputMode.file;
options.setOutput(outputmode);
options.setClassDumpDir(current+"bin\\sample\\");
Agent agent = Agent.getInstance(options);
agent.startup();

//I tried to use offline instrumentation, but it does not instrument the class
//final IExecutionDataAccessorGenerator offline = new OfflineInstrumentationAccessGenerator();
//final Instrumenter instr = new Instrumenter(offline);
final Instrumenter instr = new Instrumenter(runtime);
InputStream originalTargetClass = getTargetClass(targetName);

//creates a copy of the instrumented class
String tempDir = current+"\\temp";
File file = new File(tempDir);
if (!file.exists()) {
file.mkdir();
}
OutputStream os = new FileOutputStream(tempDir+"\\"+targetClassName);
byte[] buffer = new byte[1024];
int bytesRead;
while((bytesRead = originalTargetClass.read(buffer)) !=-1){
os.write(buffer, 0, bytesRead);
}
os.close();


originalTargetClass = getTargetClass(targetName);
final byte[] instrumented = instr.instrument(originalTargetClass, targetName);

//replace the original class by the instrumented version
OutputStream writer = new FileOutputStream(path);
writer.write(instrumented);
writer.close();

//Run JUnit test cases
JUnitCore junit = new JUnitCore();
Result result = junit.run(SampleClassTest.class);

// At the end of test execution we collect execution data and shutdown
// the runtime:

agent.shutdown();
agent.dump(false); //dumps nothing - only date and session info
runtime.shutdown();

final ExecutionDataStore executionData = new ExecutionDataStore();
final SessionInfoStore sessionInfos = new SessionInfoStore();
data.collect(executionData, sessionInfos, false);

//retrieve the original class from the temp dir
originalTargetClass = new FileInputStream(tempDir+"\\"+targetClassName);
// Together with the original class definition we can calculate coverage
// information:
final CoverageBuilder coverageBuilder = new CoverageBuilder();
final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
analyzer.analyzeClass(originalTargetClass, targetName);

// Let's dump some metrics and line coverage information:
for (final IClassCoverage cc : coverageBuilder.getClasses()) {
...
}


I don't know how to dump the execution data obtained from the execution of the instrumented class by the test cases of the JUnit File, neither how to use that execution data to perform analysis.

Anyone can help-me?

Thanks and Regards,

Marc Hoffmann

unread,
Apr 10, 2015, 4:36:21 AM4/10/15
to jac...@googlegroups.com
Hi,

why do you want to instrument your class files? Just configure the
JaCoCo agent for the JVM executing the test:

http://www.eclemma.org/jacoco/trunk/doc/agent.html

By default the agent dumps the execution data on VM termination. This is
actually one of the major benefits using JaCoCo.

Regards,
-marc

marce...@gmail.com

unread,
Apr 10, 2015, 1:31:06 PM4/10/15
to jac...@googlegroups.com
Thanks, Marc.
I had already tried to use the JaCoCo agent before, but I had some problems. Thus I tried to use offline instrumentation to dump the execution data manually.
This time I could configure the JaCoCo agent properly and everything worked just fine.

Thank you again and best regards,
Marcelo Eler
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages