Package net.sourceforge.cobertura.coveragedata

Examples of net.sourceforge.cobertura.coveragedata.ClassData


    Set<String> classNames(ProjectData data) {
        LinkedHashSet<String> result = new LinkedHashSet<String>();
        if (data != null) {
            for (Object o : data.getClasses()) {

                ClassData cd = (ClassData) o;
                result.add(cd.getName());
            }
        }
        return result;
    }
View Full Code Here


        }
        return result;
    }

    ClassData diffClasses(ClassData larger, ClassData smaller) {
        ClassData result = new ClassData(larger.getName());
        for (Object o : larger.getLines()) {
            LineData lld = (LineData) o;
            long afterHits = lld.getHits();
            long prevHits = 0;
            long prevConditions = 0;
            if (smaller != null) {
                LineData sld = smaller.getLineCoverage(lld.getLineNumber());
                if (sld != null) {
                    prevHits = sld.getHits();
                    prevConditions = sld.getConditionSize();
                }
            }
            int diffHits = (int) (afterHits - prevHits);
            result.addLine(lld.getLineNumber(), lld.getMethodName(),
                    lld.getMethodDescriptor());
            if (diffHits > 0) {
                result.touch(lld.getLineNumber(), diffHits);
            }
            if (diffHits < 0) {
                errors.add("Coverage decreased on line " + lld.getLineNumber()
                        + " of " + larger.getName());
            }
            if (prevConditions > lld.getConditionSize()) {
                errors.add("Jump or Switch disappeared on line "
                        + lld.getLineNumber() + " of " + larger.getName());
            }
            for (int i = 0; i < lld.getConditionSize(); i++) {
                Object cd = lld.getConditionData(i);
                if (cd instanceof JumpData) {
                    JumpData ljd = (JumpData) cd;
                    result.addLineJump(lld.getLineNumber(), i);
                    long prevTrue = 0;
                    long prevFalse = 0;
                    if (smaller != null) {
                        LineData sld = smaller.getLineCoverage(lld
                                .getLineNumber());
                        if (sld != null) {
                            JumpData sjd = (JumpData) sld.getConditionData(i);
                            if (sjd != null) {
                                prevTrue = sjd.getTrueHits();
                                prevFalse = sjd.getFalseHits();
                            } else {
                                errors.add("Somehow, JumpData null at condition "
                                        + i + " on line " + lld.getLineNumber());
                            }
                        }

                    }
                    long diffTrueHits = ljd.getTrueHits() - prevTrue;
                    long diffFalseHits = ljd.getFalseHits() - prevFalse;
                    if (diffTrueHits > 0) {
                        result.touchJump(lld.getLineNumber(), i, true,
                                (int) diffTrueHits);
                    }
                    if (diffFalseHits > 0) {
                        result.touchJump(lld.getLineNumber(), i, false,
                                (int) diffFalseHits);
                    }
                    if (diffFalseHits < 0 || diffTrueHits < 0) {
                        errors.add("Jump coverage on " + result.getName() + ":"
                                + lld.getLineNumber() + ":" + i + " decreased:"
                                + "true from " + prevTrue + " by "
                                + diffTrueHits + ", false from " + prevFalse
                                + " by " + diffFalseHits);
                    }
                } else if (cd instanceof SwitchData) {
                    SwitchData lsd = (SwitchData) cd;
                    result.addLineSwitch(lld.getLineNumber(), i, 0, 0, lsd.getMaxBranches());
                    long[] prevBranchHits = new long[lsd
                            .getNumberOfValidBranches() - 1];
                    long prevDefaultHits = 0;
                    if (smaller != null) {
                        LineData sld = smaller.getLineCoverage(lld
                                .getLineNumber());
                        if (sld != null) {
                            SwitchData ssd = (SwitchData) sld
                                    .getConditionData(i);
                            if (ssd != null) {
                                for (int j = 0; j < prevBranchHits.length; j++) {
                                    // returns -1 on an error condition.
                                    // probably never happens in normal
                                    // situation, but does in unit test.
                                    prevBranchHits[j] = Math.max(0,
                                            ssd.getHits(j));
                                }
                                prevDefaultHits = ssd.getDefaultHits();
                            }
                        }
                    }
                    for (int j = 0; j < prevBranchHits.length; j++) {
                        long diffBranchHits = lsd.getHits(j)
                                - prevBranchHits[j];
                        result.touchSwitch(lld.getLineNumber(), i, j,
                                (int) diffBranchHits);
                        if (diffBranchHits < 0) {
                            errors.add("Switch coverage " + i + "," + j
                                    + " on line " + lld.getLineNumber()
                                    + " decreased.");
                        }
                    }
                    long diffDefaultHits = lsd.getDefaultHits()
                            - prevDefaultHits;
                    if (diffDefaultHits > 0) {
                        result.touchSwitch(lld.getLineNumber(), i, -1,
                                (int) diffDefaultHits);
                    }
                    if (diffDefaultHits < 0) {
                        errors.add("Switch coverage " + i + ",default on line "
                                + lld.getLineNumber() + " decreased.");
                    }
                }
            }
        }
        Set<Integer> largerLines = lineNumbers(larger);
        Set<Integer> smallerLines = lineNumbers(smaller);
        smallerLines.removeAll(largerLines);
        if (smallerLines.size() > 0) {
            errors.add("Line(s) disappeared from class " + larger.getName()
                    + ": " + smallerLines);
        }

        // This arabesque accommodates .groovy files, and I suppose any others
        // that follow the java package/directory convention.
        int iName = larger.getSourceFileName().lastIndexOf("/");
        String sfName = larger.getSourceFileName().substring(iName + 1,
                larger.getSourceFileName().length());
        result.setSourceFileName(sfName);

        return result;
    }
