public static BlockLocation blockLocation(final Location location, final int block) { return new BlockLocation(location, block); }
private Function<String, TestInfo> toTestInfo(final BlockCoverage blockData) { return a -> new TestInfo(null, a, 0, Optional.ofNullable(blockData.getBlock().getLocation().getClassName()), blockData.getBlock().getBlock()); }
private Predicate<Entry<BlockLocation, Set<TestInfo>>> isFor( final ClassName clazz) { return a -> a.getKey().isFor(clazz); }
@Test public void shouldI() throws Exception { final Map<BlockLocation, Set<Integer>> actual = analyse(ThreeBlocks2.class); final Location l = Location.location(ClassName.fromClass(ThreeBlocks2.class), MethodName.fromString("foo"), "(I)I"); assertThat(actual.get(BlockLocation.blockLocation(l, 0))).containsOnly(105); assertThat(actual.get(BlockLocation.blockLocation(l, 1))).containsOnly(106); assertThat(actual.get(BlockLocation.blockLocation(l, 2))).containsOnly(108); }
private Set<Integer> getLinesForBlock(BlockLocation bl) { Set<Integer> lines = this.blocksToLines.get(bl); if (lines == null) { calculateLinesForBlocks(bl.getLocation().getClassName()); lines = this.blocksToLines.get(bl); if (lines == null) { lines = Collections.emptySet(); } } return lines; }
private Predicate<BlockLocation> hasBlock(final int block) { return a -> { System.out.println(a); return a.getBlock() == block; }; }
@Test public void shouldMapAllLinesWhenMethodContainsThreeBlocks() throws Exception { final Map<BlockLocation, Set<Integer>> actual = analyse(ThreeBlocks.class); final Location l = Location.location(ClassName.fromClass(ThreeBlocks.class), MethodName.fromString("foo"), "(I)I"); assertThat(actual.get(BlockLocation.blockLocation(l, 0))).containsOnly(5); assertThat(actual.get(BlockLocation.blockLocation(l, 1))).containsOnly(6); assertThat(actual.get(BlockLocation.blockLocation(l, 2))).containsOnly(8); }
@Test public void shouldIncludeAllCoveredLinesInCoverageSummary() { final BlockLocationBuilder block = aBlockLocation(); when(this.code.getCodeUnderTestNames()).thenReturn( Collections.singleton(block.build().getLocation().getClassName())); when(this.lm.mapLines(any(ClassName.class))).thenReturn( makeCoverageMapForBlock(block, 1, 2, 3, 4)); final CoverageResultBuilder cr = aCoverageResult().withVisitedBlocks( block.build(1)); this.testee.calculateClassCoverage(cr.build()); final CoverageSummary actual = this.testee.createSummary(); assertEquals(4, actual.getNumberOfCoveredLines()); }
@Override protected BlockCoverage mapToData(final Map<String, Object> map) { final String method = (String) map.get(METHOD); final Location location = new Location(ClassName.fromString((String) map.get(CLASSNAME)), MethodName.fromString(method.substring(0, method.indexOf(OPEN_PAREN))), method.substring(method.indexOf(OPEN_PAREN))); final BlockLocation blockLocation = new BlockLocation(location, Integer.parseInt((String) map.get(NUMBER))); @SuppressWarnings("unchecked") final Collection<String> tests = (Collection<String>) map.get(TESTS); return new BlockCoverage(blockLocation, tests); }
private void writeLineCoverage(final BlockCoverage each, final Writer out) { final Location l = each.getBlock().getLocation(); write( out, "<block classname='" + l.getClassName().asJavaName() + "'" + " method='" + StringUtil.escapeBasicHtmlChars(l.getMethodName().name()) + StringUtil.escapeBasicHtmlChars(l.getMethodDesc()) + "' number='" + each.getBlock().getBlock() + "'>"); write(out, "<tests>\n"); final List<String> ts = new ArrayList<>(each.getTests()); Collections.sort(ts); for (final String test : ts) { write(out, "<test name='" + StringUtil.escapeBasicHtmlChars(test) + "'/>\n"); } write(out, "</tests>\n"); write(out, "</block>\n"); }
@Test public void shouldMapAllLinesWhenMethodContainsThreeMultiLineBlocks() throws Exception { final Map<BlockLocation, Set<Integer>> actual = analyse(ThreeMultiLineBlocks.class); final Location l = Location.location( ClassName.fromClass(ThreeMultiLineBlocks.class), MethodName.fromString("foo"), "(I)I"); assertThat(actual.get(BlockLocation.blockLocation(l, 0))).contains(5, 6); assertThat(actual.get(BlockLocation.blockLocation(l, 1))).contains(7, 8); assertThat(actual.get(BlockLocation.blockLocation(l, 2))).contains(10, 11); }
private Predicate<BlockLocation> resultFor(final Class<?> class1) { return a -> a.isFor(ClassName.fromClass(class1)); } };
@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; }
private static Generator<BlockLocationBuilder, BlockLocation> blockLocationSeed() { return b -> BlockLocation.blockLocation(b._Location(), b._Block()); }
private void handleProbes(final SafeDataInputStream is) { final int classId = is.readInt(); final String methodName = is.readString(); final String methodSig = is.readString(); final int first = is.readInt(); final int last = is.readInt(); final Location loc = Location.location(this.classIdToName.get(classId), MethodName.fromString(methodName), methodSig); for (int i = first; i != (last + 1); i++) { // nb, convert from classwide id to method scoped index within // BlockLocation this.probeToBlock.put(CodeCoverageStore.encode(classId, i), new BlockLocation(loc, i - first)); } }
@Test public void shouldMapLinesWhenLinesSpanBlocks() throws Exception { final Map<BlockLocation, Set<Integer>> actual = analyse(com.example.LineNumbersSpanBlocks.class); final Location l = Location.location( ClassName.fromClass(com.example.LineNumbersSpanBlocks.class), MethodName.fromString("foo"), "(I)I"); assertThat(actual.get(BlockLocation.blockLocation(l, 2))).containsOnly(12); }
private Collection<BlockLocation> makeCoverage(final String clazz, final int block) { final BlockLocation cs = new BlockLocation(Location.location( ClassName.fromString(clazz), MethodName.fromString("foo"), "V"), block); return Collections.singleton(cs); }
@Test public void shouldIncludeLastLinesConstructorsInBlock() throws Exception { final Map<BlockLocation, Set<Integer>> actual = analyse(LastLineOfContructorCheck.class); final Location l = Location.location( ClassName.fromClass(LastLineOfContructorCheck.class), MethodName.fromString("<init>"), "()V"); assertThat(actual.get(BlockLocation.blockLocation(l, 0))).contains(6); }
private BlockLocation makeCoverage(final String name, final int block) { final Location l = Location.location(ClassName.fromString(name), MethodName.fromString("amethod"), "methodDesc"); final BlockLocation bl = new BlockLocation(l, block); return bl; }
@Test public void shouldCalculateCoverageForMethodThatThrowsExceptionWithFinallyBlock() throws IOException, InterruptedException, ExecutionException { final List<CoverageResult> coveredClasses = runCoverageForTest(TestThrowsExceptionInFinallyBlock.class); final ClassName clazz = ClassName .fromClass(ThrowsExceptionInFinallyBlockTestee.class); assertThat(coveredClasses).anyMatch(coverageFor(BlockLocation.blockLocation( Location.location(clazz, this.foo, "()V"), 0))); assertThat(coveredClasses).anyMatch(coverageFor(BlockLocation.blockLocation( Location.location(clazz, this.foo, "()V"), 1))); }