Search in sources :

Example 16 with GenericConstructor

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

the class ConstraintVerifierTest method testBaseTest.

@Test
public void testBaseTest() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    factory.addConstructor(tc.getTestCase(), new GenericConstructor(Object.class.getConstructor(), Object.class), 0, 0);
    Assert.assertEquals(1, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : GenericConstructor(org.evosuite.utils.generic.GenericConstructor) Test(org.junit.Test)

Example 17 with GenericConstructor

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

the class ConstraintVerifierTest method testUniqueConstructors.

@Test
public void testUniqueConstructors() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    Properties.JEE = true;
    factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    // doing it a second time should fail
    try {
        factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
        Assert.fail();
    } catch (Exception e) {
    // expected
    }
}
Also used : GenericConstructor(org.evosuite.utils.generic.GenericConstructor) Test(org.junit.Test)

Example 18 with GenericConstructor

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

the class TestCodeVisitorTest method testGenerics_methodWithExtends.

@Test
public void testGenerics_methodWithExtends() throws NoSuchMethodException, ConstructionFailedException {
    // first construct a test case for the Generic method
    TestCase tc = new DefaultTestCase();
    TestFactory.getInstance().addConstructor(tc, new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0);
    VariableReference genericClass = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(ClassWithGeneric.class.getDeclaredConstructor(), ClassWithGeneric.class), 1, 0);
    Method m = ClassWithGeneric.class.getDeclaredMethod("hello", Servlet.class);
    GenericMethod gm = new GenericMethod(m, ClassWithGeneric.class);
    TestFactory.getInstance().addMethodFor(tc, genericClass, gm, 2);
    // Check if generic types were correctly analyzed/inferred
    Type[] types = gm.getParameterTypes();
    // only 1 input
    assertEquals(1, types.length);
    Type type = types[0];
    Assert.assertNotNull(type);
    TypeVariable<?> tv = (TypeVariable<?>) type;
    assertEquals(1, tv.getBounds().length);
    Class<?> upper = (Class<?>) tv.getBounds()[0];
    assertEquals(Servlet.class, upper);
    // Finally, visit the test
    TestCodeVisitor visitor = new TestCodeVisitor();
    // should not throw exception
    tc.accept(visitor);
}
Also used : 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) Type(java.lang.reflect.Type) TypeVariable(java.lang.reflect.TypeVariable) EnumInInnerClass(com.examples.with.different.packagename.EnumInInnerClass) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) Test(org.junit.Test)

Example 19 with GenericConstructor

use of org.evosuite.utils.generic.GenericConstructor 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 20 with GenericConstructor

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

Aggregations

GenericConstructor (org.evosuite.utils.generic.GenericConstructor)76 GenericMethod (org.evosuite.utils.generic.GenericMethod)61 VariableReference (org.evosuite.testcase.variable.VariableReference)56 Test (org.junit.Test)47 Method (java.lang.reflect.Method)30 GenericClass (org.evosuite.utils.generic.GenericClass)25 MethodStatement (org.evosuite.testcase.statements.MethodStatement)22 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)19 ArrayList (java.util.ArrayList)17 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)11 Injector (org.evosuite.runtime.javaee.injection.Injector)9 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)8 GenericField (org.evosuite.utils.generic.GenericField)8 AbstractEnumInInnerClass (com.examples.with.different.packagename.AbstractEnumInInnerClass)7 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)7 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)7 EnumInInnerClass (com.examples.with.different.packagename.EnumInInnerClass)6 FactoryExample (com.examples.with.different.packagename.FactoryExample)6 Type (java.lang.reflect.Type)6