Skip to content

Commit

Permalink
test: RemoveBadTest (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb authored and monperrus committed Dec 12, 2016
1 parent c85712b commit bab193e
Show file tree
Hide file tree
Showing 22 changed files with 288 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static Random getRandom() {
public static void reset() {
cloneNumber = 1;
ampTestToParent = new HashMap<>();
importByClass = new HashMap<>();
}

public static Map<CtMethod, CtMethod> getAmpTestToParent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public List<CtMethod> apply(CtMethod method) {
try {
newMethods.add(apply(method, list, index));
} catch (Exception e) {
e.printStackTrace();
Log.debug("");
throw new RuntimeException(e);
}
}
}
Expand Down Expand Up @@ -219,23 +218,16 @@ protected boolean isValidCodeFragment(Statement cf) {

protected Map<Statement, Double> buildCodeFragmentFor(CtType cl, Coverage coverage) {
Factory factory = cl.getFactory();
Map<Statement, Double> codeFragments = new IdentityHashMap<>();
Map<Statement, Double> codeFragments = new LinkedHashMap<>();

for(CtMethod<?> mth : (Set<CtMethod>)cl.getAllMethods()) {
for(CtMethod<?> mth : (Set<CtMethod>)cl.getMethods()) {
if(! mth.getModifiers().contains(ModifierKind.ABSTRACT)
&& !mth.getModifiers().contains(ModifierKind.PRIVATE)) {
// && getCoverageForMethod(coverage, cl, mth) != 1.0) {

CtExecutableReference executableRef = factory.Executable().createReference(mth);
CtInvocation invocation;
if (mth.getModifiers().contains(ModifierKind.STATIC)) {
executableRef.setStatic(true);
invocation = factory.Code().createInvocation(null, executableRef);
} else {
executableRef.setStatic(false);
invocation = factory.Code().createInvocation(null, executableRef);
invocation.setTarget(buildVarRef(cl.getReference(), factory));
}
executableRef.setStatic(mth.getModifiers().contains(ModifierKind.STATIC));
CtInvocation invocation = factory.Code().createInvocation(buildVarRef(cl.getReference(), factory), executableRef);
invocation.setArguments(mth.getParameters().stream()
.map(param -> buildVarRef(param.getType(), factory))
.collect(Collectors.toList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -19,6 +18,7 @@
* Time: 09:36
*/
public class RemoveBadTest {

protected InputProgram inputProgram;
protected String mvnHome;

Expand Down Expand Up @@ -54,7 +54,7 @@ public List<CtType> filterTest(Collection<CtType> tests) throws IOException, Int
.collect(Collectors.toList());
}

protected List<String> findFailedTest(Collection<CtType> tests) throws IOException, InterruptedException {
private List<String> findFailedTest(Collection<CtType> tests) throws IOException, InterruptedException {
File testDir = new File(inputProgram.getAbsoluteTestSourceCodeDir());
if(!testDir.exists()) {
testDir.mkdirs();
Expand All @@ -73,4 +73,5 @@ protected List<String> findFailedTest(Collection<CtType> tests) throws IOExcepti

return builder.getFailedTests();
}

}
20 changes: 16 additions & 4 deletions src/test/java/fr/inria/diversify/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import spoon.Launcher;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;

import java.util.Set;

Expand Down Expand Up @@ -40,6 +41,7 @@ public static InputProgram getInputProgram() throws InvalidSdkException, Excepti
}

public static void reset() throws InvalidSdkException, Exception {
applicationClassLoader = null;
inputProgram = null;
compiler = null;
}
Expand All @@ -57,13 +59,21 @@ private static void loadSampleProject() throws Exception, InvalidSdkException {
InitUtils.initDependency(inputConfiguration);

compiler = DSpotUtils.initDiversityCompiler(inputProgram, true);
compiler.getFactory().getEnvironment().setLevel("OFF");

InitUtils.addApplicationClassesToClassPath(inputProgram);
applicationClassLoader = DSpotUtils.initClassLoader(inputProgram, inputConfiguration);
}

public static CtClass findClass(String className) throws InvalidSdkException, Exception {
return getInputProgram().getFactory().Class().get(className);
public static CtClass findClass(String fullQualifiedName) throws InvalidSdkException, Exception {
return getInputProgram().getFactory().Class().get(fullQualifiedName);
}

public static CtMethod findMethod(CtClass<?> ctClass, String methodName) throws InvalidSdkException, Exception {
Set<CtMethod<?>> mths = ctClass.getMethods();
return mths.stream()
.filter(mth -> mth.getSimpleName().endsWith(methodName))
.findFirst()
.orElse(null);
}

public static CtMethod findMethod(String className, String methodName) throws InvalidSdkException, Exception {
Expand All @@ -74,5 +84,7 @@ public static CtMethod findMethod(String className, String methodName) throws In
.orElse(null);
}


public static Factory getFactory() throws InvalidSdkException, Exception {
return getInputProgram().getFactory();
}
}
49 changes: 0 additions & 49 deletions src/test/java/fr/inria/diversify/dspot/Utils.java

This file was deleted.

23 changes: 14 additions & 9 deletions src/test/java/fr/inria/diversify/dspot/amp/StatementAddTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package fr.inria.diversify.dspot.amp;

import fr.inria.diversify.dspot.Utils;
import fr.inria.diversify.Utils;
import fr.inria.diversify.buildSystem.android.InvalidSdkException;
import fr.inria.diversify.dspot.value.ValueFactory;
import fr.inria.diversify.runner.InputProgram;
import fr.inria.diversify.util.FileUtils;
import org.junit.AfterClass;
import org.junit.Test;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.factory.Factory;
Expand All @@ -19,25 +22,27 @@
public class StatementAddTest {

@Test
public void testStatementAdd() throws Exception {
final String packageName = "statementadd";
public void testStatementAdd() throws Exception, InvalidSdkException {
final String packageName = "fr.inria.statementadd";
InputProgram inputProgram = Utils.getInputProgram();

final Factory factory = Utils.getFactory(inputProgram);
final Factory factory = inputProgram.getFactory();
inputProgram.setFactory(factory);
AmplifierHelper.setSeedRandom(23L);
ValueFactory valueFactory = new ValueFactory(inputProgram);
StatementAdd amplificator = new StatementAdd(inputProgram.getFactory(), valueFactory, packageName);
amplificator.reset(null, factory.Class().get(packageName + ".ClassTargetAmplify"));

CtMethod<?> ctMethod = Utils.getCtMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
List<CtMethod> amplifiedMethods = amplificator.apply(ctMethod);

assertEquals(4, amplifiedMethods.size());

amplifiedMethods.forEach(System.out::println);
}


@AfterClass
public static void tearDown() throws InvalidSdkException, Exception {
FileUtils.forceDelete(Utils.getCompiler().getBinaryOutputDirectory());
FileUtils.forceDelete(Utils.getCompiler().getSourceOutputDirectory());
Utils.reset();
}

}
47 changes: 27 additions & 20 deletions src/test/java/fr/inria/diversify/dspot/amp/TestDataMutatorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package fr.inria.diversify.dspot.amp;

import fr.inria.diversify.Utils;
import fr.inria.diversify.buildSystem.android.InvalidSdkException;
import fr.inria.diversify.util.FileUtils;
import org.junit.AfterClass;
import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.code.CtLiteral;
Expand All @@ -15,14 +19,16 @@
import static org.junit.Assert.assertTrue;

/**
* Created by Benjamin DANGLOT (benjamin.danglot@inria.fr) on 11/23/16.
* Created by Benjamin DANGLOT
* benjamin.danglot@inria.fr
* on 11/23/16
*/
public class TestDataMutatorTest {

private static final String SUFFIX_MUTATION = "_literalMutation";

@Test
public void testIntMutation() throws Exception {
public void testIntMutation() throws Exception, InvalidSdkException {

/*
Test the amplification on numbers (integer) literal
Expand All @@ -33,7 +39,7 @@ Test the amplification on numbers (integer) literal
final String nameMethod = "methodInteger";
final int originalValue = 23;
AmplifierHelper.setSeedRandom(42L);
CtClass<Object> literalMutationClass = buildLiteralMutationCtClass();
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Integer> expectedValues = Arrays.asList(22, 24, 46, (23 / 2), 32);
Expand All @@ -51,7 +57,7 @@ Test the amplification on numbers (integer) literal
}

@Test
public void testIntMutationRandom() throws Exception {
public void testIntMutationRandom() throws Exception, InvalidSdkException {

/*
Test the amplification on numbers (integer) literal
Expand All @@ -63,7 +69,7 @@ Test the amplification on numbers (integer) literal
final String nameMethod = "methodInteger";
final int originalValue = 23;
AmplifierHelper.setSeedRandom(42L);
CtClass<Object> literalMutationClass = buildLiteralMutationCtClass();
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Integer> expectedValues = Arrays.asList(22, 24, 46, (23 / 2), 32);
Expand All @@ -87,7 +93,7 @@ Test the amplification on numbers (integer) literal


@Test
public void testDoubleMutation() throws Exception {
public void testDoubleMutation() throws Exception, InvalidSdkException {

/*
Test the amplification on numbers (double) literal
Expand All @@ -97,7 +103,7 @@ Test the amplification on numbers (double) literal
final String nameMethod = "methodDouble";
final double originalValue = 23.0D;
AmplifierHelper.setSeedRandom(42L);
CtClass<Object> literalMutationClass = buildLiteralMutationCtClass();
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Double> expectedValues = Arrays.asList(22.0D, 24.0D, 46.0D, (23.0D / 2.0D), 32.0D);
Expand All @@ -115,7 +121,7 @@ Test the amplification on numbers (double) literal
}

@Test
public void testDoubleMutationRandom() throws Exception {
public void testDoubleMutationRandom() throws Exception, InvalidSdkException {

/*
Test the amplification on numbers (double) literal
Expand All @@ -126,7 +132,7 @@ Test the amplification on numbers (double) literal
final String nameMethod = "methodDouble";
final double originalValue = 23.0D;
AmplifierHelper.setSeedRandom(42L);
CtClass<Object> literalMutationClass = buildLiteralMutationCtClass();
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<Double> expectedValues = Arrays.asList(22.0D, 24.0D, 46.0D, (23.0D / 2.0D), 32.0D);
Expand All @@ -150,7 +156,7 @@ Test the amplification on numbers (double) literal


@Test
public void testStringMutation() throws Exception {
public void testStringMutation() throws Exception, InvalidSdkException {

/*
Test the amplification on string literal
Expand All @@ -161,7 +167,7 @@ public void testStringMutation() throws Exception {
final String nameMethod = "methodString";
final String originalValue = "MyStringLiteral";
AmplifierHelper.setSeedRandom(42L);
CtClass<Object> literalMutationClass = buildLiteralMutationCtClass();
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
TestDataMutator amplifcator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<CtMethod> mutantMethods = amplifcator.apply(method);
Expand All @@ -177,7 +183,7 @@ public void testStringMutation() throws Exception {
}

@Test
public void testStringMutationRandom() throws Exception {
public void testStringMutationRandom() throws Exception, InvalidSdkException {

/*
Test the amplification on string literal
Expand All @@ -189,7 +195,7 @@ public void testStringMutationRandom() throws Exception {
final String nameMethod = "methodString";
final String originalValue = "MyStringLiteral";
AmplifierHelper.setSeedRandom(42L);
CtClass<Object> literalMutationClass = buildLiteralMutationCtClass();
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
TestDataMutator amplificator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);

Expand Down Expand Up @@ -241,7 +247,7 @@ private void assertDistanceBetweenOriginalAndMuted(String original, String mutan
}

@Test
public void testBooleanMutation() throws Exception {
public void testBooleanMutation() throws Exception, InvalidSdkException {

/*
Test the amplification on boolean literal
Expand All @@ -250,7 +256,7 @@ public void testBooleanMutation() throws Exception {
final String nameMethod = "methodBoolean";
final boolean originalValue = true;
AmplifierHelper.setSeedRandom(42L);
CtClass<Object> literalMutationClass = buildLiteralMutationCtClass();
CtClass<Object> literalMutationClass = Utils.getFactory().Class().get("fr.inria.amp.LiteralMutation");
TestDataMutator mutator = getTestDataMutator(literalMutationClass);
CtMethod method = literalMutationClass.getMethod(nameMethod);
List<CtMethod> mutantMethods = mutator.apply(method);
Expand All @@ -268,10 +274,11 @@ private TestDataMutator getTestDataMutator(CtClass<Object> literalMutationClass)
return amplificator;
}

private CtClass<Object> buildLiteralMutationCtClass() {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/amp/LiteralMutation.java");
launcher.buildModel();
return launcher.getFactory().Class().get("amp.LiteralMutation");
@AfterClass
public static void tearDown() throws InvalidSdkException, Exception {
FileUtils.forceDelete(Utils.getCompiler().getBinaryOutputDirectory());
FileUtils.forceDelete(Utils.getCompiler().getSourceOutputDirectory());
Utils.reset();
}

}
Loading

0 comments on commit bab193e

Please sign in to comment.