From 1595ce2d0f3392f8d61d01d247f9c88eef72254f Mon Sep 17 00:00:00 2001 From: Nick Tan Date: Fri, 31 Mar 2017 21:56:25 -0700 Subject: [PATCH 1/3] issue #1400: don't add duplicate reporter --- CHANGES.txt | 1 + src/main/java/org/testng/TestNG.java | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 83747d2720..e2ccc63bbb 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -11,6 +11,7 @@ Fixed: GITHUB-1365: Be able to override default XML parser (@ChristiKh & Julien Fixed: GITHUB-1360: TestNG does not distinguish between methods of different priorities (Krishnan Mahadevan) Fixed: GITHUB-1144: Add Class and Constructor as legal native dependency injection (Guillaume Juillot) Fixed: GITHUB-1380: Circular dependencies may fail in parallel (Julien Herr) +Fixed: GITHUB-1400: TestNG, Multiple duplicate listener warnings on implementing multiple listener interfaces (@bipo1980 & Nick Tan) 6.11 2017/02/27 diff --git a/src/main/java/org/testng/TestNG.java b/src/main/java/org/testng/TestNG.java index 5e1f33a355..d1e5954554 100644 --- a/src/main/java/org/testng/TestNG.java +++ b/src/main/java/org/testng/TestNG.java @@ -1394,7 +1394,9 @@ private SuiteRunner createSuiteRunner(XmlSuite xmlSuite) { } for (IReporter r : result.getReporters()) { - addListener(r); + if (!m_reporters.containsKey(r.getClass())) { + addListener(r); + } } for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) { From 28198ca2afa278b1220748129e52bc70a43707d1 Mon Sep 17 00:00:00 2001 From: Nick Tan Date: Thu, 6 Apr 2017 23:36:18 -0700 Subject: [PATCH 2/3] issue #1400: use overloaded maybeAddListener --- src/main/java/org/testng/TestNG.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/testng/TestNG.java b/src/main/java/org/testng/TestNG.java index d1e5954554..85fa66aa76 100644 --- a/src/main/java/org/testng/TestNG.java +++ b/src/main/java/org/testng/TestNG.java @@ -705,8 +705,14 @@ public void addListener(Object listener) { } private static void maybeAddListener(Map, E> map, Class type, E value) { + maybeAddListener(map, type, value, false); + } + + private static void maybeAddListener(Map, E> map, Class type, E value, boolean quiet) { if (map.containsKey(value.getClass())) { - LOGGER.warn("Ignoring duplicate listener : " + value.getClass().getName()); + if (!quiet) { + LOGGER.warn("Ignoring duplicate listener : " + value.getClass().getName()); + } } else { map.put(type, value); } @@ -1394,9 +1400,7 @@ private SuiteRunner createSuiteRunner(XmlSuite xmlSuite) { } for (IReporter r : result.getReporters()) { - if (!m_reporters.containsKey(r.getClass())) { - addListener(r); - } + maybeAddListener(m_reporters, r.getClass(), r, true); } for (IConfigurationListener cl : m_configuration.getConfigurationListeners()) { From 0454118c1f2ccb1b2437e385b98284156c44784c Mon Sep 17 00:00:00 2001 From: Nick Tan Date: Fri, 7 Apr 2017 00:00:58 -0700 Subject: [PATCH 3/3] #1400: refactor maybeAddListener --- src/main/java/org/testng/TestNG.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/testng/TestNG.java b/src/main/java/org/testng/TestNG.java index 85fa66aa76..124efc0d93 100644 --- a/src/main/java/org/testng/TestNG.java +++ b/src/main/java/org/testng/TestNG.java @@ -704,14 +704,14 @@ public void addListener(Object listener) { addListener((ITestNGListener) listener); } - private static void maybeAddListener(Map, E> map, Class type, E value) { - maybeAddListener(map, type, value, false); + private static void maybeAddListener(Map, E> map, E value) { + maybeAddListener(map, (Class) value.getClass(), value, false); } private static void maybeAddListener(Map, E> map, Class type, E value, boolean quiet) { - if (map.containsKey(value.getClass())) { + if (map.containsKey(type)) { if (!quiet) { - LOGGER.warn("Ignoring duplicate listener : " + value.getClass().getName()); + LOGGER.warn("Ignoring duplicate listener : " + type.getName()); } } else { map.put(type, value); @@ -724,19 +724,19 @@ public void addListener(ITestNGListener listener) { } if (listener instanceof ISuiteListener) { ISuiteListener suite = (ISuiteListener) listener; - maybeAddListener(m_suiteListeners, suite.getClass(), suite); + maybeAddListener(m_suiteListeners, suite); } if (listener instanceof ITestListener) { ITestListener test = (ITestListener) listener; - maybeAddListener(m_testListeners, test.getClass(), test); + maybeAddListener(m_testListeners, test); } if (listener instanceof IClassListener) { IClassListener clazz = (IClassListener) listener; - maybeAddListener(m_classListeners, clazz.getClass(), clazz); + maybeAddListener(m_classListeners, clazz); } if (listener instanceof IReporter) { IReporter reporter = (IReporter) listener; - maybeAddListener(m_reporters, reporter.getClass(), reporter); + maybeAddListener(m_reporters, reporter); } if (listener instanceof IAnnotationTransformer) { setAnnotationTransformer((IAnnotationTransformer) listener); @@ -746,7 +746,7 @@ public void addListener(ITestNGListener listener) { } if (listener instanceof IInvokedMethodListener) { IInvokedMethodListener method = (IInvokedMethodListener) listener; - maybeAddListener(m_invokedMethodListeners, method.getClass(), method); + maybeAddListener(m_invokedMethodListeners, method); } if (listener instanceof IHookable) { setHookable((IHookable) listener); @@ -762,7 +762,7 @@ public void addListener(ITestNGListener listener) { } if (listener instanceof IAlterSuiteListener) { IAlterSuiteListener alter = (IAlterSuiteListener) listener; - maybeAddListener(m_alterSuiteListeners, alter.getClass(), alter); + maybeAddListener(m_alterSuiteListeners, alter); } }