Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support target_method functioning for MutationFactory #435

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@

import org.evosuite.Properties;
import org.evosuite.TestGenerationContext;
import org.evosuite.coverage.MethodNameMatcher;
import org.evosuite.instrumentation.mutation.InsertUnaryOperator;
import org.evosuite.instrumentation.mutation.ReplaceArithmeticOperator;
import org.evosuite.instrumentation.mutation.ReplaceConstant;
import org.evosuite.instrumentation.mutation.ReplaceVariable;
import org.evosuite.rmi.ClientServices;
import org.evosuite.statistics.RuntimeVariable;
import org.evosuite.testsuite.AbstractFitnessFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -46,6 +49,12 @@ public class MutationFactory extends AbstractFitnessFactory<MutationTestFitness>

protected List<MutationTestFitness> goals = null;

private static final Logger logger = LoggerFactory.getLogger(MutationFactory.class);


private final MethodNameMatcher matcher = new MethodNameMatcher();


/**
* <p>
* Constructor for MutationFactory.
Expand Down Expand Up @@ -95,6 +104,12 @@ public List<MutationTestFitness> getCoverageGoals(String targetMethod) {
if (targetMethod != null && !m.getMethodName().endsWith(targetMethod))
continue;

String methodName = m.getMethodName();
if(!matcher.methodMatches(methodName)) {
logger.info("Method {} does not match criteria. ", methodName);
continue;
}

// We need to return all mutants to make coverage values and bitstrings consistent
//if (MutationTimeoutStoppingCondition.isDisabled(m))
// continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package org.evosuite.basic;

import com.examples.with.different.packagename.TargetMethod;
import org.evosuite.EvoSuite;
import org.evosuite.Properties;
import org.evosuite.SystemTestBase;
import org.evosuite.ga.metaheuristics.GeneticAlgorithm;
import org.evosuite.testsuite.TestSuiteChromosome;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;

public class TargetMethodListSystemTest extends SystemTestBase {

@Test
public void testTargetMethodWithLineCoverage() {
EvoSuite evosuite = new EvoSuite();

String targetClass = TargetMethod.class.getCanonicalName();
String targetMethod = "foo(Ljava/lang/Integer;)Z";
Properties.TARGET_CLASS = targetClass;
Properties.TARGET_METHOD_LIST = targetMethod;
Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.BRANCH, Properties.Criterion.LINE};
String[] command = new String[]{"-generateMOSuite", "-class", targetClass};

Object result = evosuite.parseCommandLine(command);

GeneticAlgorithm<TestSuiteChromosome> ga = getGAFromResult(result);
TestSuiteChromosome best = ga.getBestIndividual();
Assertions.assertFalse(best.toString().contains("bar"));
}

@Test
public void testTargetMethodWithWeakMutation() {
EvoSuite evosuite = new EvoSuite();

String targetClass = TargetMethod.class.getCanonicalName();
String targetMethod = "foo(Ljava/lang/Integer;)Z";
Properties.TARGET_CLASS = targetClass;
Properties.TARGET_METHOD_LIST = targetMethod;
Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.BRANCH, Properties.Criterion.WEAKMUTATION};
String[] command = new String[]{"-generateMOSuite", "-class", targetClass};

Object result = evosuite.parseCommandLine(command);

GeneticAlgorithm<TestSuiteChromosome> ga = getGAFromResult(result);
TestSuiteChromosome best = ga.getBestIndividual();
Assertions.assertFalse(best.toString().contains("bar"));
}

@Test
public void testTargetMethodWithMethod() {
EvoSuite evosuite = new EvoSuite();

String targetClass = TargetMethod.class.getCanonicalName();
String targetMethod = "foo(Ljava/lang/Integer;)Z";
Properties.TARGET_CLASS = targetClass;
Properties.TARGET_METHOD_LIST = targetMethod;
Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.BRANCH, Properties.Criterion.METHOD};
String[] command = new String[]{"-generateMOSuite", "-class", targetClass};

Object result = evosuite.parseCommandLine(command);

GeneticAlgorithm<TestSuiteChromosome> ga = getGAFromResult(result);
TestSuiteChromosome best = ga.getBestIndividual();
Assertions.assertFalse(best.toString().contains("bar"));
}

@Test
public void testTargetMethodWithException() {
EvoSuite evosuite = new EvoSuite();

String targetClass = TargetMethod.class.getCanonicalName();
String targetMethod = "foo(Ljava/lang/Integer;)Z";
Properties.TARGET_CLASS = targetClass;
Properties.TARGET_METHOD_LIST = targetMethod;
Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.BRANCH, Properties.Criterion.EXCEPTION};
String[] command = new String[]{"-generateMOSuite", "-class", targetClass};

Object result = evosuite.parseCommandLine(command);

GeneticAlgorithm<TestSuiteChromosome> ga = getGAFromResult(result);
TestSuiteChromosome best = ga.getBestIndividual();
Assertions.assertFalse(best.toString().contains("bar"));
}

@Test
public void testTargetMethodWithMethodNoException() {
EvoSuite evosuite = new EvoSuite();

String targetClass = TargetMethod.class.getCanonicalName();
String targetMethod = "foo(Ljava/lang/Integer;)Z";
Properties.TARGET_CLASS = targetClass;
Properties.TARGET_METHOD_LIST = targetMethod;
Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.BRANCH, Properties.Criterion.METHODNOEXCEPTION};
String[] command = new String[]{"-generateMOSuite", "-class", targetClass};

Object result = evosuite.parseCommandLine(command);

GeneticAlgorithm<TestSuiteChromosome> ga = getGAFromResult(result);
TestSuiteChromosome best = ga.getBestIndividual();
Assertions.assertFalse(best.toString().contains("bar"));
}

@Test
public void testTargetMethodWithCBranch() {
EvoSuite evosuite = new EvoSuite();

String targetClass = TargetMethod.class.getCanonicalName();
String targetMethod = "foo(Ljava/lang/Integer;)Z";
Properties.TARGET_CLASS = targetClass;
Properties.TARGET_METHOD_LIST = targetMethod;
Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.BRANCH, Properties.Criterion.CBRANCH};
String[] command = new String[]{"-generateMOSuite", "-class", targetClass};

Object result = evosuite.parseCommandLine(command);

GeneticAlgorithm<TestSuiteChromosome> ga = getGAFromResult(result);
TestSuiteChromosome best = ga.getBestIndividual();
Assertions.assertFalse(best.toString().contains("bar"));
}

@Test
public void testTargetMethodWithOutput() {
EvoSuite evosuite = new EvoSuite();

String targetClass = TargetMethod.class.getCanonicalName();
String targetMethod = "foo(Ljava/lang/Integer;)Z";
Properties.TARGET_CLASS = targetClass;
Properties.TARGET_METHOD_LIST = targetMethod;
Properties.CRITERION = new Properties.Criterion[]{Properties.Criterion.BRANCH, Properties.Criterion.OUTPUT};
String[] command = new String[]{"-generateMOSuite", "-class", targetClass};

Object result = evosuite.parseCommandLine(command);

GeneticAlgorithm<TestSuiteChromosome> ga = getGAFromResult(result);
TestSuiteChromosome best = ga.getBestIndividual();
Assertions.assertFalse(best.toString().contains("bar"));
}
}