Search in sources :

Example 16 with GenericMethod

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

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

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

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

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

the class ConstraintVerifierTest method testExcludeMethod.

@Test
public void testExcludeMethod() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();
    // 'reset' is a method that shouldn't be used in a test
    factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("reset"), EvoServletState.class), 0, 0);
    Assert.assertEquals(tc.getTestCase().toCode(), 1, tc.size());
    Assert.assertFalse(ConstraintVerifier.verifyTest(tc));
}
Also used : EvoServletState(org.evosuite.runtime.javaee.javax.servlet.EvoServletState) GenericMethod(org.evosuite.utils.generic.GenericMethod) 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