Skip to content

Commit

Permalink
Restructered some tests and added test for summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Kutscha committed Jul 16, 2021
1 parent ddc8806 commit aee9697
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,15 @@ public synchronized void update(File result) {
browser.setUrl(homeUrlString);
}
}

@Override
public void dispose() {
// reset homeUrlString for tests in
// org.pitest.pitclipse.ui.tests.PitclipsePitSummaryViewTest
homeUrlString = null;
if (browser != null && !browser.isDisposed()) {
browser.dispose();
}
super.dispose();
}
}
12 changes: 6 additions & 6 deletions tests/org.pitest.pitclipse.ui.tests/fragment.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<fragment>
<extension
point="org.pitest.pitclipse.ui.results">
<update
class="org.pitest.pitclipse.ui.swtbot.PitResultNotifier">
</update>
</extension>
<extension
point="org.pitest.pitclipse.core.executePit">
<pit_options
class="org.pitest.pitclipse.ui.swtbot.PitOptionsTestNotifier">
</pit_options>
</extension>
<extension
point="org.pitest.pitclipse.core.results">
<results
class="org.pitest.pitclipse.ui.swtbot.PitResultNotifier">
</results>
</extension>
</fragment>
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,21 @@

import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.pitest.pitclipse.ui.behaviours.StepException;
import org.pitest.pitclipse.ui.swtbot.PitNotifier;
import org.pitest.pitclipse.ui.swtbot.PitResultsView;
import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable;
import org.eclipse.swtbot.swt.finder.results.VoidResult;
import static org.pitest.pitclipse.ui.view.PitView.BACK_BUTTON_TEXT;
import static org.pitest.pitclipse.ui.view.PitView.FORWARD_BUTTON_TEXT;
import static org.pitest.pitclipse.ui.view.PitView.HOME_BUTTON_TEXT;

