Skip to content

Commit

Permalink
Listener: handle errors outside of funtion calls
Browse files Browse the repository at this point in the history
  • Loading branch information
KBorm committed Feb 25, 2020
1 parent 5ff8fd1 commit f5782fb
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 6 deletions.
Binary file modified extra/Junit4RunListener.jar
Binary file not shown.
Binary file modified extra/Junit5RunListener.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion src/VERSION.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# -*- coding: utf-8 -*-

version = "Version 4.3.1 | 20200225"
version = "Version 4.3.2 | 20200225"
8 changes: 4 additions & 4 deletions src/checker/scripts/junit.policy
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
grant {
// JUnit5
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";

permission java.util.PropertyPermission "user.home","read";
permission java.io.FilePermission "/home/praktomat/junit.properties", "read";
permission java.io.FilePermission "/home/tester/junit.properties", "read";
Expand All @@ -17,4 +13,8 @@ grant {
permission java.io.FilePermission "save.csv", "write";
permission java.io.FilePermission "spielDieZombie.txt", "write";
permission java.io.FilePermission "spielDieZombie2.txt", "write";

// JUnit5...
permission java.lang.reflect.ReflectPermission "suppressAccessChecks";
permission java.lang.RuntimePermission "accessClassInPackage.sun.reflect";
};
21 changes: 21 additions & 0 deletions src/proforma/Java/Junit4ProFormAListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
String value();
}


public class Junit4ProFormAListener extends RunListener {


Expand All @@ -57,6 +58,8 @@ public class Junit4ProFormAListener extends RunListener {
Element feedbackList;
Element studentFeedback;

private boolean failureOutsideTest = false;


public Junit4ProFormAListener() {
writer = System.out;
Expand Down Expand Up @@ -117,6 +120,12 @@ public void testRunFinished(Result result) {
e1.printStackTrace();
}


if (this.failureOutsideTest) {
// no xml creation
return;
}


// Transform Document to XML String
TransformerFactory tf = TransformerFactory.newInstance();
Expand Down Expand Up @@ -316,6 +325,7 @@ public void testFailure(Failure failure) {
//Throwable cause1 = exception.getCause();

String exceptionText = exception.toString(); // name of exception


boolean showStackTraceToStudent = true;
StackTraceElement[] strippedStacktrace = this.stripStackTrace(failure.getException().getStackTrace());
Expand All @@ -325,6 +335,14 @@ public void testFailure(Failure failure) {
}
stackTraceString = stackTraceString + "[...]\n";

if (studentFeedback == null) {
this.failureOutsideTest = true;
writer.println(failure);
writer.println("");
writer.println(stackTraceString);
return;
}

if (strippedStacktrace.length > 0) {
if (strippedStacktrace[0].getClassName().startsWith("org.junit.")) {
// Function Error in Test Code
Expand Down Expand Up @@ -489,6 +507,9 @@ public static void main(String[] args) {
System.exit(1);
}

if (listener.failureOutsideTest)
System.exit(1);

System.exit(0);
}
}
24 changes: 23 additions & 1 deletion src/proforma/Java/Junit5ProFormAListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class Junit5ProFormAListener implements TestExecutionListener {

private Exception exception = null;

private boolean failureOutsideTest = false;


public void executionStarted(TestIdentifier testIdentifier) {
if (testIdentifier.getType() == Type.CONTAINER)
Expand Down Expand Up @@ -110,8 +112,12 @@ public void executionStarted(TestIdentifier testIdentifier) {


public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) {
if (testIdentifier.getType() == Type.CONTAINER)
if (testIdentifier.getType() == Type.CONTAINER) {
if (studentFeedback == null) {
testFailure(testExecutionResult);
}
return;
}
// writer.append("executionFinished: " + testIdentifier.getDisplayName()+ ": " + testExecutionResult + "\n");
// todo: bei failed noch den Fehlertext

Expand Down Expand Up @@ -195,6 +201,11 @@ public void testPlanExecutionFinished(TestPlan testPlan){
e1.printStackTrace();
}


if (this.failureOutsideTest) {
// no xml creation
return;
}

// Transform Document to XML String
TransformerFactory tf = TransformerFactory.newInstance();
Expand Down Expand Up @@ -267,6 +278,14 @@ private void testFailure(TestExecutionResult testExecutionResult) {
}
stackTraceString = stackTraceString + "[...]\n";

if (studentFeedback == null) {
this.failureOutsideTest = true;
writer.println(exceptionText);
writer.println("");
writer.println(stackTraceString);
return;
}

if (strippedStacktrace.length > 0) {
if (strippedStacktrace[0].getClassName().startsWith("org.junit.")) {
// Function Error in Test Code
Expand Down Expand Up @@ -390,6 +409,8 @@ public static void main(String[] args) {
System.err.println(listener.exception.getMessage());
System.exit(1);
}
if (listener.failureOutsideTest)
System.exit(1);
} catch (Exception e) {
// reset redirection
System.setOut(originalOut);
Expand All @@ -399,6 +420,7 @@ public static void main(String[] args) {
System.exit(1);
}


System.exit(0);
}

Expand Down

0 comments on commit f5782fb

Please sign in to comment.