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;
}
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;
}
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;
}
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++;
}
}
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);
}
}
Aggregations