addMethodFor(test, callee, (GenericMethod) call.copyWithNewOwner(callee.getGenericClass()),
@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); }
@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")); }
addMethodFor(test, callee, m.copyWithNewOwner(callee.getGenericClass()), position); return position; } else {
@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)); }
addMethodFor(test, callee, m.copyWithNewOwner(callee.getGenericClass()), position); } else {
@Test public void testInitializingBoundedVariable_correct() 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); //initializing bounding variable method called directly after the new factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 1, 0); //method on servlet after the bounding variable initialization: it is ok factory.addMethodFor(tc.getTestCase(), servlet, new GenericMethod(FakeServlet.class.getDeclaredMethod("foo"), FakeServlet.class), 2); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@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 }
@Test public void testGetCandidatesForReuse() throws ClassNotFoundException, NoSuchFieldException, ConstructionFailedException, NoSuchMethodException { TestFactory testFactory = TestFactory.getInstance(); Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName()); Properties.PRIMITIVE_REUSE_PROBABILITY = 1.0; Properties.OBJECT_REUSE_PROBABILITY = 1.0; DefaultTestCase test = new DefaultTestCase(); GenericConstructor constructor = new GenericConstructor(sut.getConstructor(), sut); VariableReference var1 = testFactory.addConstructor(test, constructor, 0, 0); test.addStatement(new CharPrimitiveStatement(test,'-')); GenericMethod method = new GenericMethod(sut.getMethod("testInt", int.class), sut); testFactory.addMethodFor(test, var1, method, 2); MethodStatement stmt = (MethodStatement)test.getStatement(test.size()-1); VariableReference var = stmt.getParameterReferences().get(0); assertNotEquals("Char should not be passed as Integer", var.getType(), char.class); assertEquals("Incorrect test size", 4, test.size()); } }
@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)); }
@Test public void testNoNullInputs_notNull() 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); StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), "foo"); tc.getTestCase().addStatement(foo); VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0); factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4); Assert.assertEquals(5, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@Test public void testNoNullInputs_nullString() 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); //shouldn't be able to pass it to createDispatcher StringPrimitiveStatement foo = new StringPrimitiveStatement(tc.getTestCase(), null); tc.getTestCase().addStatement(foo); VariableReference con = factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getConfiguration"), EvoServletState.class), 3, 0); factory.addMethodFor(tc.getTestCase(), con, new GenericMethod(EvoServletConfig.class.getDeclaredMethod("createDispatcher", String.class), EvoServletConfig.class), 4); /* even if "foo" is null and we have the P of object reuse to 1, still "foo" will not be used, as "null" constraint is always enforced */ Assert.assertEquals(tc.getTestCase().toCode(), 6, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@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())); }