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,

-- 
You received this message because you are subscribed to the Google Groups 
"JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jacoco/66b410b2-fcca-45e7-9ebc-d93d528bfeb5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to