Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch submission for #14. #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions de.devboost.eclipse.junitloop/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
class="de.devboost.eclipse.junitloop.JUnitLoopTestRunListener">
</testRunListener>
</extension>
<extension
id="de.devboost.eclipse.junitloop.testFailure"
point="org.eclipse.core.resources.markers"
name="Test Failure">
<super type="org.eclipse.core.resources.problemmarker"/>
</extension>
<extension
point="org.eclipse.ui.viewActions">
<viewContribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.junit.TestRunListener;
import org.eclipse.jdt.junit.model.ITestCaseElement;
import org.eclipse.jdt.junit.model.ITestElement;
import org.eclipse.jdt.junit.model.ITestElement.FailureTrace;
import org.eclipse.jdt.junit.model.ITestElement.Result;
import org.eclipse.jdt.junit.model.ITestRunSession;

Expand All @@ -39,13 +43,26 @@
*/
public class JUnitLoopTestRunListener extends TestRunListener {

public static final String MARKER_TEST_FAILURE = "de.devboost.eclipse.junitloop.testFailure";

private IJavaProject launchedProject;

@Override
public void sessionLaunched(ITestRunSession session) {
super.sessionLaunched(session);
launchedProject = session.getLaunchedProject();
JUnitLoopPlugin.getDefault().notifySessionLaunched();

// Delete all test failure markers
try {
ResourcesPlugin
.getWorkspace()
.getRoot()
.deleteMarkers(MARKER_TEST_FAILURE, true,
IResource.DEPTH_INFINITE);
} catch (final CoreException e) {
JLoopPlugin.logError("Couldn't delete old warnings", e);
}
}

@Override
Expand Down Expand Up @@ -80,9 +97,11 @@ public void testCaseFinished(ITestCaseElement testCaseElement) {
return;
}

// Add warning to Problems view
if (testResult == ITestElement.Result.FAILURE ||
testResult == ITestElement.Result.ERROR) {
failedTests.add(testClass);
writeMarkers(testCaseElement);
} else if (testResult == ITestElement.Result.OK) {
succeededTests.add(testClass);
}
Expand All @@ -91,7 +110,42 @@ public void testCaseFinished(ITestCaseElement testCaseElement) {
scheduler.addFailedTests(failedTests);
scheduler.addSucceededTests(succeededTests);
}


private void writeMarkers(ITestCaseElement testCaseElement) {
final String testClassName = testCaseElement.getTestClassName();
final FailureTrace failureTrace = testCaseElement.getFailureTrace();
final String trace = failureTrace.getTrace();
String traceLine1 = null;
for (final String traceLine : trace.split("[\\n\\r]+")) {
if (traceLine1 == null) {
traceLine1 = traceLine;
}
if (traceLine.indexOf(testClassName) > 0) {
final String line = traceLine.substring(
traceLine.indexOf(':') + 1, traceLine.length() - 1);
try {
final IType testClassType = launchedProject
.findType(testClassName);
final IResource resource = testClassType.getResource();

final IMarker marker = resource
.createMarker(MARKER_TEST_FAILURE);
marker.setAttribute(IMarker.MESSAGE, traceLine1);
marker.setAttribute(IMarker.SEVERITY,
IMarker.SEVERITY_WARNING);
marker.setAttribute(IMarker.LINE_NUMBER,
Integer.parseInt(line));
marker.setAttribute(IMarker.LOCATION, "line " + line);
marker.setAttribute(IMarker.TRANSIENT, true);

break;
} catch (final Exception e) {
JLoopPlugin.logError("Couldn't write problem marker", e);
}
}
}
}

private TestClass createTestClass(String testClassName) {
// create local copy to avoid potential multi-threading problems
IJavaProject localProject = launchedProject;
Expand Down