use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class CoverageGoalTestNameGenerationStrategy method chooseRepresentativeGoal.
/**
* Out of a set of multiple goals, select one that is representative.
* Assumes that goals is not empty, and all items in goals have the same type
* @param test
* @param goals
* @return
*/
private TestFitnessFunction chooseRepresentativeGoal(TestCase test, Collection<TestFitnessFunction> goals) {
Map<String, Integer> methodToPosition = new LinkedHashMap<>();
for (Statement st : test) {
if (st instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) st;
String name = ms.getMethodName() + ms.getDescriptor();
methodToPosition.put(name, st.getPosition());
} else if (st instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) st;
String name = "<init>" + cs.getDescriptor();
methodToPosition.put(name, st.getPosition());
}
}
// Randomness.choice(goals);
TestFitnessFunction chosenGoal = goals.iterator().next();
int chosenPosition = -1;
for (TestFitnessFunction goal : goals) {
if (methodToPosition.containsKey(goal.getTargetMethod())) {
int position = methodToPosition.get(goal.getTargetMethod());
if (position >= chosenPosition) {
chosenPosition = position;
chosenGoal = goal;
}
}
}
return chosenGoal;
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class TestCaseBuilder method appendMethod.
/**
* @param callee
* <code>null</code> for state methods
* @param method
* @param parameters
* @return <code>void reference</code> for void methods
*/
public VariableReference appendMethod(VariableReference callee, Method method, VariableReference... parameters) {
List<VariableReference> parameter_list = Arrays.asList(parameters);
MethodStatement methodStmt = null;
if (callee == null) {
methodStmt = new MethodStatement(tc, new GenericMethod(method, method.getDeclaringClass()), callee, parameter_list);
} else {
methodStmt = new MethodStatement(tc, new GenericMethod(method, callee.getType()), callee, parameter_list);
}
tc.addStatement(methodStmt);
return methodStmt.getReturnValue();
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class EnvironmentDataSystemTest method testOnSpecificTest.
@Test
public void testOnSpecificTest() throws ClassNotFoundException, ConstructionFailedException, NoSuchMethodException, SecurityException {
Properties.TARGET_CLASS = DseBar.class.getCanonicalName();
Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(DseFoo.class.getCanonicalName());
GenericClass clazz = new GenericClass(sut);
DefaultTestCase test = new DefaultTestCase();
// String string0 = "baz5";
VariableReference stringVar = test.addStatement(new StringPrimitiveStatement(test, "baz5"));
// DseFoo dseFoo0 = new DseFoo();
GenericConstructor fooConstructor = new GenericConstructor(fooClass.getConstructors()[0], fooClass);
ConstructorStatement fooConstructorStatement = new ConstructorStatement(test, fooConstructor, Arrays.asList(new VariableReference[] {}));
VariableReference fooVar = test.addStatement(fooConstructorStatement);
// String fileName = new String("/home/galeotti/README.txt")
String path = "/home/galeotti/README.txt";
EvoSuiteFile evosuiteFile = new EvoSuiteFile(path);
FileNamePrimitiveStatement fileNameStmt = new FileNamePrimitiveStatement(test, evosuiteFile);
test.addStatement(fileNameStmt);
Method fooIncMethod = fooClass.getMethod("inc", new Class<?>[] {});
GenericMethod incMethod = new GenericMethod(fooIncMethod, fooClass);
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
test.addStatement(new MethodStatement(test, incMethod, fooVar, Arrays.asList(new VariableReference[] {})));
// DseBar dseBar0 = new DseBar(string0);
GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
ConstructorStatement constructorStatement = new ConstructorStatement(test, gc, Arrays.asList(new VariableReference[] { stringVar }));
VariableReference callee = test.addStatement(constructorStatement);
// dseBar0.coverMe(dseFoo0);
Method m = clazz.getRawClass().getMethod("coverMe", new Class<?>[] { fooClass });
GenericMethod method = new GenericMethod(m, sut);
MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { fooVar }));
test.addStatement(ms);
System.out.println(test);
TestSuiteChromosome suite = new TestSuiteChromosome();
BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();
BranchCoverageMap.getInstance().searchStarted(null);
assertEquals(4.0, fitness.getFitness(suite), 0.1F);
suite.addTest(test);
assertEquals(1.0, fitness.getFitness(suite), 0.1F);
System.out.println("Test suite: " + suite);
Properties.CONCOLIC_TIMEOUT = Integer.MAX_VALUE;
TestSuiteLocalSearch localSearch = TestSuiteLocalSearch.selectTestSuiteLocalSearch();
LocalSearchObjective<TestSuiteChromosome> localObjective = new DefaultLocalSearchObjective<TestSuiteChromosome>();
localObjective.addFitnessFunction(fitness);
localSearch.doSearch(suite, localObjective);
System.out.println("Fitness: " + fitness.getFitness(suite));
System.out.println("Test suite: " + suite);
assertEquals("Local search failed to cover class", 0.0, fitness.getFitness(suite), 0.1F);
BranchCoverageMap.getInstance().searchFinished(null);
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class HashCodeReturnsNormallyContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
int position = statement.getPosition();
VariableReference a = variables.get(0);
try {
Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
GenericMethod method = new GenericMethod(hashCodeMethod, a.getGenericClass());
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] {}));
test.addStatement(st1, position + 1);
st1.addComment("Throws exception: " + exception.getMessage());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class JUnitTheoryContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
int position = statement.getPosition();
VariableReference a = variables.get(0);
int pos = a.getStPosition();
try {
Constructor<?> defaultConstructor = theoryReceiver.getClass().getConstructor();
GenericConstructor constructor = new GenericConstructor(defaultConstructor, theoryReceiver.getClass());
Statement st1 = new ConstructorStatement(test, constructor, new ArrayList<VariableReference>());
VariableReference receiver = test.addStatement(st1, position + 1);
Statement st2 = new MethodStatement(test, theoryMethod, receiver, Arrays.asList(new VariableReference[] { test.getStatement(pos).getReturnValue() }));
test.addStatement(st2, position + 2);
st2.addComment("Violates theory: " + theoryMethod.getName());
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
logger.warn("Error while creating contract violation: " + e);
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
logger.warn("Error while creating contract violation: " + e);
e.printStackTrace();
}
}
Aggregations