예제 #1
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));
  }
예제 #2
0
  @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));
  }
예제 #3
0
  @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));
  }
예제 #4
0
  @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
    }
  }
예제 #5
0
  @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));
  }
예제 #6
0
  @Test
  public void testTwoIntsWeakMutation()
      throws NoSuchMethodException, SecurityException, ClassNotFoundException,
          ConstructionFailedException {
    Properties.TARGET_CLASS = IntExampleWithNoElse.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getTwoIntTest(1, 1000));
    TestChromosome test2 = new TestChromosome();
    test2.setTestCase(getTwoIntTest(0, 0));

    TestSuiteChromosome suite = new TestSuiteChromosome();
    WeakMutationSuiteFitness fitness = new WeakMutationSuiteFitness();
    suite.addTest(test1);
    suite.addTest(test2);

    Properties.P_TEST_CHANGE = 1.0;
    Properties.P_TEST_DELETE = 0.0;
    Properties.P_TEST_INSERT = 0.0;
    Properties.PRIMITIVE_POOL = 0.0;
    double oldFitness = fitness.getFitness(suite);
    int notChanged = 0;
    for (int i = 0; i < 10000; i++) {
      TestChromosome testNew = (TestChromosome) test1.clone();
      testNew.mutate();
      if (testNew.isChanged()) {
        suite.deleteTest(test1);
        suite.addTest(testNew);
        double newFitness = fitness.getFitness(suite);
        if (newFitness < oldFitness) {
          test1 = testNew;
          oldFitness = newFitness;
          System.out.println(
              ""
                  + i
                  + ":"
                  + ((IntPrimitiveStatement) test1.getTestCase().getStatement(1)).getValue());
          System.out.println(
              "    " + ((IntPrimitiveStatement) test1.getTestCase().getStatement(2)).getValue());
          if (newFitness == 0.0) {
            System.out.println("Iterations: " + i);
            System.out.println("Not changed: " + notChanged);
            break;
          }
        } else {
          suite.deleteTest(testNew);
          suite.addTest(test1);
          fitness.getFitness(suite);
        }
      } else {
        notChanged++;
      }
    }

    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals(0.0, fitness.getFitness(suite), 0.1F);
  }
예제 #7
0
  @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()));
  }
예제 #8
0
  @Test
  public void testBaseTest() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();

    factory.addConstructor(
        tc.getTestCase(),
        new GenericConstructor(Object.class.getConstructor(), Object.class),
        0,
        0);

    Assert.assertEquals(1, tc.size());
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
  }
예제 #9
0
  @Test
  public void testNoDirectInsertion() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();

    GenericMethod gm =
        new GenericMethod(
            ConstraintVerifierTest.class.getDeclaredMethod("noDirect", null),
            ConstraintVerifierTest.class);

    // should not be possible to insert it
    int pos = ConstraintVerifier.getAValidPositionForInsertion(gm, tc.getTestCase(), 0);
    Assert.assertTrue(pos < 0);
  }
예제 #10
0
  @Test
  public void testEvoSuiteClassExclude() throws Exception {
    TestChromosome tc = new TestChromosome();
    TestFactory factory = TestFactory.getInstance();

    // shouldn't be able to instantiate EvoServletConfig directly
    factory.addConstructor(
        tc.getTestCase(),
        new GenericConstructor(EvoServletConfig.class.getConstructor(), EvoServletConfig.class),
        0,
        0);

    Assert.assertEquals(1, tc.size());
    Assert.assertFalse(ConstraintVerifier.verifyTest(tc));
  }
예제 #11
0
  @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));
  }
예제 #12
0
  @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));
  }
예제 #13
0
  @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));
  }
예제 #14
0
  @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));
  }
예제 #15
0
  @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
  }
예제 #16
0
  @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));
  }
예제 #17
0
  @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
  }
예제 #18
0
  @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));
  }
