org.pitest.mutationtest.engine.Mutater Java Examples

The following examples show how to use org.pitest.mutationtest.engine.Mutater. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InlinedFinallyBlockFilter.java    From pitest with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  final List<MutationDetails> combined = new ArrayList<>(
      mutations.size());
  final Map<LineMutatorPair, Collection<MutationDetails>> mutatorLinebuckets = bucket(
      mutations, toLineMutatorPair());

  for (final Entry<LineMutatorPair, Collection<MutationDetails>> each : mutatorLinebuckets
      .entrySet()) {
    if (each.getValue().size() > 1) {
      checkForInlinedCode(combined, each);
    } else {
      combined.addAll(each.getValue());
    }
  }

  /* FIXME tests rely on order of returned mutants */
  combined.sort(compareLineNumbers());
  return combined;
}
 
Example #2
Source File: InfiniteLoopFilter.java    From pitest with Apache License 2.0 6 votes vote down vote up
private Collection<MutationDetails> findTimeoutMutants(Location location,
    Collection<MutationDetails> mutations, Mutater m) {

  final MethodTree method = this.currentClass.methods().stream()
      .filter(forLocation(location))
      .findFirst()
      .get();

  //  give up if our matcher thinks loop is already infinite
  if (infiniteLoopMatcher().matches(method.instructions())) {
    return Collections.emptyList();
  }

  final List<MutationDetails> timeouts = new ArrayList<>();
  for ( final MutationDetails each : mutations ) {
    // avoid cost of static analysis by first checking mutant is on
    // on instruction that could affect looping
    if (couldCauseInfiniteLoop(method, each) && isInfiniteLoop(each,m) ) {
      timeouts.add(each);
    }
  }
  return timeouts;

}
 
Example #3
Source File: MutantExportInterceptor.java    From pitest with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {

  final List<MutationDetails> indexable = new ArrayList<>(mutations);

  try {
    for (int i = 0; i != indexable.size(); i++) {
      exportMutantDetails(m, indexable, i);
    }
  } catch (final IOException ex) {
    throw new RuntimeException("Error exporting mutants for report", ex);
  }

  return mutations;
}
 
Example #4
Source File: MutationSource.java    From pitest with Apache License 2.0 6 votes vote down vote up
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;
    }
  }
 
Example #5
Source File: StaticInitializerInterceptor.java    From pitest with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  if (this.isStaticInitCode != null) {
    final List<MutationDetails> altered =
        mutations.stream()
        .filter(this.isStaticInitCode)
        .map(setStaticInitializerFlag())
        .collect(Collectors.toList());

    final List<MutationDetails> notAltered =
        FCollection.filter(mutations, Prelude.not(this.isStaticInitCode));

    notAltered.addAll(altered);
    return notAltered;
  }
  return mutations;
}
 
Example #6
Source File: InfiniteLoopFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  final Map<Location,Collection<MutationDetails>> buckets = FCollection.bucket(mutations, mutationToLocation());

  final List<MutationDetails> willTimeout = new ArrayList<>();
  for (final Entry<Location, Collection<MutationDetails>> each : buckets.entrySet() ) {
    willTimeout.addAll(findTimeoutMutants(each.getKey(), each.getValue(), m));
  }
  mutations.removeAll(willTimeout);
  return mutations;
}
 
Example #7
Source File: MutationTestWorker.java    From pitest with Apache License 2.0 5 votes vote down vote up
public MutationTestWorker(
    final F3<ClassName, ClassLoader, byte[], Boolean> hotswap,
    final Mutater mutater, final ClassLoader loader, final boolean fullMutationMatrix) {
  this.loader = loader;
  this.mutater = mutater;
  this.hotswap = hotswap;
  this.fullMutationMatrix = fullMutationMatrix;
}
 
Example #8
Source File: InfiniteLoopFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #9
Source File: EquivalentReturnMutationFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
private Predicate<MutationDetails> isEquivalent(Mutater m) {
  return a -> {
    if (!MUTATOR_IDS.contains(a.getMutator())) {
      return false;
    }
    final MethodTree method = PrimitiveEquivalentFilter.this.currentClass.methods().stream()
        .filter(MethodMatchers.forLocation(a.getId().getLocation()))
        .findFirst()
        .get();
    return ZERO_CONSTANTS.contains(method.realInstructionBefore(a.getInstructionIndex()).getOpcode());
  };
}
 
Example #10
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
 final List<MutationDetails> doNotTouch = FCollection.filter(mutations, inEqualsMethod().negate());
 if (doNotTouch.size() != mutations.size()) {
   final List<MutationDetails> inEquals = FCollection.filter(mutations, inEqualsMethod());
   final List<MutationDetails> filtered = filter(inEquals, m);
   doNotTouch.addAll(filtered);
 }
 return doNotTouch;
}
 
Example #11
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
private List<MutationDetails> filter(
    List<MutationDetails> inEquals, Mutater m) {
  final Location equalsMethod = inEquals.get(0).getId().getLocation();

  final Optional<MethodTree> maybeEquals = this.currentClass.methods().stream()
      .filter(MethodMatchers.forLocation(equalsMethod))
      .findFirst();

  return inEquals.stream()
      .filter(isShortcutEquals(maybeEquals.get(), m).negate())
      .collect(Collectors.toList());
}
 
Example #12
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example #13
Source File: ExcludedAnnotationInterceptor.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  if (this.skipClass) {
    return Collections.emptyList();
  }

  return FCollection.filter(mutations, Prelude.not(this.annotatedMethodMatcher));
}
 
Example #14
Source File: CompoundMutationInterceptor.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  Collection<MutationDetails> modified = mutations;
  for (final MutationInterceptor each : this.children) {
    modified = each.intercept(modified, m);
  }
  return modified;
}
 
