Search in sources :

Example 21 with JUnitTestCarvedChromosomeFactory

use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.

the class CoverageAnalysis method analyzeCoverage.

/**
 * Identify all JUnit tests starting with the given name prefix, instrument
 * and run tests
 */
public static void analyzeCoverage() {
    Sandbox.goingToExecuteSUTCode();
    TestGenerationContext.getInstance().goingToExecuteSUTCode();
    Sandbox.goingToExecuteUnsafeCodeOnSameThread();
    ExecutionTracer.setCheckCallerThread(false);
    try {
        String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
        if (Properties.TARGET_CLASS.endsWith(".jar") || Properties.TARGET_CLASS.contains(File.separator)) {
            targetClasses = DependencyAnalysis.analyzeTarget(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        } else {
            targetClasses.add(Properties.TARGET_CLASS);
            DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
        }
        LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
    } catch (Throwable e) {
        LoggingUtils.getEvoLogger().error("* Error while initializing target class: " + (e.getMessage() != null ? e.getMessage() : e.toString()));
        logger.error("Problem for " + Properties.TARGET_CLASS + ". Full stack:", e);
        return;
    } finally {
        Sandbox.doneWithExecutingUnsafeCodeOnSameThread();
        Sandbox.doneWithExecutingSUTCode();
        TestGenerationContext.getInstance().doneWithExecutingSUTCode();
    }
    // TestCluster.getInstance();
    List<Class<?>> testClasses = getTestClasses();
    LoggingUtils.getEvoLogger().info("* Found " + testClasses.size() + " test class(es)");
    if (testClasses.isEmpty())
        return;
    /*
         * sort them in a deterministic way, in case there are 
         * static state dependencies
         */
    sortTestClasses(testClasses);
    Class<?>[] tests = testClasses.toArray(new Class<?>[testClasses.size()]);
    LoggingUtils.getEvoLogger().info("* Executing test(s)");
    if (Properties.SELECTED_JUNIT == null) {
        boolean origUseAgent = EvoRunner.useAgent;
        boolean origUseClassLoader = EvoRunner.useClassLoader;
        try {
            // avoid double instrumentation
            EvoRunner.useAgent = false;
            // avoid double instrumentation
            EvoRunner.useClassLoader = false;
            List<JUnitResult> results = executeTests(tests);
            printReport(results);
        } finally {
            EvoRunner.useAgent = origUseAgent;
            EvoRunner.useClassLoader = origUseClassLoader;
        }
    } else {
        // instead of just running junit tests, carve them
        JUnitTestCarvedChromosomeFactory carvedFactory = new JUnitTestCarvedChromosomeFactory(null);
        TestSuiteChromosome testSuite = carvedFactory.getCarvedTestSuite();
        int goals = 0;
        for (Properties.Criterion pc : Properties.CRITERION) {
            LoggingUtils.getEvoLogger().info("* Coverage analysis for criterion " + pc);
            TestFitnessFactory ffactory = FitnessFunctions.getFitnessFactory(pc);
            goals += ffactory.getCoverageGoals().size();
            FitnessFunction ffunction = FitnessFunctions.getFitnessFunction(pc);
            ffunction.getFitness(testSuite);
            CoverageCriteriaAnalyzer.analyzeCoverage(testSuite, pc);
        }
        // Generate test suite
        TestSuiteGenerator.writeJUnitTestsAndCreateResult(testSuite);
        StatisticsSender.executedAndThenSendIndividualToMaster(testSuite);
        ClientServices.getInstance().getClientNode().trackOutputVariable(RuntimeVariable.Total_Goals, goals);
        if (Properties.COVERAGE_MATRIX)
            throw new IllegalArgumentException("Coverage matrix not yet available when measuring coverage of a carved test suite");
    }
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) TestFitnessFactory(org.evosuite.coverage.TestFitnessFactory) TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) FitnessFunction(org.evosuite.ga.FitnessFunction) Properties(org.evosuite.Properties) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) TestClass(org.junit.runners.model.TestClass) Criterion(org.evosuite.Properties.Criterion)

Example 22 with JUnitTestCarvedChromosomeFactory

use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactorySystemTest method testBeanDateConverterUtils6.

