package test.sanitycheck;

import org.testng.Assert;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.TestNGException;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import test.SimpleBaseTest;

import java.util.Arrays;

public class CheckSuiteNamesTest extends SimpleBaseTest {

  /**
   * Child suites have different names
   */
  @Test
  public void checkChildSuites() {
    TestListenerAdapter tla = new TestListenerAdapter();
    TestNG tng = create();
    String testngXmlPath = getPathToResource("sanitycheck/test-s-b.xml");
    tng.setTestSuites(Arrays.asList(testngXmlPath));
    tng.addListener(tla);
    tng.run();
    Assert.assertEquals(tla.getPassedTests().size(), 4);
  }

  /**
   * Child suites have same names
   */
  @Test(expectedExceptions = TestNGException.class, expectedExceptionsMessageRegExp = "\\s*Two suites cannot have the same name.*")
  public void checkChildSuitesFails() {
    TestNG tng = create();
    String testngXmlPath = getPathToResource("sanitycheck/test-s-a.xml");
    tng.setTestSuites(Arrays.asList(testngXmlPath));
    tng.run();
  }

  /**
   * Checks that suites created programmatically also fails as expected
   */
  @Test(expectedExceptions = TestNGException.class, expectedExceptionsMessageRegExp = "\\s*Two suites cannot have the same name.*")
  public void checkProgrammaticSuitesFails() {
    XmlSuite xmlSuite1 = new XmlSuite();
    xmlSuite1.setName("SanityCheckSuite");
    {
      XmlTest result = new XmlTest(xmlSuite1);
      result.getXmlClasses().add(new XmlClass(SampleTest1.class.getCanonicalName()));
    }

    XmlSuite xmlSuite2 = new XmlSuite();
    xmlSuite2.setName("SanityCheckSuite");
    {
      XmlTest result = new XmlTest(xmlSuite2);
      result.getXmlClasses().add(new XmlClass(SampleTest2.class.getCanonicalName()));
    }

    TestNG tng = create();
    tng.setXmlSuites(Arrays.asList(xmlSuite1, xmlSuite2));
    tng.run();
  }
}

@@ -856,562 +856,797 @@
 Test
-(expectedExceptions = TestNGException.class, expectedExceptionsMessageRegExp = %22%5C%5Cs*Two suites cannot have the same name.*%22)%0A  public void checkChildSuitesFails() %7B%0A    TestNG tng = create();%0A    String testngXmlPath = getPathToResource(%22sanitycheck/test-s-a.xml%22);%0A    tng.setTestSuites(Arrays.asList(testngXmlPath));%0A    tng.run();%0A  %7D%0A%0A  /**%0A   * Checks that suites created programmatically also fails as expected%0A   */%0A  @Test(expectedExceptions = TestNGException.class, expectedExceptionsMessageRegExp = %22%5C%5Cs*Two suites cannot have the same name.*%22)
+%0A  public void checkChildSuitesFails() %7B%0A    TestListenerAdapter tla = new TestListenerAdapter();%0A    TestNG tng = create();%0A    String testngXmlPath = getPathToResource(%22sanitycheck/test-s-a.xml%22);%0A    tng.setTestSuites(Arrays.asList(testngXmlPath));%0A    tng.addListener(tla);%0A    tng.run();%0A    Assert.assertEquals(tla.getTestContexts().get(0).getSuite().getName(), %22SanityCheck suites%22);%0A    Assert.assertEquals(tla.getTestContexts().get(1).getSuite().getName(), %22SanityCheck suites%22);%0A    Assert.assertEquals(tla.getTestContexts().get(2).getSuite().getName(), %22SanityCheck suites (0)%22);%0A    Assert.assertEquals(tla.getTestContexts().get(3).getSuite().getName(), %22SanityCheck suites (0)%22);%0A  %7D%0A%0A  /**%0A   * Checks that suites created programmatically also works as expected%0A   */%0A  @Test
 %0A  p
@@ -2250,14 +2250,150 @@
 .run();%0A
+    Assert.assertEquals(xmlSuite1.getName(), %22SanityCheckSuite%22);%0A    Assert.assertEquals(xmlSuite2.getName(), %22SanityCheckSuite (0)%22);%0A
   %7D%0A%7D%0A

Fix CheckSuiteNamesTest after #829 changes