Search in sources :

Example 6 with GenericField

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

the class FactoryTestSystemTest method testAddFieldAssignmentReuse.

@Test
public void testAddFieldAssignmentReuse() throws ConstructionFailedException, NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException {
    TestFactory testFactory = TestFactory.getInstance();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
    GenericField field = new GenericField(sut.getField("setMe"), sut);
    DefaultTestCase test = new DefaultTestCase();
    testFactory.addFieldAssignment(test, field, 0, 0);
    assertEquals(3, test.size());
    Properties.PRIMITIVE_REUSE_PROBABILITY = 1.0;
    Properties.OBJECT_REUSE_PROBABILITY = 1.0;
    testFactory.reset();
    testFactory.addFieldAssignment(test, field, 3, 0);
    assertEquals(4, test.size());
    String code = test.toCode();
    System.out.println(code);
    assertTrue(code.contains("factoryExample0.setMe"));
    assertFalse(code.contains("factoryExample1"));
}
Also used : FactoryExample(com.examples.with.different.packagename.FactoryExample) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 7 with GenericField

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

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

the class FactoryTestSystemTest method testAddFieldAssignment.

@Test
public void testAddFieldAssignment() throws ConstructionFailedException, NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException {
    TestFactory testFactory = TestFactory.getInstance();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
    GenericField field = new GenericField(sut.getField("setMe"), sut);
    DefaultTestCase test = new DefaultTestCase();
    testFactory.addFieldAssignment(test, field, 0, 0);
    assertEquals(3, test.size());
    String code = test.toCode();
    System.out.println(code);
    assertTrue(code.contains("factoryExample0.setMe"));
    assertFalse(code.contains("factoryExample1"));
}
Also used : FactoryExample(com.examples.with.different.packagename.FactoryExample) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 9 with GenericField

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

the class FactoryTestSystemTest method testAddFieldNoreuse.

@Test
public void testAddFieldNoreuse() throws ConstructionFailedException, NoSuchMethodException, SecurityException, NoSuchFieldException, ClassNotFoundException {
    TestFactory testFactory = TestFactory.getInstance();
    Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName());
    GenericField field = new GenericField(sut.getField("setMe"), sut);
    DefaultTestCase test = new DefaultTestCase();
    testFactory.addField(test, field, 0, 0);
    assertEquals(2, test.size());
    testFactory.reset();
    Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0;
    Properties.OBJECT_REUSE_PROBABILITY = 0.0;
    testFactory.addField(test, field, 2, 0);
    System.out.println(test.toCode());
    assertEquals(4, test.size());
    String code = test.toCode();
    System.out.println(code);
    assertTrue(code.contains("factoryExample0.setMe"));
    assertTrue(code.contains("factoryExample1.setMe"));
    assertFalse(code.contains("factoryExample2"));
}
Also used : FactoryExample(com.examples.with.different.packagename.FactoryExample) GenericField(org.evosuite.utils.generic.GenericField) Test(org.junit.Test)

Example 10 with GenericField

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

Aggregations

GenericField (org.evosuite.utils.generic.GenericField)27 Test (org.junit.Test)11 FactoryExample (com.examples.with.different.packagename.FactoryExample)8 VariableReference (org.evosuite.testcase.variable.VariableReference)8 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)8 GenericMethod (org.evosuite.utils.generic.GenericMethod)8 AssignmentStatement (org.evosuite.testcase.statements.AssignmentStatement)6 FieldReference (org.evosuite.testcase.variable.FieldReference)6 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)5 GenericClass (org.evosuite.utils.generic.GenericClass)5 CaptureType (com.googlecode.gentyref.CaptureType)4 Field (java.lang.reflect.Field)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 FieldStatement (org.evosuite.testcase.statements.FieldStatement)3 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)3 DowncastExample (com.examples.with.different.packagename.test.DowncastExample)2 Collection (java.util.Collection)2 LinkedHashSet (java.util.LinkedHashSet)2