package test.dependent.xml;

import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import test.SimpleBaseTest;

import java.util.Arrays;

public class GroupDependencyTest extends SimpleBaseTest {
  @DataProvider
  public Object[][] dp() {
    return new Object[][] {
      new Object[] { new String[] { "a", "b", "c", "a1", "b1", "c1" } },
      new Object[] { new String[] { "a", "c", "b", "a1", "c1", "b1" } },
      new Object[] { new String[] { "b", "a", "c", "b1", "a1", "c1" } },
      new Object[] { new String[] { "b", "c", "a", "b1", "c1", "a1" } },
      new Object[] { new String[] { "c", "b", "a", "c1", "b1", "a1" } },
      new Object[] { new String[] { "c", "a", "b", "c1", "a1", "b1" } },
    };
  }

  @Test(dataProvider = "dp")
  public void verifyGroup(String[] a) {
    configureGroup(a);
  }

  private void configureGroup(String[] a) {
    XmlSuite suite = createXmlSuite("Dependencies");
    XmlTest test =
        createXmlTest(suite, "DependencyTest", GroupDependencySampleTest.class.getName());
    test.addXmlDependencyGroup(a[2], a[1]);
    test.addXmlDependencyGroup(a[1], a[0]);
    TestNG tng = create();
    tng.setXmlSuites(Arrays.asList(suite));
    TestListenerAdapter tla = new TestListenerAdapter();
    tng.addListener(tla);
    tng.run();

    assertTestResultsEqual(tla.getPassedTests(), Arrays.asList(a[3], a[4], a[5]));
  }
}

@@ -22,16 +22,73 @@
 t.xml;%0A%0A
+import org.testng.Assert;%0Aimport org.testng.ITestResult;%0A
 import o
@@ -341,16 +341,39 @@
 .Arrays;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -995,24 +995,30 @@
  verifyGroup
+Single
 (String%5B%5D a)
@@ -1040,16 +1040,156 @@
 eGroup(a
+, false /* single */);%0A  %7D%0A%0A  @Test(dataProvider = %22dp%22)%0A  public void verifyGroupMulti(String%5B%5D a) %7B%0A    configureGroup(a, true /* multi */
 );%0A  %7D%0A%0A
@@ -1220,32 +1220,47 @@
 Group(String%5B%5D a
+, boolean multi
 ) %7B%0A    XmlSuite
@@ -1406,24 +1406,43 @@
 getName());%0A
+    if (multi) %7B%0A  
     test.add
@@ -1470,19 +1470,93 @@
 2%5D, a%5B1%5D
-);%0A
+ + %22 %22 + a%5B0%5D);%0A    %7D else %7B%0A      test.addXmlDependencyGroup(a%5B2%5D, a%5B1%5D);%0A  
     test
@@ -1587,24 +1587,31 @@
 %5B1%5D, a%5B0%5D);%0A
+    %7D%0A%0A
     TestNG t
@@ -1768,16 +1768,307 @@
 run();%0A%0A
+    List%3CITestResult%3E t = tla.getPassedTests();%0A    String method2 = t.get(2).getMethod().getMethodName();%0A    if (multi) %7B%0A      // When we have %22a depends on groups b and c%22, the only certainty is that %22a%22%0A      // will be run last%0A      Assert.assertEquals(method2, a%5B5%5D);%0A    %7D else %7B%0A  
     asse
@@ -2142,14 +2142,20 @@
 a%5B5%5D));%0A
+    %7D%0A
   %7D%0A%7D%0A

Test multiple dependencies too.