Search in sources :

Example 21 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class JUnitAnalyzerTest method testSandboxIssue.

@Test
public void testSandboxIssue() throws Exception {
    // First, get a TestCase from a carved JUnit
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.sandbox.OpenStreamInATryCatch_FakeTestToCarve.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.sandbox.OpenStreamInATryCatch.class.getCanonicalName();
    Properties.CRITERION = new Properties.Criterion[] { Properties.Criterion.BRANCH };
    Properties.SEED_MUTATIONS = 0;
    Properties.SEED_CLONE = 1;
    Properties.VIRTUAL_FS = false;
    Properties.SANDBOX = true;
    // needed for setLoggingForJUnit
    Properties.ENABLE_ASSERTS_FOR_EVOSUITE = true;
    Properties.TEST_SCAFFOLDING = false;
    // FIXME
    Sandbox.initializeSecurityManagerForSUT();
    // file should never be created
    Assert.assertFalse(file.exists());
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    TestChromosome carved = factory.getChromosome();
    /*
		 * FIXME: issue with carver
		 */
    Files.deleteIfExists(file.toPath());
    Assert.assertFalse(file.exists());
    Assert.assertNotNull(carved);
    TestCase test = carved.getTestCase();
    Assert.assertEquals("Should be: constructor, 1 variable and 1 method", 3, test.size());
    // Now that we have a test case, we check its execution after
    // recompiling it to JUnit, and see if sandbox kicks in
    List<TestCase> list = new ArrayList<TestCase>();
    list.add(test);
    Assert.assertFalse(file.exists());
    // NOTE: following order of checks reflects what is done
    // in EvoSuite after the search is finished
    System.out.println("\n COMPILATION CHECK \n");
    // first try to compile (which implies execution)
    JUnitAnalyzer.removeTestsThatDoNotCompile(list);
    Assert.assertEquals(1, list.size());
    Assert.assertFalse(file.exists());
    System.out.println("\n FIRST STABILITY CHECK \n");
    // try once
    JUnitAnalyzer.handleTestsThatAreUnstable(list);
    Assert.assertEquals(1, list.size());
    Assert.assertFalse(file.exists());
    System.out.println("\n SECOND STABILITY CHECK \n");
    // try again
    JUnitAnalyzer.handleTestsThatAreUnstable(list);
    Assert.assertEquals(1, list.size());
    Assert.assertFalse(file.exists());
    System.out.println("\n FINAL VERIFICATION \n");
    JUnitAnalyzer.verifyCompilationAndExecution(list);
    Assert.assertEquals(1, list.size());
    Assert.assertFalse(file.exists());
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) TestCase(org.evosuite.testcase.TestCase) ArrayList(java.util.ArrayList) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 22 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class TestDSETestCaseLocalSearch method testCVC4Solver.

@Test
public void testCVC4Solver() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    String cvc4_path = System.getenv("cvc4_path");
    if (cvc4_path != null) {
        Properties.CVC4_PATH = cvc4_path;
    }
    Assume.assumeTrue(Properties.CVC4_PATH != null);
    Properties.DSE_SOLVER = Properties.SolverType.CVC4_SOLVER;
    Properties.CRITERION = new Properties.Criterion[] { Criterion.BRANCH };
    Properties.TARGET_CLASS = Foo.class.getName();
    TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addFitness(branchCoverageSuiteFitness);
    branchCoverageSuiteFitness.getFitness(suite);
    // no goals covered yet
    int coveredGoals0 = suite.getNumOfCoveredGoals();
    int notCoveredGoals0 = suite.getNumOfNotCoveredGoals();
    assertEquals(0, coveredGoals0);
    assertNotEquals(0, notCoveredGoals0);
    DefaultTestCase testCase0 = buildTestCase0();
    TestChromosome testChromosome0 = new TestChromosome();
    testChromosome0.setTestCase(testCase0);
    suite.addTest(testChromosome0);
    double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
    // some goal was covered
    assertTrue(coveredGoalsBeforeLocalSearch > 0);
    DefaultTestCase duplicatedTestCase0 = buildTestCase0();
    TestChromosome duplicatedTestChromosome0 = new TestChromosome();
    duplicatedTestChromosome0.setTestCase(duplicatedTestCase0);
    suite.addTest(duplicatedTestChromosome0);
    TestSuiteLocalSearchObjective localSearchObjective = TestSuiteLocalSearchObjective.buildNewTestSuiteLocalSearchObjective(Collections.singletonList(branchCoverageSuiteFitness), suite, 1);
    DSETestCaseLocalSearch localSearch = new DSETestCaseLocalSearch();
    boolean improved = localSearch.doSearch(duplicatedTestChromosome0, localSearchObjective);
    assertTrue(improved);
    double fitnessAfterLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsAfterLocalSearch = suite.getNumOfCoveredGoals();
    assertTrue(fitnessAfterLocalSearch < fitnessBeforeLocalSearch);
    assertTrue(coveredGoalsAfterLocalSearch > coveredGoalsBeforeLocalSearch);
}
Also used : TestSuiteLocalSearchObjective(org.evosuite.testsuite.localsearch.TestSuiteLocalSearchObjective) Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 23 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class TestSerialization method testSerializationNonEmptySuite.

