Search in sources :

Example 11 with GenericConstructor

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

the class ConstraintVerifierTest method testAtMostOnce.

@Test
public void testAtMostOnce() 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.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
    // 2 different methods that can be used at most once
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getRequest"), EvoServletState.class), 2, 0);
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getResponse"), EvoServletState.class), 3, 0);
    Assert.assertEquals(4, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    // add an invalid new call
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getResponse"), EvoServletState.class), 4, 0);
    Assert.assertEquals(5, tc.size());
    // check should fail
    Assert.assertFalse(ConstraintVerifier.verifyTest(tc));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 12 with GenericConstructor

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

the class ConstraintVerifierTest method testInitializingBoundedVariable_wrong_inputInOtherMethodBeforeInit.

@Test
public void testInitializingBoundedVariable_wrong_inputInOtherMethodBeforeInit() 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.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("takeServletAsInput", FakeServlet.class), ConstraintVerifierTest.class), 1, 0);
    // initializing bounding variable method cannot be called here after the bounded variable has been used as input in some other method
    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 13 with GenericConstructor

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

the class ConstraintVerifierTest method testExcludeOthers.

@Test
public void testExcludeOthers() 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.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("initServlet", Servlet.class), EvoServletState.class), 1, 0);
    VariableReference req = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getRequest"), EvoServletState.class), 2, 0);
    factory.addMethodFor(tc.getTestCase(), req, new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asGET"), EvoHttpServletRequest.class), 3);
    Assert.assertEquals(tc.getTestCase().toCode(), 4, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    // once it is set as GET, we should not be able to change it to POST
    factory.addMethodFor(tc.getTestCase(), req, new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asPOST"), EvoHttpServletRequest.class), 4);
    Assert.assertEquals(tc.getTestCase().toCode(), 5, tc.size());
    Assert.assertFalse(ConstraintVerifier.verifyTest(tc));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) EvoHttpServletRequest(org.evosuite.runtime.javaee.javax.servlet.http.EvoHttpServletRequest) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 14 with GenericConstructor

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

the class ConstraintVerifierTest method testCanDelete.

@Test
public void testCanDelete() 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);
    Assert.assertEquals(2, 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));
    boolean mutated = tc.deleteStatement(factory, 1);
    // should fail
    Assert.assertFalse(mutated);
    Assert.assertEquals(2, tc.size());
    mutated = tc.deleteStatement(factory, 0);
    Assert.assertTrue(mutated);
    // deleting first statement should have had effect of removing the second as well
    Assert.assertEquals(0, tc.size());
}
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 15 with GenericConstructor

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

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