package test.name;

import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.TestNG;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import test.SimpleBaseTest;

public class NameTest extends SimpleBaseTest {

  @Test
  public void itestTest() {
    TestNG tng = create(SimpleITestSample.class);
    TestListenerAdapter adapter = new TestListenerAdapter();
    tng.addListener(adapter);

    tng.run();

    Assert.assertTrue(adapter.getFailedTests().isEmpty());
    Assert.assertTrue(adapter.getSkippedTests().isEmpty());
    Assert.assertEquals(adapter.getPassedTests().size(), 1);
    ITestResult result = adapter.getPassedTests().get(0);
    Assert.assertEquals(result.getMethod().getMethodName(), "test");
    Assert.assertEquals(result.getName(), "NAME");
    Assert.assertEquals(result.getTestName(), "NAME");
  }

  @Test
  public void testNameTest() {
    TestNG tng = create(NameSample.class);
    TestListenerAdapter adapter = new TestListenerAdapter();
    tng.addListener(adapter);

    tng.run();

    Assert.assertTrue(adapter.getFailedTests().isEmpty());
    Assert.assertTrue(adapter.getSkippedTests().isEmpty());
    Assert.assertEquals(adapter.getPassedTests().size(), 1);
    ITestResult result = adapter.getPassedTests().get(0);
    Assert.assertEquals(result.getMethod().getMethodName(), "test");
    Assert.assertEquals(result.getName(), "NAME");
    Assert.assertEquals(result.getTestName(), "NAME");
  }

  @Test
  public void complexITestTest() {
    TestNG tng = create(ITestSample.class);
    TestListenerAdapter adapter = new TestListenerAdapter();
    tng.addListener(adapter);

    tng.run();

    Assert.assertTrue(adapter.getFailedTests().isEmpty());
    Assert.assertTrue(adapter.getSkippedTests().isEmpty());
    Assert.assertEquals(adapter.getPassedTests().size(), 5);
    List<String> testNames = new ArrayList<>(Arrays.asList("test1", "test2", "test3", "test4", "test5"));
    for (ITestResult testResult : adapter.getPassedTests()) {
      Assert.assertTrue(testNames.remove(testResult.getName()),
                        "Duplicate test names " + getNames(adapter.getPassedTests()));
    }
    Assert.assertEquals(testNames, Collections.emptyList());
  }

  private static List<String> getNames(List<ITestResult> results) {
    List<String> names = new ArrayList<>(results.size());
    for (ITestResult result : results) {
      names.add(result.getName());
    }
    return names;
  }
}

@@ -994,49 +994,1311 @@
 oid 
-testNameTest() %7B%0A    TestNG tng = create(
+itestTestWithXml() %7B%0A    TestNG tng = createTests(%22suite%22, SimpleITestSample.class);%0A    TestListenerAdapter adapter = new TestListenerAdapter();%0A    tng.addListener(adapter);%0A%0A    tng.run();%0A%0A    Assert.assertTrue(adapter.getFailedTests().isEmpty());%0A    Assert.assertTrue(adapter.getSkippedTests().isEmpty());%0A    Assert.assertEquals(adapter.getPassedTests().size(), 1);%0A    ITestResult result = adapter.getPassedTests().get(0);%0A    Assert.assertEquals(result.getMethod().getMethodName(), %22test%22);%0A    Assert.assertEquals(result.getName(), %22NAME%22);%0A    Assert.assertEquals(result.getTestName(), %22NAME%22);%0A  %7D%0A%0A  @Test%0A  public void testNameTest() %7B%0A    TestNG tng = create(NameSample.class);%0A    TestListenerAdapter adapter = new TestListenerAdapter();%0A    tng.addListener(adapter);%0A%0A    tng.run();%0A%0A    Assert.assertTrue(adapter.getFailedTests().isEmpty());%0A    Assert.assertTrue(adapter.getSkippedTests().isEmpty());%0A    Assert.assertEquals(adapter.getPassedTests().size(), 1);%0A    ITestResult result = adapter.getPassedTests().get(0);%0A    Assert.assertEquals(result.getMethod().getMethodName(), %22test%22);%0A    Assert.assertEquals(result.getName(), %22NAME%22);%0A    Assert.assertEquals(result.getTestName(), %22NAME%22);%0A  %7D%0A%0A  @Test%0A  public void testNameTestWithXml() %7B%0A    TestNG tng = createTests(%22suite%22, 
 Name

Add name tests with xml