Search in sources :

Example 21 with GenericMethod

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

the class ConstraintVerifierTest method testDeleteAfter.

@Test
public void testDeleteAfter() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    // get a var for EvoHttpServletRequest
    VariableReference req = factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("getNullEvoHttpServletRequest"), ConstraintVerifierTest.class), 0, 0);
    // make it a POST
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asPOST"), EvoHttpServletRequest.class), 1, 0);
    // now add a call to 'asMultipartFormData' which does depend on POST
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asMultipartFormData"), EvoHttpServletRequest.class), 2, 0);
    Assert.assertEquals(3, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    // deleting the last one should be fine, but not the POST, as asMultipartFormData has an 'after' dependency on it
    Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 2));
    // should not be able to delete POST directly
    Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1));
    // what about the first statement where the var is obtained? that should be valid to delete
    Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0));
    boolean mutated = tc.deleteStatement(factory, 1);
    // should fail to delete POST
    Assert.assertFalse(mutated);
    Assert.assertEquals(3, tc.size());
    mutated = tc.deleteStatement(factory, 0);
    Assert.assertTrue(mutated);
    // should end up deleting everything
    Assert.assertEquals(0, tc.size());
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) EvoHttpServletRequest(org.evosuite.runtime.javaee.javax.servlet.http.EvoHttpServletRequest) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 22 with GenericMethod

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

the class ConstraintVerifierTest method testNoDirectInsertion.

@Test
public void testNoDirectInsertion() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    GenericMethod gm = new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("noDirect", null), ConstraintVerifierTest.class);
    // should not be possible to insert it
    int pos = ConstraintVerifier.getAValidPositionForInsertion(gm, tc.getTestCase(), 0);
    Assert.assertTrue(pos < 0);
}
Also used : GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 23 with GenericMethod

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

the class ConstraintVerifierTest method testInitializingBoundedVariable_wrong_noConstructor.

@Test
public void testInitializingBoundedVariable_wrong_noConstructor() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("getAFakeServletInstance"), ConstraintVerifierTest.class), 0, 0);
    // initializing bounding variable method called on instance not generated with new
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0);
    Assert.assertEquals(2, tc.size());
    Assert.assertFalse(ConstraintVerifier.verifyTest(tc));
}
Also used : Injector(org.evosuite.runtime.javaee.injection.Injector) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 24 with GenericMethod

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

the class ConstraintVerifierTest method testInitializingBoundedVariable_wrong_callingMethodsBeforeInit.

@Test
public void testInitializingBoundedVariable_wrong_callingMethodsBeforeInit() 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);
    factory.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 1);
    // initializing bounding variable method cannot be called here after "foo" is called on the bounded variable
    factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 2, 0);
    Assert.assertEquals(3, tc.size());
    Assert.assertFalse(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 25 with GenericMethod

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

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