Search in sources :

Example 26 with ConstructorStatement

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

the class Contract method isTargetStatement.

/**
 * Check if this statement is related to the unit under test
 *
 * @param statement
 *            a {@link org.evosuite.testcase.statements.Statement} object.
 * @return a boolean.
 */
protected boolean isTargetStatement(Statement statement) {
    // return true;
    if (statement instanceof MethodStatement) {
        MethodStatement ms = (MethodStatement) statement;
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (targetClass.equals(ms.getMethod().getDeclaringClass()))
            return true;
    } else if (statement instanceof ConstructorStatement) {
        ConstructorStatement cs = (ConstructorStatement) statement;
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (targetClass.equals(cs.getConstructor().getDeclaringClass()))
            return true;
    } else if (statement instanceof FieldStatement) {
        FieldStatement fs = (FieldStatement) statement;
        final Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (targetClass.equals(fs.getField().getDeclaringClass()))
            return true;
    }
    return false;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Example 27 with ConstructorStatement

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

the class ExceptionCoverageHelper method getMethodIdentifier.

public static String getMethodIdentifier(ExecutionResult result, int exceptionPosition) {
    if (result.test.getStatement(exceptionPosition) instanceof MethodStatement) {
        MethodStatement ms = (MethodStatement) result.test.getStatement(exceptionPosition);
        Method method = ms.getMethod().getMethod();
        return method.getName() + Type.getMethodDescriptor(method);
    } else if (result.test.getStatement(exceptionPosition) instanceof ConstructorStatement) {
        ConstructorStatement cs = (ConstructorStatement) result.test.getStatement(exceptionPosition);
        Constructor<?> constructor = cs.getConstructor().getConstructor();
        return "<init>" + Type.getConstructorDescriptor(constructor);
    }
    return "";
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method)

Example 28 with ConstructorStatement

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

the class ExceptionCoverageHelper method isSutException.

public static boolean isSutException(ExecutionResult result, int exceptionPosition) {
    if (result.test.getStatement(exceptionPosition) instanceof MethodStatement) {
        MethodStatement ms = (MethodStatement) result.test.getStatement(exceptionPosition);
        Method method = ms.getMethod().getMethod();
        Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (method.getDeclaringClass().equals(targetClass)) {
            return true;
        }
    } else if (result.test.getStatement(exceptionPosition) instanceof ConstructorStatement) {
        ConstructorStatement cs = (ConstructorStatement) result.test.getStatement(exceptionPosition);
        Constructor<?> constructor = cs.getConstructor().getConstructor();
        Class<?> targetClass = Properties.getTargetClassAndDontInitialise();
        if (constructor.getDeclaringClass().equals(targetClass)) {
            return true;
        }
    }
    return false;
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method)

Example 29 with ConstructorStatement

use of org.evosuite.testcase.statements.ConstructorStatement 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 30 with ConstructorStatement

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

the class TestSimilarity method testBasicSimilarityDifferentTypes2.

@Test
public void testBasicSimilarityDifferentTypes2() {
    TestCase test1 = new DefaultTestCase();
    TestCase test2 = new DefaultTestCase();
    PrimitiveStatement<?> aInt = new LongPrimitiveStatement(test1, 42L);
    test1.addStatement(aInt);
    PrimitiveStatement<?> aInt2 = new IntPrimitiveStatement(test1, 42);
    test1.addStatement(aInt2);
    PrimitiveStatement<?> bInt = new IntPrimitiveStatement(test2, 42);
    test2.addStatement(bInt);
    Constructor<?> c = Object.class.getConstructors()[0];
    ConstructorStatement cs = new ConstructorStatement(test2, new GenericConstructor(c, Object.class), new ArrayList<VariableReference>());
    test2.addStatement(cs);
    double score = DiversityObserver.getNeedlemanWunschScore(test1, test2);
    Assert.assertTrue(score <= 0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) LongPrimitiveStatement(org.evosuite.testcase.statements.numeric.LongPrimitiveStatement) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

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