Search in sources :

Example 1 with DefaultTestCase

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

the class TestSuiteGenerator method initializeTargetClass.

private void initializeTargetClass() throws Throwable {
    String cp = ClassPathHandler.getInstance().getTargetProjectClasspath();
    // Here is where the <clinit> code should be invoked for the first time
    DefaultTestCase test = buildLoadTargetClassTestCase(Properties.TARGET_CLASS);
    ExecutionResult execResult = TestCaseExecutor.getInstance().execute(test, Integer.MAX_VALUE);
    if (hasThrownInitializerError(execResult)) {
        // create single test suite with Class.forName()
        writeJUnitTestSuiteForFailedInitialization();
        ExceptionInInitializerError ex = getInitializerError(execResult);
        throw ex;
    } else if (!execResult.getAllThrownExceptions().isEmpty()) {
        // some other exception has been thrown during initialization
        Throwable t = execResult.getAllThrownExceptions().iterator().next();
        throw t;
    }
    DependencyAnalysis.analyzeClass(Properties.TARGET_CLASS, Arrays.asList(cp.split(File.pathSeparator)));
    LoggingUtils.getEvoLogger().info("* Finished analyzing classpath");
}
Also used : ExecutionResult(org.evosuite.testcase.execution.ExecutionResult) DefaultTestCase(org.evosuite.testcase.DefaultTestCase)

Example 2 with DefaultTestCase

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

the class TestSuiteGenerator method buildLoadTargetClassTestCase.

/**
 * Creates a single Test Case that only loads the target class.
 * <code>
 * Thread currentThread = Thread.currentThread();
 * ClassLoader classLoader = currentThread.getClassLoader();
 * classLoader.load(className);
 * </code>
 * @param className the class to be loaded
 * @return
 * @throws EvosuiteError if a reflection error happens while creating the test case
 */
private static DefaultTestCase buildLoadTargetClassTestCase(String className) throws EvosuiteError {
    DefaultTestCase test = new DefaultTestCase();
    StringPrimitiveStatement stmt0 = new StringPrimitiveStatement(test, className);
    VariableReference string0 = test.addStatement(stmt0);
    try {
        Method currentThreadMethod = Thread.class.getMethod("currentThread");
        Statement currentThreadStmt = new MethodStatement(test, new GenericMethod(currentThreadMethod, currentThreadMethod.getDeclaringClass()), null, Collections.emptyList());
        VariableReference currentThreadVar = test.addStatement(currentThreadStmt);
        Method getContextClassLoaderMethod = Thread.class.getMethod("getContextClassLoader");
        Statement getContextClassLoaderStmt = new MethodStatement(test, new GenericMethod(getContextClassLoaderMethod, getContextClassLoaderMethod.getDeclaringClass()), currentThreadVar, Collections.emptyList());
        VariableReference contextClassLoaderVar = test.addStatement(getContextClassLoaderStmt);
        // Method loadClassMethod = ClassLoader.class.getMethod("loadClass", String.class);
        // Statement loadClassStmt = new MethodStatement(test,
        // new GenericMethod(loadClassMethod, loadClassMethod.getDeclaringClass()), contextClassLoaderVar,
        // Collections.singletonList(string0));
        // test.addStatement(loadClassStmt);
        BooleanPrimitiveStatement stmt1 = new BooleanPrimitiveStatement(test, true);
        VariableReference boolean0 = test.addStatement(stmt1);
        Method forNameMethod = Class.class.getMethod("forName", String.class, boolean.class, ClassLoader.class);
        Statement forNameStmt = new MethodStatement(test, new GenericMethod(forNameMethod, forNameMethod.getDeclaringClass()), null, Arrays.<VariableReference>asList(string0, boolean0, contextClassLoaderVar));
        test.addStatement(forNameStmt);
        return test;
    } catch (NoSuchMethodException | SecurityException e) {
        throw new EvosuiteError("Unexpected exception while creating Class Initializer Test Case");
    }
}
Also used : StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) VariableReference(org.evosuite.testcase.variable.VariableReference) EvosuiteError(org.evosuite.testcase.execution.EvosuiteError) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) Statement(org.evosuite.testcase.statements.Statement) MethodStatement(org.evosuite.testcase.statements.MethodStatement) StringPrimitiveStatement(org.evosuite.testcase.statements.StringPrimitiveStatement) BooleanPrimitiveStatement(org.evosuite.testcase.statements.numeric.BooleanPrimitiveStatement) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Method(java.lang.reflect.Method) GenericMethod(org.evosuite.utils.generic.GenericMethod) GenericMethod(org.evosuite.utils.generic.GenericMethod)

Example 3 with DefaultTestCase

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

the class CoverageCriteriaAnalyzer method reinstrument.

