Search in sources :

Example 11 with TestCase

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

the class RegressionSuiteStrategy method generateTests.

@Override
public TestSuiteChromosome generateTests() {
    track(RuntimeVariable.Total_Goals, 0);
    track(RuntimeVariable.Generated_Assertions, 0);
    track(RuntimeVariable.Coverage_Old, 0);
    track(RuntimeVariable.Coverage_New, 0);
    track(RuntimeVariable.Exception_Difference, 0);
    track(RuntimeVariable.State_Distance, 0);
    track(RuntimeVariable.Testsuite_Diversity, 0);
    // Disable test archive
    Properties.TEST_ARCHIVE = false;
    // Disable functional mocking stuff (due to incompatibilities)
    Properties.P_FUNCTIONAL_MOCKING = 0;
    Properties.FUNCTIONAL_MOCKING_INPUT_LIMIT = 0;
    Properties.FUNCTIONAL_MOCKING_PERCENT = 0;
    // Regression random strategy switch.
    if (Properties.REGRESSION_FITNESS == RegressionMeasure.RANDOM) {
        return generateRandomRegressionTests();
    }
    LoggingUtils.getEvoLogger().info("* Setting up search algorithm for REGRESSION suite generation");
    PropertiesSuiteGAFactory algorithmFactory = new PropertiesSuiteGAFactory();
    GeneticAlgorithm<?> algorithm = algorithmFactory.getSearchAlgorithm();
    if (Properties.SERIALIZE_GA || Properties.CLIENT_ON_THREAD) {
        TestGenerationResultBuilder.getInstance().setGeneticAlgorithm(algorithm);
    }
    long startTime = System.currentTimeMillis() / 1000;
    Properties.CRITERION = new Criterion[] { Criterion.REGRESSION };
    // What's the search target
    List<TestSuiteFitnessFunction> fitnessFunctions = getFitnessFunctions();
    // TODO: Argh, generics.
    algorithm.addFitnessFunctions((List) fitnessFunctions);
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.DEFUSE) || ArrayUtil.contains(Properties.CRITERION, Criterion.ALLDEFS) || ArrayUtil.contains(Properties.CRITERION, Criterion.STATEMENT) || ArrayUtil.contains(Properties.CRITERION, Criterion.RHO) || ArrayUtil.contains(Properties.CRITERION, Criterion.AMBIGUITY)) {
        ExecutionTracer.enableTraceCalls();
    }
    // TODO: why it was only if "analyzing"???
    // if (analyzing)
    algorithm.resetStoppingConditions();
    List<TestFitnessFunction> goals = getGoals(true);
    // List<TestSuiteChromosome> bestSuites = new
    // ArrayList<TestSuiteChromosome>();
    TestSuiteChromosome bestSuites = new TestSuiteChromosome();
    RegressionTestSuiteChromosome best = null;
    if (!Properties.STOP_ZERO || !goals.isEmpty()) {
        // logger.warn("performing search ... ############################################################");
        // Perform search
        LoggingUtils.getEvoLogger().info("* Using seed {}", Randomness.getSeed());
        LoggingUtils.getEvoLogger().info("* Starting evolution");
        ClientServices.getInstance().getClientNode().changeState(ClientState.SEARCH);
        algorithm.generateSolution();
        best = (RegressionTestSuiteChromosome) algorithm.getBestIndividual();
        // ArrayList<TestSuiteChromosome>();
        for (TestCase t : best.getTests()) {
            bestSuites.addTest(t);
        }
        // bestSuites = (List<TestSuiteChromosome>) ga.getBestIndividuals();
        if (bestSuites.size() == 0) {
            LoggingUtils.getEvoLogger().warn("Could not find any suiteable chromosome");
            return bestSuites;
        }
    } else {
        zeroFitness.setFinished();
        bestSuites = new TestSuiteChromosome();
        for (FitnessFunction<?> ff : bestSuites.getFitnessValues().keySet()) {
            bestSuites.setCoverage(ff, 1.0);
        }
    }
    long end_time = System.currentTimeMillis() / 1000;
    // recalculated now after the search, eg to handle exception fitness
    goals = getGoals(false);
    track(RuntimeVariable.Total_Goals, goals.size());
    // Newline after progress bar
    if (Properties.SHOW_PROGRESS) {
        LoggingUtils.getEvoLogger().info("");
    }
    String text = " statements, best individual has fitness: ";
    if (bestSuites.size() > 1) {
        text = " statements, best individuals have fitness: ";
    }
    LoggingUtils.getEvoLogger().info("* Search finished after " + (end_time - startTime) + "s and " + algorithm.getAge() + " generations, " + MaxStatementsStoppingCondition.getNumExecutedStatements() + text + ((best != null) ? best.getFitness() : ""));
    if (Properties.COVERAGE) {
        for (Properties.Criterion pc : Properties.CRITERION) {
            // FIXME: can
            CoverageCriteriaAnalyzer.analyzeCoverage(bestSuites, pc);
        }
    // we send
    // all
    // bestSuites?
    }
    // progressMonitor.updateStatus(99);
    int number_of_test_cases = 0;
    int totalLengthOfTestCases = 0;
    double coverage = 0.0;
    // for (TestSuiteChromosome tsc : bestSuites) {
    number_of_test_cases += bestSuites.size();
    totalLengthOfTestCases += bestSuites.totalLengthOfTestCases();
    coverage += bestSuites.getCoverage();
    if (ArrayUtil.contains(Properties.CRITERION, Criterion.MUTATION) || ArrayUtil.contains(Properties.CRITERION, Criterion.STRONGMUTATION)) {
    // SearchStatistics.getInstance().mutationScore(coverage);
    }
    // StatisticsSender.executedAndThenSendIndividualToMaster(bestSuites);
    // // FIXME: can we send all bestSuites?
    // statistics.iteration(ga);
    // statistics.minimized(bestSuites.get(0)); // FIXME: can we send all
    // bestSuites?
    LoggingUtils.getEvoLogger().info("* Generated " + number_of_test_cases + " tests with total length " + totalLengthOfTestCases);
    // TODO: In the end we will only need one analysis technique
    if (!Properties.ANALYSIS_CRITERIA.isEmpty()) {
        // SearchStatistics.getInstance().addCoverage(Properties.CRITERION.toString(),
        // coverage);
        CoverageCriteriaAnalyzer.analyzeCriteria(bestSuites, Properties.ANALYSIS_CRITERIA);
    // FIXME: can we send all bestSuites?
    }
    LoggingUtils.getEvoLogger().info("* Resulting test suite's coverage: " + NumberFormat.getPercentInstance().format(coverage));
    algorithm.printBudget();
    return bestSuites;
}
Also used : TestFitnessFunction(org.evosuite.testcase.TestFitnessFunction) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) TestSuiteFitnessFunction(org.evosuite.testsuite.TestSuiteFitnessFunction) Properties(org.evosuite.Properties) TestCase(org.evosuite.testcase.TestCase) RegressionTestSuiteChromosome(org.evosuite.regression.RegressionTestSuiteChromosome) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome) Criterion(org.evosuite.Properties.Criterion)

