private void exportMutantDetails(Mutater m, List<MutationDetails> indexable, int i) throws IOException { final MutationDetails md = indexable.get(i); final Path mutantFolder = this.mutantsDir.resolve("" + i); Files.createDirectories(mutantFolder); final Mutant mutant = m.getMutation(md.getId()); writeMutantToDisk(mutant, mutantFolder); writeBytecodeToDisk(mutant.getBytes(), mutantFolder); writeDetailsToDisk(md, mutantFolder); }
@Test public void shouldAssignTestsFromPrioritiserToMutant() { final List<TestInfo> expected = makeTestInfos(0); final List<MutationDetails> mutations = makeMutations("foo"); when(this.prioritiser.assignTests(any(MutationDetails.class))).thenReturn( expected); when(this.mutater.findMutations(any(ClassName.class))) .thenReturn(mutations); final MutationDetails actual = this.testee.createMutations(this.foo) .iterator().next(); assertEquals(expected, actual.getTestsInOrder()); }
private Boolean shortCutEquals(MethodTree tree, MutationDetails a, Mutater m) { if (!mutatesAConditionalJump(tree, a.getInstructionIndex())) { return false; } final ClassTree mutant = ClassTree.fromBytes(m.getMutation(a.getId()).getBytes()); final MethodTree mutantEquals = mutant.methods().stream() .filter(MethodMatchers.forLocation(tree.asLocation())) .findFirst() .get(); return ALWAYS_FALSE.matches(mutantEquals.instructions()); }
public Collection<MutationDetails> createMutations(final ClassName clazz) { final Mutater m = this.mutationConfig.createMutator(this.source); final Collection<MutationDetails> availableMutations = m .findMutations(clazz); if (availableMutations.isEmpty()) { return availableMutations; } else { final ClassTree tree = ClassTree .fromBytes(this.source.getBytes(clazz.asJavaName()).get()); this.interceptor.begin(tree); final Collection<MutationDetails> updatedMutations = this.interceptor .intercept(availableMutations, m); this.interceptor.end(); assignTestsToMutations(updatedMutations); return updatedMutations; } }
private boolean isInfiniteLoop(MutationDetails each, Mutater m) { final ClassTree mutantClass = ClassTree.fromBytes(m.getMutation(each.getId()).getBytes()); final Optional<MethodTree> mutantMethod = mutantClass.methods().stream() .filter(forLocation(each.getId().getLocation())) .findFirst(); return infiniteLoopMatcher().matches(mutantMethod.get().instructions()); }
@Test public void shouldFilterMethodsWithGeneratedAnnotation() { final List<MutationDetails> mutations = this.mutator.findMutations(ClassName.fromClass(MethodAnnotatedWithGenerated.class)); final Collection<MutationDetails> actual = runWithTestee(mutations, MethodAnnotatedWithGenerated.class); assertThat(actual).hasSize(1); assertThat(actual.iterator().next().getId().getLocation().getMethodName().name()).isEqualTo("bar"); }
private void processMutation(final Reporter r, final TimeOutDecoratedTestSource testSource, final MutationDetails mutationDetails) throws IOException { final MutationIdentifier mutationId = mutationDetails.getId(); final Mutant mutatedClass = this.mutater.getMutation(mutationId); // For the benefit of mocking frameworks such as PowerMock // mess with the internals of Javassist so our mutated class // bytes are returned JavassistInterceptor.setMutant(mutatedClass); if (DEBUG) { LOG.fine("mutating method " + mutatedClass.getDetails().getMethod()); } final List<TestUnit> relevantTests = testSource .translateTests(mutationDetails.getTestsInOrder()); r.describe(mutationId); final MutationStatusTestPair mutationDetected = handleMutation( mutationDetails, mutatedClass, relevantTests); r.report(mutationId, mutationDetected); if (DEBUG) { LOG.fine("Mutation " + mutationId + " detected = " + mutationDetected); } }
@Test public void shouldLeaveMutantsNotOnLoggingLinesUntouched() { final ClassName clazz = ClassName.fromClass(DoesNotLog.class); final List<MutationDetails> input = this.mutator.findMutations(clazz); final Collection<MutationDetails> actual = analyseWithTestee(DoesNotLog.class); assertThat(actual).containsExactlyElementsOf(input); }
@Test public void shouldReportErrorWhenOneOccursDuringAnalysis() { this.mutations.add(new MutationDetails(aMutationId().withIndex(0) .withMutator("foo").build(), "file", "desc", 0, 0)); when(this.mutater.getMutation(any(MutationIdentifier.class))).thenThrow( new PitError("foo")); this.testee.run(); verify(this.reporter).done(ExitCode.UNKNOWN_ERROR); }
@Test @Ignore("is triggering filter with fake classes") public void shouldReportMutationsFoundWhenSomeDetected() { this.data.setFailWhenNoMutations(false); final ClassName foo = ClassName.fromClass(Foo.class); when(this.mutater.findMutations(foo)).thenReturn( MutationDetailsMother.aMutationDetail().build(1)); when(this.code.getCodeUnderTestNames()).thenReturn( Collections.singleton(foo)); final CombinedStatistics actual = createAndRunTestee(); assertEquals(1, actual.getMutationStatistics().getTotalMutations()); }
public MutationDetails makeMutant(final String clazz, final int index) { final MutationIdentifier id = aMutationId() .withLocation(aLocation().withClass(ClassName.fromString(clazz))) .withIndex(index).withMutator("mutator").build(); final MutationDetails md = new MutationDetails(id, "sourceFile", "desc", 42, 0); when(this.mutater.getMutation(md.getId())).thenReturn( new Mutant(md, new byte[0])); return md; }
private Collection<MutationDetails> analyseWithTestee(Class<?> clazz) { final ClassName name = ClassName.fromClass(clazz); this.testee.begin(treeFor(clazz)); final List<MutationDetails> input = this.mutator.findMutations(name); return this.testee.intercept(input, this.mutator); }
private void processMutation(final Reporter r, final TimeOutDecoratedTestSource testSource, final MutationDetails mutationDetails) throws IOException { final MutationIdentifier mutationId = mutationDetails.getId(); final Mutant mutatedClass = this.mutater.getMutation(mutationId); // For the benefit of mocking frameworks such as PowerMock // mess with the internals of Javassist so our mutated class // bytes are returned JavassistInterceptor.setMutant(mutatedClass); if (DEBUG) { LOG.fine("mutating method " + mutatedClass.getDetails().getMethod()); } final List<TestUnit> relevantTests = testSource .translateTests(mutationDetails.getTestsInOrder()); r.describe(mutationId); final MutationStatusTestPair mutationDetected = handleMutation( mutationDetails, mutatedClass, relevantTests); r.report(mutationId, mutationDetected); if (DEBUG) { LOG.fine("Mutation " + mutationId + " detected = " + mutationDetected); } }