File tree 6 files changed +73
-2
lines changed
test/java/test/parameters
6 files changed +73
-2
lines changed Original file line number Diff line number Diff line change 1
1
Current
2
+ Fixed: GITHUB-1994: Prevent duplication of XmlTest objects when its used as a parameter for Native Injection (Krishnan Mahadevan)
2
3
Fixed: GITHUB-165: @AfterGroups is not executed when group member fails or is skipped (Krishnan Mahadevan)
3
4
Fixed: GITHUB-118: @BeforeGroups only called if group is specified explicitly (Krishnan Mahadevan)
4
5
Fixed: GITHUB-182: Inherited test methods do not get expected group behavior (Krishnan Mahadevan)
Original file line number Diff line number Diff line change
1
+ package org .testng .annotations ;
2
+
3
+
4
+ import java .lang .annotation .ElementType ;
5
+ import java .lang .annotation .Retention ;
6
+ import java .lang .annotation .Target ;
7
+
8
+ /**
9
+ * Marker interface which when used on a type will ensure that TestNG does not clone the object but
10
+ * instead uses it as is when TestNG resorts to dependency injection.
11
+ */
12
+ @ Retention (java .lang .annotation .RetentionPolicy .RUNTIME )
13
+ @ Target ({ElementType .TYPE })
14
+ public @interface SkipCloning {
15
+
16
+ }
Original file line number Diff line number Diff line change 12
12
import org .testng .Reporter ;
13
13
import org .testng .TestNGException ;
14
14
import org .testng .TestRunner ;
15
+ import org .testng .annotations .SkipCloning ;
15
16
import org .testng .collections .Lists ;
16
17
import org .testng .collections .Objects ;
17
18
@@ -276,7 +277,7 @@ public void setParameters(Object[] parameters) {
276
277
m_parameters = new Object [parameters .length ];
277
278
for (int i = 0 ; i < parameters .length ; i ++) {
278
279
// Copy parameter if possible because user may change it later
279
- if (parameters [i ] instanceof Cloneable ) {
280
+ if (canAttemptCloning ( parameters [i ]) ) {
280
281
try {
281
282
Method clone = parameters [i ].getClass ().getDeclaredMethod ("clone" );
282
283
m_parameters [i ] = clone .invoke (parameters [i ]);
@@ -292,6 +293,17 @@ public void setParameters(Object[] parameters) {
292
293
}
293
294
}
294
295
296
+ private static boolean canAttemptCloning (Object parameter ) {
297
+ if (parameter == null ) {
298
+ return false ;
299
+ }
300
+ SkipCloning skipCloning = parameter .getClass ().getAnnotation (SkipCloning .class );
301
+ if (skipCloning != null ) {
302
+ return false ;
303
+ }
304
+ return parameter instanceof Cloneable ;
305
+ }
306
+
295
307
@ Override
296
308
public Object getInstance () {
297
309
return IParameterInfo .embeddedInstance (this .m_method .getInstance ());
Original file line number Diff line number Diff line change 9
9
10
10
import org .testng .TestNG ;
11
11
import org .testng .TestNGException ;
12
+ import org .testng .annotations .SkipCloning ;
12
13
import org .testng .collections .Lists ;
13
14
import org .testng .collections .Maps ;
14
15
import org .testng .xml .dom .ParentSetter ;
15
16
16
17
import static org .testng .xml .XmlSuite .ParallelMode .skipDeprecatedValues ;
17
18
18
19
/** This class describes the tag <test> in testng.xml. */
20
+ @ SkipCloning
19
21
public class XmlTest implements Cloneable {
20
22
21
23
public static final int DEFAULT_TIMEOUT_MS = Integer .MAX_VALUE ;
Original file line number Diff line number Diff line change 1
1
package test .parameters ;
2
2
3
- import org .testng .ITestNGListener ;
4
3
import org .testng .ITestResult ;
5
4
import org .testng .TestListenerAdapter ;
6
5
import org .testng .TestNG ;
15
14
import java .util .HashMap ;
16
15
import java .util .List ;
17
16
import java .util .Map ;
17
+ import test .parameters .issue1994 .TestclassSample ;
18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
@@ -130,4 +130,12 @@ public void testNativeInjectionAndParamsInjection() {
130
130
testng .run ();
131
131
assertThat (listener .getPassedTests ().isEmpty ()).isFalse ();
132
132
}
133
+
134
+ @ Test (description = "GITHUB-1994" )
135
+ public void testToEnsureNativeInjectionDoesnotResortToCloning () {
136
+ XmlSuite xmlsuite = createXmlSuite ("suite" , "test" , TestclassSample .class );
137
+ TestNG testng = create (xmlsuite );
138
+ testng .run ();
139
+ assertThat (TestclassSample .count ).isEqualTo (1 );
140
+ }
133
141
}
Original file line number Diff line number Diff line change
1
+ package test .parameters .issue1994 ;
2
+
3
+ import org .testng .ISuite ;
4
+ import org .testng .ISuiteListener ;
5
+ import org .testng .annotations .BeforeClass ;
6
+ import org .testng .annotations .Listeners ;
7
+ import org .testng .annotations .Test ;
8
+ import org .testng .xml .XmlTest ;
9
+
10
+ @ Listeners (TestclassSample .class )
11
+ public class TestclassSample implements ISuiteListener {
12
+
13
+ public static int count = 0 ;
14
+
15
+ @ BeforeClass
16
+ public void beforeClass (XmlTest xmlTest ) {
17
+ }
18
+
19
+ @ Test
20
+ public void testMethod () {
21
+ }
22
+
23
+ @ Override
24
+ public void onFinish (ISuite suite ) {
25
+ setCount (suite .getXmlSuite ().getTests ().size ());
26
+ }
27
+
28
+ private static void setCount (int count ) {
29
+ TestclassSample .count = count ;
30
+ }
31
+
32
+ }
You can’t perform that action at this time.
0 commit comments