junit: proceed tests without initial tree without identity check as it won't pass...
authorAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 26 Aug 2015 10:40:26 +0000 (13:40 +0300)
committerAnna Kozlova <anna.kozlova@jetbrains.com>
Wed, 26 Aug 2015 10:53:48 +0000 (13:53 +0300)
plugins/junit/test/com/intellij/execution/junit/JUnitTreeByDescriptionHierarchyTest.java
plugins/junit_rt/src/com/intellij/junit4/JUnit4TestListener.java

index 275897f839af4d7ecbd31da0667ba9084aa9b617..cbf3d4fe8067a900630a5175411ac9585ef58a23 100644 (file)
@@ -93,6 +93,37 @@ public class JUnitTreeByDescriptionHierarchyTest {
            "##teamcity[testSuiteFinished name='TestB']\n");
   }
 
+  @Test
+  public void testClassWithMethodsWithoutSendTreeBefore() throws Exception {
+    Description root = Description.createSuiteDescription("ATest");
+    List<Description> tests = new ArrayList<Description>();
+    tests.add(Description.createTestDescription("ATest", "test1"));
+    tests.add(Description.createTestDescription("ATest", "test2"));
+
+    for (Description test : tests) {
+      root.addChild(test);
+    }
+
+    final StringBuffer buf = new StringBuffer();
+    JUnit4TestListener sender = createListener(buf);
+    
+    sender.testRunStarted(root);
+    for (Description test : tests) {
+      sender.testStarted(test);
+      sender.testFinished(test);
+    }
+    sender.testRunFinished(new Result());
+    Assert.assertEquals("output: " + buf, "##teamcity[enteredTheMatrix]\n" +
+                                          "##teamcity[testSuiteStarted name='ATest' locationHint='java:suite://ATest']\n" +
+                                          "##teamcity[testStarted name='ATest.test1' locationHint='java:test://ATest.test1']\n" +
+                                          "\n" +
+                                          "##teamcity[testFinished name='ATest.test1']\n" +
+                                          "##teamcity[testStarted name='ATest.test2' locationHint='java:test://ATest.test2']\n" +
+                                          "\n" +
+                                          "##teamcity[testFinished name='ATest.test2']\n" +
+                                          "##teamcity[testSuiteFinished name='ATest']\n", StringUtil.convertLineSeparators(buf.toString()));
+  }
+
   @Test
   public void testSameShortNames() throws Exception {
     final Description rootDescription = Description.createSuiteDescription("root");
index df62691bf7f47c76c358ab44e04efab336fd205d..d757df8370be1d9119d54085b683f52cf1a979b5 100644 (file)
@@ -32,6 +32,7 @@ import org.junit.runner.notification.RunListener;
 import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.lang.annotation.Annotation;
 import java.util.*;
 
 public class JUnit4TestListener extends RunListener {
@@ -91,7 +92,8 @@ 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);
+    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));
@@ -103,7 +105,9 @@ public class JUnit4TestListener extends RunListener {
     while (idx < myStartedSuites.size() && idx < parentsHierarchy.size()) {
       currentClass = (Description)myStartedSuites.get(idx);
       currentParent = (Description)parentsHierarchy.get(parentsHierarchy.size() - 1 - idx);
-      if (System.identityHashCode(currentClass) != System.identityHashCode(currentParent)) break;
+      if (isHierarchyDifferent(parents, currentClass, currentParent)) {
+        break;
+      }
       idx++;
     }
 
@@ -127,6 +131,17 @@ public class JUnit4TestListener extends RunListener {
     myCurrentTestStart = currentTime();
   }
 
+  private static boolean isHierarchyDifferent(List parents, 
+                                              Description currentClass, 
+                                              Description currentParent) {
+    if (parents == null) {
+      return !currentClass.equals(currentParent);
+    }
+    else {
+      return System.identityHashCode(currentClass) != System.identityHashCode(currentParent);
+    }
+  }
+
   protected long currentTime() {
     return System.currentTimeMillis();
   }