public CodeSource createCodeSource() { return new CodeSource(createProjectClassPaths()); }
public Set<ClassName> getCodeUnderTestNames() { final Set<ClassName> codeClasses = new HashSet<>(); FCollection.mapTo(getCode(), ClassInfo.toClassName(), codeClasses); return codeClasses; }
@Before public void setUp() { MockitoAnnotations.initMocks(this); when(this.lm.mapLines(any(ClassName.class))).thenReturn( new HashMap<BlockLocation, Set<Integer>>()); when(this.code.findTestee(any())).thenReturn(Optional.empty()); this.testee = new CoverageData(this.code, this.lm); }
@Test public void shouldRecordClassPath() { final ClassName clazz = ClassName.fromClass(Foo.class); final HierarchicalClassId fooId = new HierarchicalClassId( new ClassIdentifier(0, clazz), "0"); final ClassInfo foo = ClassInfoMother.make(fooId.getId()); when(this.code.getCodeUnderTestNames()).thenReturn( Collections.singleton(clazz)); when(this.code.getClassInfo(any(List.class))).thenReturn( Collections.singletonList(foo)); createAndRunTestee(); verify(this.history).recordClassPath(Arrays.asList(fooId), this.coverageDb); }
final CodeSource code = new CodeSource(cps); final Collection<ClassName> codeClasses = FCollection.map(code.getCode(), ClassInfo.toClassName());
@Override public Collection<ClassInfo> getClassInfo(final Collection<ClassName> classes) { return this.code.getClassInfo(classes); }
private Collection<ClassName> allClasses() { return this.code.getCodeUnderTestNames(); }
@Override public Map<BlockLocation, Set<Integer>> mapLines(final ClassName clazz) { final Map<BlockLocation, Set<Integer>> map = new HashMap<>(); final Optional<byte[]> maybeBytes = this.source.fetchClassBytes(clazz); // classes generated at runtime eg by mocking frameworks // will be instrumented but not available on the classpath if (maybeBytes.isPresent()) { final ClassReader cr = new ClassReader(maybeBytes.get()); final ClassNode classNode = new ClassNode(); cr.accept(classNode, ClassReader.EXPAND_FRAMES); for (final Object m : classNode.methods) { final MethodNode mn = (MethodNode) m; final Location l = Location.location(clazz, MethodName.fromString(mn.name), mn.desc); final List<Block> blocks = ControlFlowAnalyser.analyze(mn); for (int i = 0; i != blocks.size(); i++) { final BlockLocation bl = new BlockLocation(l, i); map.put(bl, blocks.get(i).getLines()); } } } return map; }
@Test public void shouldIdentifyTestClassesOnTestPath() { when(this.classPath.test()).thenReturn( Arrays.asList(this.foo.getName(), this.bar.getName())); assertThat(this.testee.getTests()).containsExactly(this.foo, this.bar); }
private BigInteger generateCoverageNumber( final Map<ClassLine, Set<TestInfo>> coverage) { BigInteger coverageNumber = BigInteger.ZERO; final Set<ClassName> testClasses = new HashSet<>(); FCollection.flatMapTo(coverage.values(), testsToClassName(), testClasses); for (final ClassInfo each : this.code.getClassInfo(testClasses)) { coverageNumber = coverageNumber.add(each.getDeepHash()); } return coverageNumber; }
private Set<ClassName> getAllClassesAndTests( final CoverageDatabase coverageData) { final Set<ClassName> names = new HashSet<>(); for (final ClassName each : this.code.getCodeUnderTestNames()) { names.add(each); FCollection.mapTo(coverageData.getTestsForClass(each), TestInfo.toDefiningClassName(), names); } return names; }
private Map<BlockLocation, Set<Integer>> analyse(Class<?> clazz) throws ClassNotFoundException { when(this.source.fetchClassBytes(any(ClassName.class))).thenReturn( Optional.ofNullable(ClassUtils.classAsBytes(clazz))); final LineMap testee = new LineMapper(this.source); return testee.mapLines(ClassName.fromClass(clazz)); }
@Override public CoverageData calculateCoverage() { try { final long t0 = System.currentTimeMillis(); this.timings.registerStart(Timings.Stage.SCAN_CLASS_PATH); final Collection<ClassInfo> tests = this.code.getTests(); this.timings.registerEnd(Timings.Stage.SCAN_CLASS_PATH); final CoverageData coverage = new CoverageData(this.code, new LineMapper( this.code)); this.timings.registerStart(Timings.Stage.COVERAGE); gatherCoverageData(tests, coverage); this.timings.registerEnd(Timings.Stage.COVERAGE); final long time = (System.currentTimeMillis() - t0) / 1000; LOG.info("Calculated coverage in " + time + " seconds."); verifyBuildSuitableForMutationTesting(coverage); this.exporter.recordCoverage(coverage.createCoverage()); return coverage; } catch (final PitHelpError phe) { throw phe; } catch (final Exception e) { throw Unchecked.translateCheckedException(e); } }
public CoverageData(final CodeSource code, final LineMap lm, Map<BlockLocation, Set<TestInfo>> blockCoverage) { this.blockCoverage = blockCoverage; this.code = code; this.lm = lm; this.classesForFile = FCollection.bucket(this.code.getCode(), keyFromClassInfo()); }
@Before public void setUp() { MockitoAnnotations.initMocks(this); this.testee = new CodeSource(this.classPath, this.repository); this.foo = makeClassInfo("Foo"); this.bar = makeClassInfo("Bar"); }
private int numberOfLines() { return FCollection.fold(numberLines(), 0, this.code.getClassInfo(allClasses())); }
@Test public void shouldProvideNamesOfCodeClasses() { final ClassInfo foo = makeClassInfo("Foo"); final ClassInfo bar = makeClassInfo("Bar"); when(this.classPath.code()).thenReturn( Arrays.asList(foo.getName(), bar.getName())); assertThat(this.testee.getCodeUnderTestNames()).containsOnly(ClassName.fromString("Foo") , ClassName.fromString("Bar")); }
private TestInfo createTestInfo(final Description description, final int executionTime, final int linesCovered) { final Optional<ClassName> testee = this.code.findTestee(description .getFirstTestClass()); return new TestInfo(description.getFirstTestClass(), description.getQualifiedName(), executionTime, testee, linesCovered); }
@Test public void shouldAllowClientsToRetrieveBytecode() { this.testee.fetchClassBytes(ClassName.fromString("Foo")); verify(this.repository).querySource(ClassName.fromString("Foo")); }
private void setupClassPath(final ClassByteArraySource source, final String clazz) { final Repository repository = new Repository(source); final ClassInfo ci = repository.fetchClass(ClassName.fromString(clazz)) .get(); when(this.code.getCode()).thenReturn(Collections.singletonList(ci)); }