Search in sources :

Example 1 with IClassCoverage

use of org.jacoco.core.analysis.IClassCoverage in project jacoco by jacoco.

the class BundleCoverageImplTest method testCounters.

@Test
public void testCounters() {
    Collection<IClassCoverage> classes = Collections.emptySet();
    Collection<ISourceFileCoverage> sourcefiles = Collections.emptySet();
    final IPackageCoverage p1 = new PackageCoverageImpl("p1", classes, sourcefiles) {

        {
            classCounter = CounterImpl.getInstance(1, 0);
            methodCounter = CounterImpl.getInstance(2, 0);
            branchCounter = CounterImpl.getInstance(3, 0);
            instructionCounter = CounterImpl.getInstance(4, 0);
            lineCounter = CounterImpl.getInstance(5, 0);
        }
    };
    final IPackageCoverage p2 = new PackageCoverageImpl("p1", classes, sourcefiles) {

        {
            classCounter = CounterImpl.getInstance(1, 0);
            methodCounter = CounterImpl.getInstance(2, 0);
            branchCounter = CounterImpl.getInstance(3, 0);
            instructionCounter = CounterImpl.getInstance(4, 0);
            lineCounter = CounterImpl.getInstance(5, 0);
        }
    };
    BundleCoverageImpl bundle = new BundleCoverageImpl("testbundle", Arrays.asList(p1, p2));
    assertEquals(CounterImpl.getInstance(2, 0), bundle.getClassCounter());
    assertEquals(CounterImpl.getInstance(4, 0), bundle.getMethodCounter());
    assertEquals(CounterImpl.getInstance(6, 0), bundle.getBranchCounter());
    assertEquals(CounterImpl.getInstance(8, 0), bundle.getInstructionCounter());
    assertEquals(CounterImpl.getInstance(10, 0), bundle.getLineCounter());
}
Also used : IPackageCoverage(org.jacoco.core.analysis.IPackageCoverage) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) Test(org.junit.Test)

Example 2 with IClassCoverage

use of org.jacoco.core.analysis.IClassCoverage in project jacoco by jacoco.

the class BundleCoverageImplTest method testGroupByPackage.

@Test
public void testGroupByPackage() {
    ClassCoverageImpl ca = new ClassCoverageImpl("p1/A", 1, false);
    ca.setSourceFileName("A.java");
    ClassCoverageImpl cb = new ClassCoverageImpl("p2/B", 2, false);
    cb.setSourceFileName("B.java");
    ISourceFileCoverage sb = new SourceFileCoverageImpl("B.java", "p2");
    ISourceFileCoverage sc = new SourceFileCoverageImpl("C.java", "p3");
    BundleCoverageImpl bundle = new BundleCoverageImpl("bundle", Arrays.asList((IClassCoverage) ca, (IClassCoverage) cb), Arrays.asList(sb, sc));
    Collection<IPackageCoverage> packages = bundle.getPackages();
    assertEquals(3, packages.size(), 0.0);
    IPackageCoverage p1 = findPackage("p1", packages);
    assertNotNull(p1);
    assertEquals(Collections.singletonList(ca), p1.getClasses());
    assertTrue(p1.getSourceFiles().isEmpty());
    IPackageCoverage p2 = findPackage("p2", packages);
    assertNotNull(p2);
    assertEquals(Collections.singletonList(cb), p2.getClasses());
    assertEquals(Collections.singletonList(sb), p2.getSourceFiles());
    IPackageCoverage p3 = findPackage("p3", packages);
    assertNotNull(p3);
    assertTrue(p3.getClasses().isEmpty());
    assertEquals(Collections.singletonList(sc), p3.getSourceFiles());
}
Also used : IPackageCoverage(org.jacoco.core.analysis.IPackageCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) Test(org.junit.Test)

Example 3 with IClassCoverage

use of org.jacoco.core.analysis.IClassCoverage in project jacoco by jacoco.

