/** * Each statement is deleted with probability 1/length * * @return */ private boolean mutationDelete() { if(test.isEmpty()){ return false; //nothing to delete } boolean changed = false; int lastMutableStatement = getLastMutatableStatement(); double pl = 1d / (lastMutableStatement + 1); TestFactory testFactory = TestFactory.getInstance(); for (int num = lastMutableStatement; num >= 0; num--) { if(num >= test.size()){ continue; //in case the delete remove more than one statement } // Each statement is deleted with probability 1/l if (Randomness.nextDouble() <= pl) { changed |= deleteStatement(testFactory, num); if(changed){ assert ConstraintVerifier.verifyTest(test); } } } if(changed){ assert ConstraintVerifier.verifyTest(test); } return changed; }
@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)); }
@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 testDeleteAfter() throws Exception{ TestChromosome tc = new TestChromosome(); TestFactory factory = TestFactory.getInstance(); //get a var for EvoHttpServletRequest VariableReference req = factory.addMethod(tc.getTestCase(), new GenericMethod(ConstraintVerifierTest.class.getDeclaredMethod("getNullEvoHttpServletRequest"), ConstraintVerifierTest.class), 0, 0); //make it a POST factory.addMethod(tc.getTestCase(), new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asPOST"),EvoHttpServletRequest.class), 1, 0); //now add a call to 'asMultipartFormData' which does depend on POST factory.addMethod(tc.getTestCase(), new GenericMethod(EvoHttpServletRequest.class.getDeclaredMethod("asMultipartFormData"), EvoHttpServletRequest.class), 2, 0); Assert.assertEquals(3, tc.size()); Assert.assertTrue(ConstraintVerifier.verifyTest(tc)); //deleting the last one should be fine, but not the POST, as asMultipartFormData has an 'after' dependency on it Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 2)); Assert.assertFalse(ConstraintVerifier.canDelete(tc.getTestCase(), 1)); // should not be able to delete POST directly //what about the first statement where the var is obtained? that should be valid to delete Assert.assertTrue(ConstraintVerifier.canDelete(tc.getTestCase(), 0)); boolean mutated = tc.deleteStatement(factory, 1); Assert.assertFalse(mutated); //should fail to delete POST Assert.assertEquals(3, tc.size()); mutated = tc.deleteStatement(factory, 0); Assert.assertTrue(mutated); Assert.assertEquals(0, tc.size()); //should end up deleting everything }
boolean mutated = tc.deleteStatement(factory, 2); Assert.assertTrue(mutated);