Example 12 with TestCase

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

the class ConcolicMutation method negateCondition.

/**
 * Generate new constraint and ask solver for solution
 *
 * @param pathCondition
 *
 * @param targetCondition
 *            a {@link org.evosuite.symbolic.BranchCondition} object.
 * @param test
 *            a {@link org.evosuite.testcase.TestCase} object.
 * @return a {@link org.evosuite.testcase.TestCase} object.
 */
// @SuppressWarnings({ "rawtypes", "unchecked" })
public static TestCase negateCondition(List<BranchCondition> pathCondition, BranchCondition targetCondition, TestCase test) {
    List<Constraint<?>> constraints = new LinkedList<Constraint<?>>();
    for (BranchCondition b : pathCondition) {
        constraints.addAll(b.getSupportingConstraints());
        if (b == targetCondition) {
            break;
        } else {
            constraints.add(b.getConstraint());
        }
    }
    final Constraint<?> targetConstraint = targetCondition.getConstraint().negate();
    constraints.add(targetConstraint);
    if (!targetConstraint.isSolveable()) {
        logger.info("Found unsolvable constraint: " + targetConstraint);
        // Could we treat this as a special case?
        return null;
    }
    int size = constraints.size();
    if (size > 0) {
        constraints = reduce(constraints);
    // logger.info("Reduced constraints from " + size + " to " +
    // constraints.size());
    // logger.info("Now solving: " + constraints);
    }
    Solver solver = SolverFactory.getInstance().buildNewSolver();
    SolverCache solverCache = SolverCache.getInstance();
    SolverResult solverResult = solverCache.solve(solver, constraints);
    if (solverResult != null) {
        // logger.info(values.toString());
        TestCase newTest = test.clone();
        Map<String, Object> model = solverResult.getModel();
        for (Object key : model.keySet()) {
            Object val = model.get(key);
            if (val != null) {
                if (val instanceof Long) {
                    Long value = (Long) val;
                    String name = ((String) key).replace("__SYM", "");
                    logger.debug("New value for " + name + " is " + value);
                    PrimitiveStatement<?> p = getStatement(newTest, name);
                    assert (p != null);
                    if (p instanceof BooleanPrimitiveStatement) {
                        BooleanPrimitiveStatement bp = (BooleanPrimitiveStatement) p;
                        bp.setValue(value.intValue() > 0);
                    } else if (p instanceof CharPrimitiveStatement) {
                        CharPrimitiveStatement cp = (CharPrimitiveStatement) p;
                        cp.setValue((char) value.intValue());
                    } else if (p instanceof BytePrimitiveStatement) {
                        BytePrimitiveStatement bp = (BytePrimitiveStatement) p;
                        bp.setValue((byte) value.intValue());
                    } else if (p instanceof ShortPrimitiveStatement) {
                        ShortPrimitiveStatement sp = (ShortPrimitiveStatement) p;
                        sp.setValue((short) value.intValue());
                    } else if (p instanceof LongPrimitiveStatement) {
                        LongPrimitiveStatement lp = (LongPrimitiveStatement) p;
                        lp.setValue(value);
                    } else {
                        assert (p instanceof IntPrimitiveStatement);
                        IntPrimitiveStatement ip = (IntPrimitiveStatement) p;
                        ip.setValue(value.intValue());
                    }
                } else {
                    logger.debug("New value is not long " + val);
                }
            } else {
                logger.debug("New value is null");
            }
        }
        return newTest;
    } else {
        logger.debug("Got null :-(");
        return null;
    }
}
Also used : BytePrimitiveStatement(org.evosuite.testcase.statements.numeric.BytePrimitiveStatement) Solver(org.evosuite.symbolic.solver.Solver) Constraint(org.evosuite.symbolic.expr.Constraint) SolverResult(org.evosuite.symbolic.solver.SolverResult) SolverCache(org.evosuite.symbolic.solver.SolverCache) LinkedList(java.util.LinkedList) Constraint(org.evosuite.symbolic.expr.Constraint) CharPrimitiveStatement(org.evosuite.testcase.statements.numeric.CharPrimitiveStatement) IntPrimitiveStatement(org.evosuite.testcase.statements.numeric.IntPrimitiveStatement) TestCase(org.evosuite.testcase.TestCase) ShortPrimitiveStatement(org.evosuite.testcase.statements.numeric.ShortPrimitiveStatement) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) LongPrimitiveStatement(org.evosuite.testcase.statements.numeric.LongPrimitiveStatement)

