Search in sources :

Example 26 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class Archive method hasFunctionalMocksForGenerableTypes.

private boolean hasFunctionalMocksForGenerableTypes(TestCase testCase) {
    for (Statement statement : testCase) {
        if (statement instanceof FunctionalMockStatement) {
            FunctionalMockStatement fm = (FunctionalMockStatement) statement;
            Class<?> target = fm.getTargetClass();
            GenericClass gc = new GenericClass(target);
            if (TestCluster.getInstance().hasGenerator(gc)) {
                return true;
            }
        }
    }
    return false;
}
Also used : GenericClass(org.evosuite.utils.generic.GenericClass) Statement(org.evosuite.testcase.statements.Statement) PrivateMethodStatement(org.evosuite.testcase.statements.reflection.PrivateMethodStatement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement) PrivateFieldStatement(org.evosuite.testcase.statements.reflection.PrivateFieldStatement) FunctionalMockStatement(org.evosuite.testcase.statements.FunctionalMockStatement)

Example 27 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class TestSuiteSerialization method loadTests.

public static List<TestChromosome> loadTests(File target) throws IllegalArgumentException {
    Inputs.checkNull(target);
    List<TestChromosome> list = new ArrayList<>();
    try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(target))) {
        try {
            Object obj = in.readObject();
            while (obj != null) {
                if (obj instanceof TestChromosome) {
                    // this check might fail if old version is used, and EvoSuite got updated
                    TestChromosome tc = (TestChromosome) obj;
                    for (Statement st : tc.getTestCase()) {
                        st.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT());
                    }
                    list.add(tc);
                }
                obj = in.readObject();
            }
        } catch (EOFException e) {
        // fine
        } catch (Exception e) {
            logger.warn("Problems when reading a serialized test from " + target.getAbsolutePath() + " : " + e.getMessage());
        }
        in.close();
    } catch (FileNotFoundException e) {
        logger.warn("Cannot load tests because file does not exist: " + target.getAbsolutePath());
    } catch (IOException e) {
        logger.error("Failed to open/handle " + target.getAbsolutePath() + " for reading: " + e.getMessage());
    }
    return list;
}
Also used : Statement(org.evosuite.testcase.statements.Statement) ArrayList(java.util.ArrayList) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 28 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class ParameterLocalSearch method doSearch.

/* (non-Javadoc)
	 * @see org.evosuite.testcase.LocalSearch#doSearch(org.evosuite.testcase.TestChromosome, int, org.evosuite.ga.LocalSearchObjective)
	 */
/**
 * {@inheritDoc}
 */
@Override
public boolean doSearch(TestChromosome test, int statement, LocalSearchObjective<TestChromosome> objective) {
    Statement stmt = test.getTestCase().getStatement(statement);
    backup(test, stmt);
    if (stmt instanceof MethodStatement) {
        return doSearch(test, (MethodStatement) stmt, objective);
    } else if (stmt instanceof ConstructorStatement) {
        return doSearch(test, (ConstructorStatement) stmt, objective);
    } else if (stmt instanceof FieldStatement) {
        return doSearch(test, (FieldStatement) stmt, objective);
    } else {
        return false;
    }
}
Also used : ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement) ConstructorStatement(org.evosuite.testcase.statements.ConstructorStatement) FieldStatement(org.evosuite.testcase.statements.FieldStatement)

Example 29 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class RegressionExceptionHelper method simpleExceptionName.

/**
 * Get a simple (and unique looking) exception name (exType or exThrowingMethodCall:exType)
 */
public static String simpleExceptionName(RegressionTestChromosome test, Integer statementPos, Throwable ex) {
    if (ex == null) {
        return "";
    }
    String exception = ex.getClass().getSimpleName();
    if (test.getTheTest().getTestCase().hasStatement(statementPos)) {
        Statement exThrowingStatement = test.getTheTest().getTestCase().getStatement(statementPos);
        if (exThrowingStatement instanceof MethodStatement) {
            String exMethodcall = ((MethodStatement) exThrowingStatement).getMethod().getName();
            exception = exMethodcall + ":" + exception;
        }
    }
    return exception;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement)

Example 30 with Statement

use of org.evosuite.testcase.statements.Statement in project evosuite by EvoSuite.

the class RegressionFitnessHelper method trackDiversity.

/**
 * Diversity is based on the test case statements, and doesn't used with execution results
 */
static void trackDiversity(RegressionTestChromosome c, TestChromosome testChromosome) {
    Map<String, Map<Integer, String>> testDiversityMap = new HashMap<>();
    for (int i = 0; i < testChromosome.getTestCase().size(); i++) {
        Statement x = testChromosome.getTestCase().getStatement(i);
        if (x instanceof MethodStatement) {
            MethodStatement methodCall = (MethodStatement) x;
            VariableReference callee = methodCall.getCallee();
            if (callee == null) {
                continue;
            }
            int calleePosition = callee.getStPosition();
            String calleeClass = callee.getClassName();
            String methodCallName = methodCall.getMethod().getName();
            Map<Integer, String> calleeMap = testDiversityMap.get(calleeClass);
            if (calleeMap == null) {
                calleeMap = new HashMap<Integer, String>();
            }
            String calledMethods = calleeMap.get(calleePosition);
            if (calledMethods == null) {
                calledMethods = "";
            }
            calledMethods += methodCallName;
            calleeMap.put(calleePosition, calledMethods);
            testDiversityMap.put(calleeClass, calleeMap);
        }
    }
    c.diversityMap = testDiversityMap;
}
Also used : MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) HashMap(java.util.HashMap) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Statement (org.evosuite.testcase.statements.Statement)48 MethodStatement (org.evosuite.testcase.statements.MethodStatement)25 VariableReference (org.evosuite.testcase.variable.VariableReference)17 ConstructorStatement (org.evosuite.testcase.statements.ConstructorStatement)16 PrimitiveStatement (org.evosuite.testcase.statements.PrimitiveStatement)11 TestCase (org.evosuite.testcase.TestCase)8 JUnitTestCarvedChromosomeFactory (org.evosuite.testcase.factories.JUnitTestCarvedChromosomeFactory)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 FieldStatement (org.evosuite.testcase.statements.FieldStatement)7 Method (java.lang.reflect.Method)6 TestChromosome (org.evosuite.testcase.TestChromosome)6 GenericMethod (org.evosuite.utils.generic.GenericMethod)6 FunctionalMockStatement (org.evosuite.testcase.statements.FunctionalMockStatement)5 NullStatement (org.evosuite.testcase.statements.NullStatement)5 StringPrimitiveStatement (org.evosuite.testcase.statements.StringPrimitiveStatement)5 ConstructionFailedException (org.evosuite.ga.ConstructionFailedException)4 ArrayStatement (org.evosuite.testcase.statements.ArrayStatement)4 TestFactory (org.evosuite.testcase.TestFactory)3 TestFitnessFunction (org.evosuite.testcase.TestFitnessFunction)3