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