Example 13 with TestCase

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

the class TestCluster method getRandomGenerator.

/**
 * Randomly select one generator
 *
 * @param clazz
 * @param excluded
 * @param test
 * @return {@code null} if there is no valid generator
 * @throws ConstructionFailedException
 */
public GenericAccessibleObject<?> getRandomGenerator(GenericClass clazz, Set<GenericAccessibleObject<?>> excluded, TestCase test, int position, VariableReference generatorRefToExclude, int recursionDepth) throws ConstructionFailedException {
    logger.debug("Getting random generator for " + clazz);
    // Instantiate generics
    if (clazz.hasWildcardOrTypeVariables()) {
        logger.debug("Target class is generic: " + clazz);
        GenericClass concreteClass = clazz.getGenericInstantiation();
        if (!concreteClass.equals(clazz)) {
            logger.debug("Target class is generic: " + clazz + ", getting instantiation " + concreteClass);
            return getRandomGenerator(concreteClass, excluded, test, position, generatorRefToExclude, recursionDepth);
        }
    }
    GenericAccessibleObject<?> generator = null;
    // Collection, Map, Number
    if (isSpecialCase(clazz)) {
        generator = Randomness.choice(getGeneratorsForSpecialCase(clazz));
        if (generator == null) {
            logger.warn("No generator for special case class: " + clazz);
            throw new ConstructionFailedException("Have no generators for special case: " + clazz);
        }
    } else {
        cacheGenerators(clazz);
        Set<GenericAccessibleObject<?>> candidates = new LinkedHashSet<>(generatorCache.get(clazz));
        candidates.removeAll(excluded);
        if (Properties.JEE) {
            Iterator<GenericAccessibleObject<?>> iter = candidates.iterator();
            while (iter.hasNext()) {
                GenericAccessibleObject<?> gao = iter.next();
                if (gao instanceof GenericConstructor) {
                    Class<?> klass = gao.getDeclaringClass();
                    if (InstanceOnlyOnce.canInstantiateOnlyOnce(klass) && ConstraintHelper.countNumberOfNewInstances(test, klass) != 0) {
                        iter.remove();
                    }
                }
                if (!ConstraintVerifier.isValidPositionForInsertion(gao, test, position)) {
                    iter.remove();
                }
            }
        }
        if (generatorRefToExclude != null) {
            Iterator<GenericAccessibleObject<?>> iter = candidates.iterator();
            while (iter.hasNext()) {
                GenericAccessibleObject<?> gao = iter.next();
                // if current generator could be called from excluded ref, then we cannot use it
                if (generatorRefToExclude.isAssignableTo(gao.getOwnerType())) {
                    iter.remove();
                }
            }
        }
        logger.debug("Candidate generators for " + clazz + ": " + candidates.size());
        if (candidates.isEmpty()) {
            return null;
        }
        if (recursionDepth >= Properties.MAX_RECURSION / 2) {
            /*
					if going long into the recursion, then do prefer direct constructors or static methods,
					as non-static methods would require to get a caller which, if it is missing, would need
					to be created, and that could lead to further calls if its generators need input parameters
				 */
            Set<GenericAccessibleObject<?>> set = candidates.stream().filter(p -> p.isStatic() || p.isConstructor()).collect(Collectors.toCollection(() -> new LinkedHashSet<GenericAccessibleObject<?>>()));
            if (!set.isEmpty()) {
                candidates = set;
            }
        }
        generator = Randomness.choice(candidates);
        logger.debug("Chosen generator: " + generator);
    }
    if (generator.getOwnerClass().hasWildcardOrTypeVariables()) {
        logger.debug("Owner class has a wildcard: " + clazz.getTypeName());
        generator = generator.copyWithNewOwner(generator.getOwnerClass().getGenericInstantiation());
    }
    if (generator.hasTypeParameters()) {
        logger.debug("Generator has a type parameter: " + generator);
        generator = generator.getGenericInstantiationFromReturnValue(clazz);
        if (!generator.getGeneratedClass().isAssignableTo(clazz)) {
            throw new ConstructionFailedException("Generics error");
        }
    }
    return generator;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) java.util(java.util) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) InstanceOnlyOnce(org.evosuite.testcase.jee.InstanceOnlyOnce) LoggerFactory(org.slf4j.LoggerFactory) Archive(org.evosuite.ga.archive.Archive) Constructor(java.lang.reflect.Constructor) GenericMethod(org.evosuite.utils.generic.GenericMethod) ConstraintHelper(org.evosuite.testcase.ConstraintHelper) TestCase(org.evosuite.testcase.TestCase) CoverageAnalysis(org.evosuite.junit.CoverageAnalysis) AtMostOnceLogger(org.evosuite.runtime.util.AtMostOnceLogger) Properties(org.evosuite.Properties) Logger(org.slf4j.Logger) VariableReference(org.evosuite.testcase.variable.VariableReference) Inputs(org.evosuite.runtime.util.Inputs) GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) CastClassManager(org.evosuite.seeding.CastClassManager) Collectors(java.util.stream.Collectors) Randomness(org.evosuite.utils.Randomness) TestGenerationContext(org.evosuite.TestGenerationContext) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) ListUtil(org.evosuite.utils.ListUtil) Type(java.lang.reflect.Type) Entry(java.util.Map.Entry) ConstraintVerifier(org.evosuite.testcase.ConstraintVerifier) GenericClass(org.evosuite.utils.generic.GenericClass) GenericConstructor(org.evosuite.utils.generic.GenericConstructor) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 14 with TestCase

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

