Search in sources :

Example 81 with VariableReference

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

the class FactoryTestSystemTest method testMethodFor.

@Test
public void testMethodFor() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException {
    TestFactory testFactory = TestFactory.getInstance();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
    GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut);
    GenericMethod method = new GenericMethod(sut.getMethod("testByte", byte.class, byte.class), sut);
    DefaultTestCase test = new DefaultTestCase();
    Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
    Properties.OBJECT_REUSE_PROBABILITY = 0.0;
    VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0);
    testFactory.reset();
    VariableReference var2 = testFactory.addConstructor(test, constructor, 1, 0);
    testFactory.addMethodFor(test, var1, method, 2);
    testFactory.reset();
    testFactory.addMethodFor(test, var2, method, 3);
    String code = test.toCode();
    System.out.println(code);
    assertEquals(8, test.size());
    assertTrue(code.contains("factoryExample0.testByte"));
    // byte2 is the first return value
    assertTrue(code.contains("factoryExample1.testByte"));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) FactoryExample(com.examples.with.different.packagename.FactoryExample) GenericMethod(org.evosuite.utils.generic.GenericMethod) Test(org.junit.Test)

Example 82 with VariableReference

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

the class FactoryTestSystemTest method testFieldFor.

@Test
public void testFieldFor() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException, NoSuchFieldException {
    TestFactory testFactory = TestFactory.getInstance();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
    GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut);
    GenericField field = new GenericField(sut.getField("setMe"), sut);
    DefaultTestCase test = new DefaultTestCase();
    Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
    Properties.OBJECT_REUSE_PROBABILITY = 0.0;
    VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0);
    testFactory.reset();
    VariableReference var2 = testFactory.addConstructor(test, constructor, 1, 0);
    testFactory.addFieldFor(test, var1, field, 2);
    testFactory.reset();
    testFactory.addFieldFor(test, var2, field, 3);
    String code = test.toCode();
    System.out.println(code);
    assertEquals(6, test.size());
    assertTrue(code.contains("factoryExample0.setMe"));
    // byte2 is the first return value
    assertTrue(code.contains("factoryExample1.setMe"));
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) FactoryExample(com.examples.with.different.packagename.FactoryExample) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 83 with VariableReference

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

the class FactoryTestSystemTest method testFieldForWrongPosition.

@Test(expected = ConstructionFailedException.class)
public void testFieldForWrongPosition() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException, NoSuchFieldException {
    TestFactory testFactory = TestFactory.getInstance();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
    GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut);
    GenericField field = new GenericField(sut.getField("setMe"), sut);
    DefaultTestCase test = new DefaultTestCase();
    Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
    Properties.OBJECT_REUSE_PROBABILITY = 0.0;
    VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0);
    testFactory.reset();
    testFactory.addFieldFor(test, var1, field, 0);
}
Also used : VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) FactoryExample(com.examples.with.different.packagename.FactoryExample) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 84 with VariableReference

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

the class InputCoverageFitnessFunctionSystemTest method testInputCoverageClassWithField.

@Test
public void testInputCoverageClassWithField() throws NoSuchFieldException, NoSuchMethodException {
    Class<?> sut = ClassWithField.class;
    DefaultTestCase tc = new DefaultTestCase();
    // ClassWithField classWithField0 = new ClassWithField();
    GenericConstructor constructor = new GenericConstructor(sut.getConstructors()[0], sut);
    ConstructorStatement constructorStatement = new ConstructorStatement(tc, constructor, Arrays.asList(new VariableReference[] {}));
    VariableReference obj = tc.addStatement(constructorStatement);
    // classWithField0.testFoo(classWithField0.BOOLEAN_FIELD);
    FieldReference field = new FieldReference(tc, new GenericField(sut.getDeclaredField("BOOLEAN_FIELD"), sut), obj);
    Method m = sut.getMethod("testFoo", new Class<?>[] { Boolean.TYPE });
    GenericMethod gm = new GenericMethod(m, sut);
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    // classWithField0.BOOLEAN_FIELD = false;
    VariableReference boolRef = tc.addStatement(new BooleanPrimitiveStatement(tc, false));
    tc.addStatement(new AssignmentStatement(tc, field, boolRef));
    tc.addStatement(new MethodStatement(tc, gm, obj, Arrays.asList(new VariableReference[] { field })));
    Properties.TARGET_CLASS = sut.getCanonicalName();
    Properties.JUNIT_TESTS = true;
    TestSuiteChromosome testSuite = new TestSuiteChromosome();
    testSuite.addTest(tc);
    FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(Properties.Criterion.INPUT);
    assertEquals("Should be 0.0", 0.0, ffunction.getFitness(testSuite), 0.0);
    assertEquals("Should be 1.0", 1.0, testSuite.getCoverage(ffunction), 0.0);
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldReference(org.evosuite.testcase.variable.FieldReference) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) ClassWithField(com.examples.with.different.packagename.coverage.ClassWithField) AssignmentStatement(org.evosuite.testcase.statements.AssignmentStatement) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 85 with VariableReference

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

the class DeleteMutationSystemTest method getTwoIntTest.

private TestCase getTwoIntTest(int x, int y, int x1, int y1) throws NoSuchMethodException, SecurityException, ConstructionFailedException, ClassNotFoundException {
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    GenericClass clazz = new GenericClass(sut);
    DefaultTestCase test = new DefaultTestCase();
    GenericConstructor gc = new GenericConstructor(clazz.getRawClass().getConstructors()[0], clazz);
    TestFactory testFactory = TestFactory.getInstance();
    VariableReference callee = testFactory.addConstructor(test, gc, 0, 0);
    test.addStatement(new IntPrimitiveStatement(test, x));
    test.addStatement(new IntPrimitiveStatement(test, y));
    VariableReference wrongIntVar1 = test.addStatement(new IntPrimitiveStatement(test, x1));
    VariableReference wrongIntVar2 = test.addStatement(new IntPrimitiveStatement(test, y1));
    Method m = clazz.getRawClass().getMethod("testMe", new Class<?>[] { int.class, int.class });
    GenericMethod method = new GenericMethod(m, sut);
    MethodStatement ms = new MethodStatement(test, method, callee, Arrays.asList(new VariableReference[] { wrongIntVar1, wrongIntVar2 }));
    test.addStatement(ms);
    return test;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) GenericClass(org.evosuite.utils.generic.GenericClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)

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