-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Yaml parser: implement loadClasses flag #2688
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import org.testng.TestNGException; | ||
import org.testng.util.Strings; | ||
import org.testng.xml.XmlClass; | ||
import org.testng.xml.XmlInclude; | ||
|
@@ -27,7 +28,8 @@ public final class Yaml { | |
|
||
private Yaml() {} | ||
|
||
public static XmlSuite parse(String filePath, InputStream is) throws FileNotFoundException { | ||
public static XmlSuite parse(String filePath, InputStream is, boolean loadClasses) | ||
throws FileNotFoundException { | ||
Constructor constructor = new TestNGConstructor(XmlSuite.class); | ||
{ | ||
TypeDescription suiteDescription = new TypeDescription(XmlSuite.class); | ||
|
@@ -46,6 +48,9 @@ public static XmlSuite parse(String filePath, InputStream is) throws FileNotFoun | |
constructor.addTypeDescription(testDescription); | ||
} | ||
|
||
TypeDescription xmlClassDescription = new XmlClassTypeDescriptor(loadClasses); | ||
constructor.addTypeDescription(xmlClassDescription); | ||
|
||
org.yaml.snakeyaml.Yaml y = new org.yaml.snakeyaml.Yaml(constructor); | ||
if (is == null) { | ||
is = new FileInputStream(new File(filePath)); | ||
|
@@ -347,4 +352,42 @@ public Object construct(Node node) { | |
} | ||
} | ||
} | ||
|
||
private static class XmlClassTypeDescriptor extends TypeDescription { | ||
|
||
private final boolean loadClasses; | ||
|
||
public XmlClassTypeDescriptor(boolean loadClasses) { | ||
super(XmlClass.class); | ||
this.loadClasses = loadClasses; | ||
} | ||
|
||
@Override | ||
public Object newInstance(Node node) { | ||
String className; | ||
|
||
try { | ||
java.lang.reflect.Constructor<?> c = | ||
XmlClass.class.getDeclaredConstructor(String.class, boolean.class); | ||
c.setAccessible(true); | ||
if (node instanceof MappingNode) { | ||
Node valueNode = | ||
((MappingNode) node) | ||
.getValue().stream() | ||
.filter( | ||
nodeTuple -> | ||
((ScalarNode) nodeTuple.getKeyNode()).getValue().equals("name")) | ||
.findFirst() | ||
.orElseThrow(RuntimeException::new) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we consider throwing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
.getValueNode(); | ||
className = ((ScalarNode) valueNode).getValue(); | ||
} else { | ||
className = ((ScalarNode) node).getValue(); | ||
} | ||
return c.newInstance(className, loadClasses); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we please use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} catch (Exception e) { | ||
throw new TestNGException("Failed to instantiate class", e); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name: My_Suite | ||
tests: | ||
- name: My_test | ||
classes: | ||
- this.class.does.not.Exists |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for traceability, it would be good if you could please help do the following:
CHANGES.txt
description
attribute of@Test
test, so that we know that the test belongs to a specific defect.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done