예제 #19
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 testTwoInts()
      throws NoSuchMethodException, SecurityException, ClassNotFoundException,
          ConstructionFailedException {
    Properties.TARGET_CLASS = IntExampleWithNoElse.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getTwoIntTest(1, 22, 22, 22));
    TestChromosome test2 = new TestChromosome();
    test2.setTestCase(getTwoIntTest(-23423423, 234234234, -23423423, 234234234));
    TestChromosome test3 = new TestChromosome();
    test3.setTestCase(getTwoIntTest(0, 0, 0, 0));

    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();

    assertEquals(6.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test1);
    assertEquals(2.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test2);
    suite.addTest(test3);

    Class<?> sut =
        TestGenerationContext.getInstance()
            .getClassLoaderForSUT()
            .loadClass(Properties.TARGET_CLASS);
    Method m = sut.getMethod("testMe", new Class<?>[] {int.class, int.class});
    GenericMethod method = new GenericMethod(m, sut);

    TestCluster.getInstance().addTestCall(method);

    Properties.P_TEST_CHANGE = 0.0;
    Properties.P_TEST_DELETE = 1.0;
    Properties.P_TEST_INSERT = 0.0;

    double oldFitness = fitness.getFitness(suite);
    int notChanged = 0;
    System.out.println("Original: " + test1);
    for (int i = 0; i < 100; i++) {
      TestChromosome testNew = (TestChromosome) test1.clone();
      testNew.mutate();
      if (testNew.isChanged()) {
        System.out.println("Trying: " + testNew);
        suite.deleteTest(test1);
        suite.addTest(testNew);
        double newFitness = fitness.getFitness(suite);
        if (newFitness < oldFitness) {
          test1 = testNew;
          oldFitness = newFitness;
          if (newFitness == 0.0) {
            System.out.println("Iterations: " + i);
            System.out.println("Not changed: " + notChanged);
            break;
          }
        } else {
          suite.deleteTest(testNew);
          suite.addTest(test1);
          fitness.getFitness(suite);
        }
      } else {
        notChanged++;
      }
    }

    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals(0.0, fitness.getFitness(suite), 0.1F);
  }
  @Test
  public void testSimpleInt()
      throws NoSuchMethodException, SecurityException, ClassNotFoundException,
          ConstructionFailedException {
    Properties.TARGET_CLASS = TrivialInt.class.getCanonicalName();
    TestChromosome test1 = new TestChromosome();
    test1.setTestCase(getIntTest(2938, -1000000));
    TestChromosome test2 = (TestChromosome) test1.clone();

    TestSuiteChromosome suite = new TestSuiteChromosome();
    BranchCoverageSuiteFitness fitness = new BranchCoverageSuiteFitness();

    assertEquals(4.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test1);
    assertEquals(1.0, fitness.getFitness(suite), 0.0F);
    suite.addTest(test2);
    assertEquals(1.0, fitness.getFitness(suite), 0.01F); // 0.99...

    Class<?> sut =
        TestGenerationContext.getInstance()
            .getClassLoaderForSUT()
            .loadClass(Properties.TARGET_CLASS);
    Method m = sut.getMethod("testMe", new Class<?>[] {int.class});
    GenericMethod method = new GenericMethod(m, sut);

    TestCluster.getInstance().addTestCall(method);

    Properties.P_TEST_CHANGE = 0.0;
    Properties.P_TEST_DELETE = 1.0;
    Properties.P_TEST_INSERT = 0.0;

    double oldFitness = suite.getFitness();
    int notChanged = 0;
    for (int i = 0; i < 100; i++) {
      TestChromosome testNew = (TestChromosome) test1.clone();
      testNew.mutate();
      if (testNew.isChanged()) {
        suite.deleteTest(test1);
        suite.addTest(testNew);
        System.out.println(testNew.getTestCase().toCode());
        double newFitness = fitness.getFitness(suite);
        if (newFitness < oldFitness) {
          System.out.println("Improved: " + newFitness);
          test1 = testNew;
          oldFitness = newFitness;
          System.out.println(
              ""
                  + i
                  + ":"
                  + ((IntPrimitiveStatement) test1.getTestCase().getStatement(1)).getValue());
          if (newFitness == 0.0) {
            System.out.println("Iterations: " + i);
            System.out.println("Not changed: " + notChanged);
            break;
          }
        } else {
          System.out.println("Not improved: " + newFitness);
          suite.deleteTest(testNew);
          suite.addTest(test1);
          fitness.getFitness(suite);
        }
      } else {
        notChanged++;
      }
    }

    System.out.println("Fitness: " + fitness.getFitness(suite));
    System.out.println("Test suite: " + suite);
    assertEquals(0.0, fitness.getFitness(suite), 0.1F);
  }
예제 #22
0
  @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));
  }
예제 #23
0
  @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
  }
예제 #24
0
  @Test
  public void testDelete_multipleVarsThatCouldBeReused() 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);

    // now do it again
    VariableReference secondServlet =
        factory.addConstructor(
            tc.getTestCase(),
            new GenericConstructor(FakeServlet.class.getDeclaredConstructor(), FakeServlet.class),
            2,
            0);
    factory.addMethod(
        tc.getTestCase(),
        new GenericMethod(
            Injector.class.getDeclaredMethod("executePostConstruct", Object.class), Injector.class),
        3,
        0);
    MethodStatement mt = (MethodStatement) tc.getTestCase().getStatement(3);
    // be sure it is using the second servlet
    mt.replace(servlet, secondServlet);

    Assert.assertEquals(4, 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
    Assert.assertTrue(
        ConstraintVerifier.canDelete(tc.getTestCase(), 2)); // bounded variable can be deleted
    Assert.assertFalse(
        ConstraintVerifier.canDelete(
            tc.getTestCase(), 3)); // method using bounded variable should not be deleted

    boolean mutated = tc.deleteStatement(factory, 2);
    Assert.assertTrue(mutated);
    /*
       deleting the bounded variable in position 2 should lead to also delete the initializing variable in 3,
       and not end up with 3 re-using 0
    */
    Assert.assertTrue(ConstraintVerifier.verifyTest(tc));
    Assert.assertEquals(2, tc.size());
  }