TestFactory testFactory = TestFactory.getInstance(); testFactory.addMethod(test, (GenericMethod) call, test.size(), 0); } else if (call.isConstructor()) { testFactory.addConstructor(test, (GenericConstructor) call, test.size(), 0); } else {
@Test(expected=ConstructionFailedException.class) public void testMethodForWrongPosition() 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(); testFactory.addMethodFor(test, var1, method, 0); }
return createNull(test, Object.class, position, recursionDepth); logger.debug("Chosen class for Object: {}", choice); if(choice.isString()) { return createOrReuseVariable(test, String.class, position, recursionDepth, null, allowNull, false, false); VariableReference ret = addField(test, (GenericField) o, position, recursionDepth + 1); ret.setDistance(recursionDepth + 1); logger.debug("Success in generating type Object.class"); VariableReference ret = addMethod(test, (GenericMethod) o, position, recursionDepth + 1); logger.debug("Success in generating type Object.class"); ret.setDistance(recursionDepth + 1); VariableReference ret = addConstructor(test, (GenericConstructor) o, position, recursionDepth + 1); logger.debug("Success in generating Object.class"); ret.setDistance(recursionDepth + 1);
/** * Create a random individual * * @param size */ private TestCase getRandomTestCase(int size) { TestCase test = new DefaultTestCase(); int num = 0; TestFactory testFactory = TestFactory.getInstance(); // Then add random stuff while (test.size() < size && num < Properties.MAX_ATTEMPTS) { testFactory.insertRandomStatement(test, test.size() - 1); num++; } //logger.debug("Randomized test case:" + test.toCode()); return test; }
+ var.getName() + ", distance: " + var.getDistance() + ", class: " + var.getClassName()); success = TestFactory.getInstance().insertRandomCallOnObjectAt(test, var, position); if(TestCluster.getInstance().getNumTestCalls() > 0) { logger.debug("Adding new call on UUT because var was null"); success = TestFactory.getInstance().insertRandomCall(test, position);
/** * 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); } }
@Test public void testInitializingBoundedVariable_wrong_callingMethodsBeforeInit() 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.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 1); //initializing bounding variable method cannot be called here after "foo" is called on the bounded variable 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)); }
@Test public void testUniqueConstructors() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); Properties.JEE = true; factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); //doing it a second time should fail try { factory.addConstructor(tc.getTestCase(), new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); Assert.fail(); } catch (Exception e){ //expected } }
(Randomness.nextDouble() < Properties.P_REFLECTION_ON_PRIVATE || TestCluster.getInstance().getNumTestCalls() == 0)){ logger.debug("Going to insert random reflection call"); return insertRandomReflectionCall(test,position, 0); logger.debug("Adding constructor call {}", c.getName()); name = c.getName(); addConstructor(test, c, position, 0); } else if (o.isMethod()) { GenericMethod m = (GenericMethod) o; callee = createObject(test, target, position, 0, null, false, false,true); //no FM for SUT position += test.size() - previousLength; previousLength = test.size(); addMethodFor(test, callee, m.copyWithNewOwner(callee.getGenericClass()), position); } else { addMethod(test, m, position, 0); logger.debug("Adding field {}",f.getName()); if (Randomness.nextBoolean()) { addFieldAssignment(test, f, position, 0); } else { addField(test, f, position, 0);
@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); }
@Test public void testIntegerDependency() throws ConstructionFailedException, NoSuchMethodException, SecurityException { TestFactory testFactory = TestFactory.getInstance(); GenericMethod method = new GenericMethod( FactoryExample.class.getMethod("testByte", byte.class, byte.class), FactoryExample.class); DefaultTestCase test = new DefaultTestCase(); Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0; testFactory.addMethod(test, method, 0, 0); String code = test.toCode(); System.out.println(code); assertEquals(4, test.size()); assertTrue(code.contains("factoryExample0.testByte(byte0, byte1)")); }
@Test public void testGenerics_methodWithExtends() throws NoSuchMethodException, ConstructionFailedException { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class), 0, 0); VariableReference genericClass = TestFactory.getInstance().addConstructor(tc, new GenericConstructor(ClassWithGeneric.class.getDeclaredConstructor(), ClassWithGeneric.class), 1, 0); Method m = ClassWithGeneric.class.getDeclaredMethod("hello", Servlet.class); GenericMethod gm = new GenericMethod(m, ClassWithGeneric.class); TestFactory.getInstance().addMethodFor(tc, genericClass, gm, 2); //Check if generic types were correctly analyzed/inferred Type[] types = gm.getParameterTypes(); assertEquals(1, types.length); //only 1 input Type type = types[0]; Assert.assertNotNull(type); TypeVariable<?> tv = (TypeVariable<?>) type; assertEquals(1, tv.getBounds().length); Class<?> upper = (Class<?>) tv.getBounds()[0]; assertEquals(Servlet.class, upper); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception }
ret = addFunctionalMock(test,type,position,recursionDepth + 1); ret = addFunctionalMock(test, type, position, recursionDepth + 1); } else if (clazz.isAbstract() && FunctionalMockStatement.canBeFunctionalMockedIncludingSUT(type)) { ret = addFunctionalMockForAbstractClass(test, type, position, recursionDepth + 1); return createObject(test, type, position, recursionDepth + 1, generatorRefToExclude, allowNull, canUseFunctionalMocks, canReuseVariables); } else { logger.debug("Found no new generators for {}", type); ret = addField(test, (GenericField) o, position, recursionDepth + 1); } else if (o.isMethod()) { logger.debug("Attempting generating of " + type + " via method " + (o) + " of type " + type); ret = addMethod(test, (GenericMethod) o, position, recursionDepth + 1); ret = addConstructor(test, (GenericConstructor) o, type, position, recursionDepth + 1); } else { logger.debug("No generators found for type {}", type);
@Test public void testObjectDependencyNoReuse() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException { TestFactory testFactory = TestFactory.getInstance(); Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName()); 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; testFactory.addMethod(test, method, 0, 0); testFactory.reset(); testFactory.addMethod(test, method, 4, 0); String code = test.toCode(); System.out.println(code); // With object reuse being 0, there should be no new instance of this object assertEquals(8, test.size()); assertTrue(code.contains("factoryExample0.testByte(byte0, byte1)")); // byte2 is the first return value assertTrue(code.contains("factoryExample1.testByte(byte3, byte4")); }
if(call.isStatic() || !method.getDeclaringClass().isAssignableFrom(callee.getVariableClass())) { addMethod(test, method, position, 0); } else { addMethodFor(test, callee, (GenericMethod) call.copyWithNewOwner(callee.getGenericClass()), addFieldAssignment(test, (GenericField) call, position, 0); } else { addFieldFor(test, callee, (GenericField) call.copyWithNewOwner(callee.getGenericClass()),
addConstructor(test, c, position, 0); return position; } else if (o.isMethod()) { callee = createObject(test, target, position, 0, null); position += test.size() - previousLength; previousLength = test.size(); addMethodFor(test, callee, m.copyWithNewOwner(callee.getGenericClass()), position); return position; } else { addMethod(test, m, position, 0); return position;
@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")); }
@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 testHasAnyOnlyForAssertionMethod() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); VariableReference req = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getRequest"), EvoServletState.class), 0, 0); Assert.assertEquals(tc.getTestCase().toCode(), 1, tc.size()); Assert.assertFalse(ConstraintVerifier.hasAnyOnlyForAssertionMethod(tc.getTestCase())); //this method should be only for assertions factory.addMethodFor(tc.getTestCase(), req, new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("isAsyncStarted"), EvoHttpServletRequest.class), 1); Assert.assertEquals(tc.getTestCase().toCode(), 2, tc.size()); Assert.assertTrue(ConstraintVerifier.hasAnyOnlyForAssertionMethod(tc.getTestCase())); }
@Test public void testAddConstructor() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException { TestFactory testFactory = TestFactory.getInstance(); Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName()); GenericConstructor method = new GenericConstructor(sut.getConstructor(), sut); DefaultTestCase test = new DefaultTestCase(); Properties.PRIMITIVE_REUSE_PROBABILITY = 0.0; Properties.OBJECT_REUSE_PROBABILITY = 0.0; testFactory.addConstructor(test, method, 0, 0); testFactory.reset(); assertEquals(1, test.size()); testFactory.addConstructor(test, method, 0, 0); assertEquals(2, test.size()); String code = test.toCode(); System.out.println(code); assertTrue(code.contains("factoryExample0")); assertTrue(code.contains("factoryExample1")); }