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());
}
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);
}
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);
}
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);
}
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);
}
Aggregations