diff --git a/testng-core/src/main/java/org/testng/internal/Configuration.java b/testng-core/src/main/java/org/testng/internal/Configuration.java index 42fdd190c..7649c23ab 100644 --- a/testng-core/src/main/java/org/testng/internal/Configuration.java +++ b/testng-core/src/main/java/org/testng/internal/Configuration.java @@ -35,7 +35,7 @@ public class Configuration implements IConfiguration { private final Map, IConfigurationListener> m_configurationListeners = Maps.newLinkedHashMap(); private boolean alwaysRunListeners = true; - private IExecutorServiceFactory executorServiceFactory = ThreadPoolExecutor::new; + private volatile IExecutorServiceFactory executorServiceFactory = ThreadPoolExecutor::new; private IInjectorFactory injectorFactory = new GuiceBackedInjectorFactory(); diff --git a/testng-core/src/test/java/test/listeners/ListenersTest.java b/testng-core/src/test/java/test/listeners/ListenersTest.java index 9a1911101..01528cfd9 100644 --- a/testng-core/src/test/java/test/listeners/ListenersTest.java +++ b/testng-core/src/test/java/test/listeners/ListenersTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.io.IOException; +import java.lang.management.ManagementFactory; import java.nio.file.Files; import java.nio.file.Path; import java.util.Arrays; @@ -10,6 +11,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.assertj.core.api.Assertions; import org.assertj.core.api.SoftAssertions; import org.testng.ITestNGListener; @@ -45,6 +47,7 @@ import test.listeners.issue2916.ExecutionListenerHolder; import test.listeners.issue2916.ExecutionVisualiserHolder; import test.listeners.issue2916.InvokedMethodListenerHolder; +import test.listeners.issue2916.LogContainer; import test.listeners.issue2916.MethodInterceptorHolder; import test.listeners.issue2916.NormalSampleTestCase; import test.listeners.issue2916.SimpleConfigTestCase; @@ -65,63 +68,58 @@ public class ListenersTest extends SimpleBaseTest { @Test(description = "GITHUB-2916") public void ensureOrderingForExecutionListenersViaApi() { - Ensure.orderingViaApi( - ExecutionListenerHolder.LOGS, - ExecutionListenerHolder.ALL, - ExecutionListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaApi(ExecutionListenerHolder.ALL, ExecutionListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForAlterSuiteListenersViaApi() { - Ensure.orderingViaApi( - AlterSuiteListenerHolder.LOGS, - AlterSuiteListenerHolder.ALL, - AlterSuiteListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaApi(AlterSuiteListenerHolder.ALL, AlterSuiteListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForSuiteListenersViaApi() { - Ensure.orderingViaApi( - SuiteListenerHolder.LOGS, SuiteListenerHolder.ALL, SuiteListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaApi(SuiteListenerHolder.ALL, SuiteListenerHolder.EXPECTED_LOGS); + } + + public static void main(String[] args) throws InterruptedException { + String arguments = String.join(" ", ManagementFactory.getRuntimeMXBean().getInputArguments()); + System.err.println("Arguments = " + arguments); + ListenersTest object = new ListenersTest(); + for (int i = 1; i <= 10000; i++) { + object.ensureOrderingForTestListenersViaApi(); + TimeUnit.MILLISECONDS.sleep(500); + } } @Test(description = "GITHUB-2916") public void ensureOrderingForTestListenersViaApi() { Ensure.orderingViaApi( - ElaborateSampleTestCase.class, - TestListenerHolder.LOGS, - TestListenerHolder.ALL, - TestListenerHolder.EXPECTED_LOGS); + ElaborateSampleTestCase.class, TestListenerHolder.ALL, TestListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForInvokedListenersViaApi() { Ensure.orderingViaApi( - InvokedMethodListenerHolder.LOGS, - InvokedMethodListenerHolder.ALL, - InvokedMethodListenerHolder.EXPECTED_LOGS); + InvokedMethodListenerHolder.ALL, InvokedMethodListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForConfigurationListenersViaApi() { Ensure.orderingViaApi( SimpleConfigTestCase.class, - ConfigurationListenerHolder.LOGS, ConfigurationListenerHolder.ALL, ConfigurationListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForClassListenersViaApi() { - Ensure.orderingViaApi( - ClassListenerHolder.LOGS, ClassListenerHolder.ALL, ClassListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaApi(ClassListenerHolder.ALL, ClassListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForDataProviderListenersViaApi() { Ensure.orderingViaApi( DataProviderSampleTestCase.class, - DataProviderListenerHolder.LOGS, DataProviderListenerHolder.ALL, DataProviderListenerHolder.EXPECTED_LOGS); } @@ -130,56 +128,41 @@ public void ensureOrderingForDataProviderListenersViaApi() { public void ensureOrderingForDataProviderInterceptorsViaApi() { Ensure.orderingViaApi( DataProviderSampleTestCase.class, - DataProviderInterceptorHolder.LOGS, DataProviderInterceptorHolder.ALL, DataProviderInterceptorHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForExecutionVisualisersViaApi() { - Ensure.orderingViaApi( - ExecutionVisualiserHolder.LOGS, - ExecutionVisualiserHolder.ALL, - ExecutionVisualiserHolder.EXPECTED_LOGS); + Ensure.orderingViaApi(ExecutionVisualiserHolder.ALL, ExecutionVisualiserHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForMethodInterceptorsViaApi() { - Ensure.orderingViaApi( - MethodInterceptorHolder.LOGS, - MethodInterceptorHolder.ALL, - MethodInterceptorHolder.EXPECTED_LOGS); + Ensure.orderingViaApi(MethodInterceptorHolder.ALL, MethodInterceptorHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForExecutionListenersViaXmlTag() { Ensure.orderingViaXmlTag( - ExecutionListenerHolder.LOGS, - ExecutionListenerHolder.ALL_STRING, - ExecutionListenerHolder.EXPECTED_LOGS); + ExecutionListenerHolder.ALL_STRING, ExecutionListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForAlterSuiteListenersViaXmlTag() { Ensure.orderingViaXmlTag( - AlterSuiteListenerHolder.LOGS, - AlterSuiteListenerHolder.ALL_STRING, - AlterSuiteListenerHolder.EXPECTED_LOGS); + AlterSuiteListenerHolder.ALL_STRING, AlterSuiteListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForSuiteListenersViaXmlTag() { - Ensure.orderingViaXmlTag( - SuiteListenerHolder.LOGS, - SuiteListenerHolder.ALL_STRING, - SuiteListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaXmlTag(SuiteListenerHolder.ALL_STRING, SuiteListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForTestListenersViaXmlTag() { Ensure.orderingViaXmlTag( ElaborateSampleTestCase.class, - TestListenerHolder.LOGS, TestListenerHolder.ALL_STRING, TestListenerHolder.EXPECTED_LOGS); } @@ -187,33 +170,26 @@ public void ensureOrderingForTestListenersViaXmlTag() { @Test(description = "GITHUB-2916") public void ensureOrderingForInvokedMethodListenersViaXmlTag() { Ensure.orderingViaXmlTag( - InvokedMethodListenerHolder.LOGS, - InvokedMethodListenerHolder.ALL_STRING, - InvokedMethodListenerHolder.EXPECTED_LOGS); + InvokedMethodListenerHolder.ALL_STRING, InvokedMethodListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForConfigurationListenersViaXmlTag() { Ensure.orderingViaXmlTag( SimpleConfigTestCase.class, - ConfigurationListenerHolder.LOGS, ConfigurationListenerHolder.ALL_STRING, ConfigurationListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForClassListenersViaXmlTag() { - Ensure.orderingViaXmlTag( - ClassListenerHolder.LOGS, - ClassListenerHolder.ALL_STRING, - ClassListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaXmlTag(ClassListenerHolder.ALL_STRING, ClassListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForDataProviderListenersViaXmlTag() { Ensure.orderingViaXmlTag( DataProviderSampleTestCase.class, - DataProviderListenerHolder.LOGS, DataProviderListenerHolder.ALL_STRING, DataProviderListenerHolder.EXPECTED_LOGS); } @@ -222,7 +198,6 @@ public void ensureOrderingForDataProviderListenersViaXmlTag() { public void ensureOrderingForDataProviderInterceptorsViaXmlTag() { Ensure.orderingViaXmlTag( DataProviderSampleTestCase.class, - DataProviderInterceptorHolder.LOGS, DataProviderInterceptorHolder.ALL_STRING, DataProviderInterceptorHolder.EXPECTED_LOGS); } @@ -230,80 +205,63 @@ public void ensureOrderingForDataProviderInterceptorsViaXmlTag() { @Test(description = "GITHUB-2916") public void ensureOrderingForExecutionVisualisersViaXmlTag() { Ensure.orderingViaXmlTag( - ExecutionVisualiserHolder.LOGS, - ExecutionVisualiserHolder.ALL_STRING, - ExecutionVisualiserHolder.EXPECTED_LOGS); + ExecutionVisualiserHolder.ALL_STRING, ExecutionVisualiserHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForMethodInterceptorsViaXmlTag() { Ensure.orderingViaXmlTag( - MethodInterceptorHolder.LOGS, - MethodInterceptorHolder.ALL_STRING, - MethodInterceptorHolder.EXPECTED_LOGS); + MethodInterceptorHolder.ALL_STRING, MethodInterceptorHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForExecutionListenersViaCli() { Ensure.orderingViaCli( - ExecutionListenerHolder.LOGS, - ExecutionListenerHolder.ALL_STRING, - ExecutionListenerHolder.EXPECTED_LOGS); + ExecutionListenerHolder.ALL_STRING, ExecutionListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForAlterSuiteListenersViaCli() { Ensure.orderingViaCli( - AlterSuiteListenerHolder.LOGS, - AlterSuiteListenerHolder.ALL_STRING, - AlterSuiteListenerHolder.EXPECTED_LOGS); + AlterSuiteListenerHolder.ALL_STRING, AlterSuiteListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForSuiteListenersViaCli() { - Ensure.orderingViaCli( - SuiteListenerHolder.LOGS, - SuiteListenerHolder.ALL_STRING, - SuiteListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaCli(SuiteListenerHolder.ALL_STRING, SuiteListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForTestListenersViaCli() { Ensure.orderingViaCli( - ElaborateSampleTestCase.class, TestListenerHolder.LOGS, - TestListenerHolder.ALL_STRING, TestListenerHolder.EXPECTED_LOGS); + ElaborateSampleTestCase.class, + TestListenerHolder.ALL_STRING, + TestListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForInvokedMethodListenersViaCli() { Ensure.orderingViaCli( - InvokedMethodListenerHolder.LOGS, - InvokedMethodListenerHolder.ALL_STRING, - InvokedMethodListenerHolder.EXPECTED_LOGS); + InvokedMethodListenerHolder.ALL_STRING, InvokedMethodListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForConfigurationListenersViaCli() { Ensure.orderingViaCli( SimpleConfigTestCase.class, - ConfigurationListenerHolder.LOGS, ConfigurationListenerHolder.ALL_STRING, ConfigurationListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForClassListenersViaCli() { - Ensure.orderingViaCli( - ClassListenerHolder.LOGS, - ClassListenerHolder.ALL_STRING, - ClassListenerHolder.EXPECTED_LOGS); + Ensure.orderingViaCli(ClassListenerHolder.ALL_STRING, ClassListenerHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForDataProviderListenersViaCli() { Ensure.orderingViaCli( DataProviderSampleTestCase.class, - DataProviderListenerHolder.LOGS, DataProviderListenerHolder.ALL_STRING, DataProviderListenerHolder.EXPECTED_LOGS); } @@ -312,7 +270,6 @@ public void ensureOrderingForDataProviderListenersViaCli() { public void ensureOrderingForDataProviderInterceptorsViaCli() { Ensure.orderingViaCli( DataProviderSampleTestCase.class, - DataProviderInterceptorHolder.LOGS, DataProviderInterceptorHolder.ALL_STRING, DataProviderInterceptorHolder.EXPECTED_LOGS); } @@ -320,17 +277,13 @@ public void ensureOrderingForDataProviderInterceptorsViaCli() { @Test(description = "GITHUB-2916") public void ensureOrderingForExecutionVisualisersViaCli() { Ensure.orderingViaCli( - ExecutionVisualiserHolder.LOGS, - ExecutionVisualiserHolder.ALL_STRING, - ExecutionVisualiserHolder.EXPECTED_LOGS); + ExecutionVisualiserHolder.ALL_STRING, ExecutionVisualiserHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") public void ensureOrderingForMethodInterceptorsViaCli() { Ensure.orderingViaCli( - MethodInterceptorHolder.LOGS, - MethodInterceptorHolder.ALL_STRING, - MethodInterceptorHolder.EXPECTED_LOGS); + MethodInterceptorHolder.ALL_STRING, MethodInterceptorHolder.EXPECTED_LOGS); } @Test(description = "GITHUB-2916") @@ -356,7 +309,6 @@ public void ensureOrderingForExecutionListenersViaAnnotation() { }; Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.ExecutionListenerSampleTestCase.class, - ExecutionListenerHolder.LOGS, ExecutionListenerHolder.SUBSET, expected); } @@ -365,7 +317,6 @@ public void ensureOrderingForExecutionListenersViaAnnotation() { public void ensureOrderingForSuiteListenersViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.SuiteListenerSampleTestCase.class, - SuiteListenerHolder.LOGS, SuiteListenerHolder.SUBSET, SuiteListenerHolder.EXPECTED_LOGS); } @@ -374,7 +325,6 @@ public void ensureOrderingForSuiteListenersViaAnnotation() { public void ensureOrderingForTestListenersViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.TestListenerSampleTestCase.class, - TestListenerHolder.LOGS, TestListenerHolder.SUBSET, TestListenerHolder.EXPECTED_LOGS); } @@ -383,7 +333,6 @@ public void ensureOrderingForTestListenersViaAnnotation() { public void ensureOrderingForInvokedMethodListenersViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.InvokedMethodListenerSampleTestCase.class, - InvokedMethodListenerHolder.LOGS, InvokedMethodListenerHolder.SUBSET, InvokedMethodListenerHolder.EXPECTED_LOGS); } @@ -392,7 +341,6 @@ public void ensureOrderingForInvokedMethodListenersViaAnnotation() { public void ensureOrderingForConfigurationListenersViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.ConfigurationListenerSampleTestCase.class, - ConfigurationListenerHolder.LOGS, ConfigurationListenerHolder.SUBSET, ConfigurationListenerHolder.EXPECTED_LOGS); } @@ -401,7 +349,6 @@ public void ensureOrderingForConfigurationListenersViaAnnotation() { public void ensureOrderingForClassListenersViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.ClassListenerSampleTestCase.class, - ClassListenerHolder.LOGS, ClassListenerHolder.SUBSET, ClassListenerHolder.EXPECTED_LOGS); } @@ -410,7 +357,6 @@ public void ensureOrderingForClassListenersViaAnnotation() { public void ensureOrderingForDataProviderListenersViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.DataProviderListenerSampleTestCase.class, - DataProviderListenerHolder.LOGS, DataProviderListenerHolder.SUBSET, DataProviderListenerHolder.EXPECTED_LOGS); } @@ -419,7 +365,6 @@ public void ensureOrderingForDataProviderListenersViaAnnotation() { public void ensureOrderingForDataProviderInterceptorsViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.DataProviderInterceptorSampleTestCase.class, - DataProviderInterceptorHolder.LOGS, DataProviderInterceptorHolder.SUBSET, DataProviderInterceptorHolder.EXPECTED_LOGS); } @@ -428,7 +373,6 @@ public void ensureOrderingForDataProviderInterceptorsViaAnnotation() { public void ensureOrderingForExecutionVisualisersViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.ExecutionVisualiserSampleTestCase.class, - ExecutionVisualiserHolder.LOGS, ExecutionVisualiserHolder.SUBSET, ExecutionVisualiserHolder.EXPECTED_LOGS); } @@ -437,7 +381,6 @@ public void ensureOrderingForExecutionVisualisersViaAnnotation() { public void ensureOrderingForMethodInterceptorsViaAnnotation() { Ensure.orderingViaAnnotation( AnnotatedTestCaseSamplesHolder.MethodInterceptorSampleTestCase.class, - MethodInterceptorHolder.LOGS, MethodInterceptorHolder.SUBSET, MethodInterceptorHolder.EXPECTED_LOGS); } @@ -750,23 +693,22 @@ private static class Ensure { private Ensure() {} static void orderingViaAnnotation( - Class testClass, List logs, List listeners, String[] expected) { - logs.clear(); + Class testClass, List listeners, String[] expected) { + LogContainer.instance.initialiseLogs(); TestNG testng = create(testClass); listeners.forEach(testng::addListener); testng.setUseDefaultListeners(false); testng.setListenerComparator(new AnnotationBackedListenerComparator()); testng.run(); - assertThat(logs).containsExactly(expected); + assertThat(LogContainer.instance.allLogs()).containsExactly(expected); } - static void orderingViaCli(List logs, List listeners, String[] expected) { - orderingViaCli(NormalSampleTestCase.class, logs, listeners, expected); + static void orderingViaCli(List listeners, String[] expected) { + orderingViaCli(NormalSampleTestCase.class, listeners, expected); } - static void orderingViaCli( - Class clazz, List logs, List listeners, String[] expected) { - logs.clear(); + static void orderingViaCli(Class clazz, List listeners, String[] expected) { + LogContainer.instance.initialiseLogs(); String[] args = new String[] { "-listener", String.join(",", listeners), @@ -775,39 +717,36 @@ static void orderingViaCli( "-listenercomparator", AnnotationBackedListenerComparator.class.getName() }; TestNG.privateMain(args, null); - assertThat(logs).containsExactly(expected); + assertThat(LogContainer.instance.allLogs()).containsExactly(expected); } - static void orderingViaXmlTag(List logs, List listeners, String[] expected) { - orderingViaXmlTag(NormalSampleTestCase.class, logs, listeners, expected); + static void orderingViaXmlTag(List listeners, String[] expected) { + orderingViaXmlTag(NormalSampleTestCase.class, listeners, expected); } - static void orderingViaXmlTag( - Class clazz, List logs, List listeners, String[] expected) { - logs.clear(); + static void orderingViaXmlTag(Class clazz, List listeners, String[] expected) { + LogContainer.instance.initialiseLogs(); XmlSuite xmlSuite = createXmlSuite("suite", "test", clazz); listeners.forEach(xmlSuite::addListener); TestNG testng = create(xmlSuite); testng.setUseDefaultListeners(false); testng.setListenerComparator(new AnnotationBackedListenerComparator()); testng.run(); - assertThat(logs).containsExactly(expected); + assertThat(LogContainer.instance.allLogs()).containsExactly(expected); } - static void orderingViaApi( - List logs, List listeners, String[] expected) { - orderingViaApi(NormalSampleTestCase.class, logs, listeners, expected); + static void orderingViaApi(List listeners, String[] expected) { + orderingViaApi(NormalSampleTestCase.class, listeners, expected); } - static void orderingViaApi( - Class clazz, List logs, List listeners, String[] expected) { - logs.clear(); + static void orderingViaApi(Class clazz, List listeners, String[] expected) { + LogContainer.instance.initialiseLogs(); TestNG testng = create(clazz); listeners.forEach(testng::addListener); testng.setUseDefaultListeners(false); testng.setListenerComparator(new AnnotationBackedListenerComparator()); testng.run(); - assertThat(logs).containsExactly(expected); + assertThat(LogContainer.instance.allLogs()).containsExactly(expected); } } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/AlterSuiteListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/AlterSuiteListenerHolder.java index 45755fea4..fe66e4924 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/AlterSuiteListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/AlterSuiteListenerHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.IAlterSuiteListener; import org.testng.ITestNGListener; @@ -10,7 +9,6 @@ public class AlterSuiteListenerHolder { public static final String[] EXPECTED_LOGS = new String[] {"MasterOogway.alter", "MasterShifu.alter", "DragonWarrior.alter"}; - public static List LOGS = new ArrayList<>(); private static final String PREFIX = AlterSuiteListenerHolder.class.getName() + "$"; public static final List ALL = @@ -25,7 +23,7 @@ public class AlterSuiteListenerHolder { public abstract static class KungFuWarrior implements IAlterSuiteListener { @Override public void alter(List suites) { - LOGS.add(getClass().getSimpleName() + ".alter"); + LogContainer.instance.log(getClass().getSimpleName() + ".alter"); } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/ClassListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/ClassListenerHolder.java index cd6416dce..ff8345db8 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/ClassListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/ClassListenerHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.IClassListener; import org.testng.ITestClass; @@ -8,7 +7,6 @@ public class ClassListenerHolder { - public static List LOGS = new ArrayList<>(); private static final String PREFIX = ClassListenerHolder.class.getName() + "$"; public static final String[] EXPECTED_LOGS = @@ -37,12 +35,12 @@ public abstract static class KungFuWarrior implements IClassListener { @Override public void onBeforeClass(ITestClass testClass) { - LOGS.add(getClass().getSimpleName() + ".onBeforeClass"); + LogContainer.instance.log(getClass().getSimpleName() + ".onBeforeClass"); } @Override public void onAfterClass(ITestClass testClass) { - LOGS.add(getClass().getSimpleName() + ".onAfterClass"); + LogContainer.instance.log(getClass().getSimpleName() + ".onAfterClass"); } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/ConfigurationListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/ConfigurationListenerHolder.java index 51ee6268a..28d8090a5 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/ConfigurationListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/ConfigurationListenerHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.IConfigurationListener; import org.testng.ITestNGListener; @@ -9,7 +8,6 @@ public class ConfigurationListenerHolder { - public static List LOGS = new ArrayList<>(); private static final String PREFIX = ConfigurationListenerHolder.class.getName() + "$"; public static final String[] EXPECTED_LOGS = @@ -58,25 +56,25 @@ public abstract static class KungFuWarrior implements IConfigurationListener { @Override public void beforeConfiguration(ITestResult tr, ITestNGMethod tm) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".beforeConfiguration_" + tr.getMethod().getMethodName()); } @Override public void onConfigurationSuccess(ITestResult tr, ITestNGMethod tm) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".onConfigurationSuccess_" + tr.getMethod().getMethodName()); } @Override public void onConfigurationFailure(ITestResult tr, ITestNGMethod tm) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".onConfigurationFailure_" + tr.getMethod().getMethodName()); } @Override public void onConfigurationSkip(ITestResult tr, ITestNGMethod tm) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".onConfigurationSkip_" + tr.getMethod().getMethodName()); } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/DataProviderInterceptorHolder.java b/testng-core/src/test/java/test/listeners/issue2916/DataProviderInterceptorHolder.java index c98d69bb9..7c38a6c86 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/DataProviderInterceptorHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/DataProviderInterceptorHolder.java @@ -1,13 +1,11 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.testng.*; public class DataProviderInterceptorHolder { - public static List LOGS = new ArrayList<>(); private static final String PREFIX = DataProviderInterceptorHolder.class.getName() + "$"; public static final String[] EXPECTED_LOGS = @@ -39,7 +37,8 @@ public Iterator intercept( IDataProviderMethod dp, ITestNGMethod method, ITestContext iTestContext) { - LOGS.add(getClass().getSimpleName() + ".intercept_" + dp.getMethod().getName()); + LogContainer.instance.log( + getClass().getSimpleName() + ".intercept_" + dp.getMethod().getName()); return original; } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/DataProviderListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/DataProviderListenerHolder.java index 3adec867c..299494d5d 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/DataProviderListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/DataProviderListenerHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.IDataProviderListener; import org.testng.IDataProviderMethod; @@ -10,7 +9,6 @@ public class DataProviderListenerHolder { - public static List LOGS = new ArrayList<>(); private static final String PREFIX = DataProviderListenerHolder.class.getName() + "$"; public static final String[] EXPECTED_LOGS = @@ -48,7 +46,7 @@ public abstract static class KungFuWarrior implements IDataProviderListener { @Override public void beforeDataProviderExecution( IDataProviderMethod dataProviderMethod, ITestNGMethod method, ITestContext iTestContext) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".beforeDataProviderExecution_" + dataProviderMethod.getMethod().getName()); @@ -57,7 +55,7 @@ public void beforeDataProviderExecution( @Override public void afterDataProviderExecution( IDataProviderMethod dataProviderMethod, ITestNGMethod method, ITestContext iTestContext) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".afterDataProviderExecution_" + dataProviderMethod.getMethod().getName()); @@ -65,7 +63,7 @@ public void afterDataProviderExecution( @Override public void onDataProviderFailure(ITestNGMethod method, ITestContext ctx, RuntimeException t) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".onDataProviderFailure_" + method.getDataProviderMethod().getMethod().getName()); diff --git a/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java b/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java index 2a8831ea9..f499fb8f7 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java +++ b/testng-core/src/test/java/test/listeners/issue2916/ElaborateSampleTestCase.java @@ -23,8 +23,12 @@ public void flakyTest() { } } - @Test(timeOut = 2, priority = 4) + @Test(timeOut = 25, priority = 4) public void timingOutTest() throws InterruptedException { - TimeUnit.MILLISECONDS.sleep(10); + TimeUnit.SECONDS.sleep(10); + new Throwable().printStackTrace(); + if (counter != 3) { + Assert.fail(); + } } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/ExecutionListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/ExecutionListenerHolder.java index 0622e2a5c..887a8da69 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/ExecutionListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/ExecutionListenerHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.IExecutionListener; import org.testng.ITestNGListener; @@ -16,7 +15,6 @@ public class ExecutionListenerHolder { "MasterShifu.onExecutionFinish", "MasterOogway.onExecutionFinish" }; - public static List LOGS = new ArrayList<>(); private static final String PREFIX = ExecutionListenerHolder.class.getName() + "$"; public static final List ALL = @@ -35,12 +33,12 @@ public class ExecutionListenerHolder { public abstract static class KungFuWarrior implements IExecutionListener { @Override public void onExecutionStart() { - LOGS.add(getClass().getSimpleName() + ".onExecutionStart"); + LogContainer.instance.log(getClass().getSimpleName() + ".onExecutionStart"); } @Override public void onExecutionFinish() { - LOGS.add(getClass().getSimpleName() + ".onExecutionFinish"); + LogContainer.instance.log(getClass().getSimpleName() + ".onExecutionFinish"); } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/ExecutionVisualiserHolder.java b/testng-core/src/test/java/test/listeners/issue2916/ExecutionVisualiserHolder.java index e7cfc44fe..669a8194b 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/ExecutionVisualiserHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/ExecutionVisualiserHolder.java @@ -1,12 +1,10 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.*; public class ExecutionVisualiserHolder { - public static List LOGS = new ArrayList<>(); private static final String PREFIX = ExecutionVisualiserHolder.class.getName() + "$"; public static final String[] EXPECTED_LOGS = @@ -34,7 +32,7 @@ public abstract static class KungFuWarrior implements IExecutionVisualiser { @Override public void consumeDotDefinition(String dotDefinition) { - LOGS.add(getClass().getSimpleName() + ".consumeDotDefinition"); + LogContainer.instance.log(getClass().getSimpleName() + ".consumeDotDefinition"); } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/InvokedMethodListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/InvokedMethodListenerHolder.java index d58c2a6ba..5cf9e470f 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/InvokedMethodListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/InvokedMethodListenerHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.IInvokedMethod; import org.testng.IInvokedMethodListener; @@ -9,7 +8,6 @@ public class InvokedMethodListenerHolder { - public static List LOGS = new ArrayList<>(); private static final String PREFIX = InvokedMethodListenerHolder.class.getName() + "$"; public static final String[] EXPECTED_LOGS = @@ -107,7 +105,7 @@ public abstract static class KungFuWarrior implements IInvokedMethodListener { @Override public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { IInvokedMethodListener.super.beforeInvocation(method, testResult); - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".beforeInvocation_" + method.getTestMethod().getMethodName()); @@ -115,7 +113,7 @@ public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { @Override public void afterInvocation(IInvokedMethod method, ITestResult testResult) { - LOGS.add( + LogContainer.instance.log( getClass().getSimpleName() + ".afterInvocation_" + method.getTestMethod().getMethodName()); diff --git a/testng-core/src/test/java/test/listeners/issue2916/LogContainer.java b/testng-core/src/test/java/test/listeners/issue2916/LogContainer.java new file mode 100644 index 000000000..10cf95ddd --- /dev/null +++ b/testng-core/src/test/java/test/listeners/issue2916/LogContainer.java @@ -0,0 +1,34 @@ +package test.listeners.issue2916; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import org.testng.internal.AutoCloseableLock; + +public enum LogContainer { + instance; + + private List logs; + + private final AutoCloseableLock lock = new AutoCloseableLock(); + + public void initialiseLogs() { + try (AutoCloseableLock ignore = lock.lock()) { + logs = new ArrayList<>(); + } + } + + public void log(String line) { + try (AutoCloseableLock ignore = lock.lock()) { + Objects.requireNonNull(logs).add(line); + } + } + + public List allLogs() { + if (logs == null || logs.isEmpty()) { + throw new IllegalStateException("Logs should have been initialised"); + } + return Collections.synchronizedList(logs); + } +} diff --git a/testng-core/src/test/java/test/listeners/issue2916/MethodInterceptorHolder.java b/testng-core/src/test/java/test/listeners/issue2916/MethodInterceptorHolder.java index c22f8e2e0..b546eae17 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/MethodInterceptorHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/MethodInterceptorHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.IMethodInstance; import org.testng.IMethodInterceptor; @@ -9,7 +8,6 @@ public class MethodInterceptorHolder { - public static List LOGS = new ArrayList<>(); private static final String PREFIX = MethodInterceptorHolder.class.getName() + "$"; public static final String[] EXPECTED_LOGS = @@ -32,7 +30,7 @@ public abstract static class KungFuWarrior implements IMethodInterceptor { @Override public List intercept(List methods, ITestContext context) { - LOGS.add(getClass().getSimpleName() + ".intercept"); + LogContainer.instance.log(getClass().getSimpleName() + ".intercept"); return methods; } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/SuiteListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/SuiteListenerHolder.java index 8cc814e70..6f1d5df0e 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/SuiteListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/SuiteListenerHolder.java @@ -1,6 +1,5 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.ISuite; import org.testng.ISuiteListener; @@ -17,7 +16,6 @@ public class SuiteListenerHolder { "MasterShifu.onFinish", "MasterOogway.onFinish" }; - public static List LOGS = new ArrayList<>(); private static final String PREFIX = SuiteListenerHolder.class.getName() + "$"; public static final List ALL = @@ -36,12 +34,12 @@ public abstract static class KungFuWarrior implements ISuiteListener { @Override public void onStart(ISuite suite) { - LOGS.add(getClass().getSimpleName() + ".onStart"); + LogContainer.instance.log(getClass().getSimpleName() + ".onStart"); } @Override public void onFinish(ISuite suite) { - LOGS.add(getClass().getSimpleName() + ".onFinish"); + LogContainer.instance.log(getClass().getSimpleName() + ".onFinish"); } } diff --git a/testng-core/src/test/java/test/listeners/issue2916/TestListenerHolder.java b/testng-core/src/test/java/test/listeners/issue2916/TestListenerHolder.java index a49ef5583..f061500da 100644 --- a/testng-core/src/test/java/test/listeners/issue2916/TestListenerHolder.java +++ b/testng-core/src/test/java/test/listeners/issue2916/TestListenerHolder.java @@ -1,11 +1,11 @@ package test.listeners.issue2916; -import java.util.ArrayList; import java.util.List; import org.testng.ITestContext; import org.testng.ITestListener; import org.testng.ITestNGListener; import org.testng.ITestResult; +import org.testng.Reporter; public class TestListenerHolder { @@ -72,7 +72,6 @@ public class TestListenerHolder { "MasterShifu.onFinish_ITestContext", "MasterOogway.onFinish_ITestContext" }; - public static List LOGS = new ArrayList<>(); private static final String PREFIX = TestListenerHolder.class.getName() + "$"; public static final List ALL = @@ -91,42 +90,50 @@ public abstract static class KungFuWarrior implements ITestListener { @Override public void onStart(ITestContext context) { - LOGS.add(getClass().getSimpleName() + ".onStart_ITestContext"); + LogContainer.instance.log(getClass().getSimpleName() + ".onStart_ITestContext"); } @Override public void onTestStart(ITestResult result) { - LOGS.add(getClass().getSimpleName() + ".onTestStart"); + LogContainer.instance.log(getClass().getSimpleName() + ".onTestStart"); } @Override public void onTestSuccess(ITestResult result) { - LOGS.add(getClass().getSimpleName() + ".onTestSuccess"); + Reporter.log(result.getMethod().getQualifiedName() + " passed", true); + LogContainer.instance.log(getClass().getSimpleName() + ".onTestSuccess"); } @Override public void onTestFailure(ITestResult result) { - LOGS.add(getClass().getSimpleName() + ".onTestFailure"); + Reporter.log(result.getMethod().getQualifiedName() + " failed", true); + LogContainer.instance.log(getClass().getSimpleName() + ".onTestFailure"); } @Override public void onTestSkipped(ITestResult result) { - LOGS.add(getClass().getSimpleName() + ".onTestSkipped"); + Reporter.log(result.getMethod().getQualifiedName() + " skipped", true); + LogContainer.instance.log(getClass().getSimpleName() + ".onTestSkipped"); } @Override public void onTestFailedWithTimeout(ITestResult result) { - LOGS.add(getClass().getSimpleName() + ".onTestFailedWithTimeout"); + Reporter.log(result.getMethod().getQualifiedName() + " failed due to timeout", true); + LogContainer.instance.log(getClass().getSimpleName() + ".onTestFailedWithTimeout"); } @Override public void onTestFailedButWithinSuccessPercentage(ITestResult result) { - LOGS.add(getClass().getSimpleName() + ".onTestFailedButWithinSuccessPercentage"); + Reporter.log( + result.getMethod().getQualifiedName() + " test failed but within success percentage", + true); + LogContainer.instance.log( + getClass().getSimpleName() + ".onTestFailedButWithinSuccessPercentage"); } @Override public void onFinish(ITestContext context) { - LOGS.add(getClass().getSimpleName() + ".onFinish_ITestContext"); + LogContainer.instance.log(getClass().getSimpleName() + ".onFinish_ITestContext"); } }