public Location asLocation() { return Location.location(this.owner,MethodName.fromString(this.rawNode.name), this.rawNode.desc); }
@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); }
@Override protected MutationResult mapToData(final Map<String, Object> map) { final Location location = new Location(ClassName.fromString((String) map.get(MUTATED_CLASS)), MethodName.fromString((String) map.get(MUTATED_METHOD)), (String) map.get(METHOD_DESCRIPTION)); final MutationIdentifier id = new MutationIdentifier(location, Arrays.asList(new Integer((String) map.get(INDEX))), (String) map.get(MUTATOR)); final MutationDetails md = new MutationDetails(id, (String) map.get(SOURCE_FILE), (String) map.get(DESCRIPTION), Integer.parseInt((String) map.get(LINE_NUMBER)), Integer.parseInt((String) map.get(BLOCK))); final MutationStatusTestPair status = new MutationStatusTestPair(Integer.parseInt((String) map.get(NUMBER_OF_TESTS_RUN)), DetectionStatus.valueOf((String) map.get(STATUS)), (String) map.get(KILLING_TEST)); return new MutationResult(md, status); }
private Predicate<MutationDetails> inEqualsMethod() { return a -> { final Location loc = a.getId().getLocation(); return loc.getMethodDesc().equals("(Ljava/lang/Object;)Z") && loc.getMethodName().equals(MethodName.fromString("equals")); }; }
@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 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 shouldSortInConsistantOrder() { final Location a = location(ClassName.fromString("A"), MethodName.fromString("A"), "A"); final Location b = location(ClassName.fromString("AA"), MethodName.fromString("A"), "A"); final Location c = location(ClassName.fromString("A"), MethodName.fromString("AA"), "A"); final Location d = location(ClassName.fromString("A"), MethodName.fromString("AA"), "AA"); final List<Location> ls = Arrays.asList(a, b, c, d); Collections.sort(ls); assertEquals(Arrays.asList(a, c, d, b), ls); }
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); }
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 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); }
@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); }
@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); }
@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 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); }
@Test public void shouldMapAllLinesWhenMethodContainsSingleBlock() throws Exception { final Map<BlockLocation, Set<Integer>> actual = analyse(OneBlock.class); final Location l = Location.location(ClassName.fromClass(OneBlock.class), MethodName.fromString("foo"), "()I"); final BlockLocation bl = new BlockLocation(l, 0); assertThat(actual.get(bl)).containsOnly(5); }
private static Generator<LocationBuilder, Location> locationSeed() { return b -> Location.location(b._Class(), MethodName.fromString(b._Method()), b._MethodDescription()); }
@Override public MethodVisitor visitMethod(final int access, final String methodName, final String methodDescriptor, final String signature, final String[] exceptions) { final MethodMutationContext methodContext = new MethodMutationContext( this.context, Location.location( ClassName.fromString(this.context.getClassInfo().getName()), MethodName.fromString(methodName), methodDescriptor)); final MethodVisitor methodVisitor = this.cv.visitMethod(access, methodName, methodDescriptor, signature, exceptions); final MethodInfo info = new MethodInfo() .withOwner(this.context.getClassInfo()).withAccess(access) .withMethodName(methodName).withMethodDescriptor(methodDescriptor); if (this.filter.test(info)) { return this.visitMethodForMutation(methodContext, info, methodVisitor); } else { return methodVisitor; } }
@Test public void shouldCalculateCoverageForSmallMethodThatThrowsException() throws IOException, InterruptedException, ExecutionException { final List<CoverageResult> coveredClasses = runCoverageForTest(TestsClassWithException.class); assertThat(coveredClasses).anyMatch(coverageFor(CoveredBeforeExceptionTestee.class)); final ClassName throwsException = ClassName .fromClass(ThrowsExceptionTestee.class); assertThat(coveredClasses).anyMatch(coverageFor(BlockLocation.blockLocation( Location.location(throwsException, this.foo, "()V"), 0))); assertThat(coveredClasses).anyMatch(coverageFor(BlockLocation.blockLocation( Location.location(throwsException, MethodName.fromString("throwsException"), "()V"), 0))); }
private MutationResult makeResult(String clazz, String method) { final Location location = Location.location(ClassName.fromString(clazz), MethodName.fromString(method), "()V"); final MutationDetails md = aMutationDetail().withId( aMutationId().withLocation(location)).build(); final MutationResult mr = new MutationResult(md, MutationStatusTestPair.notAnalysed(0, DetectionStatus.KILLED)); return mr; }
@Test public void shouldMatchRealInfiniteLoopFromJodaTimeMutants() { Location l1 = Location.location(ClassName.fromString("org.joda.time.field.BaseDateTimeField") , MethodName.fromString("set") , "(Lorg/joda/time/ReadablePartial;I[II)[I"); checkFiltered(ClassName.fromString("BaseDateTimeFieldMutated"),forLocation(l1)); checkNotFiltered(ClassName.fromString("LocalDate"),"withPeriodAdded"); checkFiltered(ClassName.fromString("LocalDateMutated"),"withPeriodAdded"); checkNotFiltered(ClassName.fromString("MonthDay"),"withPeriodAdded"); checkFiltered(ClassName.fromString("MonthDayMutated"),"withPeriodAdded"); checkFiltered(ClassName.fromString("BaseChronologyMutated"),"validate"); checkFiltered(ClassName.fromString("BaseChronologyMutated2"),"set"); Location l = Location.location(ClassName.fromString("org.joda.time.MonthDay") , MethodName.fromString("withPeriodAdded") , "(Lorg/joda/time/ReadablePeriod;I)Lorg/joda/time/MonthDay;"); checkFiltered(ClassName.fromString("MonthDayMutated2"),forLocation(l)); }