Search in sources :

Example 21 with ConstructorStatement

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

the class BranchCoverageSuiteFitness method handleConstructorExceptions.

/**
 * 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 void handleConstructorExceptions(TestChromosome test, ExecutionResult result, Map<String, Integer> callCount) {
    if (result.hasTimeout() || result.hasTestException() || result.noThrownExceptions()) {
        return;
    }
    Integer exceptionPosition = result.getFirstPositionOfThrownException();
    // TODO: Not sure why that can happen
    if (exceptionPosition >= result.test.size()) {
        return;
    }
    Statement statement = null;
    if (result.test.hasStatement(exceptionPosition)) {
        statement = result.test.getStatement(exceptionPosition);
    }
    if (statement instanceof ConstructorStatement) {
        ConstructorStatement c = (ConstructorStatement) statement;
        String className = c.getConstructor().getName();
        String methodName = "<init>" + Type.getConstructorDescriptor(c.getConstructor().getConstructor());
        String name = className + "." + methodName;
        if (!callCount.containsKey(name)) {
            callCount.put(name, 1);
            if (branchlessMethodCoverageMap.containsKey(name)) {
                TestFitnessFunction goal = branchlessMethodCoverageMap.get(name);
                test.getTestCase().addCoveredGoal(goal);
                toRemoveRootBranches.add(name);
                if (Properties.TEST_ARCHIVE) {
                    Archive.getArchiveInstance().updateArchive(goal, test, 0.0);
                }
            }
        }
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) Statement(org.evosuite.testcase.statements.Statement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction)

Example 22 with ConstructorStatement

use of org.evosuite.testcase.statements.ConstructorStatement 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();
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) 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) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) GenericConstructor(org.evosuite.utils.generic.GenericConstructor)

Example 23 with ConstructorStatement

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

the class ContractViolation method resultsFromMethod.

public boolean resultsFromMethod(String methodName) {
    if (statement instanceof MethodStatement) {
        MethodStatement ms = (MethodStatement) statement;
        String target = ms.getMethodName() + ms.getDescriptor();
        return target.equals(methodName);
    } else if (statement instanceof ConstructorStatement) {
        return methodName.startsWith("<init>");
    } else {
        return false;
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement)

Example 24 with ConstructorStatement

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

the class ContractViolation method same.

/**
 * Determine if we have already seen an instance of this violation
 *
 * @param other
 *            a {@link org.evosuite.contracts.ContractViolation} object.
 * @return a boolean.
 */
public boolean same(ContractViolation other) {
    // Same contract?
    if (!contract.getClass().equals(other.contract.getClass()))
        return false;
    // Same type of statement?
    if (!statement.getClass().equals(other.statement.getClass()))
        return false;
    // Same exception type?
    if (exception != null && other.exception != null) {
        if (!exception.getClass().equals(other.exception.getClass()))
            return false;
    }
    // Same method call / constructor?
    if (statement instanceof MethodStatement) {
        MethodStatement ms1 = (MethodStatement) statement;
        MethodStatement ms2 = (MethodStatement) other.statement;
        if (ms1.getMethod().getMethod().equals(ms2.getMethod().getMethod())) {
            return true;
        }
    } else if (statement instanceof ConstructorStatement) {
        ConstructorStatement ms1 = (ConstructorStatement) statement;
        ConstructorStatement ms2 = (ConstructorStatement) other.statement;
        if (ms1.getConstructor().getConstructor().equals(ms2.getConstructor().getConstructor())) {
            return true;
        }
    } else if (statement instanceof AssignmentStatement) {
        VariableReference var1 = statement.getReturnValue();
        VariableReference var2 = other.statement.getReturnValue();
        if (var1 instanceof FieldReference && var2 instanceof FieldReference) {
            if (((FieldReference) var1).getField().getField().equals(((FieldReference) var2).getField().getField()))
                return true;
        }
    }
    return false;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement)

Example 25 with ConstructorStatement

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

the class SameTraceObserver method visit.

/* (non-Javadoc)
	 * @see org.evosuite.assertion.AssertionTraceObserver#visit(org.evosuite.testcase.StatementInterface, org.evosuite.testcase.Scope, org.evosuite.testcase.VariableReference)
	 */
/**
 * {@inheritDoc}
 */
@Override
protected void visit(Statement statement, Scope scope, VariableReference var) {
    // TODO: Only MethodStatement?
    if (statement.isAssignmentStatement())
        return;
    if (statement instanceof PrimitiveStatement<?>)
        return;
    if (statement instanceof ArrayStatement)
        return;
    if (statement instanceof ConstructorStatement)
        return;
    try {
        Object object = var.getObject(scope);
        if (object == null)
            return;
        if (var.isPrimitive())
            return;
        if (var.isString() && Properties.INLINE)
            // After inlining the value of assertions would be different
            return;
        SameTraceEntry entry = new SameTraceEntry(var);
        for (VariableReference other : scope.getElements(var.getType())) {
            if (other == var)
                continue;
            if (other.isPrimitive())
                continue;
            if (other.isWrapperType())
                // Issues with inlining resulting in unstable assertions
                continue;
            Object otherObject = other.getObject(scope);
            if (otherObject == null)
                continue;
            if (otherObject.getClass() != object.getClass())
                continue;
            try {
                logger.debug("Comparison of {} with {}", var, other);
                entry.addEntry(other, object == otherObject);
            } catch (Throwable t) {
                logger.debug("Exception during equals: " + t);
            // ignore?
            }
        }
        trace.addEntry(statement.getPosition(), var, entry);
    } catch (CodeUnderTestException e) {
        logger.debug("", e);
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) PrimitiveStatement(org.evosuite.testcase.statements.PrimitiveStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayStatement(org.evosuite.testcase.statements.ArrayStatement) CodeUnderTestException(org.evosuite.testcase.execution.CodeUnderTestException)

Aggregations

ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)36 MethodStatement (org.evosuite.testcase.statements.MethodStatement)22 VariableReference (org.evosuite.testcase.variable.VariableReference)22 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)19 GenericMethod (org.evosuite.utils.generic.GenericMethod)15 Method (java.lang.reflect.Method)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)13 Statement (org.evosuite.testcase.statements.Statement)10 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)9 GenericClass (org.evosuite.utils.generic.GenericClass)9 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)7 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)6 FlagExample1 (com.examples.with.different.packagename.FlagExample1)5 BranchCoverageFactory (org.evosuite.coverage.branch.BranchCoverageFactory)5 TestCase (org.evosuite.testcase.TestCase)4 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)4 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)3 DefUseCoverageFactory (org.evosuite.coverage.dataflow.DefUseCoverageFactory)3 DefUseCoverageSuiteFitness (org.evosuite.coverage.dataflow.DefUseCoverageSuiteFitness)3