Search in sources :

Example 26 with EvoSuite

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

the class DeleteFileSystemTest method testDeleteOnExit.

@Test
public void testDeleteOnExit() throws IOException {
    String tmpdir = System.getProperty("java.io.tmpdir");
    File toDelete = new File(tmpdir + File.separator + "this_file_should_not_be_deleted_by_evosuite");
    FileUtils.write(toDelete, "BlahBlah");
    assertTrue(toDelete.exists());
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DeleteFileExample.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.SANDBOX = true;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    evosuite.parseCommandLine(command);
    assertTrue("File has been deleted: " + toDelete.getAbsolutePath(), toDelete.exists());
    toDelete.delete();
}
Also used : EvoSuite(org.evosuite.EvoSuite) File(java.io.File) Test(org.junit.Test)

Example 27 with EvoSuite

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

the class ReadWriteSystemPropertiesSystemTest method testReadLineSeparator.

@Test
public void testReadLineSeparator() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ReadTimezone.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.SANDBOX = true;
    Properties.REPLACE_CALLS = true;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    double cov = best.getCoverage();
    Assert.assertEquals("Non-optimal coverage: ", 1d, cov, 0.001);
    // now check the JUnit generation
    List<TestCase> list = best.getTests();
    int n = list.size();
    Assert.assertTrue(n > 0);
    // needed because it gets pulled down after the search
    TestCaseExecutor.initExecutor();
    try {
        Sandbox.initializeSecurityManagerForSUT();
        JUnitAnalyzer.removeTestsThatDoNotCompile(list);
    } finally {
        Sandbox.resetDefaultSecurityManager();
    }
    Assert.assertEquals(n, list.size());
    TestGenerationResult tgr = TestGenerationResultBuilder.buildSuccessResult();
    String code = tgr.getTestSuiteCode();
    Assert.assertTrue("Test code:\n" + code, code.contains("user.timezone"));
    /*
		 * This is tricky. The property 'debug' is read, but it does not exist. 
		 * Ideally, we should still have in the test case a call to be sure the variable
		 * is set to null. But that would lead to a lot of problems :( eg cases
		 * in which we end up in reading hundreds of thousands variables that do not exist
		 */
    Assert.assertTrue("Test code:\n" + code, !code.contains("debug"));
}
Also used : TestGenerationResult(org.evosuite.result.TestGenerationResult) TestCase(org.evosuite.testcase.TestCase) EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 28 with EvoSuite

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

the class ObjectPoolSystemTest method testDifficultClassWithWrongPoolFails.

@Ignore
@Test
public void testDifficultClassWithWrongPoolFails() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DifficultClassWithoutCarving.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.P_OBJECT_POOL = 0.8;
    Properties.CARVE_OBJECT_POOL = true;
    Properties.SELECTED_JUNIT = ArrayConverterTestCase.class.getCanonicalName();
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertTrue("Did not expect optimal coverage: ", best.getCoverage() < 1d);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) ArrayConverterTestCase(com.examples.with.different.packagename.testcarver.ArrayConverterTestCase) Ignore(org.junit.Ignore) Test(org.junit.Test) DifficultClassTest(com.examples.with.different.packagename.testcarver.DifficultClassTest) DifficultClassWithoutCarvingTest(com.examples.with.different.packagename.testcarver.DifficultClassWithoutCarvingTest)

Example 29 with EvoSuite

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

the class TypeSeedingSystemTest method testTypeExampleOff.

@Test
public void testTypeExampleOff() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = TypeExample.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.SEED_TYPES = false;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    int goals = TestSuiteGenerator.getFitnessFactories().get(0).getCoverageGoals().size();
    Assert.assertEquals("Wrong number of goals: ", 4, goals);
    Assert.assertTrue("Did not expect optimal coverage: ", best.getCoverage() < 1d);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Example 30 with EvoSuite

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

the class TypeSeedingSystemTest method testObjectToStringCase.

@Test
public void testObjectToStringCase() {
    EvoSuite evosuite = new EvoSuite();
    String targetClass = ObjectCastExample.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.SEED_TYPES = true;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Test(org.junit.Test)

Aggregations

EvoSuite (org.evosuite.EvoSuite)462 Test (org.junit.Test)410 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)391 Properties (org.evosuite.Properties)86 OutputVariable (org.evosuite.statistics.OutputVariable)61 GenericSUTString (com.examples.with.different.packagename.generic.GenericSUTString)49 SearchStatistics (org.evosuite.statistics.SearchStatistics)16 Ignore (org.junit.Ignore)15 CheapPurityAnalyzer (org.evosuite.assertion.CheapPurityAnalyzer)13 CSVReader (com.opencsv.CSVReader)12 FileReader (java.io.FileReader)12 File (java.io.File)9 TestCase (org.evosuite.testcase.TestCase)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 MethodCoverageFitnessFunctionSystemTest (org.evosuite.coverage.method.MethodCoverageFitnessFunctionSystemTest)7 Before (org.junit.Before)7 TypeSeedingExampleString (com.examples.with.different.packagename.TypeSeedingExampleString)6 TestGenerationResult (org.evosuite.result.TestGenerationResult)6 CalculatorTest (com.examples.with.different.packagename.CalculatorTest)5