From 35a6410281e5f2af69f084864d30d777b1503a6f Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Mon, 7 Aug 2017 17:14:20 +0300 Subject: [PATCH 1/2] add support for testsuites root element --- plugins/junit-xml-plugin/build.gradle | 3 +- .../allure/junitxml/JunitXmlPlugin.java | 28 ++++++++++++------- .../allure/junitxml/JunitXmlPluginTest.java | 20 +++++++++++++ .../test/resources/junitdata/testsuites.xml | 18 ++++++++++++ 4 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 plugins/junit-xml-plugin/src/test/resources/junitdata/testsuites.xml diff --git a/plugins/junit-xml-plugin/build.gradle b/plugins/junit-xml-plugin/build.gradle index ceefb7873..0f58554c6 100644 --- a/plugins/junit-xml-plugin/build.gradle +++ b/plugins/junit-xml-plugin/build.gradle @@ -10,7 +10,8 @@ dependencies { testCompile('junit:junit') testCompile('org.assertj:assertj-core') testCompile('org.mockito:mockito-core') - testCompile("org.slf4j:slf4j-simple") + testCompile('org.slf4j:slf4j-simple') + testCompile('io.qameta.allure:allure-java-commons') } configurations.archives.artifacts.removeAll { it.archiveTask.is jar } diff --git a/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java b/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java index c7f7205f9..6ef99128b 100644 --- a/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java +++ b/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java @@ -53,6 +53,7 @@ public class JunitXmlPlugin implements Reader { private static final BigDecimal MULTIPLICAND = new BigDecimal(1000); private static final String TEST_SUITE_ELEMENT_NAME = "testsuite"; + private static final String TEST_SUITES_ELEMENT_NAME = "testsuites"; private static final String TEST_CASE_ELEMENT_NAME = "testcase"; private static final String CLASS_NAME_ATTRIBUTE_NAME = "classname"; private static final String NAME_ATTRIBUTE_NAME = "name"; @@ -77,25 +78,32 @@ public class JunitXmlPlugin implements Reader { @Override public void readResults(final Configuration configuration, final ResultsVisitor visitor, final Path directory) { final RandomUidContext context = configuration.requireContext(RandomUidContext.class); - listResults(directory).forEach(result -> parseTestSuite(directory, result, context, visitor)); + listResults(directory).forEach(result -> parseRootElement(directory, result, context, visitor)); } - private void parseTestSuite(final Path resultsDirectory, final Path parsedFile, - final RandomUidContext context, final ResultsVisitor visitor) { + private void parseRootElement(final Path resultsDirectory, final Path parsedFile, + final RandomUidContext context, final ResultsVisitor visitor) { try { LOGGER.debug("Parsing file {}", parsedFile); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); - final XmlElement testSuiteElement = new XmlElement(builder.parse(parsedFile.toFile()).getDocumentElement()); - final String elementName = testSuiteElement.getName(); - if (!TEST_SUITE_ELEMENT_NAME.equals(elementName)) { - LOGGER.debug("File {} is not a valid JUnit xml. Unknown root element {}", parsedFile, elementName); + final XmlElement rootElement = new XmlElement(builder.parse(parsedFile.toFile()).getDocumentElement()); + final String elementName = rootElement.getName(); + + if (TEST_SUITE_ELEMENT_NAME.equals(elementName)) { + rootElement.get(TEST_CASE_ELEMENT_NAME) + .forEach(element -> parseTestCase(element, resultsDirectory, parsedFile, context, visitor)); return; } - - testSuiteElement.get(TEST_CASE_ELEMENT_NAME) - .forEach(element -> parseTestCase(element, resultsDirectory, parsedFile, context, visitor)); + if (TEST_SUITES_ELEMENT_NAME.equals(elementName)) { + rootElement.get("testsuite").stream() + .map(testSuiteElement -> testSuiteElement.get(TEST_CASE_ELEMENT_NAME)) + .flatMap(Collection::stream) + .forEach(element -> parseTestCase(element, resultsDirectory, parsedFile, context, visitor)); + return; + } + LOGGER.debug("File {} is not a valid JUnit xml. Unknown root element {}", parsedFile, elementName); } catch (SAXException | ParserConfigurationException | IOException e) { LOGGER.error("Could not parse file {}: {}", parsedFile, e); } diff --git a/plugins/junit-xml-plugin/src/test/java/io/qameta/allure/junitxml/JunitXmlPluginTest.java b/plugins/junit-xml-plugin/src/test/java/io/qameta/allure/junitxml/JunitXmlPluginTest.java index d91450034..39d7a25ad 100644 --- a/plugins/junit-xml-plugin/src/test/java/io/qameta/allure/junitxml/JunitXmlPluginTest.java +++ b/plugins/junit-xml-plugin/src/test/java/io/qameta/allure/junitxml/JunitXmlPluginTest.java @@ -1,5 +1,6 @@ package io.qameta.allure.junitxml; +import io.qameta.allure.Issue; import io.qameta.allure.context.RandomUidContext; import io.qameta.allure.core.Configuration; import io.qameta.allure.core.ResultsVisitor; @@ -198,6 +199,25 @@ public void shouldReadCdataMessage() throws Exception { } + @Issue("532") + @Test + public void shouldParseSuitesTag() throws Exception { + process( + "junitdata/testsuites.xml", "TEST-test.SampleTest.xml" + ); + + final ArgumentCaptor captor = ArgumentCaptor.forClass(TestResult.class); + verify(visitor, times(3)).visitTestResult(captor.capture()); + + assertThat(captor.getAllValues()) + .extracting(TestResult::getName) + .containsExactlyInAnyOrder( + "should default path to an empty string", + "should default consolidate to true", + "should default useDotNotation to true" + ); + } + private void process(String... strings) throws IOException { Path resultsDirectory = folder.newFolder().toPath(); Iterator iterator = Arrays.asList(strings).iterator(); diff --git a/plugins/junit-xml-plugin/src/test/resources/junitdata/testsuites.xml b/plugins/junit-xml-plugin/src/test/resources/junitdata/testsuites.xml new file mode 100644 index 000000000..19fdc5088 --- /dev/null +++ b/plugins/junit-xml-plugin/src/test/resources/junitdata/testsuites.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + Assertion failed + + + + + + + \ No newline at end of file From c5c6e5fcb4f1f5f87677faabb9af980bb221cc7e Mon Sep 17 00:00:00 2001 From: Dmitry Baev Date: Mon, 7 Aug 2017 17:24:10 +0300 Subject: [PATCH 2/2] rft --- .../src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java b/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java index 6ef99128b..638b24df1 100644 --- a/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java +++ b/plugins/junit-xml-plugin/src/main/java/io/qameta/allure/junitxml/JunitXmlPlugin.java @@ -97,7 +97,7 @@ private void parseRootElement(final Path resultsDirectory, final Path parsedFile return; } if (TEST_SUITES_ELEMENT_NAME.equals(elementName)) { - rootElement.get("testsuite").stream() + rootElement.get(TEST_SUITE_ELEMENT_NAME).stream() .map(testSuiteElement -> testSuiteElement.get(TEST_CASE_ELEMENT_NAME)) .flatMap(Collection::stream) .forEach(element -> parseTestCase(element, resultsDirectory, parsedFile, context, visitor));