use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.
the class ReadWriteSystemPropertiesSystemTest method testReadLineSeparator.
@Test
public void testReadLineSeparator() {
EvoSuite evosuite = new EvoSuite();
String targetClass = ReadTimezone.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.SANDBOX = true;
Properties.REPLACE_CALLS = true;
String[] command = new String[] { "-generateSuite", "-class", targetClass };
Object result = evosuite.parseCommandLine(command);
GeneticAlgorithm<?> ga = getGAFromResult(result);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
System.out.println("EvolvedTestSuite:\n" + best);
double cov = best.getCoverage();
Assert.assertEquals("Non-optimal coverage: ", 1d, cov, 0.001);
// now check the JUnit generation
List<TestCase> list = best.getTests();
int n = list.size();
Assert.assertTrue(n > 0);
// needed because it gets pulled down after the search
TestCaseExecutor.initExecutor();
try {
Sandbox.initializeSecurityManagerForSUT();
JUnitAnalyzer.removeTestsThatDoNotCompile(list);
} finally {
Sandbox.resetDefaultSecurityManager();
}
Assert.assertEquals(n, list.size());
TestGenerationResult tgr = TestGenerationResultBuilder.buildSuccessResult();
String code = tgr.getTestSuiteCode();
Assert.assertTrue("Test code:\n" + code, code.contains("user.timezone"));
/*
* This is tricky. The property 'debug' is read, but it does not exist.
* Ideally, we should still have in the test case a call to be sure the variable
* is set to null. But that would lead to a lot of problems :( eg cases
* in which we end up in reading hundreds of thousands variables that do not exist
*/
Assert.assertTrue("Test code:\n" + code, !code.contains("debug"));
}
use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.
the class TestGenerationJob method runEvoSuite.
protected ArrayList<TestGenerationResult> runEvoSuite(final IProgressMonitor monitor) {
monitor.beginTask("EvoSuite test suite generation", 100);
ArrayList<TestGenerationResult> tgrs = new ArrayList<TestGenerationResult>();
try {
List<String> commands = createCommand();
commands.addAll(getAdditionalParameters());
String[] command = new String[commands.size()];
commands.toArray(command);
System.out.println("* EvoSuite command: " + Arrays.asList(command));
setupRMI();
Thread progressMonitor = new Thread() {
@Override
public void run() {
int percent = 0;
int last = 0;
String subTask = "";
// try {
while (percent != -1 && !isInterrupted()) {
MasterNodeLocal masterNode = MasterServices.getInstance().getMasterNode();
if (masterNode != null) {
Collection<ClientStateInformation> currentStates = MasterServices.getInstance().getMasterNode().getCurrentStateInformation();
if (currentStates.size() == 1) {
ClientStateInformation currentState = currentStates.iterator().next();
lastState = currentState;
percent = currentState.getOverallProgress();
if (percent >= 100 && currentState.getState() == ClientState.NOT_STARTED)
continue;
String currentTask = currentState.getState().getDescription();
// + "%]";
if (percent > last || !subTask.equals(currentTask)) {
subTask = currentTask;
monitor.worked(percent - last);
monitor.subTask(subTask);
last = percent;
}
}
}
try {
// TODO - should use observer pattern
sleep(250);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.out.println("* Shut down progress monitor");
}
// } catch (Exception e) {
// System.err.println(this.getClass().getCanonicalName() + ": Exception while reading output of client process " + e);
// System.err.println(e.getStackTrace().toString());
// }
}
}
};
progressMonitor.start();
tgrs = launchProcess(command);
progressMonitor.interrupt();
try {
target.getProject().refreshLocal(IProject.DEPTH_INFINITE, null);
} catch (CoreException e) {
e.printStackTrace();
}
System.out.println("Job returned normally");
monitor.done();
/*
GenerationResult result = new GenerationResult(shell, SWT.DIALOG_TRIM
| SWT.APPLICATION_MODAL);
result.open();
return Status.OK_STATUS;
*/
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
return tgrs;
}
use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.
the class TestGenerationJob method run.
@Override
protected IStatus run(final IProgressMonitor monitor) {
// && System.getProperty("evosuite.disable").equals("1")
Boolean disabled = System.getProperty("evosuite.disable") != null;
if (disabled) {
System.out.println("TestGenerationJob: The EvoSuite plugin is disabled :(");
return Status.OK_STATUS;
}
final String suiteFileName = getSuiteFileName(suiteClass);
final IFile fileSuite = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(suiteFileName));
if (fileSuite != null && fileSuite.exists()) {
// Open test suite in editor
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
IWorkbenchWindow iw = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = iw.getActivePage();
try {
IDE.openEditor(page, fileSuite, true);
} catch (PartInitException e1) {
System.out.println("Could not open test suite");
e1.printStackTrace();
}
}
});
// Generated tests should be checked by tester?
Boolean checkMarkers = System.getProperty("evosuite.markers.enforce") != null;
if (checkMarkers) {
String fileContents = readFileToString(suiteFileName);
ASTParser parser = ASTParser.newParser(AST.JLS8);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setStatementsRecovery(true);
@SuppressWarnings("unchecked") Map<String, String> COMPILER_OPTIONS = new HashMap<String, String>(JavaCore.getOptions());
COMPILER_OPTIONS.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
COMPILER_OPTIONS.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
COMPILER_OPTIONS.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
parser.setUnitName(suiteClass);
String[] encodings = { ENCODING };
String[] classpaths = { classPath };
String[] sources = { new File(suiteFileName).getParent() };
parser.setEnvironment(classpaths, sources, encodings, true);
parser.setSource(fileContents.toCharArray());
CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
final List<String> uncheckedMethods = new ArrayList<String>();
compilationUnit.accept(new ASTVisitor() {
@Override
public boolean visit(MemberValuePair node) {
if (node.getName().toString().equals("checked") && !((BooleanLiteral) node.getValue()).booleanValue()) {
NormalAnnotation ann = (NormalAnnotation) node.getParent();
MethodDeclaration method = (MethodDeclaration) ann.getParent();
uncheckedMethods.add(method.getName().toString());
return false;
}
return true;
}
});
if (uncheckedMethods.size() > 0) {
Display.getDefault().syncExec(new Runnable() {
@Override
public void run() {
MessageDialog dialog = new MessageDialog(shell, "JUnit test suite contains unit tests that need to be checked", // image
null, "The JUnit test suite " + suiteClass + " contains test cases that need to be checked:\n" + uncheckedMethods.toString(), MessageDialog.OK, new String[] { "Ok" }, 0);
dialog.open();
}
});
return Status.OK_STATUS;
}
} else
System.out.println("Not checking markers.");
} else {
System.out.println("File " + suiteFileName + " does not exist");
// TODO: Dialog
// Display.getDefault().syncExec(new Runnable() {
// @Override
// public void run() {
// MessageDialog dialog = new MessageDialog(
// shell,
// "Error during test generation",
// null, // image
// "EvoSuite failed to generate tests for class"
// + suiteClass,
// MessageDialog.OK, new String[] { "Ok" }, 0);
// dialog.open();
// }
// });
// return Status.CANCEL_STATUS;
}
setThread(new Thread());
running = true;
clearMarkersTarget();
String oldTgr = getOldTestGenerationResult();
lastTest = oldTgr;
ArrayList<TestGenerationResult> results = runEvoSuite(monitor);
writeMarkersTarget(results);
// uncomment after experiment
if (writeAllMarkers)
writeMarkersTestSuite();
try {
target.getProject().refreshLocal(IProject.DEPTH_INFINITE, null);
// if ("true".equals(target.getProject().getPersistentProperty(
// EvoSuitePropertyPage.REPORT_PROP_KEY))) {
// syncWithUi(target);
// };
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
try {
final IFile generatedSuite = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(suiteFileName));
ICompilationUnit cu = JavaCore.createCompilationUnitFrom(generatedSuite);
IWorkbenchWindow iw = Activator.getDefault().getWorkbench().getActiveWorkbenchWindow();
IWorkbenchPage page = iw.getActivePage();
IEditorPart part = IDE.openEditor(page, generatedSuite, true);
if (Activator.organizeImports()) {
OrganizeImportsAction a = new OrganizeImportsAction(part.getSite());
a.run(cu);
cu.commitWorkingCopy(true, null);
cu.save(null, true);
}
} catch (PartInitException e1) {
System.out.println("Could not open test suite to organize imports");
e1.printStackTrace();
} catch (JavaModelException e) {
System.out.println("Something went wrong while saving test suite after organizing imports");
e.printStackTrace();
}
;
}
});
} catch (CoreException e) {
System.out.println("Dear me");
e.printStackTrace();
}
Activator.CURRENT_WRITING_FILE = null;
running = false;
monitor.done();
done(ASYNC_FINISH);
Activator.FILE_QUEUE.update();
return Status.OK_STATUS;
}
use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.
the class AmbiguityFitnessSystemTest method testNonZeroAmbiguityScore.
@Test
public void testNonZeroAmbiguityScore() {
EvoSuite evosuite = new EvoSuite();
String targetClass = IndirectlyCoverableBranches.class.getCanonicalName();
Properties.TARGET_CLASS = targetClass;
Properties.SEARCH_BUDGET = 35_000;
String[] command = new String[] { "-class", targetClass, "-generateSuite" };
List<List<TestGenerationResult>> result = (List<List<TestGenerationResult>>) evosuite.parseCommandLine(command);
assertNotNull(result);
List<?> goals = AmbiguityCoverageFactory.getGoals();
assertEquals(12, goals.size());
GeneticAlgorithm<?> ga = result.get(0).get(0).getGeneticAlgorithm();
assertNotNull(ga);
TestSuiteChromosome best = (TestSuiteChromosome) ga.getBestIndividual();
// goals of 'IndirectlyCoverableBranches': 22, 24, 25, 28, 29, 30, 31, 34, 35, 38, 39, 41
//
// minimum ambiguity:
// {22}, {24}, {25}, {28,29,30,31}, {34,35}, {38,39,41}
// {22}
double ambiguity = 0.0;
// {24}
ambiguity += 0.0;
// {25}
ambiguity += 0.0;
// {28,29,30,31}
ambiguity += (4.0 / ((double) goals.size())) * (3.0 / 2.0);
// {34,35}
ambiguity += (2.0 / ((double) goals.size())) * (1.0 / 2.0);
// {38,39,41}
ambiguity += (3.0 / ((double) goals.size())) * (2.0 / 2.0);
assertEquals(0.8333, ambiguity, 0.0001);
// assertEquals(ambiguity * 1.0 / ((double) goals.size()), best.getFitnessInstanceOf(AmbiguityCoverageSuiteFitness.class), 0.001);
assertEquals(FitnessFunction.normalize(ambiguity), best.getFitnessInstanceOf(AmbiguityCoverageSuiteFitness.class), 0.001);
}
use of org.evosuite.result.TestGenerationResult in project evosuite by EvoSuite.
the class TestGeneration method generateTests.
private static List<List<TestGenerationResult>> generateTests(Properties.Strategy strategy, String target, List<String> args) {
LoggingUtils.getEvoLogger().info("* Going to generate test cases for class: " + target);
if (!findTargetClass(target)) {
return Arrays.asList(Arrays.asList(new TestGenerationResult[] { TestGenerationResultBuilder.buildErrorResult("Could not find target class") }));
}
if (!BytecodeInstrumentation.checkIfCanInstrument(target)) {
throw new IllegalArgumentException("Cannot consider " + target + " because it belongs to one of the packages EvoSuite cannot currently handle");
}
List<String> cmdLine = new ArrayList<>();
cmdLine.add(JavaExecCmdUtil.getJavaBinExecutablePath(true));
handleClassPath(cmdLine);
if (Properties.SPAWN_PROCESS_MANAGER_PORT != null) {
cmdLine.add("-Dspawn_process_manager_port=" + Properties.SPAWN_PROCESS_MANAGER_PORT);
}
ExternalProcessHandler handler = new ExternalProcessHandler();
int port = handler.openServer();
if (port <= 0) {
throw new RuntimeException("Not possible to start RMI service");
}
cmdLine.add("-Dprocess_communication_port=" + port);
cmdLine.add("-Dinline=true");
if (Properties.HEADLESS_MODE == true) {
cmdLine.add("-Djava.awt.headless=true");
}
cmdLine.add("-Dlogback.configurationFile=" + LoggingUtils.getLogbackFileName());
cmdLine.add("-Dlog4j.configuration=SUT.log4j.properties");
/*
* FIXME: following 3 should be refactored, as not particularly clean.
* First 2 does not work for master, as logback is read
* before Properties is initialized
*/
if (Properties.LOG_LEVEL != null) {
cmdLine.add("-Dlog.level=" + Properties.LOG_LEVEL);
}
if (Properties.LOG_TARGET != null) {
cmdLine.add("-Dlog.target=" + Properties.LOG_TARGET);
}
String logDir = System.getProperty("evosuite.log.folder");
if (logDir != null) {
// this parameter is for example used in logback-ctg.xml
cmdLine.add(" -Devosuite.log.folder=" + logDir);
}
// ------------------------------------------------
cmdLine.add("-Djava.library.path=lib");
if (Properties.DEBUG) {
// enabling debugging mode to e.g. connect the eclipse remote debugger to the given port
cmdLine.add("-Ddebug=true");
cmdLine.add("-Xdebug");
cmdLine.add("-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=" + Properties.PORT);
LoggingUtils.getEvoLogger().info("* Waiting for remote debugger to connect on port " + Properties.PORT + // TODO find the right
"...");
// place for this
}
if (!Properties.PROFILE.isEmpty()) {
// enabling debugging mode to e.g. connect the eclipse remote debugger to the given port
File agentFile = new File(Properties.PROFILE);
if (!agentFile.exists()) {
LoggingUtils.getEvoLogger().info("* Error: " + Properties.PROFILE + " not found");
} else {
cmdLine.add("-agentpath:" + Properties.PROFILE);
LoggingUtils.getEvoLogger().info("* Using profiling agent " + Properties.PROFILE);
}
}
if (Properties.JMC) {
// FIXME: does not seem to work, at least on Mac. Looks like some RMI conflict
cmdLine.add("-XX:+UnlockCommercialFeatures");
cmdLine.add("-XX:+FlightRecorder");
cmdLine.add("-Dcom.sun.management.jmxremote");
cmdLine.add("-Dcom.sun.management.jmxremote.autodiscovery");
cmdLine.add("-Dcom.sun.management.jmxremote.authenticate=false");
cmdLine.add("-Dcom.sun.management.jmxremote.ssl=false");
}
cmdLine.add("-XX:MaxJavaStackTraceDepth=1000000");
cmdLine.add("-XX:+StartAttachListener");
for (String arg : args) {
if (!arg.startsWith("-DCP=")) {
cmdLine.add(arg);
}
}
switch(strategy) {
case EVOSUITE:
cmdLine.add("-Dstrategy=EvoSuite");
break;
case ONEBRANCH:
cmdLine.add("-Dstrategy=OneBranch");
break;
case RANDOM:
cmdLine.add("-Dstrategy=Random");
break;
case RANDOM_FIXED:
cmdLine.add("-Dstrategy=Random_Fixed");
break;
case REGRESSION:
cmdLine.add("-Dstrategy=Regression");
break;
case ENTBUG:
cmdLine.add("-Dstrategy=EntBug");
break;
case MOSUITE:
cmdLine.add("-Dstrategy=MOSuite");
break;
case DSE:
cmdLine.add("-Dstrategy=Dynamic_Symbolic_Execution");
break;
case NOVELTY:
cmdLine.add("-Dstrategy=Novelty");
break;
default:
throw new RuntimeException("Unsupported strategy: " + strategy);
}
cmdLine.add("-DTARGET_CLASS=" + target);
if (Properties.PROJECT_PREFIX != null) {
cmdLine.add("-DPROJECT_PREFIX=" + Properties.PROJECT_PREFIX);
}
cmdLine.add(ClientProcess.class.getName());
/*
* TODO: here we start the client with several properties that are set through -D. These properties are not visible to the master process (ie
* this process), when we access the Properties file. At the moment, we only need few parameters, so we can hack them
*/
// should force the load, just to be sure
Properties.getInstance();
Properties.TARGET_CLASS = target;
Properties.PROCESS_COMMUNICATION_PORT = port;
/*
* FIXME: refactor, and double-check if indeed correct
*
* The use of "assertions" in the client is pretty tricky, as those properties need to be transformed into JVM options before starting the
* client. Furthermore, the properties in the property file might be overwritten from the commands coming from shell
*/
String definedEAforClient = null;
String definedEAforSUT = null;
final String DISABLE_ASSERTIONS_EVO = "-da:" + PackageInfo.getEvoSuitePackage() + "...";
final String ENABLE_ASSERTIONS_EVO = "-ea:" + PackageInfo.getEvoSuitePackage() + "...";
final String DISABLE_ASSERTIONS_SUT = "-da:" + Properties.PROJECT_PREFIX + "...";
final String ENABLE_ASSERTIONS_SUT = "-ea:" + Properties.PROJECT_PREFIX + "...";
for (String s : cmdLine) {
// first check client
if (s.startsWith("-Denable_asserts_for_evosuite")) {
if (s.endsWith("false")) {
definedEAforClient = DISABLE_ASSERTIONS_EVO;
} else if (s.endsWith("true")) {
definedEAforClient = ENABLE_ASSERTIONS_EVO;
}
}
// then check SUT
if (s.startsWith("-Denable_asserts_for_sut")) {
if (s.endsWith("false")) {
definedEAforSUT = DISABLE_ASSERTIONS_SUT;
} else if (s.endsWith("true")) {
definedEAforSUT = ENABLE_ASSERTIONS_SUT;
}
}
}
if (definedEAforSUT == null) {
if (Properties.ENABLE_ASSERTS_FOR_SUT) {
definedEAforSUT = ENABLE_ASSERTIONS_SUT;
} else {
definedEAforSUT = DISABLE_ASSERTIONS_SUT;
}
}
if (definedEAforClient == null) {
if (Properties.ENABLE_ASSERTS_FOR_EVOSUITE) {
definedEAforClient = ENABLE_ASSERTIONS_EVO;
} else {
definedEAforClient = DISABLE_ASSERTIONS_EVO;
}
}
/*
* We add them in first position, after the java command To avoid confusion, we only add them if they are enabled. NOTE: this might have side
* effects "if" in the future we have something like a generic "-ea"
*/
if (definedEAforClient.equals(ENABLE_ASSERTIONS_EVO)) {
cmdLine.add(1, definedEAforClient);
}
if (definedEAforSUT.equals(ENABLE_ASSERTIONS_SUT)) {
cmdLine.add(1, definedEAforSUT);
}
LoggingUtils logUtils = new LoggingUtils();
if (!Properties.CLIENT_ON_THREAD) {
/*
* We want to completely mute the SUT. So, we block all outputs from client, and use a remote logging
*/
boolean logServerStarted = logUtils.startLogServer();
if (!logServerStarted) {
logger.error("Cannot start the log server");
return null;
}
//
int logPort = logUtils.getLogServerPort();
cmdLine.add(1, "-Dmaster_log_port=" + logPort);
cmdLine.add(1, "-Devosuite.log.appender=CLIENT");
}
String[] newArgs = cmdLine.toArray(new String[cmdLine.size()]);
for (String entry : ClassPathHandler.getInstance().getTargetProjectClasspath().split(File.pathSeparator)) {
try {
ClassPathHacker.addFile(entry);
} catch (IOException e) {
LoggingUtils.getEvoLogger().info("* Error while adding classpath entry: " + entry);
}
}
handler.setBaseDir(EvoSuite.base_dir_path);
if (handler.startProcess(newArgs)) {
Set<ClientNodeRemote> clients = null;
try {
// FIXME: timeout here should be handled by TimeController
clients = MasterServices.getInstance().getMasterNode().getClientsOnceAllConnected(60000);
} catch (InterruptedException e) {
}
if (clients == null) {
logger.error("Not possible to access to clients. Clients' state: " + handler.getProcessState() + ". Master registry port: " + MasterServices.getInstance().getRegistryPort());
} else {
/*
* The clients have started, and connected back to Master.
* So now we just need to tell them to start a search
*/
for (ClientNodeRemote client : clients) {
try {
client.startNewSearch();
} catch (RemoteException e) {
logger.error("Error in starting clients", e);
}
}
int time = TimeController.getInstance().calculateForHowLongClientWillRunInSeconds();
handler.waitForResult(time * 1000);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
if (Properties.CLIENT_ON_THREAD) {
handler.stopAndWaitForClientOnThread(10000);
}
handler.killProcess();
} else {
LoggingUtils.getEvoLogger().info("* Could not connect to client process");
}
boolean hasFailed = false;
if (Properties.NEW_STATISTICS) {
if (MasterServices.getInstance().getMasterNode() == null) {
logger.error("Cannot write results as RMI master node is not running");
hasFailed = true;
} else {
boolean written = SearchStatistics.getInstance().writeStatistics();
hasFailed = !written;
}
}
/*
* FIXME: it is unclear what is the relation between TestGenerationResult and writeStatistics()
*/
List<List<TestGenerationResult>> results = SearchStatistics.getInstance().getTestGenerationResults();
SearchStatistics.clearInstance();
handler.closeServer();
if (Properties.CLIENT_ON_THREAD) {
handler.stopAndWaitForClientOnThread(10000);
} else {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
logUtils.closeLogServer();
}
logger.debug("Master process has finished to wait for client");
// FIXME: tmp hack till understood what TestGenerationResult is...
if (hasFailed) {
logger.error("failed to write statistics data");
// note: cannot throw exception because would require refactoring of many SystemTests
return new ArrayList<List<TestGenerationResult>>();
}
return results;
}
Aggregations