@Test
public void testBeanDateConverterUtils6() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.DateConverterTest6.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.DateConverter.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertEquals(1, factory.getNumCarvedTestCases());
    String code = factory.getChromosome().getTestCase().toCode();
    System.out.println(code);
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Test(org.junit.Test)

Example 23 with JUnitTestCarvedChromosomeFactory

use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactorySystemTest method testBeanDateConverterUtils9.

@Test
public void testBeanDateConverterUtils9() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.DateConverterTest9.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.DateConverter.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertEquals(1, factory.getNumCarvedTestCases());
    String code = factory.getChromosome().getTestCase().toCode();
    System.out.println(code);
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Test(org.junit.Test)

Example 24 with JUnitTestCarvedChromosomeFactory

use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactorySystemTest method testBeanDateConverterUtils1.

@Test
public void testBeanDateConverterUtils1() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.DateConverterTest1.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.DateConverter.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertEquals(1, factory.getNumCarvedTestCases());
    String code = factory.getChromosome().getTestCase().toCode();
    System.out.println(code);
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Test(org.junit.Test)

Example 25 with JUnitTestCarvedChromosomeFactory

use of org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory in project evosuite by EvoSuite.

the class JUnitTestCarvedChromosomeFactorySystemTest method testGenericClassList.

@Test
public void testGenericClassList() {
    Properties.SELECTED_JUNIT = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithListTest.class.getCanonicalName();
    Properties.TARGET_CLASS = com.examples.with.different.packagename.testcarver.GenericObjectWrapperWithList.class.getCanonicalName();
    Properties.SEED_MUTATIONS = 1;
    Properties.SEED_CLONE = 1;
    JUnitTestCarvedChromosomeFactory factory = new JUnitTestCarvedChromosomeFactory(null);
    Assert.assertTrue(factory.hasCarvedTestCases());
    TestChromosome carved = factory.getChromosome();
    Assert.assertNotNull(carved);
    Assert.assertEquals("", 10, carved.test.size());
    for (int i = 0; i < carved.test.size(); i++) {
        Statement stmt = carved.test.getStatement(i);
        boolean valid = stmt.isValid();
        Assert.assertTrue("Invalid stmt at position " + i, valid);
    }
    String code = carved.toString();
    String setLong = "GenericObjectWrapperWithList<GenericObjectWrapperWithListTest.Foo>";
    Assert.assertTrue("generated code does not contain " + setLong + "\n" + code, code.contains(setLong));
}
Also used : JUnitTestCarvedChromosomeFactory(org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory) Statement(org.evosuite.testcase.statements.Statement) Test(org.junit.Test)

Aggregations

JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)41 Test (org.junit.Test)38 Statement (org.evosuite.testcase.statements.Statement)8 Properties (org.evosuite.Properties)2 ArchiveTestChromosomeFactory (org.evosuite.ga.archive.ArchiveTestChromosomeFactory)2 AllMethodsTestChromosomeFactory (org.evosuite.testcase.factories.AllMethodsTestChromosomeFactory)2 RandomLengthTestFactory (org.evosuite.testcase.factories.RandomLengthTestFactory)2 MethodWithSeveralInputArguments (com.examples.with.different.packagename.coverage.MethodWithSeveralInputArguments)1 TestMethodWithSeveralInputArguments (com.examples.with.different.packagename.coverage.TestMethodWithSeveralInputArguments)1 ArrayList (java.util.ArrayList)1 Criterion (org.evosuite.Properties.Criterion)1 TestFitnessFactory (org.evosuite.coverage.TestFitnessFactory)1 FitnessFunction (org.evosuite.ga.FitnessFunction)1 RegressionTestSuiteChromosomeFactory (org.evosuite.regression.RegressionTestSuiteChromosomeFactory)1 CarvedTestCase (org.evosuite.testcarver.testcase.CarvedTestCase)1 TestCase (org.evosuite.testcase.TestCase)1 TestChromosome (org.evosuite.testcase.TestChromosome)1 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)1 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)1 SerializationSuiteChromosomeFactory (org.evosuite.testsuite.factories.SerializationSuiteChromosomeFactory)1