use of org.evosuite.testcase.variable.VariableReference 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.variable.VariableReference in project evosuite by EvoSuite.
the class EqualsContract 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) {
for (VariableReference var : getAllVariables(scope)) {
Object object = scope.getObject(var);
if (object == null)
continue;
// We do not want to call equals if it is the default implementation
Class<?>[] parameters = { Object.class };
try {
Method equalsMethod = object.getClass().getMethod("equals", parameters);
if (equalsMethod.getDeclaringClass().equals(Object.class))
continue;
} catch (SecurityException e1) {
continue;
} catch (NoSuchMethodException e1) {
continue;
}
try {
// An object always has to equal itself
if (!object.equals(object))
return new ContractViolation(this, statement, exception, var);
} catch (NullPointerException e) {
continue;
// No nullpointer exceptions may be thrown if the parameter was not null
// TODO: Use UndeclaredExceptionContract instead?
// return new ContractViolation(this, statement, e, var);
// Returning this contract violation is definitely wrong as it would look like equals returned false
} catch (Throwable t) {
continue;
}
}
return null;
}
use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.
the class EqualsSymmetricContract method check.
/* (non-Javadoc)
* @see org.evosuite.contracts.Contract#check(org.evosuite.testcase.Statement, org.evosuite.testcase.Scope, java.lang.Throwable)
*/
/**
* {@inheritDoc}
*/
@Override
public ContractViolation check(Statement statement, Scope scope, Throwable exception) {
for (Pair<VariableReference> pair : getAllVariablePairs(scope)) {
// Equals self is covered by EqualsContract
if (pair.object1 == pair.object2)
continue;
Object object1 = scope.getObject(pair.object1);
Object object2 = scope.getObject(pair.object2);
if (object1 == null || object2 == null)
continue;
// We do not want to call equals if it is the default implementation
Class<?>[] parameters = { Object.class };
try {
Method equalsMethod = object1.getClass().getMethod("equals", parameters);
if (equalsMethod.getDeclaringClass().equals(Object.class))
continue;
} catch (SecurityException e1) {
continue;
} catch (NoSuchMethodException e1) {
continue;
}
ExecutionTracer.disable();
if (object1.equals(object2)) {
if (!object2.equals(object1)) {
ExecutionTracer.enable();
return new ContractViolation(this, statement, exception, pair.object1, pair.object2);
}
} else {
if (object2.equals(object1)) {
ExecutionTracer.enable();
return new ContractViolation(this, statement, exception, pair.object1, pair.object2);
}
}
ExecutionTracer.enable();
}
return null;
}
use of org.evosuite.testcase.variable.VariableReference 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.variable.VariableReference 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();
}
}
Aggregations