the class BIMethodSeedingTestSuiteChromosomeFactory method getChromosome.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.evosuite.ga.ChromosomeFactory#getChromosome()
	 */
/**
 * {@inheritDoc}
 */
@Override
public TestSuiteChromosome getChromosome() {
    /*
		 * double P_delta = 0.1d; double P_clone = 0.1d; int MAX_CHANGES = 10;
		 */
    TestSuiteChromosome chromosome = defaultFactory.getChromosome();
    int numTests = chromosome.getTests().size();
    // reduce seed probablility by number of tests to be generated
    final double SEED_CHANCE = Properties.SEED_PROBABILITY / numTests;
    for (int i = 0; i < numTests; i++) {
        if (Randomness.nextDouble() < SEED_CHANCE) {
            int testSize = bestIndividual.getTests().size();
            TestCase test = bestIndividual.getTests().get(Randomness.nextInt(testSize));
            if (test != null) {
                List<TestCase> tests = chromosome.getTests();
                tests.remove(i);
                tests.add(i, test);
                chromosome.clearTests();
                for (TestCase t : tests) {
                    chromosome.addTest(t);
                }
            }
        }
    // chromosome.tests.add(test);
    }
    return chromosome;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Example 15 with TestCase

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

the class RandomMethodSeedingTestSuiteChromosomeFactory method getChromosome.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.evosuite.ga.ChromosomeFactory#getChromosome()
	 */
/**
 * {@inheritDoc}
 */
@Override
public TestSuiteChromosome getChromosome() {
    /*
		 * double P_delta = 0.1d; double P_clone = 0.1d; int MAX_CHANGES = 10;
		 */
    TestSuiteChromosome chromosome = defaultFactory.getChromosome();
    int numTests = chromosome.getTests().size();
    // reduce seed probablility by number of tests to be generated
    final double SEED_CHANCE = Properties.SEED_PROBABILITY / numTests;
    for (int i = 0; i < numTests; i++) {
        if (geneticAlgorithm != null && Randomness.nextDouble() < SEED_CHANCE) {
            int populationSize = geneticAlgorithm.getPopulation().size();
            TestSuiteChromosome tsc = geneticAlgorithm.getPopulation().get(Randomness.nextInt(populationSize));
            int testSize = tsc.getTests().size();
            TestCase test = tsc.getTests().get(Random.nextInt(testSize));
            if (test != null) {
                List<TestCase> tests = chromosome.getTests();
                tests.remove(i);
                tests.add(i, test);
                chromosome.clearTests();
                for (TestCase t : tests) {
                    chromosome.addTest(t);
                }
            }
        }
    }
    return chromosome;
}
Also used : TestCase(org.evosuite.testcase.TestCase) TestSuiteChromosome(org.evosuite.testsuite.TestSuiteChromosome)

Aggregations

TestCase (org.evosuite.testcase.TestCase)192 Test (org.junit.Test)119 DefaultTestCase (org.evosuite.testcase.DefaultTestCase)90 ArrayList (java.util.ArrayList)67 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)47 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)45 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)43 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)39 VariableReference (org.evosuite.testcase.variable.VariableReference)26 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)25 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)25 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)24 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)17 TestChromosome (org.evosuite.testcase.TestChromosome)17 InstrumentingClassLoader (org.evosuite.instrumentation.InstrumentingClassLoader)15 Ignore (org.junit.Ignore)15 VariableReferenceImpl (org.evosuite.testcase.variable.VariableReferenceImpl)14 GenericMethod (org.evosuite.utils.generic.GenericMethod)12 ExceptionCoverageTestFitness (org.evosuite.coverage.exception.ExceptionCoverageTestFitness)11 InputCoverageGoal (org.evosuite.coverage.io.input.InputCoverageGoal)10