/*
 * 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
	}

}

@@ -712,16 +712,65 @@
 i.Test;%0A
+import org.junit.gen5.engine.TestDescriptorStub;%0A
 import o
@@ -827,16 +827,55 @@
 riptor;%0A
+import org.junit.gen5.launcher.TestId;%0A
 import o
@@ -961,12 +961,25 @@
 ites
-File
+TestsuiteElements
 With
@@ -1617,10 +1617,960 @@
 :on%0A%09%7D%0A%0A
+%09@Test%0A%09void writesEmptySkippedElementForSkippedTestWithoutReason() throws Exception %7B%0A%09%09EngineDescriptor engineDescriptor = new EngineDescriptor(%22engine%22, %22Engine%22);%0A%09%09engineDescriptor.addChild(new TestDescriptorStub(%22skippedTest%22));%0A%0A%09%09TestPlan testPlan = TestPlan.from(singleton(engineDescriptor));%0A%09%09XmlReportData reportData = new XmlReportData(testPlan, Clock.systemDefaultZone());%0A%09%09reportData.markSkipped(testPlan.getTestIdentifier(new TestId(%22skippedTest%22)), null);%0A%0A%09%09StringWriter out = new StringWriter();%0A%09%09new XmlReportWriter(reportData).writeXmlReport(getOnlyElement(testPlan.getRoots()), out);%0A%0A%09%09String content = ensureValidAccordingToJenkinsSchema(out.toString());%0A%09%09//@formatter:off%0A%09%09assertThat(content)%0A%09%09%09.containsSequence(%0A%09%09%09%09%22%3Ctestsuite name=%5C%22Engine%5C%22 tests=%5C%221%5C%22 skipped=%5C%221%5C%22 failures=%5C%220%5C%22 errors=%5C%220%5C%22%22,%0A%09%09%09%09%22%3Ctestcase name=%5C%22skippedTest%5C%22%22,%0A%09%09%09%09%22%3Cskipped/%3E%22,%0A%09%09%09%09%22%3C/testcase%3E%22,%0A%09%09%09%09%22%3C/testsuite%3E%22);%0A%09%09//@formatter:on%0A%09%7D%0A%0A
 %7D%0A

Write empty skipped element for skipped tests without reason