View Full Code Here

public class ProjectDataDifferenceCalculatorTest extends Assert {

    void addSomeStuff(ProjectData data, String className,
            int numberOfLinesToTouch) {
        ClassData cd = data.getClassData(className);
        if (cd == null) {
            cd = new ClassData(className);
            data.addClassData(cd);
        }
        for (int i = 0; i < numberOfLinesToTouch; i++) {
            cd.touch(i, 1);
        }
    }
View Full Code Here

        }
    }

    void touchAJump(ProjectData data, String className, int lineNumber,
            boolean whichBranch) {
        ClassData cd = data.getClassData(className);
        cd.touchJump(lineNumber, 0, whichBranch, 1);
    }
View Full Code Here

    }

    ProjectData diffProjects(ProjectData larger, ProjectData smaller) {
        ProjectData result = new ProjectData();
        for (Object o : larger.getClasses()) {
            ClassData lcd = (ClassData) o;
            ClassData scd = (ClassData) smaller.getClassData(lcd.getName());
            result.addClassData(diffClasses(lcd, scd));
        }
        Set<String> largerNames = classNames(larger);
        Set<String> smallerNames = classNames(smaller);
        smallerNames.removeAll(largerNames);
View Full Code Here

        cd.touchJump(lineNumber, 0, whichBranch, 1);
    }

    void touchASwitch(ProjectData data, String className, int lineNumber,
            int switchNumber) {
        ClassData cd = data.getClassData(className);
        cd.touchSwitch(lineNumber, 0, switchNumber, 1);
    }
View Full Code Here

        ProjectDataDifferenceCalculator calc = new ProjectDataDifferenceCalculator(
                two, one);
        ProjectData diff = calc.getDifference();
        assertSame(calc.getLarger(), two);
        assertSame(calc.getSmaller(), one);
        ClassData foo = diff.getClassData("foo");
        assertEquals(3, foo.getLines().size());
        for (int i = 0; i < 2; i++) {
            LineData x = foo.getLineCoverage(i);
            assertEquals(x.getHits(), 1);
            assertEquals(x.hasBranch(), i == 1);
            if (x.hasBranch()) {
                JumpData jd = (JumpData) x.getConditionData(0);
                assertEquals(jd.getTrueHits(), 1);
                assertEquals(jd.getFalseHits(), 1);
            }
        }
        assertEquals(foo.getLineCoverage(2).getHits(), 0);
        ClassData bar = diff.getClassData("bar");
        assertEquals(3, bar.getLines().size());
        for (int i = 0; i < 3; i++) {
            LineData x = bar.getLineCoverage(i);
            assertEquals(x.getHits(), 0);
            assertEquals(x.hasBranch(), i == 2, "Switches for " + i);
            if (x.hasBranch()) {
                SwitchData sd = (SwitchData) x.getConditionData(0);
                assertEquals(1, sd.getDefaultHits());
                assertEquals(0, sd.getHits(0));
                assertEquals(0, sd.getHits(1));
                assertEquals(1, sd.getHits(2));

            }
        }
        ClassData baz = diff.getClassData("baz");
        assertEquals(8, baz.getLines().size());
        for (int i = 0; i < 8; i++) {
            LineData x = baz.getLineCoverage(i);
            assertEquals(x.getHits(), 1);
            assertFalse(x.hasBranch());
        }
        assertEquals(0, calc.errors.size());
        addSomeStuff(one, "baz", 2);
View Full Code Here

        buildKeyForLine();
    }

    void buildClassDataHelpers() {
        for (Object o : getData().getClasses()) {
            ClassData cd = (ClassData) o;
            ClassData dcd = null;
            if (getSamePackageData() != null) {
                dcd = (ClassData) getSamePackageData().getChild(
                        cd.getBaseName());
            }
            try {
View Full Code Here

public class PackageCoverageCalculatorTest extends Assert {

    void addSomeStuff(ProjectData data, String className,
            int numberOfLinesToTouch) {
        ClassData cd = data.getClassData(className);
        if (cd == null) {
            cd = new ClassData(className);
            data.addClassData(cd);
        }
        for (int i = 0; i < numberOfLinesToTouch; i++) {
            cd.touch(i, 1);
        }
    }
View Full Code Here

            public void instrument( String file, int line )
            {
              if ( isExcluded( file ) ) {
                getLogger().debug("ignoring " + file);
              } else {
                  ClassData classData = coverageProjectData.getOrCreateClassData( ApparatUtil.toClassname( file ) );
                  classData.setSourceFileName( getSourceFilePath( file ) );
                  classData.addLine( line, null, null );
              }
            }
        };
    }
View Full Code Here

TOP

Related Classes of net.sourceforge.cobertura.coveragedata.ClassData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.