use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class TestChromosome method mockChange.
private boolean mockChange() {
/*
Be sure to update the mocked values if there has been any change in
behavior in the last execution.
Note: mock "expansion" cannot be done after a test has been mutated and executed,
as the expansion itself might have side effects. Therefore, it has to be done
before a test is evaluated.
*/
boolean changed = false;
for (int i = 0; i < test.size(); i++) {
Statement st = test.getStatement(i);
if (!(st instanceof FunctionalMockStatement)) {
continue;
}
FunctionalMockStatement fms = (FunctionalMockStatement) st;
if (!fms.doesNeedToUpdateInputs()) {
continue;
}
int preLength = test.size();
try {
List<Type> missing = fms.updateMockedMethods();
int pos = st.getPosition();
logger.debug("Generating parameters for mock call");
// Added 'null' as additional parameter - fix for @NotNull annotations issue on evo mailing list
List<VariableReference> refs = TestFactory.getInstance().satisfyParameters(test, null, missing, null, pos, 0, true, false, true);
fms.addMissingInputs(refs);
} catch (Exception e) {
// shouldn't really happen because, in the worst case, we could create mocks for missing parameters
String msg = "Functional mock problem: " + e.toString();
AtMostOnceLogger.warn(logger, msg);
fms.fillWithNullRefs();
return changed;
}
changed = true;
int increase = test.size() - preLength;
i += increase;
}
return changed;
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class TestClassInitialization method buildLoadTargetClassTestCase.
private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
DefaultTestCase test = new DefaultTestCase();
StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
VariableReference string0 = test.addStatement(stmt0);
try {
Method currentThreadMethod = Thread.class.getMethod("currentThread");
Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
Statement loadClassStmt = new MethodStatement(test, new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar, Collections.singletonList(string0));
test.addStatement(loadClassStmt);
return test;
} catch (NoSuchMethodException | SecurityException e) {
throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
}
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassTwoParameter.
@Test
public void testGenericClassTwoParameter() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperTwoParameterTest.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperTwoParameter.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertTrue(factory.hasCarvedTestCases());
TestChromosome carved = factory.getChromosome();
Assert.assertNotNull(carved);
Assert.assertEquals("", 8, carved.test.size());
for (int i = 0; i < carved.test.size(); i++) {
Statement stmt = carved.test.getStatement(i);
boolean valid = stmt.isValid();
Assert.assertTrue("Invalid stmt at position " + i, valid);
}
String code = carved.toString();
String setLong = "GenericObjectWrapperTwoParameter<String, String>";
Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassSequence.
@Test
public void testGenericClassSequence() {
Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperSequenceTest.class.getCanonicalName();
Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapper.class.getCanonicalName();
Properties.SEED_MUTATIONS = 1;
Properties.SEED_CLONE = 1;
JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
Assert.assertTrue(factory.hasCarvedTestCases());
TestChromosome carved = factory.getChromosome();
Assert.assertNotNull(carved);
Assert.assertEquals("", 6, carved.test.size());
for (int i = 0; i < carved.test.size(); i++) {
Statement stmt = carved.test.getStatement(i);
boolean valid = stmt.isValid();
Assert.assertTrue("Invalid stmt at position " + i, valid);
}
String code = carved.toString();
String setLong = "GenericObjectWrapper<GenericObjectWrapperSequenceTest.Foo>";
Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
code = carved.toString();
setLong = "(Object)";
Assert.assertFalse("generated code contains object cast " + setLong + "\n" + code, code.contains(setLong));
}
use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.
the class RegressionSuiteMinimizer method removeDuplicateAssertions.
private void removeDuplicateAssertions(RegressionTestSuiteChromosome suite) {
Iterator<TestChromosome> it = suite.getTestChromosomes().iterator();
Map<String, List<String>> uniqueAssertions = new HashMap<String, List<String>>();
// int i = -1;
while (it.hasNext()) {
// i++;
RegressionTestChromosome test = (RegressionTestChromosome) it.next();
boolean changed = false;
boolean hadAssertion = false;
// keep track of new unique assertions, and if not unique, remove the assertion
for (Assertion a : test.getTheTest().getTestCase().getAssertions()) {
String aClass = a.getClass().getSimpleName();
List<String> aTypes = uniqueAssertions.get(aClass);
if (aTypes == null) {
aTypes = new ArrayList<String>();
}
String aType = "";
if (a instanceof InspectorAssertion) {
InspectorAssertion ia = (InspectorAssertion) a;
try {
aType = ia.getInspector().getMethod().getName();
} catch (NullPointerException e) {
// technically this should not happen
Statement s = ia.getStatement();
if (s instanceof MethodStatement) {
aType = ((MethodStatement) s).getMethod().getName();
}
}
}
if (aTypes.contains(aType)) {
// logger.warn("removing non-unique assertion: {}-{}", aClass, aType);
changed = true;
a.getStatement().getPosition();
test.getTheTest().getTestCase().removeAssertion(a);
continue;
}
aTypes.add(aType);
uniqueAssertions.put(aClass, aTypes);
hadAssertion = true;
}
if (changed) {
test.updateClassloader();
}
}
if (uniqueAssertions.size() > 0) {
logger.warn("unique assertions: {}", uniqueAssertions);
}
}
Aggregations