Search in sources :

Example 26 with GenericMethod

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

the class TestCodeVisitorTest method testInnerClassAbstractEnum.

/*
     * There are some weird enum constructs in Closure, so we need to check that enum names
     * don't contain the name of the anonymous class they might represent
     */
@Test
public void testInnerClassAbstractEnum() throws NoSuchMethodException, ConstructionFailedException {
    // first construct a test case for the Generic method
    TestCase tc = new DefaultTestCase();
    VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(AbstractEnumUser.class.getDeclaredConstructor(), AbstractEnumUser.class), 0, 0);
    EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, AbstractEnumInInnerClass.AnEnum.class);
    primitiveStatement.setValue(AbstractEnumInInnerClass.AnEnum.FOO);
    VariableReference enumObject = tc.addStatement(primitiveStatement);
    Method m = AbstractEnumUser.class.getDeclaredMethod("foo", AbstractEnumInInnerClass.AnEnum.class);
    GenericMethod gm = new GenericMethod(m, AbstractEnumUser.class);
    MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject));
    tc.addStatement(ms);
    // Finally, visit the test
    TestCodeVisitor visitor = new TestCodeVisitor();
    // should not throw exception
    tc.accept(visitor);
    String code = visitor.getCode();
    System.out.println(code);
    assertFalse(code.contains("= AbstractEnumInInnerClass.AnEnum.1.FOO"));
    assertTrue(code.contains("= AbstractEnumInInnerClass.AnEnum.FOO"));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) AbstractEnumUser(com.examples.with.different.packagename.AbstractEnumUser) Test(org.junit.Test)

Example 27 with GenericMethod

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

the class TestCodeVisitorTest method testInnerClassEnum.

@Test
public void testInnerClassEnum() throws Throwable {
    // first construct a test case for the Generic method
    TestCase tc = new DefaultTestCase();
    VariableReference userObject = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(EnumUser.class.getDeclaredConstructor(), EnumUser.class), 0, 0);
    EnumPrimitiveStatement primitiveStatement = new EnumPrimitiveStatement(tc, EnumInInnerClass.AnEnum.class);
    primitiveStatement.setValue(EnumInInnerClass.AnEnum.FOO);
    VariableReference enumObject = tc.addStatement(primitiveStatement);
    Method m = EnumUser.class.getDeclaredMethod("foo", EnumInInnerClass.AnEnum.class);
    GenericMethod gm = new GenericMethod(m, EnumUser.class);
    MethodStatement ms = new MethodStatement(tc, gm, userObject, Arrays.asList(enumObject));
    tc.addStatement(ms);
    // Finally, visit the test
    TestCodeVisitor visitor = new TestCodeVisitor();
    // should not throw exception
    tc.accept(visitor);
    String code = visitor.getCode();
    assertTrue(code.contains("= EnumInInnerClass.AnEnum.FOO"));
}
Also used : EnumUser(com.examples.with.different.packagename.EnumUser) AbstractEnumUser(com.examples.with.different.packagename.AbstractEnumUser) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) EnumInInnerClass(com.examples.with.different.packagename.EnumInInnerClass) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) Test(org.junit.Test)

Example 28 with GenericMethod

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

the class TestGenericAccessibleObject method testLinkedList.

