@Test public void testObjectDependencyReuse() 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 = 1.0; Properties.OBJECT_REUSE_PROBABILITY = 1.0; testFactory.addMethod(test, method, 0, 0); testFactory.addMethod(test, method, 3, 0); String code = test.toCode(); System.out.println(code); // With object reuse being 0, there should be no new instance of this object assertEquals(4, test.size()); assertTrue(code.contains("factoryExample0.testByte(byte0, byte0)")); assertFalse(code.contains("factoryExample1")); }
@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 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)); }
/** * 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 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 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 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 testInitializingBoundedVariable_wrong_noConstructor() throws Exception { TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("getAFakeServletInstance"), ConstraintVerifierTest.class), 0, 0); //initializing bounding variable method called on instance not generated with new factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class), 1, 0); Assert.assertEquals(2, tc.size()); Assert.assertFalse(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())); }
@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")); }
@Test public void testAtMostOnce() 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); //2 different methods that can be used at most once factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getRequest"), EvoServletState.class), 2, 0); factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getResponse"),EvoServletState.class),3,0); Assert.assertEquals(4, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); //add an invalid new call factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getResponse"), EvoServletState.class), 4, 0); Assert.assertEquals(5, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); //check should fail }
@Test public void testStaticMethod() throws ConstructionFailedException, NoSuchMethodException, SecurityException, ClassNotFoundException { TestFactory testFactory = TestFactory.getInstance(); Class<?> sut = TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(FactoryExample.class.getCanonicalName()); GenericMethod method = new GenericMethod(sut.getMethod("testStatic"), sut); DefaultTestCase test = new DefaultTestCase(); testFactory.addMethod(test, method, 0, 0); assertEquals(1, test.size()); testFactory.reset(); testFactory.addMethod(test, method, 1, 0); assertEquals(2, test.size()); String code = test.toCode(); System.out.println(code); // With object reuse being 0, there should be no new instance of this object assertTrue(code.contains("FactoryExample.testStatic()")); // No instance assertFalse(code.contains("FactoryExample0")); }
@Test public void testInitializingBoundedVariable_wrong_inputInOtherMethodBeforeInit() 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(ConstraintVerifierTest.class.getDeclaredMethod("takeServletAsInput", FakeServlet.class), ConstraintVerifierTest.class), 1, 0); //initializing bounding variable method cannot be called here after the bounded variable has been used as input in some other method 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 testInitializingBoundedVariable_correct_severalCalls() 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); //both calls on same bounding variable factory.addMethod(tc.getTestCase(), new GenericMethod( ConstraintVerifierTest.class.getDeclaredMethod("fakeInjection",Servlet.class), ConstraintVerifierTest.class), 1, 0); //this is an atMostOnce type factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct",Object.class), Injector.class), 2, 0); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@Test public void testAfter() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); //this method has an "after" constraint on initServlet factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("getRequest"), EvoServletState.class), 0, 0); Assert.assertEquals(1, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); VariableReference con = 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); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }
@Test public void testInitializingBoundedVariable_wrong_atMostOnce() 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); Assert.assertEquals(2, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); // this should be invalid, as executePostConstruct can be used only once on same 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 testExcludeMethod() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); //'reset' is a method that shouldn't be used in a test factory.addMethod(tc.getTestCase(), new GenericMethod(EvoServletState.class.getDeclaredMethod("reset"), EvoServletState.class), 0, 0); Assert.assertEquals(tc.getTestCase().toCode(), 1, tc.size()); Assert.assertFalse(ConstraintVerifier.verifyTest(tc)); }
@Test public void testGenerics_staticMethod() throws NoSuchMethodException, ConstructionFailedException { //first construct a test case for the Generic method TestCase tc = new DefaultTestCase(); TestFactory.getInstance().addConstructor(tc, new GenericConstructor(Object.class.getDeclaredConstructor(), Object.class), 0, 0); Method m = TestCodeVisitorTest.class.getDeclaredMethod("bar", Object.class); GenericMethod gm = new GenericMethod(m, TestCodeVisitorTest.class); TestFactory.getInstance().addMethod(tc, gm, 1, 0); //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); WildcardTypeImpl wt = (WildcardTypeImpl) type; assertEquals(0, wt.getLowerBounds().length); assertEquals(1, wt.getUpperBounds().length); Class<?> upper = (Class<?>) wt.getUpperBounds()[0]; assertEquals(Object.class, upper); //Finally, visit the test TestCodeVisitor visitor = new TestCodeVisitor(); tc.accept(visitor); //should not throw exception System.out.println(visitor.getCode()); }
@Test public void testCanDelete() 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); Assert.assertEquals(2, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0)); //bounded variable can be deleted Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1)); // method using bounded variable should not be deleted boolean mutated = tc.deleteStatement(factory, 1); Assert.assertFalse(mutated); //should fail Assert.assertEquals(2, tc.size()); mutated = tc.deleteStatement(factory, 0); Assert.assertTrue(mutated); Assert.assertEquals(0, tc.size());// deleting first statement should have had effect of removing the second as well }
@Test public void testPostConstructIssue() 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); //this will also add a statement for the Class factory.addMethod(tc.getTestCase(), new GenericMethod(Injector.class.getDeclaredMethod("executePostConstruct", Object.class, Class.class), Injector.class), 1, 0); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); boolean mutated = tc.deleteStatement(factory, 2); Assert.assertFalse(mutated); //should fail to delete the post construct Assert.assertEquals(3, tc.size()); Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1)); mutated = tc.deleteStatement(factory, 1); Assert.assertFalse(mutated); //eliminating the Class.class var should fail as well, as post construct accepts no null Assert.assertEquals(3, tc.size()); mutated = tc.deleteStatement(factory, 0); Assert.assertTrue(mutated); //eliminating the servlet is fine Assert.assertEquals(1, tc.size()); // tricky, as Class.class is not bounded to the servlet Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); }