Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write XML report even when there are no tests
Issue: #86
- Loading branch information
1 parent
59a95bb
commit 932c765
Showing
6 changed files
with
118 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
junit-tests/src/test/java/org/junit/gen5/console/tasks/XmlReportAssertions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.console.tasks; | ||
|
||
import java.io.StringReader; | ||
import java.net.URL; | ||
|
||
import javax.xml.XMLConstants; | ||
import javax.xml.transform.stream.StreamSource; | ||
import javax.xml.validation.SchemaFactory; | ||
import javax.xml.validation.Validator; | ||
|
||
import org.opentest4j.AssertionFailedError; | ||
import org.xml.sax.SAXException; | ||
|
||
public class XmlReportAssertions { | ||
|
||
private static Validator schemaValidator; | ||
|
||
static String ensureValidAccordingToJenkinsSchema(String content) throws Exception { | ||
try { | ||
getSchemaValidator().validate(new StreamSource(new StringReader(content))); | ||
return content; | ||
} | ||
catch (SAXException e) { | ||
throw new AssertionFailedError("Invalid XML document: " + content, e); | ||
} | ||
} | ||
|
||
private static Validator getSchemaValidator() throws SAXException { | ||
if (schemaValidator == null) { | ||
URL schemaFile = XmlReportsWritingListener.class.getResource("/jenkins-junit.xsd"); | ||
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); | ||
schemaValidator = schemaFactory.newSchema(schemaFile).newValidator(); | ||
} | ||
return schemaValidator; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
junit-tests/src/test/java/org/junit/gen5/console/tasks/XmlReportWriterTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v1.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
|
||
package org.junit.gen5.console.tasks; | ||
|
||
import static java.util.Collections.singleton; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.gen5.commons.util.CollectionUtils.getOnlyElement; | ||
import static org.junit.gen5.console.tasks.XmlReportAssertions.ensureValidAccordingToJenkinsSchema; | ||
|
||
import java.io.StringWriter; | ||
import java.time.Clock; | ||
|
||
import org.junit.gen5.api.Test; | ||
import org.junit.gen5.engine.support.descriptor.EngineDescriptor; | ||
import org.junit.gen5.launcher.TestPlan; | ||
|
||
class XmlReportWriterTests { | ||
|
||
@Test | ||
void writesFileWithoutTestcaseElementsWithoutAnyTests() throws Exception { | ||
TestPlan testPlan = TestPlan.from(singleton(new EngineDescriptor("emptyEngine", "Empty Engine"))); | ||
XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone()); | ||
|
||
StringWriter out = new StringWriter(); | ||
new XmlReportWriter(reportData).writeXmlReport(getOnlyElement(testPlan.getRoots()), out); | ||
|
||
String content = ensureValidAccordingToJenkinsSchema(out.toString()); | ||
//@formatter:off | ||
assertThat(content) | ||
.containsSequence( | ||
"<testsuite name=\"Empty Engine\" tests=\"0\"", | ||
"</testsuite>") | ||
.doesNotContain("<testcase"); | ||
//@formatter:on | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters