Search in sources :

Example 6 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testLimit.

@Test
public void testLimit() throws Exception {
    TestCase tc = new DefaultTestCase();
    final int LIMIT_5 = 5;
    Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT = LIMIT_5;
    final int LOOP_0 = 0, LOOP_3 = 3, LOOP_5 = 5, LOOP_7 = 7;
    IntPrimitiveStatement x = new IntPrimitiveStatement(tc, LOOP_3);
    VariableReference loop = tc.addStatement(x);
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, true));
    VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
    VariableReference mock = tc.addStatement(mockStmt);
    tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("limit", Foo.class, int.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock, loop)));
    // execute first time with default mock
    execute(tc);
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(LOOP_3, types.size());
    for (Type t : types) {
        Assert.assertEquals(boolean.class, t);
    }
    // add the 3 missing values
    mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef, boolRef));
    // before re-executing, change loops to the limit
    x.setValue(LOOP_5);
    execute(tc);
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    Assert.assertEquals(LOOP_5 - LOOP_3, types.size());
    for (Type t : types) {
        Assert.assertEquals(boolean.class, t);
    }
    // add the 2 missing values
    mockStmt.addMissingInputs(Arrays.asList(boolRef, boolRef));
    Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
    // before re-executing 3rd time, change loops above the limit
    x.setValue(LOOP_7);
    execute(tc);
    // no update should be required
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
    // decrease, but to the limit, so still no required change
    x.setValue(LOOP_5);
    execute(tc);
    // no update should be required
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_5, mockStmt.getNumParameters());
    // further decrease, but now we need to remove parameters
    x.setValue(LOOP_3);
    execute(tc);
    // do update
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    // but no new types to add
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_3, mockStmt.getNumParameters());
    // remove all
    x.setValue(LOOP_0);
    execute(tc);
    // do update
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    types = mockStmt.updateMockedMethods();
    // but no new types to add
    Assert.assertEquals(0, types.size());
    Assert.assertEquals(LOOP_0, mockStmt.getNumParameters());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Type(java.lang.reflect.Type) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) Test(org.junit.Test)

Example 7 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testPackageLevel_differentPackage_instrumentation_package.

@Test
public void testPackageLevel_differentPackage_instrumentation_package() throws Exception {
    TestCase tc = new DefaultTestCase();
    ClassPathHandler.getInstance().changeTargetCPtoTheSameAsEvoSuite();
    InstrumentingClassLoader loader = new InstrumentingClassLoader();
    Class<?> example = loader.loadClass("com.examples.with.different.packagename.fm.ExamplePackageLevel");
    VariableReference ref = new VariableReferenceImpl(tc, example);
    try {
        FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, example);
        fail();
    } catch (java.lang.IllegalArgumentException e) {
    // expected
    }
// tc.addStatement(mockStmt);
// execute(tc);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) NonInstrumentingClassLoader(org.evosuite.instrumentation.NonInstrumentingClassLoader) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) Test(org.junit.Test)

Example 8 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class PrimitiveExpression method copy.

/**
 * {@inheritDoc}
 */
@Override
public Statement copy(TestCase newTestCase, int offset) {
    VariableReference newRetVal = new VariableReferenceImpl(newTestCase, retval.getType());
    VariableReference newLeftOperand = newTestCase.getStatement(leftOperand.getStPosition()).getReturnValue();
    VariableReference newRightOperand = newTestCase.getStatement(rightOperand.getStPosition()).getReturnValue();
    return new PrimitiveExpression(newTestCase, newRetVal, newLeftOperand, operator, newRightOperand);
// return new PrimitiveExpression(newTestCase, retval, leftOperand, operator, rightOperand);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl)

Example 9 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class EqualsHashcodeContract 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 });
        Method hashCodeMethod = a.getGenericClass().getRawClass().getMethod("hashCode", new Class<?>[] {});
        GenericMethod genericEqualsMethod = new GenericMethod(equalsMethod, a.getGenericClass());
        GenericMethod genericHashCodeMethod = new GenericMethod(hashCodeMethod, a.getGenericClass());
        // Create x = a.equals(b)
        Statement st1 = new MethodStatement(test, genericEqualsMethod, a, Arrays.asList(new VariableReference[] { b }));
        VariableReference x = test.addStatement(st1, statement.getPosition() + 1);
        // Create y = a.hashCode();
        Statement st2 = new MethodStatement(test, genericHashCodeMethod, a, Arrays.asList(new VariableReference[] {}));
        VariableReference y = test.addStatement(st2, statement.getPosition() + 2);
        // Create z = b.hashCode();
        Statement st3 = new MethodStatement(test, genericHashCodeMethod, b, Arrays.asList(new VariableReference[] {}));
        VariableReference z = test.addStatement(st3, statement.getPosition() + 3);
        // Create w = z == z
        VariableReference w = new VariableReferenceImpl(test, boolean.class);
        PrimitiveExpression exp = new PrimitiveExpression(test, w, y, Operator.EQUALS, z);
        w = test.addStatement(exp, statement.getPosition() + 4);
        Statement newStatement = test.getStatement(w.getStPosition());
        // Create assertEquals(x, w)
        EqualsAssertion assertion = new EqualsAssertion();
        assertion.setStatement(newStatement);
        assertion.setSource(x);
        assertion.setDest(w);
        assertion.setValue(true);
        newStatement.addAssertion(assertion);
        newStatement.addComment("Violates contract equals - hashcode");
    } 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) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) PrimitiveExpression(org.evosuite.testcase.statements.PrimitiveExpression) EqualsAssertion(org.evosuite.assertion.EqualsAssertion)

Example 10 with VariableReferenceImpl

use of org.evosuite.testcase.variable.VariableReferenceImpl in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testAll_twice.

@Test
public void testAll_twice() throws Exception {
    TestCase tc = new DefaultTestCase();
    VariableReference ref = new VariableReferenceImpl(tc, Foo.class);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, ref, Foo.class);
    VariableReference mock = tc.addStatement(mockStmt);
    VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("all_twice", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    Assert.assertEquals(0, mockStmt.getNumParameters());
    // execute first time with default mock
    Scope scope = execute(tc);
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(14, types.size());
}
Also used : Type(java.lang.reflect.Type) VariableReference(org.evosuite.testcase.variable.VariableReference) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) VariableReferenceImpl(org.evosuite.testcase.variable.VariableReferenceImpl) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 TestCase (org.evosuite.testcase.TestCase)14 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)13 Test (org.junit.Test)13 GenericMethod (org.evosuite.utils.generic.GenericMethod)5 Type (java.lang.reflect.Type)4 ConstantValue (org.evosuite.testcase.variable.ConstantValue)4 Method (java.lang.reflect.Method)3 NonInstrumentingClassLoader (org.evosuite.instrumentation.NonInstrumentingClassLoader)3 Scope (org.evosuite.testcase.execution.Scope)3 ClassWithOverloadedMethods (com.examples.with.different.packagename.utils.generic.ClassWithOverloadedMethods)2 ClassWithoutOverloadedMethods (com.examples.with.different.packagename.utils.generic.ClassWithoutOverloadedMethods)2 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)2 BooleanPrimitiveStatement (org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement)2 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)2 EqualsAssertion (org.evosuite.assertion.EqualsAssertion)1 MethodStatement (org.evosuite.testcase.statements.MethodStatement)1 PrimitiveExpression (org.evosuite.testcase.statements.PrimitiveExpression)1 Statement (org.evosuite.testcase.statements.Statement)1