Search in sources :

Example 31 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericMethodAlternativeBounds.

@Test
public void testGenericMethodAlternativeBounds() throws NoSuchMethodException, RuntimeException, ClassNotFoundException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericMethodAlternativeBounds.class;
    Method targetMethod = targetClass.getMethod("create", new Class<?>[] { Class.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    Assert.assertFalse(genericMethod.getOwnerClass().hasTypeVariables());
    List<GenericClass> parameters = genericMethod.getParameterClasses();
    Assert.assertFalse(parameters.get(0).hasTypeVariables());
    Assert.assertTrue(parameters.get(0).hasWildcardTypes());
    Assert.assertTrue(genericMethod.getGeneratedClass().hasWildcardTypes());
// Cannot instantiate because it requires inheritance tree to set up
// TODO
// GenericMethod instantiatedMethod = genericMethod.getGenericInstantiation();
// parameters = instantiatedMethod.getParameterClasses();
// Assert.assertFalse(parameters.get(0).hasTypeVariables());
// Assert.assertFalse(parameters.get(0).hasWildcardTypes());
// Assert.assertFalse(instantiatedMethod.getGeneratedClass().hasWildcardTypes());
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 32 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class TestGenericAccessibleObject method testGenericMethodFromReturnValueTypeVariable2.

@Test
public void testGenericMethodFromReturnValueTypeVariable2() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GuavaExample4.class;
    Method targetMethod = targetClass.getMethod("create", new Class<?>[] {});
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    GenericClass iterableIntegerClass = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GuavaExample4<java.lang.Iterable<Integer>>>() {
    }.getType());
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(iterableIntegerClass);
    System.out.println(instantiatedMethod.getGeneratedClass().toString());
    Assert.assertEquals(instantiatedMethod.getGeneratedClass().getRawClass(), GuavaExample4.class);
}
Also used : GuavaExample4(com.examples.with.different.packagename.generic.GuavaExample4) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 33 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testAll_once.

@Test
public void testAll_once() 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_once", 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(7, 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)

Example 34 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod in project evosuite by EvoSuite.

the class FunctionalMockStatementTest method testArray.

@Test
public void testArray() throws Exception {
    TestCase tc = new DefaultTestCase();
    /*
            String s = "...";
            String[] array = new String[1];
            array[0] = s;
            Foo foo = mock(Foo.class);
            getFirstInArray(foo);
         */
    final String MOCKED_VALUE = "Hello 42!!!";
    VariableReference aString = tc.addStatement(new StringPrimitiveStatement(tc, MOCKED_VALUE));
    ArrayReference mockedArray = (ArrayReference) tc.addStatement(new ArrayStatement(tc, String[].class, 1));
    ArrayIndex arrayIndex = new ArrayIndex(tc, mockedArray, 0);
    AssignmentStatement stmt = new AssignmentStatement(tc, arrayIndex, aString);
    tc.addStatement(stmt);
    FunctionalMockStatement mockStmt = new FunctionalMockStatement(tc, Foo.class, Foo.class);
    VariableReference mock = tc.addStatement(mockStmt);
    VariableReference result = tc.addStatement(new MethodStatement(tc, new GenericMethod(this.getClass().getDeclaredMethod("getFirstInArray", Foo.class), FunctionalMockStatementTest.class), null, Arrays.asList(mock)));
    // if not executed, should be no way to tell if needs new inputs
    Assert.assertFalse(mockStmt.doesNeedToUpdateInputs());
    Assert.assertEquals(0, mockStmt.getNumParameters());
    // execute first time with default mock
    Scope scope = execute(tc);
    Object obj = scope.getObject(result);
    // default mock value should be null for objects/arrays
    Assert.assertNull(obj);
    // after execution, there should be one variable to provide
    Assert.assertTrue(mockStmt.doesNeedToUpdateInputs());
    List<Type> types = mockStmt.updateMockedMethods();
    Assert.assertEquals(1, types.size());
    Assert.assertEquals(String[].class, types.get(0));
    // add int variable to list of mock expected returns
    mockStmt.addMissingInputs(Arrays.asList(mockedArray));
    Assert.assertEquals(1, mockStmt.getNumParameters());
    Assert.assertTrue(mockStmt.getParameterReferences().get(0).same(mockedArray));
    // re-execute with initialized mock
    scope = execute(tc);
    String val = (String) scope.getObject(result);
    Assert.assertEquals(MOCKED_VALUE, val);
}
Also used : ArrayReference(org.evosuite.testcase.variable.ArrayReference) VariableReference(org.evosuite.testcase.variable.VariableReference) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) ArrayIndex(org.evosuite.testcase.variable.ArrayIndex) Type(java.lang.reflect.Type) Scope(org.evosuite.testcase.execution.Scope) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Example 35 with GenericMethod

use of org.evosuite.utils.generic.GenericMethod 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)

Aggregations

GenericMethod (org.evosuite.utils.generic.GenericMethod)110 Test (org.junit.Test)68 VariableReference (org.evosuite.testcase.variable.VariableReference)64 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)60 Method (java.lang.reflect.Method)57 GenericClass (org.evosuite.utils.generic.GenericClass)40 MethodStatement (org.evosuite.testcase.statements.MethodStatement)29 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 ArrayList (java.util.ArrayList)18 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)15 TestCase (org.evosuite.testcase.TestCase)12 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)11 Injector (org.evosuite.runtime.javaee.injection.Injector)10 Type (java.lang.reflect.Type)9 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)8 EvoServletState (org.evosuite.runtime.javaee.javax.servlet.EvoServletState)8 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)8 GenericField (org.evosuite.utils.generic.GenericField)8 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)7