the class PackageCoverageTest method testCountersWithSources.

@Test
public void testCountersWithSources() {
    // Classes with source reference will not considered for counters:
    final ClassCoverageImpl classnode = new ClassCoverageImpl("org/jacoco/test/Sample", 0, false) {

        {
            classCounter = CounterImpl.getInstance(9, 0);
            methodCounter = CounterImpl.getInstance(9, 0);
            branchCounter = CounterImpl.getInstance(9, 0);
            instructionCounter = CounterImpl.getInstance(9, 0);
        }
    };
    classnode.setSourceFileName("Sample.java");
    // Only source files will be considered for counters:
    final ISourceFileCoverage sourceFile = new SourceFileCoverageImpl("Sample.java", "org/jacoco/test/Sample") {

        {
            classCounter = CounterImpl.getInstance(1, 0);
            methodCounter = CounterImpl.getInstance(2, 0);
            branchCounter = CounterImpl.getInstance(3, 0);
            instructionCounter = CounterImpl.getInstance(4, 0);
        }
    };
    PackageCoverageImpl data = new PackageCoverageImpl("org/jacoco/test", Collections.singleton((IClassCoverage) classnode), Collections.singleton(sourceFile));
    assertEquals(CounterImpl.getInstance(1, 0), data.getClassCounter());
    assertEquals(CounterImpl.getInstance(2, 0), data.getMethodCounter());
    assertEquals(CounterImpl.getInstance(3, 0), data.getBranchCounter());
    assertEquals(CounterImpl.getInstance(4, 0), data.getInstructionCounter());
}
Also used : ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) Test(org.junit.Test)

Example 4 with IClassCoverage

use of org.jacoco.core.analysis.IClassCoverage in project jacoco by jacoco.

the class BundleCoverageImpl method groupByPackage.

