Search in sources :

Example 56 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestDSETestCaseLocalSearch method buildTestCase0.

/**
 * Creates the test case:
 *
 * <code>
 * int int0 = 10;
 * int int1 = 10;
 * int int2 = 10;
 * Foo.bar(int0,int1,int2);
 * </code>
 *
 * @return
 * @throws NoSuchMethodException
 * @throws SecurityException
 * @throws ClassNotFoundException
 */
private static DefaultTestCase buildTestCase0() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    TestCaseBuilder builder = new TestCaseBuilder();
    VariableReference int0 = builder.appendIntPrimitive(10);
    VariableReference int1 = builder.appendIntPrimitive(10);
    VariableReference int2 = builder.appendIntPrimitive(10);
    Class<?> fooClass = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    Method barMethod = fooClass.getMethod("bar", int.class, int.class, int.class);
    builder.appendMethod(null, barMethod, int0, int1, int2);
    return builder.getDefaultTestCase();
}
Also used : TestCaseBuilder(org.evosuite.symbolic.TestCaseBuilder) VariableReference(org.evosuite.testcase.variable.VariableReference) Method(java.lang.reflect.Method)

Example 57 with VariableReference

use of org.evosuite.testcase.variable.VariableReference in project evosuite by EvoSuite.

the class TestSimilarity method testBasicSimilarityDifferentTypes.

@Test
public void testBasicSimilarityDifferentTypes() {
    TestCase test1 = new DefaultTestCase();
    TestCase test2 = new DefaultTestCase();
    PrimitiveStatement<?> aInt = new IntPrimitiveStatement(test1, 42);
    test1.addStatement(aInt);
    PrimitiveStatement<?> aInt2 = new IntPrimitiveStatement(test1, 42);
    test1.addStatement(aInt2);
    PrimitiveStatement<?> bInt = new IntPrimitiveStatement(test2, 42);
    test2.addStatement(bInt);
    Constructor<?> c = Object.class.getConstructors()[0];
    ConstructorStatement cs = new ConstructorStatement(test2, new GenericConstructor(c, Object.class), new ArrayList<VariableReference>());
    test2.addStatement(cs);
    double score = DiversityObserver.getNeedlemanWunschScore(test1, test2);
    Assert.assertTrue(score <= 0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) Test(org.junit.Test)

Example 58 with VariableReference

use of org.evosuite.testcase.variable.VariableReference 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 59 with VariableReference

use of org.evosuite.testcase.variable.VariableReference 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 60 with VariableReference

use of org.evosuite.testcase.variable.VariableReference 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)

Aggregations

VariableReference (org.evosuite.testcase.variable.VariableReference)472 Method (java.lang.reflect.Method)289 TestCaseBuilder (org.evosuite.symbolic.TestCaseBuilder)143 Test (org.junit.Test)73 GenericMethod (org.evosuite.utils.generic.GenericMethod)68 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)55 MethodStatement (org.evosuite.testcase.statements.MethodStatement)44 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)38 ArrayList (java.util.ArrayList)31 GenericClass (org.evosuite.utils.generic.GenericClass)27 TestCase (org.evosuite.testcase.TestCase)26 CodeUnderTestException (org.evosuite.testcase.execution.CodeUnderTestException)26 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)25 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)19 Type (java.lang.reflect.Type)17 Statement (org.evosuite.testcase.statements.Statement)17 EvosuiteError (org.evosuite.testcase.execution.EvosuiteError)15 ArrayReference (org.evosuite.testcase.variable.ArrayReference)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)15 ReferenceConstant (org.evosuite.symbolic.expr.ref.ReferenceConstant)14