use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class UnitAssertionGenerator method isRelevant.
private boolean isRelevant(Statement s, TestCase t) {
// Always allow assertions on the last statement
if (s.getPosition() == (t.size() - 1))
return true;
// Allow assertions after method calls on the UUT
if (s instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) s;
String declaringClass = ms.getMethod().getDeclaringClass().getName();
while (declaringClass.contains("$")) declaringClass = declaringClass.substring(0, declaringClass.indexOf("$"));
if (declaringClass.equals(Properties.TARGET_CLASS) || (!Properties.TARGET_CLASS_PREFIX.isEmpty() && declaringClass.startsWith(Properties.TARGET_CLASS_PREFIX)))
return true;
}
return false;
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class TestSuiteGenerator method buildLoadTargetClassTestCase.
/**
* Creates a single Test Case that only loads the target class.
* <code>
* Thread currentThread = Thread.currentThread();
* ClassLoader classLoader = currentThread.getClassLoader();
* classLoader.load(className);
* </code>
* @param className the class to be loaded
* @return
* @throws EvosuiteError if a reflection error happens while creating the test case
*/
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);
BooleanPrimitiveStatement stmt1 = new BooleanPrimitiveStatement(test, true);
VariableReference boolean0 = test.addStatement(stmt1);
Method forNameMethod = Class.class.getMethod("forName", String.class, boolean.class, ClassLoader.class);
Statement forNameStmt = new MethodStatement(test, new GenericMethod(forNameMethod, forNameMethod.getDeclaringClass()), null, Arrays.<VariableReference>asList(string0, boolean0, contextClassLoaderVar));
test.addStatement(forNameStmt);
return test;
} catch (NoSuchMethodException | SecurityException e) {
throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
}
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class EqualsSymmetricContract method addAssertionAndComments.
@Override
public void addAssertionAndComments(Statement statement, List<VariableReference> variables, Throwable exception) {
TestCase test = statement.getTestCase();
VariableReference a = variables.get(0);
VariableReference b = variables.get(1);
try {
Method equalsMethod = a.getGenericClass().getRawClass().getMethod("equals", new Class<?>[] { Object.class });
GenericMethod method = new GenericMethod(equalsMethod, a.getGenericClass());
// Create x = a.equals(b)
Statement st1 = new MethodStatement(test, method, a, Arrays.asList(new VariableReference[] { b }));
VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
// Create y = b.equals(a);
Statement st2 = new MethodStatement(test, method, b, Arrays.asList(new VariableReference[] { a }));
VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
Statement newStatement = test.getStatement(y.getStPosition());
// Create assertEquals(x, y)
EqualsAssertion assertion = new EqualsAssertion();
assertion.setStatement(newStatement);
assertion.setSource(x);
assertion.setDest(y);
assertion.setValue(true);
newStatement.addAssertion(assertion);
newStatement.addComment("Violates contract a.equals(b) <-> b.equals(a)");
} 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 NullPointerExceptionContract method check.
/* (non-Javadoc)
* @see org.evosuite.contracts.Contract#check(org.evosuite.testcase.TestCase, org.evosuite.testcase.Statement, org.evosuite.testcase.Scope, java.lang.Throwable)
*/
/**
* {@inheritDoc}
*/
@Override
public ContractViolation check(Statement statement, Scope scope, Throwable exception) {
if (!isTargetStatement(statement))
return null;
try {
if (exception != null) {
// method throws no NullPointerException if no input parameter was null
if (exception instanceof NullPointerException) {
StackTraceElement element = exception.getStackTrace()[0];
// If the exception was thrown in the test directly, it is also not interesting
if (element.getClassName().startsWith(PackageInfo.getEvoSuitePackage() + ".testcase")) {
return null;
}
List<VariableReference> parameters = new ArrayList<VariableReference>();
if (statement instanceof MethodStatement) {
MethodStatement ms = (MethodStatement) statement;
parameters.addAll(ms.getParameterReferences());
} else if (statement instanceof ConstructorStatement) {
ConstructorStatement cs = (ConstructorStatement) statement;
parameters.addAll(cs.getParameterReferences());
} else {
return null;
}
boolean hasNull = false;
for (VariableReference var : parameters) {
if (var.getObject(scope) == null) {
hasNull = true;
break;
}
}
if (!hasNull) {
return new ContractViolation(this, statement, exception);
}
}
}
return null;
} catch (CodeUnderTestException e) {
throw new UnsupportedOperationException();
}
}
use of org.evosuite.testcase.statements.MethodStatement in project evosuite by EvoSuite.
the class MutationAssertionGenerator method filterInspectorPrimitiveDuplication.
/**
* Remove inspector assertions that follow method calls of the same method
*
* @param statement
*/
protected void filterInspectorPrimitiveDuplication(Statement statement) {
Set<Assertion> assertions = new HashSet<Assertion>(statement.getAssertions());
if (assertions.size() < 2)
return;
if (!(statement instanceof MethodStatement))
return;
MethodStatement methodStatement = (MethodStatement) statement;
boolean hasPrimitive = false;
for (Assertion assertion : assertions) {
if (assertion instanceof PrimitiveAssertion) {
if (assertion.getStatement().equals(statement)) {
hasPrimitive = true;
}
}
}
if (hasPrimitive) {
for (Assertion assertion : assertions) {
if (assertion instanceof InspectorAssertion) {
InspectorAssertion ia = (InspectorAssertion) assertion;
if (ia.getInspector().getMethod().equals(methodStatement.getMethod().getMethod())) {
statement.removeAssertion(assertion);
return;
}
}
}
}
}
Aggregations