Search in sources :

Example 26 with ConstructionFailedException

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

the class TestCluster method getRandomObjectGenerator.

/**
 * Randomly select a generator for an Object.class instance
 *
 * @return a generator of type GenericAccessibleObject<?> or <code>null</code>
 * @throws ConstructionFailedException
 */
public GenericAccessibleObject<?> getRandomObjectGenerator() throws ConstructionFailedException {
    logger.debug("Getting random object generator");
    GenericAccessibleObject<?> generator = Randomness.choice(getObjectGenerators());
    if (generator == null) {
        // should NOT occur (from getObjectGenerators())
        logger.warn("Random object generator is null");
        throw new ConstructionFailedException("Random object generator is null");
    }
    if (generator.getOwnerClass().hasWildcardOrTypeVariables()) {
        logger.debug("Generator has wildcard or type: " + generator);
        GenericClass concreteClass = generator.getOwnerClass().getGenericInstantiation();
        generator = generator.copyWithNewOwner(concreteClass);
    }
    if (generator.hasTypeParameters()) {
        logger.debug("Generator has type parameters");
        generator = generator.getGenericInstantiation();
    }
    return generator;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 27 with ConstructionFailedException

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

the class TestCluster method getRandomGenerator.

/**
 * Randomly select one generator
 *
 * @param clazz
 * @return
 * @throws ConstructionFailedException
 */
public GenericAccessibleObject<?> getRandomGenerator(GenericClass clazz) throws ConstructionFailedException {
    if (clazz.hasWildcardOrTypeVariables()) {
        GenericClass concreteClass = clazz.getGenericInstantiation();
        if (concreteClass.hasWildcardOrTypeVariables())
            throw new ConstructionFailedException("Could not found concrete instantiation of generic type");
        return getRandomGenerator(concreteClass);
    }
    GenericAccessibleObject<?> generator = null;
    if (isSpecialCase(clazz)) {
        Collection<GenericAccessibleObject<?>> generators = getGeneratorsForSpecialCase(clazz);
        if (generators.isEmpty()) {
            logger.warn("No generators for class: " + clazz);
        }
        generator = Randomness.choice(generators);
    } else {
        if (!hasGenerator(clazz))
            throw new ConstructionFailedException("No generators of type " + clazz);
        generator = Randomness.choice(generatorCache.get(clazz));
    }
    if (generator == null)
        throw new ConstructionFailedException("No generators of type " + clazz);
    if (generator.hasTypeParameters()) {
        generator = generator.getGenericInstantiation(clazz);
    }
    return generator;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 28 with ConstructionFailedException

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

the class TestCluster method getTestCalls.

/**
 * Get a list of all test calls (i.e., constructors and methods)
 *
 * @return
 * @throws ConstructionFailedException
 */
public List<GenericAccessibleObject<?>> getTestCalls() {
    // TODO: Check for generic methods
    List<GenericAccessibleObject<?>> result = new ArrayList<>();
    for (GenericAccessibleObject<?> ao : testMethods) {
        if (ao.getOwnerClass().hasWildcardOrTypeVariables()) {
            try {
                GenericClass concreteClass = ao.getOwnerClass().getGenericInstantiation();
                result.add(ao.copyWithNewOwner(concreteClass));
            } catch (ConstructionFailedException e) {
                logger.debug("Failed to instantiate " + ao);
            }
        } else {
            result.add(ao);
        }
    }
    return result;
}
Also used : GenericAccessibleObject(org.evosuite.utils.generic.GenericAccessibleObject) GenericClass(org.evosuite.utils.generic.GenericClass) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 29 with ConstructionFailedException

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

the class RegressionSuiteMinimizer method minimizeSuite.

private void minimizeSuite(RegressionTestSuiteChromosome suite) {
    // logger.warn("minimizeSuite:\n{}", suite);
    Iterator<TestChromosome> it = suite.getTestChromosomes().iterator();
    int testCount = 0;
    while (it.hasNext()) {
        if (isTimeoutReached()) {
            logger.warn("minimization timeout reached. skipping minimization");
            break;
        }
        // logger.warn("##########################   TEST{}   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%",
        // testCount);
        RegressionTestChromosome test = (RegressionTestChromosome) it.next();
        // see if the assertion is still failing after removing the statement
        for (int i = test.getTheTest().size() - 1; i >= 0; i--) {
            if (isTimeoutReached()) {
                logger.warn("minimization timeout reached. skipping minimization");
                break;
            }
            logger.debug("Current size: " + suite.size() + "/" + suite.totalLengthOfTestCases());
            logger.debug("Deleting statement {} " + test.getTheTest().getTestCase().getStatement(i).getCode() + " from test", i);
            TestChromosome originalTestChromosome = (TestChromosome) test.getTheTest().clone();
            executeTest(test);
            /*
         * if(test.getLastExecutionResult()==null ||
				 * test.getLastRegressionExecutionResult()==null){
				 * logger.error("test execution result was null"); //continue; }
				 */
            // originalTestChromosome.setLastExecutionResult(test.getLastExecutionResult());
            int preRemovalAssertions = numFailingAssertions(test);
            try {
                TestFactory testFactory = TestFactory.getInstance();
                testFactory.deleteStatementGracefully(test.getTheTest().getTestCase(), i);
                test.getTheTest().setChanged(true);
            } catch (ConstructionFailedException e) {
                test.getTheTest().setChanged(false);
                test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                logger.error("Deleting failed");
                continue;
            }
            RegressionTestChromosome rtc = new RegressionTestChromosome();
            rtc.setTest((TestChromosome) test.getTheTest().clone());
            // rtc.updateClassloader();
            executeTest(rtc);
            int postRemovalAssertions = numFailingAssertions(rtc);
            // preRemovalAssertions, postRemovalAssertions);
            if (postRemovalAssertions == preRemovalAssertions) {
                test.updateClassloader();
                // the change had no effect
                continue;
            } else {
                // Restore previous state
                logger.debug("Can't remove statement " + originalTestChromosome.getTestCase().getStatement(i).getCode());
                test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                test.getTheTest().setLastExecutionResult(originalTestChromosome.getLastExecutionResult());
                test.getTheTest().setChanged(false);
            }
        }
        test.updateClassloader();
        if (test.getTheTest().isChanged()) {
            executeTest(test);
        }
        testCount++;
    }
}
Also used : TestFactory(org.evosuite.testcase.TestFactory) TestChromosome(org.evosuite.testcase.TestChromosome) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException)

Example 30 with ConstructionFailedException

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

the class RegressionSuiteMinimizer method removeDuplicateExceptions.

private void removeDuplicateExceptions(RegressionTestSuiteChromosome suite) {
    Set<String> uniqueExceptions = new HashSet<>();
    Map<String, String> exceptionStatementMapping = new HashMap<>();
    List<TestChromosome> chromosomes = suite.getTestChromosomes();
    for (int i = 0; i < chromosomes.size(); i++) {
        RegressionTestChromosome test = (RegressionTestChromosome) chromosomes.get(i);
        boolean changed = false;
        boolean hadAssertion = test.getTheTest().getTestCase().getAssertions().size() > 0;
        ExecutionResult resultA = test.getLastExecutionResult();
        ExecutionResult resultB = test.getLastRegressionExecutionResult();
        // re-execute the tests and check
        if (resultA == null || resultB == null) {
            executeTest(test);
            resultA = test.getLastExecutionResult();
            resultB = test.getLastRegressionExecutionResult();
            if (resultA == null || resultB == null) {
                continue;
            }
        }
        Map<Integer, Throwable> exceptionMapA = resultA.getCopyOfExceptionMapping();
        Map<Integer, Throwable> exceptionMapB = resultB.getCopyOfExceptionMapping();
        // exceptionMapA.size(), exceptionMapB.size());
        if (!resultA.noThrownExceptions() || !resultB.noThrownExceptions()) {
            double exDiff = RegressionExceptionHelper.compareExceptionDiffs(exceptionMapA, exceptionMapB);
            logger.warn("Test{} - Difference in number of exceptions: {}", i, exDiff);
            if (exDiff > 0) {
                /*
           * Three scenarios:
           * 1. Same exception, different messages
           * 2. Different exception in A
           * 3. Different exception in B
           */
                for (Entry<Integer, Throwable> ex : exceptionMapA.entrySet()) {
                    Throwable exA = ex.getValue();
                    // unique statement key over all tests (to avoid removing the same exception twice)
                    String exKey = i + ":" + ex.getKey();
                    // exceptionA signatures
                    String exception = RegressionExceptionHelper.simpleExceptionName(test, ex.getKey(), exA);
                    String exSignatureA = RegressionExceptionHelper.getExceptionSignature(exA, Properties.TARGET_CLASS);
                    String signaturePair = exSignatureA + ",";
                    Throwable exB = exceptionMapB.get(ex.getKey());
                    if (exB != null) {
                        // if the same statement in B also throws an exception
                        // exceptionB signatures
                        String exceptionB = RegressionExceptionHelper.simpleExceptionName(test, ex.getKey(), exB);
                        String exSignatureB = RegressionExceptionHelper.getExceptionSignature(exA, Properties.TARGET_CLASS);
                        signaturePair += exSignatureB;
                        if (exception.equals(exceptionB) || exSignatureA.equals(exSignatureB)) {
                            // We will be taking care of removing this exception when checking from A to B
                            // so there isn't a need to check again from B to A
                            exceptionMapB.remove(ex.getKey());
                        }
                    }
                    logger.warn("Test{}, uniqueExceptions: {}", i, uniqueExceptions);
                    logger.warn("checking exception: {} at {}", exception, ex.getKey());
                    List<String> signatures = Arrays.asList(exception, signaturePair);
                    for (String sig : signatures) {
                        // Compare exceptions on the merit of their message or signature
                        if (uniqueExceptions.contains(sig) && exceptionStatementMapping.get(sig) != exKey && !hadAssertion) {
                            TestChromosome originalTestChromosome = (TestChromosome) test.getTheTest().clone();
                            try {
                                TestFactory testFactory = TestFactory.getInstance();
                                testFactory.deleteStatementGracefully(test.getTheTest().getTestCase(), ex.getKey());
                                test.getTheTest().setChanged(true);
                                logger.warn("Removed exceptionA throwing line {}", ex.getKey());
                            } catch (ConstructionFailedException e) {
                                test.getTheTest().setChanged(false);
                                test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                                logger.error("ExceptionA deletion failed");
                                continue;
                            }
                            changed = true;
                            break;
                        } else {
                            uniqueExceptions.add(sig);
                            exceptionStatementMapping.put(sig, exKey);
                        }
                    }
                }
                // Check from the other way around (B to A)
                for (Entry<Integer, Throwable> ex : exceptionMapB.entrySet()) {
                    String exception = RegressionExceptionHelper.simpleExceptionName(test, ex.getKey(), ex.getValue());
                    String exKey = i + ":" + ex.getKey();
                    logger.warn("Test{}, uniqueExceptions: {}", i, uniqueExceptions);
                    logger.warn("checking exceptionB: {} at {}", exception, ex.getKey());
                    if (uniqueExceptions.contains(exception) && exceptionStatementMapping.get(exception) != exKey && !hadAssertion && test.getTheTest().getTestCase().hasStatement(ex.getKey())) {
                        TestChromosome originalTestChromosome = (TestChromosome) test.getTheTest().clone();
                        try {
                            TestFactory testFactory = TestFactory.getInstance();
                            logger.warn("removing statementB: {}", test.getTheTest().getTestCase().getStatement(ex.getKey()));
                            testFactory.deleteStatementGracefully(test.getTheTest().getTestCase(), ex.getKey());
                            test.getTheTest().setChanged(true);
                            logger.warn("removed exceptionB throwing line {}", ex.getKey());
                        } catch (ConstructionFailedException e) {
                            test.getTheTest().setChanged(false);
                            test.getTheTest().setTestCase(originalTestChromosome.getTestCase());
                            logger.error("ExceptionB deletion failed");
                            continue;
                        }
                        changed = true;
                    } else {
                        uniqueExceptions.add(exception);
                        exceptionStatementMapping.put(exception, exKey);
                    }
                }
            }
        }
        if (changed) {
            test.updateClassloader();
            executeTest(test);
            i--;
        }
    }
    if (uniqueExceptions.size() > 0) {
        logger.warn("unique exceptions: {}", uniqueExceptions);
    }
}
Also used : HashMap(java.util.HashMap) ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) ConstructionFailedException(org.evosuite.ga.ConstructionFailedException) TestFactory(org.evosuite.testcase.TestFactory) TestChromosome(org.evosuite.testcase.TestChromosome) HashSet(java.util.HashSet)

Aggregations

ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)43 GenericClass (org.evosuite.utils.generic.GenericClass)16 PrivateFieldStatement (org.evosuite.testcase.statements.reflection.PrivateFieldStatement)14 PrivateMethodStatement (org.evosuite.testcase.statements.reflection.PrivateMethodStatement)14 CaptureType (com.googlecode.gentyref.CaptureType)11 GenericAccessibleObject (org.evosuite.utils.generic.GenericAccessibleObject)11 GenericMethod (org.evosuite.utils.generic.GenericMethod)10 ArrayList (java.util.ArrayList)8 GenericField (org.evosuite.utils.generic.GenericField)7 TestFactory (org.evosuite.testcase.TestFactory)6 GenericConstructor (org.evosuite.utils.generic.GenericConstructor)6 TestCase (org.evosuite.testcase.TestCase)4 VariableReference (org.evosuite.testcase.variable.VariableReference)4 Type (java.lang.reflect.Type)3 GenericArrayType (java.lang.reflect.GenericArrayType)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 TypeVariable (java.lang.reflect.TypeVariable)2 WildcardType (java.lang.reflect.WildcardType)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2