This is an automated email from the ASF dual-hosted git repository. azotcsit pushed a commit to branch cassandra-16630_junit5 in repository https://gitbox.apache.org/repos/asf/cassandra.git
commit 31d8eea6bb9a5cb3829ccfc3de632c989e900409 Author: Aleksei Zotov <[email protected]> AuthorDate: Tue Nov 16 00:55:45 2021 +0400 CASSANDRA-16630. Updated Ant JUnit classes. --- .../unit/org/apache/cassandra/JStackJUnitTask.java | 25 +--- .../junitlauncher/LegacyBriefResultFormatter.java | 34 ------ .../AbstractJUnitResultFormatter.java | 90 +++++++++++--- .../junitlauncher2/LegacyBriefResultFormatter.java | 36 ++++++ .../LegacyPlainResultFormatter.java | 52 ++++---- .../LegacyXmlResultFormatter.java | 135 ++++++++------------- 6 files changed, 185 insertions(+), 187 deletions(-) diff --git a/test/unit/org/apache/cassandra/JStackJUnitTask.java b/test/unit/org/apache/cassandra/JStackJUnitTask.java index 69480a6..f8f806c 100644 --- a/test/unit/org/apache/cassandra/JStackJUnitTask.java +++ b/test/unit/org/apache/cassandra/JStackJUnitTask.java @@ -22,28 +22,14 @@ import java.io.IOException; import java.io.InputStreamReader; import java.lang.reflect.Field; -import org.apache.tools.ant.BuildException; import org.apache.tools.ant.taskdefs.ExecuteWatchdog; -import org.apache.tools.ant.taskdefs.optional.junit.JUnitTask; +import org.apache.tools.ant.taskdefs.optional.junitlauncher.confined.JUnitLauncherTask; import org.apache.tools.ant.util.Watchdog; -public class JStackJUnitTask extends JUnitTask +public class JStackJUnitTask extends JUnitLauncherTask { - private Integer timeout; - - public JStackJUnitTask() throws Exception - { - } - - @Override - public void setTimeout(Integer timeout) - { - this.timeout = timeout; - super.setTimeout(timeout); - } - @Override - public ExecuteWatchdog createWatchdog() throws BuildException + protected ExecuteWatchdog createExecuteWatchdog(long timeout) { return new JStackWatchDog(timeout); } @@ -57,11 +43,6 @@ public class JStackJUnitTask extends JUnitTask super(timeout); } - public JStackWatchDog(int timeout) - { - super(timeout); - } - @Override public synchronized void start(Process process) { diff --git a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyBriefResultFormatter.java b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyBriefResultFormatter.java deleted file mode 100644 index 7debbf0..0000000 --- a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyBriefResultFormatter.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -package org.apache.tools.ant.taskdefs.optional.junitlauncher; - -import org.junit.platform.engine.TestExecutionResult; -import org.junit.platform.launcher.TestIdentifier; - -/** - * A {@link TestResultFormatter} which prints a brief statistic for tests that have - * failed, aborted or skipped - */ -class LegacyBriefResultFormatter extends LegacyPlainResultFormatter implements TestResultFormatter { - - @Override - protected boolean shouldReportExecutionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) { - final TestExecutionResult.Status resultStatus = testExecutionResult.getStatus(); - return resultStatus == TestExecutionResult.Status.ABORTED || resultStatus == TestExecutionResult.Status.FAILED; - } -} diff --git a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/AbstractJUnitResultFormatter.java b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/AbstractJUnitResultFormatter.java similarity index 78% rename from test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/AbstractJUnitResultFormatter.java rename to test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/AbstractJUnitResultFormatter.java index 221aadb..456a2cd 100644 --- a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/AbstractJUnitResultFormatter.java +++ b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/AbstractJUnitResultFormatter.java @@ -1,23 +1,25 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package org.apache.tools.ant.taskdefs.optional.junitlauncher; +package org.apache.tools.ant.taskdefs.optional.junitlauncher2; import org.apache.tools.ant.Project; +import org.apache.tools.ant.taskdefs.optional.junitlauncher.TestExecutionContext; +import org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter; import org.apache.tools.ant.util.FileUtils; import org.junit.platform.engine.TestSource; import org.junit.platform.engine.support.descriptor.ClassSource; @@ -39,6 +41,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.Objects; import java.util.Optional; +import java.util.Set; /** * Contains some common behaviour that's used by our internal {@link TestResultFormatter}s @@ -46,11 +49,18 @@ import java.util.Optional; abstract class AbstractJUnitResultFormatter implements TestResultFormatter { protected TestExecutionContext context; + protected TestPlan testPlan; + protected boolean useLegacyReportingName = true; private SysOutErrContentStore sysOutStore; private SysOutErrContentStore sysErrStore; @Override + public void testPlanExecutionStarted(final TestPlan testPlan) { + this.testPlan = testPlan; + } + + @Override public void sysOutAvailable(final byte[] data) { if (this.sysOutStore == null) { this.sysOutStore = new SysOutErrContentStore(context, true); @@ -83,7 +93,7 @@ abstract class AbstractJUnitResultFormatter implements TestResultFormatter { * @return Returns true if there's any stdout data, that was generated during the * tests, is available for use. Else returns false. */ - boolean hasSysOut() { + protected boolean hasSysOut() { return this.sysOutStore != null && this.sysOutStore.hasData(); } @@ -91,7 +101,7 @@ abstract class AbstractJUnitResultFormatter implements TestResultFormatter { * @return Returns true if there's any stderr data, that was generated during the * tests, is available for use. Else returns false. */ - boolean hasSysErr() { + protected boolean hasSysErr() { return this.sysErrStore != null && this.sysErrStore.hasData(); } @@ -102,7 +112,7 @@ abstract class AbstractJUnitResultFormatter implements TestResultFormatter { * be called * @throws IOException If there's any I/O problem while creating the {@link Reader} */ - Reader getSysOutReader() throws IOException { + protected Reader getSysOutReader() throws IOException { return this.sysOutStore.getReader(); } @@ -113,7 +123,7 @@ abstract class AbstractJUnitResultFormatter implements TestResultFormatter { * be called * @throws IOException If there's any I/O problem while creating the {@link Reader} */ - Reader getSysErrReader() throws IOException { + protected Reader getSysErrReader() throws IOException { return this.sysErrStore.getReader(); } @@ -136,7 +146,7 @@ abstract class AbstractJUnitResultFormatter implements TestResultFormatter { * @param writer The {@link Writer} to use. Cannot be null. * @throws IOException If any I/O problem occurs during writing the data */ - void writeSysErr(final Writer writer) throws IOException { + protected void writeSysErr(final Writer writer) throws IOException { Objects.requireNonNull(writer, "Writer cannot be null"); this.writeFrom(this.sysErrStore, writer); } @@ -175,6 +185,16 @@ abstract class AbstractJUnitResultFormatter implements TestResultFormatter { } @Override + public void setUseLegacyReportingName(final boolean useLegacyReportingName) { + this.useLegacyReportingName = useLegacyReportingName; + } + + protected String determineTestName(TestIdentifier testId) { + return useLegacyReportingName ? testId.getLegacyReportingName() + : testId.getDisplayName(); + } + + @Override public void close() throws IOException { FileUtils.close(this.sysOutStore); FileUtils.close(this.sysErrStore); @@ -186,6 +206,38 @@ abstract class AbstractJUnitResultFormatter implements TestResultFormatter { + AbstractJUnitResultFormatter.this.getClass().getName(), t, Project.MSG_DEBUG)); } + protected String determineTestSuiteName() { + // this is really a hack to try and match the expectations of the XML report in JUnit4.x + // world. In JUnit5, the TestPlan doesn't have a name and a TestPlan (for which this is a + // listener) can have numerous tests within it + final Set<TestIdentifier> roots = testPlan.getRoots(); + if (roots.isEmpty()) { + return "UNKNOWN"; + } + for (final TestIdentifier root : roots) { + final Optional<ClassSource> classSource = findFirstClassSource(root); + if (classSource.isPresent()) { + return classSource.get().getClassName(); + } + } + return "UNKNOWN"; + } + + protected Optional<ClassSource> findFirstClassSource(final TestIdentifier root) { + if (root.getSource().isPresent()) { + final TestSource source = root.getSource().get(); + if (source instanceof ClassSource) { + return Optional.of((ClassSource) source); + } + } + for (final TestIdentifier child : testPlan.getChildren(root)) { + final Optional<ClassSource> classSource = findFirstClassSource(child); + if (classSource.isPresent()) { + return classSource; + } + } + return Optional.empty(); + } /* A "store" for sysout/syserr content that gets sent to the AbstractJUnitResultFormatter. diff --git a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyBriefResultFormatter.java b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyBriefResultFormatter.java new file mode 100644 index 0000000..fda75f6 --- /dev/null +++ b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyBriefResultFormatter.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tools.ant.taskdefs.optional.junitlauncher2; + +import org.junit.platform.engine.TestExecutionResult; +import org.junit.platform.launcher.TestIdentifier; + +import org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter; + +/** + * A {@link TestResultFormatter} which prints a brief statistic for tests that have + * failed, aborted or skipped + */ +public class LegacyBriefResultFormatter extends org.apache.tools.ant.taskdefs.optional.junitlauncher2.LegacyPlainResultFormatter implements TestResultFormatter { + + @Override + protected boolean shouldReportExecutionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) { + final TestExecutionResult.Status resultStatus = testExecutionResult.getStatus(); + return resultStatus == TestExecutionResult.Status.ABORTED || resultStatus == TestExecutionResult.Status.FAILED; + } +} diff --git a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyPlainResultFormatter.java similarity index 85% rename from test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java rename to test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyPlainResultFormatter.java index 7583d78..5c2c885 100644 --- a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyPlainResultFormatter.java +++ b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyPlainResultFormatter.java @@ -1,22 +1,22 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package org.apache.tools.ant.taskdefs.optional.junitlauncher; +package org.apache.tools.ant.taskdefs.optional.junitlauncher2; import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.reporting.ReportEntry; @@ -37,22 +37,17 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; +import org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter; + /** * A {@link TestResultFormatter} which prints a short statistic for each of the tests */ -class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements TestResultFormatter { +public class LegacyPlainResultFormatter extends org.apache.tools.ant.taskdefs.optional.junitlauncher2.AbstractJUnitResultFormatter implements TestResultFormatter { private OutputStream outputStream; private final Map<TestIdentifier, Stats> testIds = new ConcurrentHashMap<>(); - private TestPlan testPlan; private BufferedWriter writer; - private boolean useLegacyReportingName = true; - - @Override - public void testPlanExecutionStarted(final TestPlan testPlan) { - this.testPlan = testPlan; - } @Override public void testPlanExecutionFinished(final TestPlan testPlan) { @@ -63,7 +58,9 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements continue; } final Stats stats = entry.getValue(); - final StringBuilder sb = new StringBuilder("Tests run: ").append(stats.numTestsRun.get()); + // TODO: am I really required? + final StringBuilder sb = new StringBuilder("Testsuite: ").append(super.determineTestSuiteName()); + sb.append(", Tests run: ").append(stats.numTestsRun.get()); sb.append(", Failures: ").append(stats.numTestsFailed.get()); sb.append(", Skipped: ").append(stats.numTestsSkipped.get()); sb.append(", Aborted: ").append(stats.numTestsAborted.get()); @@ -112,7 +109,7 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements if (testIdentifier.isTest()) { final StringBuilder sb = new StringBuilder(); sb.append("Test: "); - sb.append(this.useLegacyReportingName ? testIdentifier.getLegacyReportingName() : testIdentifier.getDisplayName()); + sb.append(determineTestName(testIdentifier)); sb.append(" took "); stats.appendElapsed(sb); sb.append(" SKIPPED"); @@ -176,7 +173,7 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements if (testIdentifier.isTest() && shouldReportExecutionFinished(testIdentifier, testExecutionResult)) { final StringBuilder sb = new StringBuilder(); sb.append("Test: "); - sb.append(this.useLegacyReportingName ? testIdentifier.getLegacyReportingName() : testIdentifier.getDisplayName()); + sb.append(determineTestName(testIdentifier)); if (stats != null) { sb.append(" took "); stats.appendElapsed(sb); @@ -231,11 +228,6 @@ class LegacyPlainResultFormatter extends AbstractJUnitResultFormatter implements this.writer = new BufferedWriter(new OutputStreamWriter(this.outputStream, StandardCharsets.UTF_8)); } - @Override - public void setUseLegacyReportingName(final boolean useLegacyReportingName) { - this.useLegacyReportingName = useLegacyReportingName; - } - protected boolean shouldReportExecutionFinished(final TestIdentifier testIdentifier, final TestExecutionResult testExecutionResult) { return true; } diff --git a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyXmlResultFormatter.java similarity index 73% rename from test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java rename to test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyXmlResultFormatter.java index bb9a963..47aaa2d 100644 --- a/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher/LegacyXmlResultFormatter.java +++ b/test/unit/org/apache/tools/ant/taskdefs/optional/junitlauncher2/LegacyXmlResultFormatter.java @@ -1,22 +1,23 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * http://www.apache.org/licenses/LICENSE-2.0 * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -package org.apache.tools.ant.taskdefs.optional.junitlauncher; +package org.apache.tools.ant.taskdefs.optional.junitlauncher2; +import org.apache.tools.ant.taskdefs.optional.junitlauncher.TestResultFormatter; import org.apache.tools.ant.util.DOMElementWriter; import org.apache.tools.ant.util.DateUtils; import org.apache.tools.ant.util.StringUtils; @@ -37,7 +38,6 @@ import java.util.Date; import java.util.Map; import java.util.Optional; import java.util.Properties; -import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; @@ -46,25 +46,22 @@ import java.util.concurrent.atomic.AtomicLong; * conforms to the schema of the XML that was generated by the {@code junit} task's XML * report formatter and can be used by the {@code junitreport} task */ -class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements TestResultFormatter { +public class LegacyXmlResultFormatter extends org.apache.tools.ant.taskdefs.optional.junitlauncher2.AbstractJUnitResultFormatter implements TestResultFormatter { private static final double ONE_SECOND = 1000.0; private OutputStream outputStream; private final Map<TestIdentifier, Stats> testIds = new ConcurrentHashMap<>(); - private final Map<TestIdentifier, Optional<String>> skipped = new ConcurrentHashMap<>(); - private final Map<TestIdentifier, Optional<Throwable>> failed = new ConcurrentHashMap<>(); - private final Map<TestIdentifier, Optional<Throwable>> aborted = new ConcurrentHashMap<>(); + protected final Map<TestIdentifier, Optional<String>> skipped = new ConcurrentHashMap<>(); + protected final Map<TestIdentifier, Optional<Throwable>> failed = new ConcurrentHashMap<>(); + protected final Map<TestIdentifier, Optional<Throwable>> aborted = new ConcurrentHashMap<>(); - private TestPlan testPlan; private long testPlanStartedAt = -1; private long testPlanEndedAt = -1; private final AtomicLong numTestsRun = new AtomicLong(0); private final AtomicLong numTestsFailed = new AtomicLong(0); private final AtomicLong numTestsSkipped = new AtomicLong(0); private final AtomicLong numTestsAborted = new AtomicLong(0); - private boolean useLegacyReportingName = true; - @Override public void testPlanExecutionStarted(final TestPlan testPlan) { @@ -77,7 +74,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T this.testPlanEndedAt = System.currentTimeMillis(); // format and print out the result try { - new XMLReportWriter().write(); + createXMLReportWriter().write(); } catch (IOException | XMLStreamException e) { handleException(e); } @@ -142,9 +139,8 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T this.outputStream = os; } - @Override - public void setUseLegacyReportingName(final boolean useLegacyReportingName) { - this.useLegacyReportingName = useLegacyReportingName; + protected XMLReportWriter createXMLReportWriter() { + return new XMLReportWriter(); } private final class Stats { @@ -159,7 +155,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T } } - private final class XMLReportWriter { + protected class XMLReportWriter { private static final String ELEM_TESTSUITE = "testsuite"; private static final String ELEM_PROPERTIES = "properties"; @@ -184,8 +180,8 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T private static final String ATTR_MESSAGE = "message"; private static final String ATTR_TYPE = "type"; - void write() throws XMLStreamException, IOException { - final XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(outputStream, "UTF-8"); + protected void write() throws XMLStreamException, IOException { + final XMLStreamWriter writer = createXMLStreamWriter(); try { writer.writeStartDocument(); writeTestSuite(writer); @@ -195,11 +191,15 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T } } - void writeTestSuite(final XMLStreamWriter writer) throws XMLStreamException, IOException { + protected XMLStreamWriter createXMLStreamWriter() throws XMLStreamException { + return XMLOutputFactory.newFactory().createXMLStreamWriter(outputStream, "UTF-8"); + } + + protected void writeTestSuite(final XMLStreamWriter writer) throws XMLStreamException, IOException { // write the testsuite element writer.writeStartElement(ELEM_TESTSUITE); - final String testsuiteName = determineTestSuiteName(); - writeAttribute(writer, ATTR_NAME, testsuiteName); + final String testSuiteName = determineTestSuiteName(); + writeAttribute(writer, ATTR_NAME, testSuiteName); // time taken for the tests execution writeAttribute(writer, ATTR_TIME, String.valueOf((testPlanEndedAt - testPlanStartedAt) / ONE_SECOND)); // add the timestamp of report generation @@ -210,6 +210,8 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T writeAttribute(writer, ATTR_NUM_SKIPPED, String.valueOf(numTestsSkipped.longValue())); writeAttribute(writer, ATTR_NUM_ABORTED, String.valueOf(numTestsAborted.longValue())); + writeCustomAttributes(writer); + // write the properties writeProperties(writer); // write the tests @@ -220,7 +222,11 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T writer.writeEndElement(); } - void writeProperties(final XMLStreamWriter writer) throws XMLStreamException { + protected void writeCustomAttributes(XMLStreamWriter writer) throws XMLStreamException + { + } + + protected void writeProperties(final XMLStreamWriter writer) throws XMLStreamException { final Properties properties = LegacyXmlResultFormatter.this.context.getProperties(); if (properties == null || properties.isEmpty()) { return; @@ -235,7 +241,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T writer.writeEndElement(); } - void writeTestCase(final XMLStreamWriter writer) throws XMLStreamException { + protected void writeTestCase(final XMLStreamWriter writer) throws XMLStreamException { for (final Map.Entry<TestIdentifier, Stats> entry : testIds.entrySet()) { final TestIdentifier testId = entry.getKey(); if (!testId.isTest() && !failed.containsKey(testId)) { @@ -258,8 +264,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T final String classname = (parentClassSource.get()).getClassName(); writer.writeStartElement(ELEM_TESTCASE); writeAttribute(writer, ATTR_CLASSNAME, classname); - writeAttribute(writer, ATTR_NAME, useLegacyReportingName ? testId.getLegacyReportingName() - : testId.getDisplayName()); + writeAttribute(writer, ATTR_NAME, determineTestName(testId)); final Stats stats = entry.getValue(); writeAttribute(writer, ATTR_TIME, String.valueOf((stats.endedAt - stats.startedAt) / ONE_SECOND)); // skipped element if the test was skipped @@ -273,7 +278,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T } } - private void writeSkipped(final XMLStreamWriter writer, final TestIdentifier testIdentifier) throws XMLStreamException { + protected void writeSkipped(final XMLStreamWriter writer, final TestIdentifier testIdentifier) throws XMLStreamException { if (!skipped.containsKey(testIdentifier)) { return; } @@ -285,7 +290,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T writer.writeEndElement(); } - private void writeFailed(final XMLStreamWriter writer, final TestIdentifier testIdentifier) throws XMLStreamException { + protected void writeFailed(final XMLStreamWriter writer, final TestIdentifier testIdentifier) throws XMLStreamException { if (!failed.containsKey(testIdentifier)) { return; } @@ -304,7 +309,7 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T writer.writeEndElement(); } - private void writeAborted(final XMLStreamWriter writer, final TestIdentifier testIdentifier) throws XMLStreamException { + protected void writeAborted(final XMLStreamWriter writer, final TestIdentifier testIdentifier) throws XMLStreamException { if (!aborted.containsKey(testIdentifier)) { return; } @@ -323,29 +328,29 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T writer.writeEndElement(); } - private void writeSysOut(final XMLStreamWriter writer) throws XMLStreamException, IOException { - if (!LegacyXmlResultFormatter.this.hasSysOut()) { + protected void writeSysOut(final XMLStreamWriter writer) throws XMLStreamException, IOException { + if (!hasSysOut()) { return; } writer.writeStartElement(ELEM_SYSTEM_OUT); - try (final Reader reader = LegacyXmlResultFormatter.this.getSysOutReader()) { + try (final Reader reader = getSysOutReader()) { writeCharactersFrom(reader, writer); } writer.writeEndElement(); } - private void writeSysErr(final XMLStreamWriter writer) throws XMLStreamException, IOException { - if (!LegacyXmlResultFormatter.this.hasSysErr()) { + protected void writeSysErr(final XMLStreamWriter writer) throws XMLStreamException, IOException { + if (!hasSysErr()) { return; } writer.writeStartElement(ELEM_SYSTEM_ERR); - try (final Reader reader = LegacyXmlResultFormatter.this.getSysErrReader()) { + try (final Reader reader = getSysErrReader()) { writeCharactersFrom(reader, writer); } writer.writeEndElement(); } - private void writeCharactersFrom(final Reader reader, final XMLStreamWriter writer) throws IOException, XMLStreamException { + protected void writeCharactersFrom(final Reader reader, final XMLStreamWriter writer) throws IOException, XMLStreamException { final char[] chars = new char[1024]; int numRead = -1; while ((numRead = reader.read(chars)) != -1) { @@ -353,12 +358,11 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T } } - private void writeAttribute(final XMLStreamWriter writer, final String name, final String value) - throws XMLStreamException { + protected void writeAttribute(final XMLStreamWriter writer, final String name, final String value) throws XMLStreamException { writer.writeAttribute(name, encode(value)); } - private String encode(final String s) { + protected String encode(final String s) { boolean changed = false; final StringBuilder sb = new StringBuilder(); for (char c : s.toCharArray()) { @@ -372,39 +376,6 @@ class LegacyXmlResultFormatter extends AbstractJUnitResultFormatter implements T return changed ? sb.toString() : s; } - private String determineTestSuiteName() { - // this is really a hack to try and match the expectations of the XML report in JUnit4.x - // world. In JUnit5, the TestPlan doesn't have a name and a TestPlan (for which this is a - // listener) can have numerous tests within it - final Set<TestIdentifier> roots = testPlan.getRoots(); - if (roots.isEmpty()) { - return "UNKNOWN"; - } - for (final TestIdentifier root : roots) { - final Optional<ClassSource> classSource = findFirstClassSource(root); - if (classSource.isPresent()) { - return classSource.get().getClassName(); - } - } - return "UNKNOWN"; - } - - private Optional<ClassSource> findFirstClassSource(final TestIdentifier root) { - if (root.getSource().isPresent()) { - final TestSource source = root.getSource().get(); - if (source instanceof ClassSource) { - return Optional.of((ClassSource) source); - } - } - for (final TestIdentifier child : testPlan.getChildren(root)) { - final Optional<ClassSource> classSource = findFirstClassSource(child); - if (classSource.isPresent()) { - return classSource; - } - } - return Optional.empty(); - } - private Optional<ClassSource> findFirstParentClassSource(final TestIdentifier testId) { final Optional<TestIdentifier> parent = testPlan.getParent(testId); if (!parent.isPresent()) { --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
