VariableReference ret = addField(test, (GenericField) o, position, recursionDepth + 1); ret.setDistance(recursionDepth + 1); logger.debug("Success in generating type Object.class");
addFieldAssignment(test, f, position, 0); } else { addField(test, f, position, 0);
@Test public void testAddField() 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()); String code = test.toCode(); System.out.println(code); assertTrue(code.contains("factoryExample0.setMe")); assertFalse(code.contains("factoryExample1")); }
/** * Append statement s, trying to satisfy parameters * * Called from TestChromosome when doing crossover * * @param test * @param statement */ public void appendStatement(TestCase test, Statement statement) throws ConstructionFailedException { currentRecursion.clear(); if (statement instanceof ConstructorStatement) { addConstructor(test, ((ConstructorStatement) statement).getConstructor(), test.size(), 0); } else if (statement instanceof MethodStatement) { GenericMethod method = ((MethodStatement) statement).getMethod(); addMethod(test, method, test.size(), 0); } else if (statement instanceof PrimitiveStatement<?>) { addPrimitive(test, (PrimitiveStatement<?>) statement, test.size()); // test.statements.add((PrimitiveStatement) statement); } else if (statement instanceof FieldStatement) { addField(test, ((FieldStatement) statement).getField(), test.size(), 0); } }
ret = addField(test, (GenericField) o, position, recursionDepth + 1); } else if (o.isMethod()) { logger.debug("Attempting generating of " + type + " via method " + (o) + " of type " + type);
@Test public void testAddFieldReuse() 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()); Properties.PRIMITIVE_REUSE_PROBABILITY = 1.0; Properties.OBJECT_REUSE_PROBABILITY = 1.0; testFactory.reset(); testFactory.addField(test, field, 2, 0); assertEquals(3, test.size()); String code = test.toCode(); System.out.println(code); assertTrue(code.contains("factoryExample0.setMe")); assertFalse(code.contains("factoryExample1")); }
@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")); }