Search in sources :

Example 41 with MethodStatement

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

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

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

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

the class MutationAssertionGenerator method justNullAssertion.

/**
 * Returns true if the statement has nothing but null assertions
 *
 * @param statement
 * @return
 */
protected boolean justNullAssertion(Statement statement) {
    Set<Assertion> assertions = statement.getAssertions();
    if (assertions.isEmpty())
        return false;
    else {
        Iterator<Assertion> iterator = assertions.iterator();
        VariableReference ret = statement.getReturnValue();
        VariableReference callee = null;
        if (statement instanceof MethodStatement) {
            callee = ((MethodStatement) statement).getCallee();
        }
        boolean just = true;
        while (iterator.hasNext()) {
            Assertion ass = iterator.next();
            if (!(ass instanceof NullAssertion)) {
                if (ass.getReferencedVariables().contains(ret) || ass.getReferencedVariables().contains(callee)) {
                    just = false;
                    break;
                }
            }
        }
        return just;
    }
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference)

Example 45 with MethodStatement

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

the class ConstraintVerifierTest method testDelete_multipleVarsThatCouldBeReused.

@Test
public void testDelete_multipleVarsThatCouldBeReused() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    VariableReference servlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    // initializing bounding variable method called directly after the new
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0);
    // now do it again
    VariableReference secondServlet = factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 2, 0);
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 3, 0);
    MethodStatement mt = (MethodStatement) tc.getTestCase().getStatement(3);
    // be sure it is using the second servlet
    mt.replace(servlet, secondServlet);
    Assert.assertEquals(4, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    // bounded variable can be deleted
    Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0));
    // method using bounded variable should not be deleted
    Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1));
    // bounded variable can be deleted
    Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 2));
    // method using bounded variable should not be deleted
    Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 3));
    boolean mutated = tc.deleteStatement(factory, 2);
    Assert.assertTrue(mutated);
    /*
            deleting the bounded variable in position 2 should lead to also delete the initializing variable in 3,
            and not end up with 3 re-using 0
         */
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    Assert.assertEquals(2, tc.size());
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Injector(org.evosuite.runtime.javaee.injection.Injector) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Aggregations

MethodStatement (org.evosuite.testcase.statements.MethodStatement)54 VariableReference (org.evosuite.testcase.variable.VariableReference)40 GenericMethod (org.evosuite.utils.generic.GenericMethod)29 Method (java.lang.reflect.Method)25 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)22 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)22 Statement (org.evosuite.testcase.statements.Statement)19 GenericClass (org.evosuite.utils.generic.GenericClass)15 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)11 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)8 TestCase (org.evosuite.testcase.TestCase)7 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)5 FieldStatement (org.evosuite.testcase.statements.FieldStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 HashSet (java.util.HashSet)4 TestFactory (org.evosuite.testcase.TestFactory)4 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)4 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)4