Search in sources :

Example 36 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class ToStringReturnsNormallyContract 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("toString", 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();
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 37 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class ControlFlowDistanceCalculator method hasConstructorException.

/**
 * If there is an exception in a superconstructor, then the corresponding
 * constructor might not be included in the execution trace
 *
 * @param results
 * @param callCount
 */
private static boolean hasConstructorException(ExecutionResult result, String className, String methodName) {
    if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions())
        return false;
    Integer exceptionPosition = result.getFirstPositionOfThrownException();
    if (!result.test.hasStatement(exceptionPosition)) {
        return false;
    }
    Statement statement = result.test.getStatement(exceptionPosition);
    if (statement instanceof ConstructorStatement) {
        ConstructorStatement c = (ConstructorStatement) statement;
        String constructorClassName = c.getConstructor().getName();
        String constructorMethodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
        if (constructorClassName.equals(className) && constructorMethodName.equals(methodName)) {
            return true;
        }
    }
    return false;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement)

Example 38 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class TestCaseMinimizer method removeUnusedVariables.

/**
 * Remove all unreferenced variables
 *
 * @param t
 *            The test case
 * @return True if something was deleted
 */
public static boolean removeUnusedVariables(TestCase t) {
    List<Integer> to_delete = new ArrayList<Integer>();
    boolean has_deleted = false;
    int num = 0;
    for (Statement s : t) {
        VariableReference var = s.getReturnValue();
        if (!t.hasReferences(var)) {
            to_delete.add(num);
            has_deleted = true;
        }
        num++;
    }
    Collections.sort(to_delete, Collections.reverseOrder());
    for (Integer position : to_delete) {
        t.remove(position);
    }
    return has_deleted;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) ArrayList(java.util.ArrayList)

Example 39 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class ConstantInliner method removeUnusedVariables.

/**
 * Remove all unreferenced variables
 *
 * @param t
 *            The test case
 * @return True if something was deleted
 */
public boolean removeUnusedVariables(TestCase t) {
    List<Integer> toDelete = new ArrayList<Integer>();
    boolean hasDeleted = false;
    int num = 0;
    for (Statement s : t) {
        if (s instanceof PrimitiveStatement) {
            VariableReference var = s.getReturnValue();
            if (!t.hasReferences(var)) {
                toDelete.add(num);
                hasDeleted = true;
            }
        }
        num++;
    }
    Collections.sort(toDelete, Collections.reverseOrder());
    for (Integer position : toDelete) {
        t.remove(position);
    }
    return hasDeleted;
}
Also used : PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) ArrayList(java.util.ArrayList)

Example 40 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class ReferenceLocalSearch method addCall.

/**
 * Add a method call on the return value of the object at position statement
 *
 * @param test
 * @param statement
 */
private boolean addCall(TestChromosome test, int statement) {
    logger.debug("Adding call");
    TestFactory factory = TestFactory.getInstance();
    Statement theStatement = test.getTestCase().getStatement(statement);
    VariableReference var = theStatement.getReturnValue();
    int oldLength = test.size();
    factory.insertRandomCallOnObjectAt(test.getTestCase(), var, statement + 1);
    test.setChanged(test.size() != oldLength);
    return false;
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestFactory(org.evosuite.testcase.TestFactory) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) NullStatement(org.evosuite.testcase.statements.NullStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement)

Aggregations

Statement (org.evosuite.testcase.statements.Statement)48 MethodStatement (org.evosuite.testcase.statements.MethodStatement)25 VariableReference (org.evosuite.testcase.variable.VariableReference)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)16 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)11 TestCase (org.evosuite.testcase.TestCase)8 JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 FieldStatement (org.evosuite.testcase.statements.FieldStatement)7 Method (java.lang.reflect.Method)6 TestChromosome (org.evosuite.testcase.TestChromosome)6 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 FunctionalMockStatement (org.evosuite.testcase.statements.FunctionalMockStatement)5 NullStatement (org.evosuite.testcase.statements.NullStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)4 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)4 TestFactory (org.evosuite.testcase.TestFactory)3 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)3