junit sm runner: restore support for exceptions in before class methods
authorAnna.Kozlova <anna.kozlova@jetbrains.com>
Tue, 3 May 2016 18:43:32 +0000 (20:43 +0200)
committerAnna.Kozlova <anna.kozlova@jetbrains.com>
Tue, 3 May 2016 18:50:31 +0000 (20:50 +0200)
plugins/junit/test/com/intellij/execution/junit/JUnitTreeByDescriptionHierarchyTest.java
plugins/junit_rt/src/com/intellij/junit4/JUnit4TestListener.java

index 9e558b585a073350047871519965e159974891b8..d288aa4cb6ebe7bd0f7232a92bd2379643811f0c 100644 (file)
@@ -367,9 +367,7 @@ public class JUnitTreeByDescriptionHierarchyTest {
     sender.testRunStarted(testA);
     final Exception exception = new Exception();
     exception.setStackTrace(new StackTraceElement[0]);
-    sender.testStarted(testA);
     sender.testAssumptionFailure(new Failure(testA, exception));
-    sender.testFinished(testA);
     sender.testRunFinished(new Result());
 
     Assert.assertEquals("output: " + buf, "##teamcity[rootName name = 'root' location = 'java:suite://root']\n" +
@@ -381,8 +379,8 @@ public class JUnitTreeByDescriptionHierarchyTest {
                                           "\n" +
                                           "##teamcity[testFinished name='TestA.testName']\n" +
                                           "##teamcity[testSuiteFinished name='TestA']\n", StringUtil.convertLineSeparators(buf.toString()));
-  } 
-  
+  }
+
   @Test
   public void testSetupClassFailure() throws Exception {
     final Description root = Description.createSuiteDescription("root");
@@ -406,9 +404,7 @@ public class JUnitTreeByDescriptionHierarchyTest {
     sender.testRunStarted(testA);
     final Exception exception = new Exception();
     exception.setStackTrace(new StackTraceElement[0]);
-    sender.testStarted(testA);
     sender.testFailure(new Failure(testA, exception));
-    sender.testFinished(testA);
     sender.testRunFinished(new Result());
 
     Assert.assertEquals("output: " + buf, "##teamcity[rootName name = 'root' location = 'java:suite://root']\n" +
@@ -497,9 +493,7 @@ public class JUnitTreeByDescriptionHierarchyTest {
     exception.setStackTrace(new StackTraceElement[0]);
     sender.testStarted(testName);
     sender.testFinished(testName);
-    sender.testStarted(testA);
     sender.testFailure(new Failure(testA, exception));
-    sender.testFinished(testA);
     sender.testRunFinished(new Result());
 
     Assert.assertEquals("output: " + buf, "##teamcity[rootName name = 'root' location = 'java:suite://root']\n" +
@@ -515,7 +509,7 @@ public class JUnitTreeByDescriptionHierarchyTest {
                                           "##teamcity[testFinished name='Class Configuration']\n" +
                                           "##teamcity[testSuiteFinished name='TestA']\n", StringUtil.convertLineSeparators(buf.toString()));
   }
-  
+
   @Test
   public void testSetupClassFailureForParameterizedClass() throws Exception {
     final Description root = Description.createSuiteDescription("root");
@@ -537,15 +531,13 @@ public class JUnitTreeByDescriptionHierarchyTest {
                                           "##teamcity[suiteTreeEnded name='param']\n" +
                                           "##teamcity[suiteTreeEnded name='TestA']\n" +
                                           "##teamcity[treeEnded]\n", StringUtil.convertLineSeparators(buf.toString()));
-    
+
     buf.setLength(0);
 
     sender.testRunStarted(testA);
     final Exception exception = new Exception();
     exception.setStackTrace(new StackTraceElement[0]);
-    sender.testStarted(testA);
     sender.testAssumptionFailure(new Failure(testA, exception));
-    sender.testFinished(testA);
     sender.testRunFinished(new Result());
 
     Assert.assertEquals("output: " + buf, "##teamcity[rootName name = 'root' location = 'java:suite://root']\n" +
@@ -561,21 +553,14 @@ public class JUnitTreeByDescriptionHierarchyTest {
                                           "##teamcity[testSuiteFinished name='TestA']\n", StringUtil.convertLineSeparators(buf.toString()));
     buf.setLength(0);
 
+    //testStarted and testFinished are called by the framework
     sender.testRunStarted(testA);
-    sender.testStarted(testName);
     sender.testAssumptionFailure(new Failure(testName, exception));
-    sender.testFinished(testName);
     sender.testRunFinished(new Result());
 
     Assert.assertEquals("output: " + buf, "##teamcity[rootName name = 'root' location = 'java:suite://root']\n" +
-                                          "##teamcity[testSuiteStarted name='TestA']\n" +
                                           "\n" +
-                                          "##teamcity[testStarted name='TestA.testName' locationHint='java:test://TestA.testName']\n" +
-                                          "\n" +
-                                          "##teamcity[testIgnored name='TestA.testName' details='java.lang.Exception|n' error='true' message='']\n" +
-                                          "\n" +
-                                          "##teamcity[testFinished name='TestA.testName']\n" +
-                                          "##teamcity[testSuiteFinished name='TestA']\n", StringUtil.convertLineSeparators(buf.toString()));
+                                          "##teamcity[testIgnored name='TestA.testName' details='java.lang.Exception|n' error='true' message='']\n", StringUtil.convertLineSeparators(buf.toString()));
     
   }
 
index 4b121044e0baf39dffd79153a811cc068ee163bb..d5fdf6247d2a15f1e6db192940d5bb651c61ba12 100644 (file)
@@ -37,6 +37,7 @@ import java.util.*;
 public class JUnit4TestListener extends RunListener {
   public static final String EMPTY_SUITE_NAME = "junit.framework.TestSuite$1";
   public static final String EMPTY_SUITE_WARNING = "warning";
+  public static final String CLASS_CONFIGURATION = "Class Configuration";
 
   private List myStartedSuites = new ArrayList();
   private Map   myParents = new HashMap();
@@ -82,7 +83,7 @@ public class JUnit4TestListener extends RunListener {
   }
 
   public void testRunFinished(Result result) {
-    dumpQueue();
+    dumpQueue(true);
     for (int i = myStartedSuites.size() - 1; i>= 0; i--) {
       Object parent = JUnit4ReflectionUtil.getClassName((Description)myStartedSuites.get(i));
       myPrintStream.println("##teamcity[testSuiteFinished name=\'" + escapeName(getShortName((String)parent)) + "\']");
@@ -91,7 +92,12 @@ public class JUnit4TestListener extends RunListener {
   }
 
   public void testStarted(Description description) {
-    if (myCurrentTest != null) {
+    testStarted(description, null);
+  }
+
+  private void testStarted(Description description, String methodName) {
+    final List parents = (List)myParents.get(description);
+    if (myCurrentTest != null && (parents == null || parents.isEmpty() || !((List)parents.get(0)).contains(myCurrentTest))) {
       myWaitingQueue.put(description, new TestEvent());
       return;
     }
@@ -100,13 +106,15 @@ public class JUnit4TestListener extends RunListener {
 
     final String classFQN = JUnit4ReflectionUtil.getClassName(description);
 
-    final List parents = (List)myParents.get(description);
+
     List parentsHierarchy = parents != null && !parents.isEmpty() ? (List)parents.remove(0) 
                                                                   : Collections.singletonList(Description.createSuiteDescription(classFQN, new Annotation[0]));
-    
-    final String methodName = getFullMethodName(description, parentsHierarchy.isEmpty() ? null 
-                                                                                        : (Description)parentsHierarchy.get(parentsHierarchy.size() - 1));
-    if (methodName == null) return;
+
+    if (methodName == null) {
+      methodName = getFullMethodName(description, parentsHierarchy.isEmpty() ? null
+                                                                             : (Description)parentsHierarchy.get(parentsHierarchy.size() - 1));
+      if (methodName == null) return;
+    }
 
     int idx = 0;
     Description currentClass;
@@ -166,9 +174,9 @@ public class JUnit4TestListener extends RunListener {
       testEvent.setFinished(true);
       return;
     }
-    testFinishedNoDumping(description);
+    testFinishedNoDumping(getFullMethodName(description));
 
-    dumpQueue();
+    dumpQueue(false);
   }
 
   /**
@@ -182,11 +190,10 @@ public class JUnit4TestListener extends RunListener {
    * if in the previous example test2 finishes before test1, then myCurrentTest != description, testEvent in myWaitingQueue needs to be updated
    */
   private boolean startedInParallel(Description description) {
-    return myCurrentTest == null || !myCurrentTest.equals(description);
+    return myWaitingQueue.containsKey(description) && (myCurrentTest == null || !myCurrentTest.equals(description));
   }
 
-  private void testFinishedNoDumping(Description description) {
-    final String methodName = getFullMethodName(description);
+  private void testFinishedNoDumping(final String methodName) {
     if (methodName != null) {
       myFinishedCount++;
       final long duration = currentTime() - myCurrentTestStart;
@@ -202,23 +209,12 @@ public class JUnit4TestListener extends RunListener {
 
   private void testFailure(Failure failure, Description description, String messageName) {
     final boolean isIgnored = MapSerializerUtil.TEST_IGNORED.equals(messageName);
-    if (startedInParallel(description)) {
-      TestEvent testEvent = (TestEvent)myWaitingQueue.get(description);
-      if (testEvent == null) {
-        testEvent = new TestEvent();
-        myWaitingQueue.put(description, testEvent);
-      }
-      testEvent.setIgnored(isIgnored);
-      testEvent.setFailure(failure);
-      return;
-    }
     String methodName = getFullMethodName(description);
     if (methodName == null) { //class setUp/tearDown failed
       if (!isIgnored) {
-        methodName = "Class Configuration";
-        myPrintStream.println("##teamcity[testStarted name=\'" + escapeName(methodName) + "\' " + getClassLocation(JUnit4ReflectionUtil.getClassName(description))+ " ]");
-        testFailure(failure, messageName, methodName);
-        myPrintStream.println("\n##teamcity[testFinished name=\'" + escapeName(methodName) + "\']");
+        classConfigurationStarted(description);
+        testFailure(failure, description, messageName, CLASS_CONFIGURATION);
+        classConfigurationFinished(description);
       }
 
       if (myFinishedCount == 0) {
@@ -232,11 +228,46 @@ public class JUnit4TestListener extends RunListener {
       }
     }
     else {
-      testFailure(failure, messageName, methodName);
+      testFailure(failure, description, messageName, methodName);
+    }
+  }
+
+  private void classConfigurationFinished(Description description) {
+    if (startedInParallel(description)) {
+      TestEvent testEvent = (TestEvent)myWaitingQueue.get(description);
+      testEvent.setFinished(true);
+      return;
+    }
+
+    myPrintStream.println("\n##teamcity[testFinished name=\'" + escapeName(CLASS_CONFIGURATION) + "\']");
+    myCurrentTest = null;
+  }
+
+  private void classConfigurationStarted(Description description) {
+    if (myCurrentTest != null) {
+      TestEvent value = new TestEvent();
+      value.setMethodName(CLASS_CONFIGURATION);
+      myWaitingQueue.put(description, value);
+      return;
     }
+
+    myCurrentTest = description;
+    myPrintStream.println("##teamcity[testStarted name=\'" + escapeName(CLASS_CONFIGURATION) + "\' " + getClassLocation(JUnit4ReflectionUtil.getClassName(description)) + " ]");
   }
 
-  private void testFailure(Failure failure, String messageName, String methodName) {
+  private void testFailure(Failure failure, Description description, String messageName, String methodName) {
+    final boolean isIgnored = MapSerializerUtil.TEST_IGNORED.equals(messageName);
+    if (startedInParallel(description)) {
+      TestEvent testEvent = (TestEvent)myWaitingQueue.get(description);
+      if (testEvent == null) {
+        testEvent = new TestEvent();
+        myWaitingQueue.put(description, testEvent);
+      }
+      testEvent.setIgnored(isIgnored);
+      testEvent.setFailure(failure);
+      return;
+    }
+
     final Map attrs = new HashMap();
     attrs.put("name", methodName);
     final long duration = currentTime() - myCurrentTestStart;
@@ -336,14 +367,14 @@ public class JUnit4TestListener extends RunListener {
     testFinished(description);
   }
 
-  private void dumpQueue() {
+  private void dumpQueue(boolean acceptUnfinished) {
     for (Iterator iterator = myWaitingQueue.keySet().iterator(); iterator.hasNext(); ) {
       Description description = (Description)iterator.next();
       TestEvent testEvent = (TestEvent)myWaitingQueue.get(description);
-      if (testEvent != null && testEvent.isFinished()) {
+      if (acceptUnfinished || testEvent.isFinished()) {
         iterator.remove();
 
-        testStarted(description);
+        testStarted(description, testEvent.getMethodName());
 
         Failure failure = testEvent.getFailure();
         if (testEvent.isIgnored()) {
@@ -359,7 +390,8 @@ public class JUnit4TestListener extends RunListener {
           testFailure(failure);
         }
 
-        testFinishedNoDumping(description);
+        final String methodName = testEvent.getMethodName();
+        testFinishedNoDumping(methodName != null ? methodName : getFullMethodName(description));
       }
     }
   }
@@ -369,6 +401,7 @@ public class JUnit4TestListener extends RunListener {
     private boolean myIgnored;
     private boolean myFinished;
     private Map myAttrs;
+    private String myMethodName;
 
     public Failure getFailure() {
       return myFailure;
@@ -401,6 +434,14 @@ public class JUnit4TestListener extends RunListener {
     public Map getAttrs() {
       return myAttrs;
     }
+
+    public void setMethodName(String methodName) {
+      myMethodName = methodName;
+    }
+
+    public String getMethodName() {
+      return myMethodName;
+    }
   }
 
   private void sendTree(Description description, Description parent, List currentParents) {
@@ -413,16 +454,17 @@ public class JUnit4TestListener extends RunListener {
       }
     }
 
+    List parents = (List)myParents.get(description);
+    if (parents == null) {
+      parents = new ArrayList(1);
+      myParents.put(description, parents);
+    }
+    parents.add(pParents);
+
     String className = JUnit4ReflectionUtil.getClassName(description);
-    if (description.getChildren().isEmpty()) {
+    if (description.isTest()) {
       final String methodName = getFullMethodName((Description)description, parent);
       if (methodName != null && parent != null) {
-        List parents = (List)myParents.get(description);
-        if (parents == null) {
-          parents = new ArrayList(1);
-          myParents.put(description, parents);
-        }
-        parents.add(pParents);
 
         if (isWarning(methodName, className)) {
           className = JUnit4ReflectionUtil.getClassName(parent);