Search in sources :

Example 1 with ObjectPool

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

the class PoolSystemTest method testPool.

@Test
public void testPool() throws IOException {
    File f = File.createTempFile("EvoSuiteTestPool", null, FileUtils.getTempDirectory());
    String filename = f.getAbsolutePath();
    f.delete();
    System.out.println(filename);
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DependencyClass.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.SEARCH_BUDGET = 100000;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    ObjectPool pool = ObjectPool.getPoolFromTestSuite(best);
    pool.writePool(filename);
    System.out.println("EvolvedTestSuite:\n" + best);
    resetStaticVariables();
    setDefaultPropertiesForTestCases();
    targetClass = OtherClass.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.P_OBJECT_POOL = 1.0;
    Properties.OBJECT_POOLS = filename;
    Properties.SEARCH_BUDGET = 10000;
    ObjectPoolManager.getInstance().initialisePool();
    // Properties.SEARCH_BUDGET = 50000;
    command = new String[] { "-generateSuite", "-class", targetClass, "-Dobject_pools=" + filename };
    result = evosuite.parseCommandLine(command);
    ga = getGAFromResult(result);
    TestSuiteChromosome best2 = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best2);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best2.getCoverage(), 0.001);
    f = new File(filename);
    f.delete();
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) File(java.io.File) ObjectPool(org.evosuite.seeding.ObjectPool) OtherClass(com.examples.with.different.packagename.pool.OtherClass) Test(org.junit.Test)

Example 2 with ObjectPool

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

the class PoolSystemTest method testPoolWithException.

@Test
public void testPoolWithException() throws IOException, NoSuchMethodException, SecurityException {
    File f = File.createTempFile("EvoSuiteTestPool", null, FileUtils.getTempDirectory());
    String filename = f.getAbsolutePath();
    f.delete();
    System.out.println(filename);
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DependencyClassWithException.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.getTargetClassAndDontInitialise();
    TestCase test = new DefaultTestCase();
    VariableReference instance = test.addStatement(new ConstructorStatement(test, new GenericConstructor(DependencyClassWithException.class.getConstructors()[0], DependencyClassWithException.class), new ArrayList<VariableReference>()));
    VariableReference int42 = test.addStatement(new IntPrimitiveStatement(test, 42));
    GenericMethod foo = new GenericMethod(DependencyClassWithException.class.getMethod("foo", int.class), DependencyClassWithException.class);
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    test.addStatement(new MethodStatement(test, foo, instance, Arrays.asList(new VariableReference[] { int42 })));
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    TestSuiteChromosome best = new TestSuiteChromosome();
    best.addTest(test);
    ObjectPool pool = new ObjectPool();
    pool.addSequence(new GenericClass(DependencyClassWithException.class), test);
    pool.writePool(filename);
    System.out.println("EvolvedTestSuite:\n" + best);
    resetStaticVariables();
    setDefaultPropertiesForTestCases();
    targetClass = ClassDependingOnExceptionClass.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.P_OBJECT_POOL = 0.8;
    Properties.OBJECT_POOLS = filename;
    ObjectPoolManager.getInstance().initialisePool();
    // Properties.SEARCH_BUDGET = 50000;
    command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
    f = new File(filename);
    f.delete();
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) ArrayList(java.util.ArrayList) DependencyClassWithException(com.examples.with.different.packagename.pool.DependencyClassWithException) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) GenericClass(org.evosuite.utils.generic.GenericClass) EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) File(java.io.File) ObjectPool(org.evosuite.seeding.ObjectPool) ClassDependingOnExceptionClass(com.examples.with.different.packagename.pool.ClassDependingOnExceptionClass) Test(org.junit.Test)

Example 3 with ObjectPool

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

the class TestSuiteGenerator method writeObjectPool.

private void writeObjectPool(TestSuiteChromosome suite) {
    if (!Properties.WRITE_POOL.isEmpty()) {
        LoggingUtils.getEvoLogger().info("* Writing sequences to pool");
        ObjectPool pool = ObjectPool.getPoolFromTestSuite(suite);
        pool.writePool(Properties.WRITE_POOL);
    }
}
Also used : ObjectPool(org.evosuite.seeding.ObjectPool)

Example 4 with ObjectPool

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

the class PoolSystemTest method testPoolWithSubClass.

@Test
public void testPoolWithSubClass() throws IOException {
    File f = File.createTempFile("EvoSuiteTestPool", null, FileUtils.getTempDirectory());
    String filename = f.getAbsolutePath();
    f.delete();
    System.out.println(filename);
    EvoSuite evosuite = new EvoSuite();
    String targetClass = DependencySubClass.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    // It takes a bit longer to cover the branch here
    Properties.SEARCH_BUDGET = 50000;
    String[] command = new String[] { "-generateSuite", "-class", targetClass };
    Object result = evosuite.parseCommandLine(command);
    GeneticAlgorithm<?> ga = getGAFromResult(result);
    TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
    ObjectPool pool = ObjectPool.getPoolFromTestSuite(best);
    pool.writePool(filename);
    System.out.println("EvolvedTestSuite:\n" + best);
    resetStaticVariables();
    setDefaultPropertiesForTestCases();
    targetClass = OtherClass.class.getCanonicalName();
    Properties.TARGET_CLASS = targetClass;
    Properties.P_OBJECT_POOL = 1.0;
    Properties.OBJECT_POOLS = filename;
    ObjectPoolManager.getInstance().initialisePool();
    command = new String[] { "-generateSuite", "-class", targetClass, "-Dobject_pools=" + filename };
    result = evosuite.parseCommandLine(command);
    ga = getGAFromResult(result);
    best = (TestSuiteChromosome) ga.getBestIndividual();
    System.out.println("EvolvedTestSuite:\n" + best);
    Assert.assertEquals("Non-optimal coverage: ", 1d, best.getCoverage(), 0.001);
    f = new File(filename);
    f.delete();
}
Also used : EvoSuite(org.evosuite.EvoSuite) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) File(java.io.File) ObjectPool(org.evosuite.seeding.ObjectPool) OtherClass(com.examples.with.different.packagename.pool.OtherClass) Test(org.junit.Test)

Aggregations

ObjectPool (org.evosuite.seeding.ObjectPool)4 File (java.io.File)3 EvoSuite (org.evosuite.EvoSuite)3 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)3 Test (org.junit.Test)3 OtherClass (com.examples.with.different.packagename.pool.OtherClass)2 ClassDependingOnExceptionClass (com.examples.with.different.packagename.pool.ClassDependingOnExceptionClass)1 DependencyClassWithException (com.examples.with.different.packagename.pool.DependencyClassWithException)1 ArrayList (java.util.ArrayList)1 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)1 MethodStatement (org.evosuite.testcase.statements.MethodStatement)1 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)1 VariableReference (org.evosuite.testcase.variable.VariableReference)1 GenericClass (org.evosuite.utils.generic.GenericClass)1 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)1 GenericMethod (org.evosuite.utils.generic.GenericMethod)1