From 12d9dcda6a5ff31704235f49133b2c26e2bfabd8 Mon Sep 17 00:00:00 2001 From: Henry Coles Date: Wed, 23 Mar 2022 10:51:42 +0000 Subject: [PATCH] migreate BigDecimalMutatorTest --- .../experimental/BigDecimalMutator.java | 2 +- .../mutators/BigDecimalMutatorTest.java | 528 +++++++----------- .../mutants/BiFunctionMutantVerifier.java | 74 +++ .../mutants/CallableMutantVerifier.java | 40 -- .../mutants/DoubleMutantVerifier.java | 30 - .../verifier/mutants/IntMutantVerifier.java | 39 -- .../verifier/mutants/LongMutantVerifier.java | 35 -- .../verifier/mutants/MutantVerifier.java | 40 -- .../verifier/mutants/MutatorVerifier.java | 45 +- .../mutants/MutatorVerifierStart.java | 9 +- 10 files changed, 339 insertions(+), 503 deletions(-) create mode 100644 pitest/src/test/java/org/pitest/verifier/mutants/BiFunctionMutantVerifier.java diff --git a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/mutators/experimental/BigDecimalMutator.java b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/mutators/experimental/BigDecimalMutator.java index bb516a313..96b446dc4 100644 --- a/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/mutators/experimental/BigDecimalMutator.java +++ b/pitest/src/main/java/org/pitest/mutationtest/engine/gregor/mutators/experimental/BigDecimalMutator.java @@ -13,7 +13,7 @@ import java.util.Map; public enum BigDecimalMutator implements MethodMutatorFactory { - INSTANCE; + EXPERIMENTAL_BIG_DECIMAL; @Override public MethodVisitor create(MutationContext context, MethodInfo info, MethodVisitor visitor) { diff --git a/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/mutators/BigDecimalMutatorTest.java b/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/mutators/BigDecimalMutatorTest.java index 1a916d5bc..a9fb5ec6d 100644 --- a/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/mutators/BigDecimalMutatorTest.java +++ b/pitest/src/test/java/org/pitest/mutationtest/engine/gregor/mutators/BigDecimalMutatorTest.java @@ -14,402 +14,304 @@ */ package org.pitest.mutationtest.engine.gregor.mutators; -import org.junit.Before; import org.junit.Test; -import org.pitest.mutationtest.engine.Mutant; -import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.gregor.MutatorTestBase; -import org.pitest.mutationtest.engine.gregor.mutators.experimental.BigDecimalMutator; +import org.pitest.verifier.mutants.MutatorVerifierStart; import java.math.BigDecimal; -import java.util.Collection; -import java.util.concurrent.Callable; import java.util.function.BiFunction; import java.util.function.Function; -public class BigDecimalMutatorTest extends MutatorTestBase { +import static org.pitest.mutationtest.engine.gregor.mutators.experimental.BigDecimalMutator.EXPERIMENTAL_BIG_DECIMAL; - @Before - public void setupEngineToMutateOnlyReturnVals() { - createTesteeWith(BigDecimalMutator.INSTANCE); - } - - @Test - public void add() throws Exception { - final Collection actual = findMutationsFor(Add.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Add(0, 2), mutant, "-2"); - } - - @Test - public void subtract() throws Exception { - final Collection actual = findMutationsFor(Subtract.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Subtract(0, 3), mutant, "3"); - } - - @Test - public void mutliply() throws Exception { - final Collection actual = findMutationsFor(Multiply.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Multiply(2, 2), mutant, "1"); - } - - @Test - public void divide() throws Exception { - final Collection actual = findMutationsFor(Divide.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Divide(2, 2), mutant, "4"); - } - - @Test - public void abs() throws Exception { - final Collection actual = findMutationsFor(Abs.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Abs(-25, 6), mutant, "25"); - assertMutantCallableReturns(new Abs(25, 6), mutant, "-25"); - } - - @Test - public void negate() throws Exception { - final Collection actual = findMutationsFor(Negate.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Negate(0xFFFFFF00), mutant, String.valueOf(0xFFFFFF00)); - assertMutantCallableReturns(new Negate(0), mutant, String.valueOf(0)); - assertMutantCallableReturns(new Negate(0xFF00FF00), mutant, String.valueOf(0xFF00FF00)); - } - - @Test - public void plus() throws Exception { - final Collection actual = findMutationsFor(Plus.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Plus(0xFFFFFF00), mutant, String.valueOf(0x00000100)); - assertMutantCallableReturns(new Plus(0), mutant, String.valueOf(0)); - assertMutantCallableReturns(new Plus(0xFF00FF00), mutant, String.valueOf(0x00FF0100)); - } - - @Test - public void min() throws Exception { - final Collection actual = findMutationsFor(Min.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Min(-25, 6), mutant, "6"); - assertMutantCallableReturns(new Min(25, 6), mutant, "25"); - } - - @Test - public void max() throws Exception { - final Collection actual = findMutationsFor(Max.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new Max(-25, 6), mutant, "-25"); - assertMutantCallableReturns(new Max(25, 6), mutant, "6"); - } - - @Test - public void addLambda() throws Exception { - final Collection actual = findMutationsFor(AddLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new AddLambda(0, 2), mutant, "-2"); - } - - @Test - public void subtractLambda() throws Exception { - final Collection actual = findMutationsFor(SubtractLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new SubtractLambda(0, 3), mutant, "3"); - } - - @Test - public void mutliplyLambda() throws Exception { - final Collection actual = findMutationsFor(MultiplyLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new MultiplyLambda(2, 2), mutant, "1"); - } - - @Test - public void divideLambda() throws Exception { - final Collection actual = findMutationsFor(DivideLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new DivideLambda(2, 2), mutant, "4"); - } - - @Test - public void absLambda() throws Exception { - final Collection actual = findMutationsFor(AbsLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new AbsLambda(-25, 6), mutant, "25"); - assertMutantCallableReturns(new AbsLambda(25, 6), mutant, "-25"); - } - - @Test - public void negateLambda() throws Exception { - final Collection actual = findMutationsFor(NegateLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new NegateLambda(0xFFFFFF00), mutant, String.valueOf(0xFFFFFF00)); - assertMutantCallableReturns(new NegateLambda(0), mutant, String.valueOf(0)); - assertMutantCallableReturns(new NegateLambda(0xFF00FF00), mutant, String.valueOf(0xFF00FF00)); - } - - @Test - public void plusLambda() throws Exception { - final Collection actual = findMutationsFor(PlusLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new PlusLambda(0xFFFFFF00), mutant, String.valueOf(0x00000100)); - assertMutantCallableReturns(new PlusLambda(0), mutant, String.valueOf(0)); - assertMutantCallableReturns(new PlusLambda(0xFF00FF00), mutant, String.valueOf(0x00FF0100)); - } +public class BigDecimalMutatorTest extends MutatorTestBase { - @Test - public void minLambda() throws Exception { - final Collection actual = findMutationsFor(MinLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new MinLambda(-25, 6), mutant, "6"); - assertMutantCallableReturns(new MinLambda(25, 6), mutant, "25"); - } + MutatorVerifierStart v = MutatorVerifierStart.forMutator(EXPERIMENTAL_BIG_DECIMAL); - @Test - public void maxLambda() throws Exception { - final Collection actual = findMutationsFor(MaxLambda.class); - final Mutant mutant = getFirstMutant(actual); - assertMutantCallableReturns(new MaxLambda(-25, 6), mutant, "-25"); - assertMutantCallableReturns(new MaxLambda(25, 6), mutant, "6"); - } + @Test + public void add() { + v.forBiFunctionClass(Add.class) + .firstMutantShouldReturn(0L, 2L, "-2"); + } - private static abstract class AbstractMath implements Callable { + @Test + public void subtract() { + v.forBiFunctionClass(Subtract.class) + .firstMutantShouldReturn(0L, 3L, "3"); + } - private final BigDecimal value1; - private final BigDecimal value2; + @Test + public void multiply() { + v.forBiFunctionClass(Multiply.class) + .firstMutantShouldReturn(2L, 2L, "1"); + } - AbstractMath(long v1, long v2) { - this.value1 = BigDecimal.valueOf(v1); - this.value2 = BigDecimal.valueOf(v2); + @Test + public void divide() { + v.forBiFunctionClass(Divide.class) + .firstMutantShouldReturn(2L, 2L, "4"); } - abstract BigDecimal apply(BigDecimal left, BigDecimal right); + @Test + public void abs() { + v.forBiFunctionClass(Abs.class) + .firstMutantShouldReturn(-25L, 6L, "25"); - @Override - public String call() throws Exception { - return String.valueOf(apply(value1, value2)); + v.forBiFunctionClass(Abs.class) + .firstMutantShouldReturn(25L, 6L, "-25"); } - } - private static class Add extends AbstractMath { + @Test + public void negate() { + v.forBiFunctionClass(Negate.class) + .firstMutantShouldReturn(0xFFFFFF00L, 0l, String.valueOf(0xFFFFFF00L)); + v.forBiFunctionClass(Negate.class) + .firstMutantShouldReturn(0L, 0L, String.valueOf(0L)); + v.forBiFunctionClass(Negate.class) + .firstMutantShouldReturn(0xFF00FF00L, 0l, String.valueOf(0xFF00FF00L)); - Add(long v1, long v2) { - super(v1, v2); } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.add(right); + @Test + public void plus() { + v.forBiFunctionClass(Plus.class) + .firstMutantShouldReturn(BigDecimal.valueOf(0xFFFFFF00), null, BigDecimal.valueOf(0x00000100)); + v.forBiFunctionClass(Plus.class) + .firstMutantShouldReturn(BigDecimal.ZERO, null, BigDecimal.ZERO); + v.forBiFunctionClass(Plus.class) + .firstMutantShouldReturn(BigDecimal.valueOf(0xFF00FF00), null, BigDecimal.valueOf(0x00FF0100)); } - } - - private static class Subtract extends AbstractMath { - Subtract(long v1, long v2) { - super(v1, v2); - } + @Test + public void min() { + v.forBiFunctionClass(Min.class) + .firstMutantShouldReturn(-25L, 6L, "6"); + v.forBiFunctionClass(Min.class) + .firstMutantShouldReturn(25L, 6L, "25"); - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.subtract(right); } - } - private static class Divide extends AbstractMath { + @Test + public void max() { + v.forBiFunctionClass(Max.class) + .firstMutantShouldReturn(-25L, 6L, "-25"); - Divide(long v1, long v2) { - super(v1, v2); + v.forBiFunctionClass(Max.class) + .firstMutantShouldReturn(25L, 6L, "6"); } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.divide(right); + @Test + public void addLambda() { + v.forBiFunctionClass(AddLambda.class) + .firstMutantShouldReturn(0L, 2L, "-2"); } - } - private static class Multiply extends AbstractMath { - - Multiply(long v1, long v2) { - super(v1, v2); + @Test + public void subtractLambda() { + v.forBiFunctionClass(SubtractLambda.class) + .firstMutantShouldReturn(0L, 3L, "3"); } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.multiply(right); + @Test + public void mutliplyLambda() { + v.forBiFunctionClass(MultiplyLambda.class) + .firstMutantShouldReturn(2L, 2L, "1"); } - } - - private static class Max extends AbstractMath { - Max(long v1, long v2) { - super(v1, v2); + @Test + public void divideLambda() { + v.forBiFunctionClass(DivideLambda.class) + .firstMutantShouldReturn(2L, 2L, "4"); } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.max(right); - } - } + @Test + public void absLambda() { + v.forBiFunctionClass(AbsLambda.class) + .firstMutantShouldReturn(-25L, 6L, "25"); - private static class Min extends AbstractMath { + v.forBiFunctionClass(AbsLambda.class) + .firstMutantShouldReturn(25L, 6L, "-25"); + } - Min(long v1, long v2) { - super(v1, v2); + @Test + public void negateLambda() { + v.forBiFunctionClass(NegateLambda.class) + .firstMutantShouldReturn(0xFFFFFF00L, 0L, String.valueOf(0xFFFFFF00L)); + v.forBiFunctionClass(NegateLambda.class) + .firstMutantShouldReturn(0L, 0l, String.valueOf(0L)); + v.forBiFunctionClass(NegateLambda.class) + .firstMutantShouldReturn(0xFF00FF00L, 0L, String.valueOf(0xFF00FF00L)); } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.min(right); + @Test + public void plusLambda() { + v.forBiFunctionClass(PlusLambda.class) + .firstMutantShouldReturn(BigDecimal.valueOf(0xFFFFFF00), null, BigDecimal.valueOf(0x00000100)); + v.forBiFunctionClass(PlusLambda.class) + .firstMutantShouldReturn(BigDecimal.ZERO, null, BigDecimal.valueOf(0L)); + v.forBiFunctionClass(PlusLambda.class) + .firstMutantShouldReturn(BigDecimal.valueOf(0xFF00FF00), null, BigDecimal.valueOf(0x00FF0100)); } - } - private static class Negate extends AbstractMath { + @Test + public void minLambda() { + v.forBiFunctionClass(MinLambda.class) + .firstMutantShouldReturn(-25L, 6L, "6"); - Negate(long v1) { - super(v1, 0L); + v.forBiFunctionClass(MinLambda.class) + .firstMutantShouldReturn(25L, 6L, "25"); } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.negate(); + @Test + public void maxLambda() { + v.forBiFunctionClass(MaxLambda.class) + .firstMutantShouldReturn(-25L, 6L, "-25"); + + v.forBiFunctionClass(MaxLambda.class) + .firstMutantShouldReturn(25L, 6L, "6"); } - } - private static class Plus extends AbstractMath { + private static abstract class AbstractMath implements BiFunction { + public String apply(Long left, Long right) { + return String.valueOf(apply(BigDecimal.valueOf(left), BigDecimal.valueOf(right))); + } - Plus(long v1) { - super(v1, 0L); + abstract BigDecimal apply(BigDecimal left, BigDecimal right); } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.plus(); + private static class Add extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.add(right); + } } - } - - private static class Abs extends AbstractMath { - Abs(long v1, long v2) { - super(v1, v2); + private static class Subtract extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.subtract(right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - return left.abs(); + private static class Divide extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.divide(right); + } } - } - - private static class AddLambda extends AbstractMath { - AddLambda(long v1, long v2) { - super(v1, v2); + private static class Multiply extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.multiply(right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - BiFunction function = BigDecimal::add; - return function.apply(left, right); } - } - - private static class SubtractLambda extends AbstractMath { - - SubtractLambda(long v1, long v2) { - super(v1, v2); + private static class Max extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.max(right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - BiFunction function = BigDecimal::subtract; - return function.apply(left, right); } - } - - private static class DivideLambda extends AbstractMath { - - DivideLambda(long v1, long v2) { - super(v1, v2); + private static class Min extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.min(right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - BiFunction function = BigDecimal::divide; - return function.apply(left, right); + private static class Negate extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.negate(); + } } - } - private static class MultiplyLambda extends AbstractMath { - - MultiplyLambda(long v1, long v2) { - super(v1, v2); + private static class Plus implements BiFunction { + @Override + public BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.plus(); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - BiFunction function = BigDecimal::multiply; - return function.apply(left, right); + private static class Abs extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + return left.abs(); + } } - } - - private static class MaxLambda extends AbstractMath { - MaxLambda(long v1, long v2) { - super(v1, v2); + private static class AddLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + BiFunction function = BigDecimal::add; + return function.apply(left, right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - BiFunction function = BigDecimal::max; - return function.apply(left, right); } - } - - private static class MinLambda extends AbstractMath { - - MinLambda(long v1, long v2) { - super(v1, v2); + private static class SubtractLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + BiFunction function = BigDecimal::subtract; + return function.apply(left, right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - BiFunction function = BigDecimal::min; - return function.apply(left, right); } - } - - private static class NegateLambda extends AbstractMath { - - NegateLambda(long v1) { - super(v1, 0L); + private static class DivideLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + BiFunction function = BigDecimal::divide; + return function.apply(left, right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - Function function = BigDecimal::negate; - return function.apply(left); + private static class MultiplyLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + BiFunction function = BigDecimal::multiply; + return function.apply(left, right); + } } - } - private static class PlusLambda extends AbstractMath { - - PlusLambda(long v1) { - super(v1, 0L); + private static class MaxLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + BiFunction function = BigDecimal::max; + return function.apply(left, right); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - Function function = BigDecimal::plus; - return function.apply(left); + private static class MinLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + BiFunction function = BigDecimal::min; + return function.apply(left, right); + } } - } - private static class AbsLambda extends AbstractMath { + private static class NegateLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + Function function = BigDecimal::negate; + return function.apply(left); + } + } - AbsLambda(long v1, long v2) { - super(v1, v2); + private static class PlusLambda implements BiFunction { + @Override + public BigDecimal apply(BigDecimal left, BigDecimal right) { + Function function = BigDecimal::plus; + return function.apply(left); + } } - @Override - BigDecimal apply(BigDecimal left, BigDecimal right) { - Function function = BigDecimal::abs; - return function.apply(left); } - } + private static class AbsLambda extends AbstractMath { + @Override + BigDecimal apply(BigDecimal left, BigDecimal right) { + Function function = BigDecimal::abs; + return function.apply(left); + } + } } diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/BiFunctionMutantVerifier.java b/pitest/src/test/java/org/pitest/verifier/mutants/BiFunctionMutantVerifier.java new file mode 100644 index 000000000..6b78ae780 --- /dev/null +++ b/pitest/src/test/java/org/pitest/verifier/mutants/BiFunctionMutantVerifier.java @@ -0,0 +1,74 @@ +package org.pitest.verifier.mutants; + +import org.pitest.mutationtest.engine.Mutant; +import org.pitest.mutationtest.engine.MutationDetails; +import org.pitest.mutationtest.engine.gregor.GregorMutater; +import org.pitest.util.Unchecked; + +import java.lang.reflect.Constructor; +import java.util.Collection; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Predicate; +import java.util.function.Supplier; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BiFunctionMutantVerifier extends MutatorVerifier { + + private final GregorMutater engine; + private final Class> target; + + public BiFunctionMutantVerifier(GregorMutater engine, Class> target, Predicate filter) { + super(engine, target, filter); + this.engine = engine; + this.target = target; + } + + public void firstMutantShouldReturn(A a, B b, C expected) { + firstMutantShouldReturn( () -> a, () -> b, expected); + } + + /** + * Suppliers allow consumable inputs (eg streams) can be reused + */ + public void firstMutantShouldReturn(Supplier a, Supplier b, C expected) { + List mutations = findMutations(); + assertThat(mutateAndCall(a.get(), b.get(), getFirstMutant(mutations))) + .isEqualTo(expected); + } + + + private C runWithoutMutation(A a, B b) { + return this.runInClassLoader(target.getClassLoader(), a, b); + } + + private C mutateAndCall(A a, B b, Mutant mutant) { + ClassLoader loader = this.createClassLoader(mutant); + return this.runInClassLoader(loader, a, b); + } + + private C runInClassLoader(ClassLoader loader, A a, B b) { + try { + Class forLoader = loader.loadClass(target.getName()); + + Constructor c = forLoader.getDeclaredConstructor(); + c.setAccessible(true); + BiFunction instance = (BiFunction) c.newInstance(); + return instance.apply(a, b); + } catch (ReflectiveOperationException ex) { + throw Unchecked.translateCheckedException(ex); + } + } + + protected Mutant getFirstMutant(final Collection actual) { + assertThat(actual) + .describedAs("Expecting at least one mutant to be generated") + .isNotEmpty(); + final Mutant mutant = this.engine.getMutation(actual.iterator().next() + .getId()); + verifyMutant(mutant); + return mutant; + } + +} diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/CallableMutantVerifier.java b/pitest/src/test/java/org/pitest/verifier/mutants/CallableMutantVerifier.java index 1d04ddced..88ed20574 100644 --- a/pitest/src/test/java/org/pitest/verifier/mutants/CallableMutantVerifier.java +++ b/pitest/src/test/java/org/pitest/verifier/mutants/CallableMutantVerifier.java @@ -1,21 +1,10 @@ package org.pitest.verifier.mutants; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.util.ASMifier; -import org.objectweb.asm.util.CheckClassAdapter; -import org.objectweb.asm.util.TraceClassVisitor; -import org.pitest.classpath.ClassPath; import org.pitest.mutationtest.engine.Mutant; import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.gregor.GregorMutater; -import org.pitest.simpletest.ExcludedPrefixIsolationStrategy; -import org.pitest.simpletest.Transformation; -import org.pitest.simpletest.TransformingClassLoader; import org.pitest.util.Unchecked; -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.List; @@ -69,17 +58,6 @@ private B mutateAndCall(Mutant mutant) { return this.runInClassLoader(loader); } - private ClassLoader createClassLoader(Mutant mutant) { - return new TransformingClassLoader(new ClassPath(), - this.createTransformation(mutant), - new ExcludedPrefixIsolationStrategy(new String[0]), - Object.class.getClassLoader()); - } - - private Transformation createTransformation(Mutant mutant) { - return (name, bytes) -> name.equals(mutant.getDetails().getClassName().asJavaName()) ? mutant.getBytes() : bytes; - } - private B runInClassLoader(ClassLoader loader) { try { Class forLoader = loader.loadClass(target.getName()); @@ -105,22 +83,4 @@ protected Mutant getFirstMutant(final Collection actual) { return mutant; } - private void verifyMutant(Mutant mutant) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - CheckClassAdapter.verify(new ClassReader(mutant.getBytes()), false, pw); - assertThat(sw.toString()) - .describedAs("Mutant is not a valid class") - .isEmpty(); - } - - private String printMutant(Mutant mutant) { - ClassReader reader = new ClassReader(mutant.getBytes()); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - reader.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(bos)), 8); - return bos.toString(); - } - - - } diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/DoubleMutantVerifier.java b/pitest/src/test/java/org/pitest/verifier/mutants/DoubleMutantVerifier.java index a7c2c9cad..af3759c2b 100644 --- a/pitest/src/test/java/org/pitest/verifier/mutants/DoubleMutantVerifier.java +++ b/pitest/src/test/java/org/pitest/verifier/mutants/DoubleMutantVerifier.java @@ -1,25 +1,15 @@ package org.pitest.verifier.mutants; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.util.CheckClassAdapter; -import org.pitest.classpath.ClassPath; import org.pitest.mutationtest.engine.Mutant; import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.gregor.GregorMutater; -import org.pitest.simpletest.ExcludedPrefixIsolationStrategy; -import org.pitest.simpletest.Transformation; -import org.pitest.simpletest.TransformingClassLoader; import org.pitest.util.Unchecked; -import java.io.PrintWriter; -import java.io.StringWriter; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.List; import java.util.function.DoubleFunction; import java.util.function.DoubleSupplier; -import java.util.function.LongFunction; -import java.util.function.LongSupplier; import java.util.function.Predicate; import static org.assertj.core.api.Assertions.assertThat; @@ -63,17 +53,6 @@ private B mutateAndCall(double input, Mutant mutant) { return this.runInClassLoader(loader, input); } - private ClassLoader createClassLoader(Mutant mutant) { - return new TransformingClassLoader(new ClassPath(), - this.createTransformation(mutant), - new ExcludedPrefixIsolationStrategy(new String[0]), - Object.class.getClassLoader()); - } - - private Transformation createTransformation(Mutant mutant) { - return (name, bytes) -> name.equals(mutant.getDetails().getClassName().asJavaName()) ? mutant.getBytes() : bytes; - } - private B runInClassLoader(ClassLoader loader, double input) { try { Class forLoader = loader.loadClass(target.getName()); @@ -97,13 +76,4 @@ protected Mutant getFirstMutant(final Collection actual) { return mutant; } - private void verifyMutant(Mutant mutant) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - CheckClassAdapter.verify(new ClassReader(mutant.getBytes()), false, pw); - assertThat(sw.toString()) - .describedAs("Mutant is not a valid class") - .isEmpty(); - } - } diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/IntMutantVerifier.java b/pitest/src/test/java/org/pitest/verifier/mutants/IntMutantVerifier.java index 1166afa36..ebf6107db 100644 --- a/pitest/src/test/java/org/pitest/verifier/mutants/IntMutantVerifier.java +++ b/pitest/src/test/java/org/pitest/verifier/mutants/IntMutantVerifier.java @@ -1,29 +1,16 @@ package org.pitest.verifier.mutants; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.util.ASMifier; -import org.objectweb.asm.util.CheckClassAdapter; -import org.objectweb.asm.util.TraceClassVisitor; -import org.pitest.classpath.ClassPath; import org.pitest.mutationtest.engine.Mutant; import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.gregor.GregorMutater; -import org.pitest.simpletest.ExcludedPrefixIsolationStrategy; -import org.pitest.simpletest.Transformation; -import org.pitest.simpletest.TransformingClassLoader; import org.pitest.util.Unchecked; -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.List; -import java.util.function.Function; import java.util.function.IntFunction; import java.util.function.IntSupplier; import java.util.function.Predicate; -import java.util.function.Supplier; import static org.assertj.core.api.Assertions.assertThat; @@ -66,17 +53,6 @@ private B mutateAndCall(int input, Mutant mutant) { return this.runInClassLoader(loader, input); } - private ClassLoader createClassLoader(Mutant mutant) { - return new TransformingClassLoader(new ClassPath(), - this.createTransformation(mutant), - new ExcludedPrefixIsolationStrategy(new String[0]), - Object.class.getClassLoader()); - } - - private Transformation createTransformation(Mutant mutant) { - return (name, bytes) -> name.equals(mutant.getDetails().getClassName().asJavaName()) ? mutant.getBytes() : bytes; - } - private B runInClassLoader(ClassLoader loader, int input) { try { Class forLoader = loader.loadClass(target.getName()); @@ -100,19 +76,4 @@ protected Mutant getFirstMutant(final Collection actual) { return mutant; } - private void verifyMutant(Mutant mutant) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - CheckClassAdapter.verify(new ClassReader(mutant.getBytes()), false, pw); - assertThat(sw.toString()) - .describedAs("Mutant is not a valid class") - .isEmpty(); - } - - private String printMutant(Mutant mutant) { - ClassReader reader = new ClassReader(mutant.getBytes()); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - reader.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(bos)), 8); - return bos.toString(); - } } diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/LongMutantVerifier.java b/pitest/src/test/java/org/pitest/verifier/mutants/LongMutantVerifier.java index 6337316c3..345b50e13 100644 --- a/pitest/src/test/java/org/pitest/verifier/mutants/LongMutantVerifier.java +++ b/pitest/src/test/java/org/pitest/verifier/mutants/LongMutantVerifier.java @@ -1,31 +1,16 @@ package org.pitest.verifier.mutants; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.util.ASMifier; -import org.objectweb.asm.util.CheckClassAdapter; -import org.objectweb.asm.util.TraceClassVisitor; -import org.pitest.classpath.ClassPath; import org.pitest.mutationtest.engine.Mutant; import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.gregor.GregorMutater; -import org.pitest.simpletest.ExcludedPrefixIsolationStrategy; -import org.pitest.simpletest.Transformation; -import org.pitest.simpletest.TransformingClassLoader; import org.pitest.util.Unchecked; -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.List; -import java.util.function.Function; -import java.util.function.IntFunction; -import java.util.function.IntSupplier; import java.util.function.LongFunction; import java.util.function.LongSupplier; import java.util.function.Predicate; -import java.util.function.Supplier; import static org.assertj.core.api.Assertions.assertThat; @@ -68,17 +53,6 @@ private B mutateAndCall(long input, Mutant mutant) { return this.runInClassLoader(loader, input); } - private ClassLoader createClassLoader(Mutant mutant) { - return new TransformingClassLoader(new ClassPath(), - this.createTransformation(mutant), - new ExcludedPrefixIsolationStrategy(new String[0]), - Object.class.getClassLoader()); - } - - private Transformation createTransformation(Mutant mutant) { - return (name, bytes) -> name.equals(mutant.getDetails().getClassName().asJavaName()) ? mutant.getBytes() : bytes; - } - private B runInClassLoader(ClassLoader loader, long input) { try { Class forLoader = loader.loadClass(target.getName()); @@ -102,13 +76,4 @@ protected Mutant getFirstMutant(final Collection actual) { return mutant; } - private void verifyMutant(Mutant mutant) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - CheckClassAdapter.verify(new ClassReader(mutant.getBytes()), false, pw); - assertThat(sw.toString()) - .describedAs("Mutant is not a valid class") - .isEmpty(); - } - } diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/MutantVerifier.java b/pitest/src/test/java/org/pitest/verifier/mutants/MutantVerifier.java index cb5549dab..dc7042935 100644 --- a/pitest/src/test/java/org/pitest/verifier/mutants/MutantVerifier.java +++ b/pitest/src/test/java/org/pitest/verifier/mutants/MutantVerifier.java @@ -1,21 +1,10 @@ package org.pitest.verifier.mutants; -import org.objectweb.asm.ClassReader; -import org.objectweb.asm.util.ASMifier; -import org.objectweb.asm.util.CheckClassAdapter; -import org.objectweb.asm.util.TraceClassVisitor; -import org.pitest.classpath.ClassPath; import org.pitest.mutationtest.engine.Mutant; import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.gregor.GregorMutater; -import org.pitest.simpletest.ExcludedPrefixIsolationStrategy; -import org.pitest.simpletest.Transformation; -import org.pitest.simpletest.TransformingClassLoader; import org.pitest.util.Unchecked; -import java.io.ByteArrayOutputStream; -import java.io.PrintWriter; -import java.io.StringWriter; import java.lang.reflect.Constructor; import java.util.Collection; import java.util.List; @@ -64,17 +53,6 @@ private B mutateAndCall(A input, Mutant mutant) { return this.runInClassLoader(loader, input); } - private ClassLoader createClassLoader(Mutant mutant) { - return new TransformingClassLoader(new ClassPath(), - this.createTransformation(mutant), - new ExcludedPrefixIsolationStrategy(new String[0]), - Object.class.getClassLoader()); - } - - private Transformation createTransformation(Mutant mutant) { - return (name, bytes) -> name.equals(mutant.getDetails().getClassName().asJavaName()) ? mutant.getBytes() : bytes; - } - private B runInClassLoader(ClassLoader loader, A input) { try { Class forLoader = loader.loadClass(target.getName()); @@ -98,22 +76,4 @@ protected Mutant getFirstMutant(final Collection actual) { return mutant; } - private void verifyMutant(Mutant mutant) { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - CheckClassAdapter.verify(new ClassReader(mutant.getBytes()), false, pw); - assertThat(sw.toString()) - .describedAs("Mutant is not a valid class") - .isEmpty(); - } - - private String printMutant(Mutant mutant) { - ClassReader reader = new ClassReader(mutant.getBytes()); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - reader.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(bos)), 8); - return bos.toString(); - } - - - } diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifier.java b/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifier.java index c0e8df3e9..3b95f20ca 100644 --- a/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifier.java +++ b/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifier.java @@ -1,10 +1,22 @@ package org.pitest.verifier.mutants; import org.assertj.core.api.StringAssert; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.util.ASMifier; +import org.objectweb.asm.util.CheckClassAdapter; +import org.objectweb.asm.util.TraceClassVisitor; import org.pitest.classinfo.ClassName; +import org.pitest.classpath.ClassPath; +import org.pitest.mutationtest.engine.Mutant; import org.pitest.mutationtest.engine.MutationDetails; import org.pitest.mutationtest.engine.gregor.GregorMutater; +import org.pitest.simpletest.ExcludedPrefixIsolationStrategy; +import org.pitest.simpletest.Transformation; +import org.pitest.simpletest.TransformingClassLoader; +import java.io.ByteArrayOutputStream; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -34,22 +46,49 @@ public void noMutantsCreated() { assertThat(findMutations()).isEmpty(); } - public StringAssert firstMutantIsDescription() { + public final StringAssert firstMutantIsDescription() { return new StringAssert(firstMutant().getDescription()); } - List findMutations() { + public final List findMutations() { return this.engine.findMutations(ClassName.fromClass(clazz)).stream() .filter(filter) .collect(Collectors.toList()); } - public MutationDetails firstMutant() { + public final MutationDetails firstMutant() { List mutants = findMutations(); assertThat(mutants) .describedAs("No mutations created") .isNotEmpty(); return mutants.get(0); } + + protected void verifyMutant(Mutant mutant) { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + CheckClassAdapter.verify(new ClassReader(mutant.getBytes()), false, pw); + assertThat(sw.toString()) + .describedAs("Mutant is not a valid class") + .isEmpty(); + } + + protected ClassLoader createClassLoader(Mutant mutant) { + return new TransformingClassLoader(new ClassPath(), + this.createTransformation(mutant), + new ExcludedPrefixIsolationStrategy(new String[0]), + Object.class.getClassLoader()); + } + + private Transformation createTransformation(Mutant mutant) { + return (name, bytes) -> name.equals(mutant.getDetails().getClassName().asJavaName()) ? mutant.getBytes() : bytes; + } + + protected String printMutant(Mutant mutant) { + ClassReader reader = new ClassReader(mutant.getBytes()); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + reader.accept(new TraceClassVisitor(null, new ASMifier(), new PrintWriter(bos)), 8); + return bos.toString(); + } } diff --git a/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifierStart.java b/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifierStart.java index 8c7424165..e72db041e 100644 --- a/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifierStart.java +++ b/pitest/src/test/java/org/pitest/verifier/mutants/MutatorVerifierStart.java @@ -5,10 +5,10 @@ import org.pitest.mutationtest.engine.gregor.GregorMutater; import org.pitest.mutationtest.engine.gregor.MethodInfo; import org.pitest.mutationtest.engine.gregor.MethodMutatorFactory; -import org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutatorTest; import java.util.Arrays; import java.util.concurrent.Callable; +import java.util.function.BiFunction; import java.util.function.DoubleFunction; import java.util.function.Function; import java.util.function.IntFunction; @@ -69,9 +69,14 @@ public DoubleMutantVerifier forDoubleFunctionClass(Class(engine, clazz, mutantFilter); } + + public BiFunctionMutantVerifier forBiFunctionClass(Class> clazz) { + GregorMutater engine = makeEngine(); + return new BiFunctionMutantVerifier<>(engine, clazz, mutantFilter); + } + private GregorMutater makeEngine() { return new GregorMutater(ClassloaderByteArraySource.fromContext(), filter, Arrays.asList(new NullMutator(), mmf, new NullMutator())); } - }