@Test
public void testSerializationNonEmptySuite() throws IOException, ClassNotFoundException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    double fitness = 0.9950513142057124d;
    TestSuiteChromosome chromosome = new TestSuiteChromosome();
    TestChromosome testChromosome = new TestChromosome();
    TestCase test = new DefaultTestCase();
    PrimitiveStatement<?> statement = PrimitiveStatement.getPrimitiveStatement(test, int.class);
    test.addStatement(statement);
    testChromosome.setTestCase(test);
    testChromosome.setFitness(null, 3.14d);
    chromosome.setFitness(null, fitness);
    chromosome.setCoverage(null, 0.5);
    chromosome.updateAge(24);
    chromosome.setChanged(true);
    chromosome.addTest(testChromosome);
    oos.writeObject(chromosome);
    byte[] baSerialized = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(baSerialized);
    ObjectInputStream ois = new ObjectInputStream(bais);
    TestSuiteChromosome copy = (TestSuiteChromosome) ois.readObject();
    Assert.assertEquals(chromosome.getFitness(), copy.getFitness(), 0.0);
    Assert.assertEquals(chromosome.getAge(), copy.getAge());
    Assert.assertEquals(chromosome.getCoverage(), copy.getCoverage(), 0.0);
    Assert.assertEquals(chromosome.getCoveredGoals(), copy.getCoveredGoals());
    Assert.assertEquals(chromosome.isChanged(), copy.isChanged());
    Assert.assertEquals(chromosome.getTestChromosome(0).getFitness(), copy.getTestChromosome(0).getFitness(), 0.0);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestCase(org.evosuite.testcase.TestCase) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) ObjectOutputStream(java.io.ObjectOutputStream) TestChromosome(org.evosuite.testcase.TestChromosome) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 24 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class TestDSETestSuiteCookie method testCVC4Solver.

@Test
public void testCVC4Solver() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    String cvc4_path = System.getenv("cvc4_path");
    if (cvc4_path != null) {
        Properties.CVC4_PATH = cvc4_path;
    }
    Assume.assumeTrue(Properties.CVC4_PATH != null);
    Properties.DSE_SOLVER = Properties.SolverType.CVC4_SOLVER;
    Properties.CRITERION = new Properties.Criterion[] { Criterion.BRANCH };
    Properties.TARGET_CLASS = Foo.class.getName();
    TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addFitness(branchCoverageSuiteFitness);
    branchCoverageSuiteFitness.getFitness(suite);
    // no goals covered yet
    int coveredGoals0 = suite.getNumOfCoveredGoals();
    int notCoveredGoals0 = suite.getNumOfNotCoveredGoals();
    assertEquals(0, coveredGoals0);
    assertNotEquals(0, notCoveredGoals0);
    DefaultTestCase testCase0 = buildTestCase0();
    TestChromosome testChromosome0 = new TestChromosome();
    testChromosome0.setTestCase(testCase0);
    suite.addTest(testChromosome0);
    double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
    // some goal was covered
    assertTrue(coveredGoalsBeforeLocalSearch > 0);
    LocalSearchObjective<TestSuiteChromosome> localSearchObjective = new DefaultLocalSearchObjective<>();
    localSearchObjective.addFitnessFunction(branchCoverageSuiteFitness);
    boolean improved;
    do {
        TestSuiteLocalSearch localSearch = new TestSuiteLocalSearch();
        improved = localSearch.doSearch(suite, localSearchObjective);
    } while (improved);
    double fitnessAfterLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsAfterLocalSearch = suite.getNumOfCoveredGoals();
    assertTrue(fitnessAfterLocalSearch < fitnessBeforeLocalSearch);
    assertTrue(coveredGoalsAfterLocalSearch > coveredGoalsBeforeLocalSearch);
    int finalSuiteSize = suite.size();
    assertTrue(coveredGoalsAfterLocalSearch == 8);
    assertTrue(finalSuiteSize >= 5);
}
Also used : Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Example 25 with TestChromosome