private static void reinstrument(TestSuiteChromosome testSuite, Properties.Criterion criterion) {
    if (ArrayUtil.contains(Properties.SECONDARY_OBJECTIVE, Properties.SecondaryObjective.IBRANCH)) {
        ExecutionTracer.enableContext();
    }
    if (!ExecutionTracer.isTraceCallsEnabled()) {
        ExecutionTracer.enableTraceCalls();
    }
    testSuite.setChanged(true);
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        test.setChanged(true);
        // clears last execution result and last mutation result
        test.clearCachedResults();
    }
    Properties.Criterion[] oldCriterion = Arrays.copyOf(Properties.CRITERION, Properties.CRITERION.length);
    Properties.CRITERION = new Properties.Criterion[] { criterion };
    logger.info("Re-instrumenting for criterion: " + criterion);
    TestGenerationContext.getInstance().resetContext();
    // Need to load class explicitly in case there are no test cases.
    // If there are tests, then this is redundant
    Properties.getInitializedTargetClass();
    // TODO: Now all existing test cases have reflection objects pointing to the wrong classloader
    logger.info("Changing classloader of test suite for criterion: " + criterion);
    for (TestChromosome test : testSuite.getTestChromosomes()) {
        DefaultTestCase dtest = (DefaultTestCase) test.getTestCase();
        dtest.changeClassLoader(TestGenerationContext.getInstance().getClassLoaderForSUT());
    }
    Properties.CRITERION = oldCriterion;
}
Also used : Criterion(org.evosuite.Properties.Criterion) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Properties(org.evosuite.Properties) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 4 with DefaultTestCase

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

the class ConcolicExecution method getSymbolicPath.

/**
 * Retrieve the path condition for a given test case
 *
 * @param test
 *            a {@link org.evosuite.testcase.TestChromosome} object.
 * @return a {@link java.util.List} object.
 */
public static List<BranchCondition> getSymbolicPath(TestChromosome test) {
    TestChromosome dscCopy = (TestChromosome) test.clone();
    DefaultTestCase defaultTestCase = (DefaultTestCase) dscCopy.getTestCase();
    return executeConcolic(defaultTestCase);
}
Also used : DefaultTestCase(org.evosuite.testcase.DefaultTestCase) TestChromosome(org.evosuite.testcase.TestChromosome)

Example 5 with DefaultTestCase

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

the class TestCoverageGoalNameGeneration method testConstructorExceptionWithFullyQualifiedClassName.

@Test
public void testConstructorExceptionWithFullyQualifiedClassName() {
    TestCase test = new DefaultTestCase();
    ExceptionCoverageTestFitness goal = new ExceptionCoverageTestFitness("org.package.name.FooClass", "<init>()", RuntimeException.class, ExceptionCoverageTestFitness.ExceptionType.EXPLICIT);
    test.addCoveredGoal(goal);
    List<TestCase> tests = new ArrayList<>();
    tests.add(test);
    CoverageGoalTestNameGenerationStrategy naming = new CoverageGoalTestNameGenerationStrategy(tests);
    String generatedName = naming.getName(test);
    assertEquals("testFailsToCreateFooClassThrowsRuntimeException", generatedName);
}
Also used : ExceptionCoverageTestFitness(org.evosuite.coverage.exception.ExceptionCoverageTestFitness) MethodNoExceptionCoverageTestFitness(org.evosuite.coverage.method.MethodNoExceptionCoverageTestFitness) TestCase(org.evosuite.testcase.TestCase) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) CoverageGoalTestNameGenerationStrategy(org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy) ArrayList(java.util.ArrayList) DefaultTestCase(org.evosuite.testcase.DefaultTestCase) Test(org.junit.Test)

Aggregations

DefaultTestCase (org.evosuite.testcase.DefaultTestCase)347 Test (org.junit.Test)234 Constraint (org.evosuite.symbolic.expr.Constraint)101 TestCase (org.evosuite.testcase.TestCase)78 ArrayList (java.util.ArrayList)63 CoverageGoalTestNameGenerationStrategy (org.evosuite.junit.naming.methods.CoverageGoalTestNameGenerationStrategy)47 MethodCoverageTestFitness (org.evosuite.coverage.method.MethodCoverageTestFitness)45 IntPrimitiveStatement (org.evosuite.testcase.statements.numeric.IntPrimitiveStatement)41 VariableReference (org.evosuite.testcase.variable.VariableReference)37 TestCaseStringAppendString (com.examples.with.different.packagename.solver.TestCaseStringAppendString)27 TestCaseStringIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringIndexOfString)27 TestCaseStringLastIndexOfString (com.examples.with.different.packagename.solver.TestCaseStringLastIndexOfString)27 IntegerConstraint (org.evosuite.symbolic.expr.IntegerConstraint)27 OutputCoverageGoal (org.evosuite.coverage.io.output.OutputCoverageGoal)25 OutputCoverageTestFitness (org.evosuite.coverage.io.output.OutputCoverageTestFitness)25 TestSuiteChromosome (org.evosuite.testsuite.TestSuiteChromosome)18 GenericMethod (org.evosuite.utils.generic.GenericMethod)17 TestChromosome (org.evosuite.testcase.TestChromosome)16 Method (java.lang.reflect.Method)14 BranchCoverageSuiteFitness (org.evosuite.coverage.branch.BranchCoverageSuiteFitness)14