Search in sources :

Example 56 with GenericConstructor

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

Example 57 with GenericConstructor

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

the class ConstraintVerifierTest method testPostConstructIssue.

@Test
public void testPostConstructIssue() 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);
    // this will also add a statement for the Class
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class, Class.class), Injector.class), 1, 0);
    Assert.assertEquals(3, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    boolean mutated = tc.deleteStatement(factory, 2);
    // should fail to delete the post construct
    Assert.assertFalse(mutated);
    Assert.assertEquals(3, tc.size());
    Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1));
    mutated = tc.deleteStatement(factory, 1);
    // eliminating the Class.class var should fail as well, as post construct accepts no null
    Assert.assertFalse(mutated);
    Assert.assertEquals(3, tc.size());
    mutated = tc.deleteStatement(factory, 0);
    // eliminating the servlet is fine
    Assert.assertTrue(mutated);
    // tricky, as Class.class is not bounded to the servlet
    Assert.assertEquals(1, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
}
Also used : 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)

Example 58 with GenericConstructor

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

the class TestCodeVisitorTest method testClashingImportNames.

@Test
public void testClashingImportNames() throws NoSuchMethodException, ConstructionFailedException {
    TestCase tc = new DefaultTestCase();
    TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class.getDeclaredConstructor(), com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class), 0, 0);
    TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class.getDeclaredConstructor(), com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class), 1, 0);
    TestCodeVisitor visitor = new TestCodeVisitor();
    tc.accept(visitor);
    System.out.println(visitor.getCode());
    Set<Class<?>> imports = visitor.getImports();
    // Imported
    assertTrue(imports.contains(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
    // Not imported as the fully qualified name is used
    assertFalse(imports.contains(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
    assertEquals("ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
    assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
}
Also used : GenericConstructor(org.evosuite.utils.generic.GenericConstructor) EnumInInnerClass(com.examples.with.different.packagename.EnumInInnerClass) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) Test(org.junit.Test)

Example 59 with GenericConstructor

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

the class TestCodeVisitorTest method testClashingImportNamesSubClasses.

@Test
public void testClashingImportNamesSubClasses() throws NoSuchMethodException, ConstructionFailedException {
    TestCase tc = new DefaultTestCase();
    TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class.getDeclaredConstructor(), com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class), 0, 0);
    TestFactory.getInstance().addConstructor(tc, new GenericConstructor(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class.getDeclaredConstructor(), com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class), 1, 0);
    TestCodeVisitor visitor = new TestCodeVisitor();
    tc.accept(visitor);
    System.out.println(visitor.getCode());
    Set<Class<?>> imports = visitor.getImports();
    // Imported
    assertTrue(imports.contains(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
    // Not imported as the fully qualified name is used
    assertFalse(imports.contains(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
    assertEquals("ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.class));
    assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.class));
    assertEquals("ExampleWithInnerClass.Foo", visitor.getClassName(com.examples.with.different.packagename.otherpackage.ExampleWithInnerClass.Foo.class));
    assertEquals("com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar", visitor.getClassName(com.examples.with.different.packagename.subpackage.ExampleWithInnerClass.Bar.class));
}
Also used : GenericConstructor(org.evosuite.utils.generic.GenericConstructor) EnumInInnerClass(com.examples.with.different.packagename.EnumInInnerClass) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) Test(org.junit.Test)

Example 60 with GenericConstructor

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

the class TestCodeVisitorTest method testGenerics_staticMethod.

@Test
public void testGenerics_staticMethod() throws NoSuchMethodException, ConstructionFailedException {
    // first construct a test case for the Generic method
    TestCase tc = new DefaultTestCase();
    TestFactory.getInstance().addConstructor(tc, new GenericConstructor(Object.class.getDeclaredConstructor(), Object.class), 0, 0);
    Method m = TestCodeVisitorTest.class.getDeclaredMethod("bar", Object.class);
    GenericMethod gm = new GenericMethod(m, TestCodeVisitorTest.class);
    TestFactory.getInstance().addMethod(tc, gm, 1, 0);
    // 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);
    WildcardTypeImpl wt = (WildcardTypeImpl) type;
    assertEquals(0, wt.getLowerBounds().length);
    assertEquals(1, wt.getUpperBounds().length);
    Class<?> upper = (Class<?>) wt.getUpperBounds()[0];
    assertEquals(Object.class, upper);
    // Finally, visit the test
    TestCodeVisitor visitor = new TestCodeVisitor();
    // should not throw exception
    tc.accept(visitor);
    System.out.println(visitor.getCode());
}
Also used : Type(java.lang.reflect.Type) WildcardTypeImpl(org.evosuite.utils.generic.WildcardTypeImpl) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) EnumInInnerClass(com.examples.with.different.packagename.EnumInInnerClass) AbstractEnumInInnerClass(com.examples.with.different.packagename.AbstractEnumInInnerClass) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) 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