use of org.evosuite.testcase.TestChromosome in project evosuite by EvoSuite.

the class TestDSETestSuiteFoo method testAVMSolver.

@Test
public void testAVMSolver() throws NoSuchMethodException, SecurityException, ClassNotFoundException {
    Properties.DSE_SOLVER = Properties.SolverType.EVOSUITE_SOLVER;
    Properties.CRITERION = new Properties.Criterion[] { Criterion.BRANCH };
    Properties.TARGET_CLASS = Foo.class.getName();
    TestGenerationContext.getInstance().getClassLoaderForSUT().loadClass(Properties.TARGET_CLASS);
    BranchCoverageSuiteFitness branchCoverageSuiteFitness = new BranchCoverageSuiteFitness();
    TestSuiteChromosome suite = new TestSuiteChromosome();
    suite.addFitness(branchCoverageSuiteFitness);
    branchCoverageSuiteFitness.getFitness(suite);
    // no goals covered yet
    int coveredGoals0 = suite.getNumOfCoveredGoals();
    int notCoveredGoals0 = suite.getNumOfNotCoveredGoals();
    assertEquals(0, coveredGoals0);
    assertNotEquals(0, notCoveredGoals0);
    DefaultTestCase testCase0 = buildTestCase0();
    TestChromosome testChromosome0 = new TestChromosome();
    testChromosome0.setTestCase(testCase0);
    suite.addTest(testChromosome0);
    double fitnessBeforeLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsBeforeLocalSearch = suite.getNumOfCoveredGoals();
    // some goal was covered
    assertTrue(coveredGoalsBeforeLocalSearch > 0);
    LocalSearchObjective<TestSuiteChromosome> localSearchObjective = new DefaultLocalSearchObjective<>();
    localSearchObjective.addFitnessFunction(branchCoverageSuiteFitness);
    TestSuiteLocalSearch localSearch = new TestSuiteLocalSearch();
    boolean improved;
    do {
        improved = localSearch.doSearch(suite, localSearchObjective);
    } while (improved);
    double fitnessAfterLocalSearch = branchCoverageSuiteFitness.getFitness(suite);
    int coveredGoalsAfterLocalSearch = suite.getNumOfCoveredGoals();
    assertTrue(fitnessAfterLocalSearch < fitnessBeforeLocalSearch);
    assertTrue(coveredGoalsAfterLocalSearch > coveredGoalsBeforeLocalSearch);
    int finalSuiteSize = suite.size();
    assertTrue(coveredGoalsAfterLocalSearch >= 7);
    assertTrue(finalSuiteSize >= 4);
}
Also used : DefaultLocalSearchObjective(org.evosuite.ga.localsearch.DefaultLocalSearchObjective) Foo(com.examples.with.different.packagename.symbolic.Foo) BranchCoverageSuiteFitness(org.evosuite.coverage.branch.BranchCoverageSuiteFitness) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome) Test(org.junit.Test)

Aggregations

TestChromosome (org.evosuite.testcase.TestChromosome)128 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)47 ExecutionResult (org.evosuite.testcase.execution.ExecutionResult)33 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)22 ArrayList (java.util.ArrayList)17 TestCase (org.evosuite.testcase.TestCase)17 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)16 HashSet (java.util.HashSet)15 Properties (org.evosuite.Properties)15 Test (org.junit.Test)15 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14 HashMap (java.util.HashMap)11 DefaultLocalSearchObjective (org.evosuite.ga.localsearch.DefaultLocalSearchObjective)10 AbstractTestSuiteChromosome (org.evosuite.testsuite.AbstractTestSuiteChromosome)8 LinkedHashMap (java.util.LinkedHashMap)7 Foo (com.examples.with.different.packagename.symbolic.Foo)6 LinkedHashSet (java.util.LinkedHashSet)6 Set (java.util.Set)6 TestSuiteFitnessFunction (org.evosuite.testsuite.TestSuiteFitnessFunction)6 Map (java.util.Map)5