File tree Expand file tree Collapse file tree 7 files changed +39
-15
lines changed
testng-core-api/src/main/java/org/testng
main/java/org/testng/internal
test/java/test/factory/issue3079 Expand file tree Collapse file tree 7 files changed +39
-15
lines changed Original file line number Diff line number Diff line change 33import java .util .List ;
44import java .util .Map ;
55import java .util .Set ;
6- import java .util .UUID ;
76import java .util .concurrent .Callable ;
87import org .testng .annotations .CustomAttribute ;
98import org .testng .internal .ConstructorOrMethod ;
@@ -297,12 +296,4 @@ default Class<?>[] getParameterTypes() {
297296 default boolean isIgnoreFailure () {
298297 return false ;
299298 }
300-
301- /**
302- * @return - A <code>{@link UUID}</code> that represents a unique id which is associated with
303- * every test class object.
304- */
305- default UUID getInstanceId () {
306- return null ;
307- }
308299}
Original file line number Diff line number Diff line change 3535import org .testng .xml .XmlTest ;
3636
3737/** Superclass to represent both @Test and @Configuration methods. */
38- public abstract class BaseTestMethod implements ITestNGMethod , IInvocationStatus {
38+ public abstract class BaseTestMethod
39+ implements ITestNGMethod , IInvocationStatus , IInstanceIdentity {
3940
4041 private static final Pattern SPACE_SEPARATOR_PATTERN = Pattern .compile (" +" );
4142
Original file line number Diff line number Diff line change 1+ package org .testng .internal ;
2+
3+ import java .util .Optional ;
4+ import java .util .UUID ;
5+ import org .testng .log4testng .Logger ;
6+
7+ public interface IInstanceIdentity {
8+
9+ /**
10+ * @return - A <code>{@link UUID}</code> that represents a unique id which is associated with
11+ * every test class object.
12+ */
13+ UUID getInstanceId ();
14+
15+ String fmt = "[Attention] %s does not implement %s. Generating an on the fly UUID." ;
16+ Logger logger = Logger .getLogger (IInstanceIdentity .class );
17+
18+ static UUID getInstanceId (Object object ) {
19+ if (object instanceof IInstanceIdentity ) {
20+ return ((IInstanceIdentity ) object ).getInstanceId ();
21+ }
22+ if (logger .isDebugEnabled ()) {
23+ String className =
24+ Optional .ofNullable (object ).map (it -> it .getClass ().getName ()).orElse ("[Unknown Class]" );
25+ String msg = String .format (fmt , className , IInstanceIdentity .class .getName ());
26+ Logger .getLogger (IInstanceIdentity .class ).debug (msg );
27+ }
28+ return UUID .randomUUID ();
29+ }
30+ }
Original file line number Diff line number Diff line change 1717 * generates a unique hashcode that is different from the original {@link ITestNGMethod} instance
1818 * that it wraps.
1919 */
20- public class WrappedTestNGMethod implements ITestNGMethod {
20+ public class WrappedTestNGMethod implements ITestNGMethod , IInstanceIdentity {
2121 private final ITestNGMethod testNGMethod ;
2222 private final int multiplicationFactor = new Random ().nextInt ();
2323
@@ -367,7 +367,7 @@ public String getQualifiedName() {
367367
368368 @ Override
369369 public UUID getInstanceId () {
370- return testNGMethod .getInstanceId ();
370+ return IInstanceIdentity .getInstanceId (testNGMethod );
371371 }
372372
373373 @ Override
Original file line number Diff line number Diff line change @@ -123,7 +123,7 @@ && doesTaskHavePreRequisites()
123123
124124 for (IMethodInstance testMethodInstance : m_methodInstances ) {
125125 ITestNGMethod testMethod = testMethodInstance .getMethod ();
126- UUID key = Objects .requireNonNull (testMethod .getInstanceId ());
126+ UUID key = Objects .requireNonNull (IInstanceIdentity .getInstanceId (testMethod ));
127127 if (canInvokeBeforeClassMethods ()) {
128128 try (KeyAwareAutoCloseableLock .AutoReleasable ignored = lock .lockForObject (key )) {
129129 invokeBeforeClassMethods (testMethod .getTestClass (), testMethodInstance );
Original file line number Diff line number Diff line change 1515import org .testng .annotations .DataProvider ;
1616import org .testng .annotations .Factory ;
1717import org .testng .annotations .Test ;
18+ import org .testng .internal .IInstanceIdentity ;
1819
1920public class FactoryTestCase {
2021
@@ -60,7 +61,7 @@ private static void record() {
6061 ITestResult itr = Reporter .getCurrentTestResult ();
6162 ITestNGMethod itm = itr .getMethod ();
6263 objectMap
63- .computeIfAbsent (itm .getInstanceId (), k -> ConcurrentHashMap .newKeySet ())
64+ .computeIfAbsent (IInstanceIdentity .getInstanceId (itm ), k -> ConcurrentHashMap .newKeySet ())
6465 .add (itm .getInstance ());
6566 }
6667}
Original file line number Diff line number Diff line change 1313import org .testng .annotations .BeforeMethod ;
1414import org .testng .annotations .DataProvider ;
1515import org .testng .annotations .Test ;
16+ import org .testng .internal .IInstanceIdentity ;
1617
1718public class SampleTestCase {
1819 public static Map <UUID , Set <Object >> objectMap = new ConcurrentHashMap <>();
@@ -61,7 +62,7 @@ private static void record() {
6162 ITestResult itr = Reporter .getCurrentTestResult ();
6263 ITestNGMethod itm = itr .getMethod ();
6364 objectMap
64- .computeIfAbsent (itm .getInstanceId (), k -> ConcurrentHashMap .newKeySet ())
65+ .computeIfAbsent (IInstanceIdentity .getInstanceId (itm ), k -> ConcurrentHashMap .newKeySet ())
6566 .add (itm .getInstance ());
6667 }
6768}
You can’t perform that action at this time.
0 commit comments