public class PitSummaryView {
private static String SUMMARY_VIEW_TITLE = "PIT Summary";
private PitResultsView lastResults = null;
private SWTWorkbenchBot bot;
private SWTBotView summaryView;

public PitSummaryView(SWTWorkbenchBot bot) {
this.bot = bot;
}

public void waitForUpdate() {
try {
lastResults = PitNotifier.INSTANCE.getResults();
} catch (InterruptedException e) {
throw new StepException(e);
}
}

public int getClassesTested() {
return lastResults.getClassesTested();
}

public double getOverallCoverage() {
return lastResults.getTotalCoverage();
}

public double getMutationCoverage() {
return lastResults.getMutationCoverage();
}

public void showViewIfNotOpen() {
if (summaryView == null) {
summaryView = bot.viewByTitle(SUMMARY_VIEW_TITLE);
Expand All @@ -73,7 +51,6 @@ public String getCurrentBrowserUrl() {
}

public String clickBack() {

clickButtonWithText(BACK_BUTTON_TEXT);
summaryView.bot().browser().waitForPageLoaded();
return getCurrentBrowserUrl();
Expand All @@ -91,9 +68,23 @@ public String clickForward() {
return getCurrentBrowserUrl();
}

/**
* Sets the link in the browser via javaScript, because bot can't click links in
* browser
* @param hyperLinkText link which to set as url, can be relative
* @return new url of browser
*/
public String setLink(String hyperLinkText) {
showViewIfNotOpen();
summaryView.bot().browser().setUrl(hyperLinkText);
// use this instead of the SWTBrowser.execute(), because we need syncExec
final String link = "window.location.href = '" + hyperLinkText + "'";
summaryView.bot().browser().waitForPageLoaded();
UIThreadRunnable.syncExec(new VoidResult() {
@Override
public void run() {
summaryView.bot().browser().widget.execute(link);
}
});
summaryView.bot().browser().waitForPageLoaded();
return getCurrentBrowserUrl();
}
Expand All @@ -102,8 +93,8 @@ public String setLink(String hyperLinkText) {
* Closes the view, if it is opened.
*/
public void closeView() {
showViewIfNotOpen();
if (summaryView != null) {
showViewIfNotOpen();
summaryView.close();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public void runJUnit() {
}

public void runPit() {
// focus package explorer to ensure the menu is found
bot.viewByTitle("Package Explorer").setFocus();
SWTBotMenuHelper menuHelper = new SWTBotMenuHelper();
SWTBotMenu runAsMenu = bot.menu(RUN)
.menu(RUN_AS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
import org.pitest.pitclipse.runner.PitOptions;
import org.pitest.pitclipse.runner.results.DetectionStatus;
import org.pitest.pitclipse.ui.behaviours.pageobjects.PackageContext;
import org.pitest.pitclipse.ui.behaviours.pageobjects.PitSummaryView;
import org.pitest.pitclipse.ui.swtbot.PitNotifier;
import org.pitest.pitclipse.ui.swtbot.PitResultNotifier.PitSummary;

import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
Expand All @@ -60,24 +59,24 @@
public class PitclipseSteps {

@When("test {word} in package {word} is run for project {word}")
public void runTest(final String testClassName, final String packageName, final String projectName) throws CoreException {
public void runTest(final String testClassName, final String packageName, final String projectName)
throws CoreException {
// No need to do a full build: we should be now synchronized with building
// Build the whole workspace to prevent random compilation failures
// ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor());
System.out.println
(String.format("Run PIT on: %s %s.%s",
projectName, packageName, testClassName));
// ResourcesPlugin.getWorkspace().build(IncrementalProjectBuilder.FULL_BUILD,
// new NullProgressMonitor());
System.out.println(String.format("Run PIT on: %s %s.%s", projectName, packageName, testClassName));
runPit(new SelectTestClass(testClassName, packageName, projectName));
}

@Then("a coverage report is generated with {int} class/classes tested with overall coverage of {int}% and mutation coverage of {int}%")
public void coverageReportGenerated(int classes, double totalCoverage, double mutationCoverage) {
PitSummaryView pitView = PAGES.getPitSummaryView();
pitView.waitForUpdate();
PAGES.views().waitForTestsAreRunOnConsole();
try {
assertEquals("Number of tested classes mismatch", classes, pitView.getClassesTested());
assertDoubleEquals("Total coverage mismatch", totalCoverage, pitView.getOverallCoverage());
assertDoubleEquals("Mutation coverage mismatch", mutationCoverage, pitView.getMutationCoverage());
assertEquals("Number of tested classes mismatch", classes, PitSummary.INSTANCE.getClasses());
assertDoubleEquals("Total coverage mismatch", totalCoverage, PitSummary.INSTANCE.getCodeCoverage());
assertDoubleEquals("Mutation coverage mismatch", mutationCoverage,
PitSummary.INSTANCE.getMutationCoverage());
} catch (Error e) {
e.printStackTrace();
throw e;
Expand Down Expand Up @@ -161,8 +160,8 @@ private void runPit(Runnable runnable) {
// make sure to clear the console to avoid interferences
// with the output of previous runs
PAGES.views().clearConsole();
// make sure notifications not read are cleared
PitNotifier.INSTANCE.reset();
// make sure summary is cleared
PitSummary.INSTANCE.reset();
int retryCount = 20;
int counter = 0;
while (counter < retryCount) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,66 @@

package org.pitest.pitclipse.ui.swtbot;

import java.io.IOException;

import org.pitest.pitclipse.core.extension.point.ResultNotifier;
import org.pitest.pitclipse.ui.extension.point.PitUiUpdate;
import org.pitest.pitclipse.runner.PitResults;
import org.pitest.pitclipse.ui.swtbot.ResultsParser.Summary;

public class PitResultNotifier implements ResultNotifier<PitUiUpdate> {
@Override
public void handleResults(PitUiUpdate updateEvent) {
notifiyTestsOfHtmlResults(updateEvent);
}
public class PitResultNotifier implements ResultNotifier<PitResults> {
public enum PitSummary {
INSTANCE;

private void notifiyTestsOfHtmlResults(PitUiUpdate updateEvent) {
PitResultsView view = buildResultsView(updateEvent);
tryNotifyResults(view);
}
private Summary summary;

/**
* @return covered classes or 0 if no summary is present
*/
public int getClasses() {
if (summary != null) {
return summary.getClasses();
}
return 0;
}

/**
* @return code coverage or 100 if no summary is present
*/
public double getCodeCoverage() {
if (summary != null) {
return summary.getCodeCoverage();
}
return 100;
}

private PitResultsView buildResultsView(PitUiUpdate results) {
ResultsParser parser = new ResultsParser(results.getHtml());
Summary summary = parser.getSummary();
PitResultsView view = PitResultsView.builder().withClassesTested(summary.getClasses())
.withTotalCoverage(summary.getCodeCoverage()).withMutationCoverage(summary.getMutationCoverage())
.build();
return view;
/**
* @return mutation coverage or 100 if no summary is present
*/
public double getMutationCoverage() {
if (summary != null) {
return summary.getMutationCoverage();
}
return 100;
}

public void reset() {
summary = null;
}

void setSummary(Summary summary) {
this.summary = summary;
}
}

private void tryNotifyResults(PitResultsView view) {
@Override
public void handleResults(PitResults results) {
try {
PitNotifier.INSTANCE.notifyResults(view);
} catch (InterruptedException e) {
throw new RuntimeException(e);
// file only exists, if mutations were done
if (results.getHtmlResultFile() != null) {
PitSummary.INSTANCE.setSummary(new ResultsParser(results.getHtmlResultFile()).getSummary());
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
import static java.lang.Double.parseDouble;
import static java.lang.Integer.parseInt;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class ResultsParser {

public static final class Summary {
Expand Down Expand Up @@ -56,8 +61,8 @@ public double getMutationCoverage() {

private final String html;

public ResultsParser(String html) {
this.html = html;
public ResultsParser(File file) throws IOException {
this.html = new String(Files.readAllBytes(Paths.get(file.getAbsolutePath())));
}

private String getProjectSummary() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ protected static void runTest(final String testClassName, final String packageNa
}

protected static void runPackageTest(final String packageName, final String projectName) throws CoreException {
// package explorer needs focus, otherwise will not run
bot.viewByTitle("Package Explorer").setFocus();
new PitclipseSteps().runPackageTest(packageName, projectName);
}

Expand Down
Loading

0 comments on commit aee9697

Please sign in to comment.