Example #15
Source File: LimitNumberOfMutationPerClassFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  if (mutations.size() <= this.maxMutationsPerClass) {
    return mutations;
  } else {
    return createEvenlyDistributedSampling(mutations);
  }
}
 
Example #16
Source File: CompoundMutationInterceptorTest.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldChainModifiedMutantListsThroughChildrenInCorrectOrder() {

  // add out of order
  this.testee = new CompoundMutationInterceptor(Arrays.asList(this.cosmeticChild, this.otherChild, this.modifyChild, this.reportChild, this.filterChild));

  final Collection<MutationDetails> original = aMutationDetail().build(1);
  final Collection<MutationDetails> modifyResult =  aMutationDetail().build(2);
  final Collection<MutationDetails> filterResult =  aMutationDetail().build(3);
  final Collection<MutationDetails> reportResult =  aMutationDetail().build(3);
  final Collection<MutationDetails> cosmeticResult =  aMutationDetail().build(3);
  final Collection<MutationDetails> otherResult =  aMutationDetail().build(3);

  when(this.modifyChild.intercept(any(Collection.class), any(Mutater.class))).thenReturn(modifyResult);
  when(this.filterChild.intercept(any(Collection.class), any(Mutater.class))).thenReturn(filterResult);
  when(this.reportChild.intercept(any(Collection.class), any(Mutater.class))).thenReturn(reportResult);
  when(this.cosmeticChild.intercept(any(Collection.class), any(Mutater.class))).thenReturn(cosmeticResult);
  when(this.otherChild.intercept(any(Collection.class), any(Mutater.class))).thenReturn(otherResult);

  final Collection<MutationDetails> actual = this.testee.intercept(original, this.mutater);

  assertThat(actual).isEqualTo(reportResult);

  verify(this.otherChild).intercept(original,this.mutater);
  verify(this.modifyChild).intercept(otherResult,this.mutater);
  verify(this.filterChild).intercept(modifyResult,this.mutater);
  verify(this.cosmeticChild).intercept(cosmeticResult,this.mutater);
  verify(this.reportChild).intercept(cosmeticResult,this.mutater);
}
 
Example #17
Source File: FilterTester.java    From pitest with Apache License 2.0 5 votes vote down vote up
private Collection<MutationDetails> filter(ClassTree clazz,
    List<MutationDetails> mutations, Mutater mutator) {
  this.testee.begin(clazz);
  final Collection<MutationDetails> actual = this.testee.intercept(mutations, mutator);
  this.testee.end();
  return actual;
}
 
Example #18
Source File: KotlinInterceptor.java    From pitest-kotlin with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  if(!isKotlinClass) {
    return mutations;
  }
  return FCollection.filter(mutations, isKotlinJunkMutation(currentClass).negate());
}
 
Example #19
Source File: EnumConstructorFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
        Collection<MutationDetails> mutations, Mutater m) {
    return mutations.stream()
            .filter(isInEnumConstructor().negate())
            .collect(Collectors.toList());
}
 
Example #20
Source File: StopMethodInterceptor.java    From pitest-descartes with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(Collection<MutationDetails> collection, Mutater mutater) {
    return collection.stream().filter(
            details -> {
                Optional<MethodTree> methodTree = getMethod(details);
                return methodTree.isPresent() && !matcher.matches(getCurrentClass(), methodTree.get());
                })
            .collect(Collectors.toList());
}
 
Example #21
Source File: DescartesEngineFactoryTest.java    From pitest-descartes with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void shouldFindMutatonsAndExclude() {
    DescartesEngineFactory factory = new DescartesEngineFactory();
    MutationEngine engine = factory.createEngine(EngineArguments.arguments().withExcludedMethods(Arrays.asList("get*")));
    Mutater mutater = engine.createMutator(ClassloaderByteArraySource.fromContext());
    List<MutationDetails> mutations = mutater.findMutations(ClassName.fromString("eu.stamp_project.mutationtest.test.input.Calculator"));
    assertEquals(5, mutations.size());
}
 
Example #22
Source File: MutantExportInterceptor.java    From pitest with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #23
Source File: EqualsPerformanceShortcutFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
private Predicate<MutationDetails> isShortcutEquals(final MethodTree tree, final Mutater m) {
  return a -> shortCutEquals(tree,a, m);
}
 
Example #24
Source File: GregorMutationEngine.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public Mutater createMutator(final ClassByteArraySource byteSource) {
  return new GregorMutater(byteSource, this.methodFilter,
      this.mutationOperators);
}
 
Example #25
Source File: AvoidNullInNotNullInterceptor.java    From pitest-descartes with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(Collection<MutationDetails> collection, Mutater mutater) {
    return collection.stream()
            .filter(this::allows)
            .collect(Collectors.toList());
}
 
Example #26
Source File: MutationConfig.java    From pitest with Apache License 2.0 4 votes vote down vote up
public Mutater createMutator(final ClassByteArraySource source) {
  return this.engine.createMutator(source);
}
 
Example #27
Source File: KotlinFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  return FCollection.filter(mutations, Prelude.not(isKotlinJunkMutation()));
}
 
Example #28
Source File: StaticInitializerFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  return FCollection.filter(mutations, Prelude.not(isInStaticInitCode()));
}
 
Example #29
Source File: AvoidForLoopCounterFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  return FCollection.filter(mutations, Prelude.not(mutatesAForLoopCounter()));
}
 
Example #30
Source File: LoggingCallsFilter.java    From pitest with Apache License 2.0 4 votes vote down vote up
@Override
public Collection<MutationDetails> intercept(
    Collection<MutationDetails> mutations, Mutater m) {
  return FCollection.filter(mutations, Prelude.not(isOnLoggingLine()));
}