@Test
public void testLinkedList() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = java.util.LinkedList.class;
    Method targetMethod = targetClass.getMethod("get", new Class<?>[] { int.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    System.out.println(genericMethod.getGeneratedClass().toString());
    Assert.assertTrue(genericMethod.getGeneratedClass().hasWildcardOrTypeVariables());
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiation();
    System.out.println(instantiatedMethod.getGeneratedClass().toString());
    Assert.assertFalse(instantiatedMethod.getGeneratedClass().hasWildcardOrTypeVariables());
}
Also used : GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 29 with GenericMethod

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

the class TestGenericAccessibleObject method testClassLoaderChange.

@Test
public void testClassLoaderChange() throws NoSuchMethodException, SecurityException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GenericClassTwoParameters.class;
    Method creatorMethod = targetClass.getMethod("create", new Class<?>[] {});
    Method targetMethod = targetClass.getMethod("get", new Class<?>[] { Object.class });
    Method inspectorMethod = targetClass.getMethod("testMe", new Class<?>[] {});
    Constructor<?> intConst = Integer.class.getConstructor(new Class<?>[] { int.class });
    GenericClass listOfInteger = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GenericClassTwoParameters<Integer, Integer>>() {
    }.getType());
    GenericMethod genericCreatorMethod = new GenericMethod(creatorMethod, targetClass).getGenericInstantiationFromReturnValue(listOfInteger);
    System.out.println(genericCreatorMethod.getGeneratedClass().toString());
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass).copyWithNewOwner(genericCreatorMethod.getGeneratedClass());
    System.out.println(genericMethod.getGeneratedClass().toString());
    DefaultTestCase test = new DefaultTestCase();
    MethodStatement ms1 = new MethodStatement(test, genericCreatorMethod, (VariableReference) null, new ArrayList<VariableReference>());
    test.addStatement(ms1);
    IntPrimitiveStatement ps1 = (IntPrimitiveStatement) PrimitiveStatement.getPrimitiveStatement(test, int.class);
    test.addStatement(ps1);
    GenericConstructor intConstructor = new GenericConstructor(intConst, Integer.class);
    List<VariableReference> constParam = new ArrayList<VariableReference>();
    constParam.add(ps1.getReturnValue());
    ConstructorStatement cs1 = new ConstructorStatement(test, intConstructor, constParam);
    // test.addStatement(cs1);
    List<VariableReference> callParam = new ArrayList<VariableReference>();
    callParam.add(ps1.getReturnValue());
    MethodStatement ms2 = new MethodStatement(test, genericMethod, ms1.getReturnValue(), callParam);
    test.addStatement(ms2);
    Inspector inspector = new Inspector(targetClass, inspectorMethod);
    Assertion assertion = new InspectorAssertion(inspector, ms2, ms1.getReturnValue(), 0);
    ms2.addAssertion(assertion);
    String code = test.toCode();
    ClassLoader loader = new InstrumentingClassLoader();
    Properties.TARGET_CLASS = targetClass.getCanonicalName();
    Properties.CRITERION = new Criterion[1];
    Properties.CRITERION[0] = Criterion.MUTATION;
    DefaultTestCase testCopy = test.clone();
    testCopy.changeClassLoader(loader);
    String code2 = testCopy.toCode();
    Assert.assertEquals(code, code2);
    Assert.assertEquals(code, test.toCode());
    testCopy.removeAssertion(assertion);
    Assert.assertEquals(code, test.toCode());
    // test.removeAssertion(assertion);
    test.removeAssertions();
    System.out.println(test.toCode());
}
Also used : InspectorAssertion(org.evosuite.assertion.InspectorAssertion) ArrayList(java.util.ArrayList) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericClass(org.evosuite.utils.generic.GenericClass) Inspector(org.evosuite.assertion.Inspector) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) Assertion(org.evosuite.assertion.Assertion) InspectorAssertion(org.evosuite.assertion.InspectorAssertion) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) InstrumentingClassLoader(org.evosuite.instrumentation.InstrumentingClassLoader) TypeToken(com.googlecode.gentyref.TypeToken) Test(org.junit.Test)

Example 30 with GenericMethod

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

the class TestGenericAccessibleObject method testGuavaExample3.

@Test
public void testGuavaExample3() throws SecurityException, NoSuchMethodException, ConstructionFailedException {
    Class<?> targetClass = com.examples.with.different.packagename.generic.GuavaExample3.class;
    GenericClass genericInstantiation = new GenericClass(new TypeToken<com.examples.with.different.packagename.generic.GuavaExample3<String, String, Object>>() {
    }.getType());
    Method targetMethod = targetClass.getMethod("create", new Class<?>[] { com.examples.with.different.packagename.generic.GuavaExample3.class });
    GenericMethod genericMethod = new GenericMethod(targetMethod, targetClass);
    System.out.println(genericMethod.getGeneratedClass().toString());
    Assert.assertTrue(genericMethod.getGeneratedClass().hasWildcardOrTypeVariables());
    System.out.println("------------------");
    GenericMethod instantiatedMethod = genericMethod.getGenericInstantiationFromReturnValue(genericInstantiation);
    System.out.println(instantiatedMethod.getGeneratedClass().toString());
    Assert.assertFalse(instantiatedMethod.getGeneratedClass().hasWildcardOrTypeVariables());
    Assert.assertEquals(genericInstantiation, instantiatedMethod.getGeneratedClass());
}
Also used : 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)

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