private static Collection<IPackageCoverage> groupByPackage(final Collection<IClassCoverage> classes, final Collection<ISourceFileCoverage> sourcefiles) {
    final Map<String, Collection<IClassCoverage>> classesByPackage = new HashMap<String, Collection<IClassCoverage>>();
    for (final IClassCoverage c : classes) {
        addByName(classesByPackage, c.getPackageName(), c);
    }
    final Map<String, Collection<ISourceFileCoverage>> sourceFilesByPackage = new HashMap<String, Collection<ISourceFileCoverage>>();
    for (final ISourceFileCoverage s : sourcefiles) {
        addByName(sourceFilesByPackage, s.getPackageName(), s);
    }
    final Set<String> packageNames = new HashSet<String>();
    packageNames.addAll(classesByPackage.keySet());
    packageNames.addAll(sourceFilesByPackage.keySet());
    final Collection<IPackageCoverage> result = new ArrayList<IPackageCoverage>();
    for (final String name : packageNames) {
        Collection<IClassCoverage> c = classesByPackage.get(name);
        if (c == null) {
            c = Collections.emptyList();
        }
        Collection<ISourceFileCoverage> s = sourceFilesByPackage.get(name);
        if (s == null) {
            s = Collections.emptyList();
        }
        result.add(new PackageCoverageImpl(name, c, s));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IPackageCoverage(org.jacoco.core.analysis.IPackageCoverage) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) ISourceFileCoverage(org.jacoco.core.analysis.ISourceFileCoverage) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 5 with IClassCoverage

use of org.jacoco.core.analysis.IClassCoverage in project jacoco by jacoco.

the class CoreTutorial method execute.

/**
 * Run this example.
 *
 * @throws Exception
 *             in case of errors
 */
public void execute() throws Exception {
    final String targetName = TestTarget.class.getName();
    // For instrumentation and runtime we need a IRuntime instance
    // to collect execution data:
    final IRuntime runtime = new LoggerRuntime();
    // The Instrumenter creates a modified version of our test target class
    // that contains additional probes for execution data recording:
    final Instrumenter instr = new Instrumenter(runtime);
    InputStream original = getTargetClass(targetName);
    final byte[] instrumented = instr.instrument(original, targetName);
    original.close();
    // Now we're ready to run our instrumented class and need to startup the
    // runtime first:
    final RuntimeData data = new RuntimeData();
    runtime.startup(data);
    // In this tutorial we use a special class loader to directly load the
    // instrumented class definition from a byte[] instances.
    final MemoryClassLoader memoryClassLoader = new MemoryClassLoader();
    memoryClassLoader.addDefinition(targetName, instrumented);
    final Class<?> targetClass = memoryClassLoader.loadClass(targetName);
    // Here we execute our test target class through its Runnable interface:
    final Runnable targetInstance = (Runnable) targetClass.newInstance();
    targetInstance.run();
    // At the end of test execution we collect execution data and shutdown
    // the runtime:
    final ExecutionDataStore executionData = new ExecutionDataStore();
    final SessionInfoStore sessionInfos = new SessionInfoStore();
    data.collect(executionData, sessionInfos, false);
    runtime.shutdown();
    // Together with the original class definition we can calculate coverage
    // information:
    final CoverageBuilder coverageBuilder = new CoverageBuilder();
    final Analyzer analyzer = new Analyzer(executionData, coverageBuilder);
    original = getTargetClass(targetName);
    analyzer.analyzeClass(original, targetName);
    original.close();
    // Let's dump some metrics and line coverage information:
    for (final IClassCoverage cc : coverageBuilder.getClasses()) {
        out.printf("Coverage of class %s%n", cc.getName());
        printCounter("instructions", cc.getInstructionCounter());
        printCounter("branches", cc.getBranchCounter());
        printCounter("lines", cc.getLineCounter());
        printCounter("methods", cc.getMethodCounter());
        printCounter("complexity", cc.getComplexityCounter());
        for (int i = cc.getFirstLine(); i <= cc.getLastLine(); i++) {
            out.printf("Line %s: %s%n", Integer.valueOf(i), getColor(cc.getLine(i).getStatus()));
        }
    }
}
Also used : RuntimeData(org.jacoco.core.runtime.RuntimeData) LoggerRuntime(org.jacoco.core.runtime.LoggerRuntime) InputStream(java.io.InputStream) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) Analyzer(org.jacoco.core.analysis.Analyzer) IRuntime(org.jacoco.core.runtime.IRuntime) ExecutionDataStore(org.jacoco.core.data.ExecutionDataStore) IClassCoverage(org.jacoco.core.analysis.IClassCoverage) Instrumenter(org.jacoco.core.instr.Instrumenter) SessionInfoStore(org.jacoco.core.data.SessionInfoStore)

Aggregations

IClassCoverage (org.jacoco.core.analysis.IClassCoverage)20 ISourceFileCoverage (org.jacoco.core.analysis.ISourceFileCoverage)11 Test (org.junit.Test)7 IPackageCoverage (org.jacoco.core.analysis.IPackageCoverage)5 ClassCoverageImpl (org.jacoco.core.internal.analysis.ClassCoverageImpl)5 PackageCoverageImpl (org.jacoco.core.internal.analysis.PackageCoverageImpl)4 Analyzer (org.jacoco.core.analysis.Analyzer)3 IMethodCoverage (org.jacoco.core.analysis.IMethodCoverage)3 ExecutionDataStore (org.jacoco.core.data.ExecutionDataStore)3 SourceFileCoverageImpl (org.jacoco.core.internal.analysis.SourceFileCoverageImpl)3 Collection (java.util.Collection)2 CoverageBuilder (org.jacoco.core.analysis.CoverageBuilder)2 SessionInfoStore (org.jacoco.core.data.SessionInfoStore)2 ISourceFileLocator (org.jacoco.report.ISourceFileLocator)2 ILinkable (org.jacoco.report.internal.html.ILinkable)2 Document (org.w3c.dom.Document)2 BufferedReader (java.io.BufferedReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1