7
7
import java .util .List ;
8
8
import java .util .Map ;
9
9
import java .util .function .Consumer ;
10
+ import org .testng .TestNGException ;
11
+ import org .testng .internal .objects .InstanceCreator ;
10
12
import org .testng .util .Strings ;
11
13
import org .testng .xml .XmlClass ;
12
14
import org .testng .xml .XmlInclude ;
@@ -27,7 +29,8 @@ public final class Yaml {
27
29
28
30
private Yaml () {}
29
31
30
- public static XmlSuite parse (String filePath , InputStream is ) throws FileNotFoundException {
32
+ public static XmlSuite parse (String filePath , InputStream is , boolean loadClasses )
33
+ throws FileNotFoundException {
31
34
Constructor constructor = new TestNGConstructor (XmlSuite .class );
32
35
{
33
36
TypeDescription suiteDescription = new TypeDescription (XmlSuite .class );
@@ -46,6 +49,9 @@ public static XmlSuite parse(String filePath, InputStream is) throws FileNotFoun
46
49
constructor .addTypeDescription (testDescription );
47
50
}
48
51
52
+ TypeDescription xmlClassDescription = new XmlClassTypeDescriptor (loadClasses );
53
+ constructor .addTypeDescription (xmlClassDescription );
54
+
49
55
org .yaml .snakeyaml .Yaml y = new org .yaml .snakeyaml .Yaml (constructor );
50
56
if (is == null ) {
51
57
is = new FileInputStream (new File (filePath ));
@@ -347,4 +353,42 @@ public Object construct(Node node) {
347
353
}
348
354
}
349
355
}
356
+
357
+ private static class XmlClassTypeDescriptor extends TypeDescription {
358
+
359
+ private final boolean loadClasses ;
360
+
361
+ public XmlClassTypeDescriptor (boolean loadClasses ) {
362
+ super (XmlClass .class );
363
+ this .loadClasses = loadClasses ;
364
+ }
365
+
366
+ @ Override
367
+ public Object newInstance (Node node ) {
368
+ String className ;
369
+
370
+ try {
371
+ java .lang .reflect .Constructor <?> c =
372
+ XmlClass .class .getDeclaredConstructor (String .class , boolean .class );
373
+ c .setAccessible (true );
374
+ if (node instanceof MappingNode ) {
375
+ Node valueNode =
376
+ ((MappingNode ) node )
377
+ .getValue ().stream ()
378
+ .filter (
379
+ nodeTuple ->
380
+ ((ScalarNode ) nodeTuple .getKeyNode ()).getValue ().equals ("name" ))
381
+ .findFirst ()
382
+ .orElseThrow (() -> new TestNGException ("Node 'name' not found" ))
383
+ .getValueNode ();
384
+ className = ((ScalarNode ) valueNode ).getValue ();
385
+ } else {
386
+ className = ((ScalarNode ) node ).getValue ();
387
+ }
388
+ return InstanceCreator .newInstance (c , className , loadClasses );
389
+ } catch (Exception e ) {
390
+ throw new TestNGException ("Failed to instantiate class" , e );
391
+ }
392
+ }
393
+ }
350
394
}
0 commit comments