-
Notifications
You must be signed in to change notification settings - Fork 1k
/
YamlTest.java
106 lines (93 loc) · 3.95 KB
/
YamlTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package test.yaml;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.internal.Yaml;
import org.testng.internal.YamlParser;
import org.testng.xml.SuiteXmlParser;
import org.testng.xml.XmlSuite;
import org.testng.xml.internal.Parser;
import test.SimpleBaseTest;
public class YamlTest extends SimpleBaseTest {
public static final String CLASS_NOT_FOUND_MESSAGE = "Cannot find class in classpath";
@DataProvider
public Object[][] dp() {
return new Object[][] {
new Object[] {"a1"}, new Object[] {"a2"}, new Object[] {"a3"}, new Object[] {"a4"},
};
}
@Test(
description =
"Validate that the YamlParser accepts yaml files with a .yaml or a .yml file extension, but not other file types.")
public void accept() {
YamlParser yamlParser = new YamlParser();
assertThat(yamlParser.accept("TestSuite.yml")).isTrue();
assertThat(yamlParser.accept("TestSuite.yaml")).isTrue();
assertThat(yamlParser.accept("TestSuite.xml")).isFalse();
}
@Test(dataProvider = "dp")
public void compareFiles(String name) throws IOException {
Collection<XmlSuite> s1 =
new Parser(getPathToResource("yaml" + File.separator + name + ".yaml")).parse();
Collection<XmlSuite> s2 =
new Parser(getPathToResource("yaml" + File.separator + name + ".xml")).parse();
Assert.assertEquals(s1, s2);
}
@Test(description = "GITHUB-1787")
public void testParameterInclusion() throws IOException {
SuiteXmlParser parser = new SuiteXmlParser();
String file = "src/test/resources/yaml/1787.xml";
XmlSuite xmlSuite = parser.parse(file, new FileInputStream(file), false);
StringBuilder yaml = org.testng.internal.Yaml.toYaml(xmlSuite);
Matcher m = Pattern.compile("parameters:").matcher(yaml.toString());
int count = 0;
while (m.find()) {
count++;
}
assertThat(count).isEqualTo(5);
File newSuite = File.createTempFile("suite", ".xml");
newSuite.deleteOnExit();
Files.write(newSuite.toPath(), yaml.toString().getBytes(StandardCharsets.UTF_8));
assertThat(parser.parse(newSuite.getAbsolutePath(), new FileInputStream(file), false))
.isEqualTo(xmlSuite);
}
@Test(description = "GITHUB-2078")
public void testXmlDependencyGroups() throws IOException {
String actualXmlFile = "src/test/resources/yaml/2078.xml";
XmlSuite actualXmlSuite =
new SuiteXmlParser().parse(actualXmlFile, new FileInputStream(actualXmlFile), false);
String expectedYamlFile = "src/test/resources/yaml/2078.yaml";
String expectedYaml =
new String(
java.nio.file.Files.readAllBytes(Paths.get(expectedYamlFile)), StandardCharsets.UTF_8);
assertThat(Yaml.toYaml(actualXmlSuite).toString()).isEqualToNormalizingNewlines(expectedYaml);
}
@Test(description = "GITHUB-2689")
public void testLoadClassesFlag() throws IOException {
YamlParser yamlParser = new YamlParser();
String yamlSuiteFile = "src/test/resources/yaml/suiteWithNonExistentTest.yaml";
try {
yamlParser.parse(yamlSuiteFile, new FileInputStream(yamlSuiteFile), false);
} catch (Throwable throwable) {
Throwable rootCause = getRootCause(throwable);
String rootCauseMessage = rootCause.getMessage();
if (rootCauseMessage.contains(CLASS_NOT_FOUND_MESSAGE)) {
throw new AssertionError("TestNG shouldn't attempt to load test class", throwable);
}
throw new AssertionError("Yaml parser failed to parse suite", throwable);
}
}
private Throwable getRootCause(Throwable throwable) {
return throwable.getCause() != null ? getRootCause(throwable.getCause()) : throwable;
}
}