From dde719dd575090b36aaa3ad85bb3cabf33f36c5a Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 20 Oct 2021 13:36:36 +0800 Subject: +luaunit test --- Resources/Libraries/luaunit/junitxml/Makefile | 17 ++ .../junitxml/XMLJUnitResultFormatter.java.txt | 301 +++++++++++++++++++++ .../luaunit/junitxml/example-apache-ant.xml | 31 +++ .../luaunit/junitxml/example-bamboo-1.xml | 70 +++++ .../luaunit/junitxml/example-bamboo-2.xml | 56 ++++ .../Libraries/luaunit/junitxml/example-jenkins.xml | 29 ++ .../luaunit/junitxml/junit-apache-ant.xsd | 206 ++++++++++++++ .../Libraries/luaunit/junitxml/junit-jenkins.xsd | 91 +++++++ .../luaunit/junitxml/notes-junit-compatibility.txt | 34 +++ 9 files changed, 835 insertions(+) create mode 100644 Resources/Libraries/luaunit/junitxml/Makefile create mode 100644 Resources/Libraries/luaunit/junitxml/XMLJUnitResultFormatter.java.txt create mode 100644 Resources/Libraries/luaunit/junitxml/example-apache-ant.xml create mode 100644 Resources/Libraries/luaunit/junitxml/example-bamboo-1.xml create mode 100644 Resources/Libraries/luaunit/junitxml/example-bamboo-2.xml create mode 100644 Resources/Libraries/luaunit/junitxml/example-jenkins.xml create mode 100644 Resources/Libraries/luaunit/junitxml/junit-apache-ant.xsd create mode 100644 Resources/Libraries/luaunit/junitxml/junit-jenkins.xsd create mode 100644 Resources/Libraries/luaunit/junitxml/notes-junit-compatibility.txt (limited to 'Resources/Libraries/luaunit/junitxml') diff --git a/Resources/Libraries/luaunit/junitxml/Makefile b/Resources/Libraries/luaunit/junitxml/Makefile new file mode 100644 index 0000000..9679016 --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/Makefile @@ -0,0 +1,17 @@ +# +# junitxml/Makefile +# + +XMLLINT ?= xmllint +TEST_APACHE = --noout --schema junit-apache-ant.xsd +TEST_JENKINS = --noout --schema junit-jenkins.xsd + +validate-examples: validate-apache validate-jenkins + +# This example file is the only one that satisfies the Apache schema +validate-apache: example-apache-ant.xml + $(XMLLINT) $(TEST_APACHE) $< + +# The Jenkins schema is more relaxed, and should apply to all .xml files +validate-jenkins: $(wildcard *.xml) + for file in $^; do $(XMLLINT) $(TEST_JENKINS) $$file || exit 1; done diff --git a/Resources/Libraries/luaunit/junitxml/XMLJUnitResultFormatter.java.txt b/Resources/Libraries/luaunit/junitxml/XMLJUnitResultFormatter.java.txt new file mode 100644 index 0000000..5e3d1d7 --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/XMLJUnitResultFormatter.java.txt @@ -0,0 +1,301 @@ +/* + * 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.junit; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Properties; +import java.util.Date; +import java.net.InetAddress; +import java.net.UnknownHostException; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import junit.framework.AssertionFailedError; +import junit.framework.Test; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.util.DOMElementWriter; +import org.apache.tools.ant.util.DateUtils; +import org.apache.tools.ant.util.FileUtils; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Text; + + +/** + * Prints XML output of the test to a specified Writer. + * + * @see FormatterElement + */ + +public class XMLJUnitResultFormatter implements JUnitResultFormatter, XMLConstants { + + private static final double ONE_SECOND = 1000.0; + + /** constant for unnnamed testsuites/cases */ + private static final String UNKNOWN = "unknown"; + + private static DocumentBuilder getDocumentBuilder() { + try { + return DocumentBuilderFactory.newInstance().newDocumentBuilder(); + } catch (Exception exc) { + throw new ExceptionInInitializerError(exc); + } + } + + /** + * The XML document. + */ + private Document doc; + /** + * The wrapper for the whole testsuite. + */ + private Element rootElement; + /** + * Element for the current test. + */ + private Hashtable testElements = new Hashtable(); + /** + * tests that failed. + */ + private Hashtable failedTests = new Hashtable(); + /** + * Timing helper. + */ + private Hashtable testStarts = new Hashtable(); + /** + * Where to write the log to. + */ + private OutputStream out; + + /** No arg constructor. */ + public XMLJUnitResultFormatter() { + } + + /** {@inheritDoc}. */ + public void setOutput(OutputStream out) { + this.out = out; + } + + /** {@inheritDoc}. */ + public void setSystemOutput(String out) { + formatOutput(SYSTEM_OUT, out); + } + + /** {@inheritDoc}. */ + public void setSystemError(String out) { + formatOutput(SYSTEM_ERR, out); + } + + /** + * The whole testsuite started. + * @param suite the testsuite. + */ + public void startTestSuite(JUnitTest suite) { + doc = getDocumentBuilder().newDocument(); + rootElement = doc.createElement(TESTSUITE); + String n = suite.getName(); + rootElement.setAttribute(ATTR_NAME, n == null ? UNKNOWN : n); + + //add the timestamp + final String timestamp = DateUtils.format(new Date(), + DateUtils.ISO8601_DATETIME_PATTERN); + rootElement.setAttribute(TIMESTAMP, timestamp); + //and the hostname. + rootElement.setAttribute(HOSTNAME, getHostname()); + + // Output properties + Element propsElement = doc.createElement(PROPERTIES); + rootElement.appendChild(propsElement); + Properties props = suite.getProperties(); + if (props != null) { + Enumeration e = props.propertyNames(); + while (e.hasMoreElements()) { + String name = (String) e.nextElement(); + Element propElement = doc.createElement(PROPERTY); + propElement.setAttribute(ATTR_NAME, name); + propElement.setAttribute(ATTR_VALUE, props.getProperty(name)); + propsElement.appendChild(propElement); + } + } + } + + /** + * get the local hostname + * @return the name of the local host, or "localhost" if we cannot work it out + */ + private String getHostname() { + try { + return InetAddress.getLocalHost().getHostName(); + } catch (UnknownHostException e) { + return "localhost"; + } + } + + /** + * The whole testsuite ended. + * @param suite the testsuite. + * @throws BuildException on error. + */ + public void endTestSuite(JUnitTest suite) throws BuildException { + rootElement.setAttribute(ATTR_TESTS, "" + suite.runCount()); + rootElement.setAttribute(ATTR_FAILURES, "" + suite.failureCount()); + rootElement.setAttribute(ATTR_ERRORS, "" + suite.errorCount()); + rootElement.setAttribute( + ATTR_TIME, "" + (suite.getRunTime() / ONE_SECOND)); + if (out != null) { + Writer wri = null; + try { + wri = new BufferedWriter(new OutputStreamWriter(out, "UTF8")); + wri.write("\n"); + (new DOMElementWriter()).write(rootElement, wri, 0, " "); + } catch (IOException exc) { + throw new BuildException("Unable to write log file", exc); + } finally { + if (wri != null) { + try { + wri.flush(); + } catch (IOException ex) { + // ignore + } + } + if (out != System.out && out != System.err) { + FileUtils.close(wri); + } + } + } + } + + /** + * Interface TestListener. + * + *

A new Test is started. + * @param t the test. + */ + public void startTest(Test t) { + testStarts.put(t, new Long(System.currentTimeMillis())); + } + + /** + * Interface TestListener. + * + *

A Test is finished. + * @param test the test. + */ + public void endTest(Test test) { + // Fix for bug #5637 - if a junit.extensions.TestSetup is + // used and throws an exception during setUp then startTest + // would never have been called + if (!testStarts.containsKey(test)) { + startTest(test); + } + + Element currentTest = null; + if (!failedTests.containsKey(test)) { + currentTest = doc.createElement(TESTCASE); + String n = JUnitVersionHelper.getTestCaseName(test); + currentTest.setAttribute(ATTR_NAME, + n == null ? UNKNOWN : n); + // a TestSuite can contain Tests from multiple classes, + // even tests with the same name - disambiguate them. + currentTest.setAttribute(ATTR_CLASSNAME, + JUnitVersionHelper.getTestCaseClassName(test)); + rootElement.appendChild(currentTest); + testElements.put(test, currentTest); + } else { + currentTest = (Element) testElements.get(test); + } + + Long l = (Long) testStarts.get(test); + currentTest.setAttribute(ATTR_TIME, + "" + ((System.currentTimeMillis() + - l.longValue()) / ONE_SECOND)); + } + + /** + * Interface TestListener for JUnit <= 3.4. + * + *

A Test failed. + * @param test the test. + * @param t the exception. + */ + public void addFailure(Test test, Throwable t) { + formatError(FAILURE, test, t); + } + + /** + * Interface TestListener for JUnit > 3.4. + * + *

A Test failed. + * @param test the test. + * @param t the assertion. + */ + public void addFailure(Test test, AssertionFailedError t) { + addFailure(test, (Throwable) t); + } + + /** + * Interface TestListener. + * + *

An error occurred while running the test. + * @param test the test. + * @param t the error. + */ + public void addError(Test test, Throwable t) { + formatError(ERROR, test, t); + } + + private void formatError(String type, Test test, Throwable t) { + if (test != null) { + endTest(test); + failedTests.put(test, test); + } + + Element nested = doc.createElement(type); + Element currentTest = null; + if (test != null) { + currentTest = (Element) testElements.get(test); + } else { + currentTest = rootElement; + } + + currentTest.appendChild(nested); + + String message = t.getMessage(); + if (message != null && message.length() > 0) { + nested.setAttribute(ATTR_MESSAGE, t.getMessage()); + } + nested.setAttribute(ATTR_TYPE, t.getClass().getName()); + + String strace = JUnitTestRunner.getFilteredTrace(t); + Text trace = doc.createTextNode(strace); + nested.appendChild(trace); + } + + private void formatOutput(String type, String output) { + Element nested = doc.createElement(type); + rootElement.appendChild(nested); + nested.appendChild(doc.createCDATASection(output)); + } + +} // XMLJUnitResultFormatter diff --git a/Resources/Libraries/luaunit/junitxml/example-apache-ant.xml b/Resources/Libraries/luaunit/junitxml/example-apache-ant.xml new file mode 100644 index 0000000..6123da0 --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/example-apache-ant.xml @@ -0,0 +1,31 @@ + + + + + + + + + + Detailed failure content + + + + + + + Detailed error content + + + bla bla output bla bla + bla bla error bla bla + + \ No newline at end of file diff --git a/Resources/Libraries/luaunit/junitxml/example-bamboo-1.xml b/Resources/Libraries/luaunit/junitxml/example-bamboo-1.xml new file mode 100644 index 0000000..f0b9933 --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/example-bamboo-1.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + junit.framework.AssertionFailedError: Should not have any errors. [Perforce client error:, Connect to server + failed; check $P4PORT., TCP connect to keg failed., keg: host unknown.] expected:<0> but was:<4> + at junit.framework.Assert.fail(Assert.java:47) + at junit.framework.Assert.failNotEquals(Assert.java:282) + at junit.framework.Assert.assertEquals(Assert.java:64) + at junit.framework.Assert.assertEquals(Assert.java:201) + at com.atlassian.bamboo.repository.perforce.PerforceSyncCommandTest.testUsingPerforceWhenNoFilesHaveChanged(PerforceSyncCommandTest.java:60) + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) + at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) + at java.lang.reflect.Method.invoke(Method.java:585) + at junit.framework.TestCase.runTest(TestCase.java:154) + at junit.framework.TestCase.runBare(TestCase.java:127) + at junit.framework.TestResult$1.protect(TestResult.java:106) + at junit.framework.TestResult.runProtected(TestResult.java:124) + at junit.framework.TestResult.run(TestResult.java:109) + at junit.framework.TestCase.run(TestCase.java:118) + at junit.framework.TestSuite.runTest(TestSuite.java:208) + at junit.framework.TestSuite.run(TestSuite.java:203) + at sun.reflect.GeneratedMethodAccessor17.invoke(Unknown Source) + at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) + at java.lang.reflect.Method.invoke(Method.java:585) + at org.apache.maven.surefire.battery.JUnitBattery.executeJUnit(JUnitBattery.java:242) + at org.apache.maven.surefire.battery.JUnitBattery.execute(JUnitBattery.java:216) + at org.apache.maven.surefire.Surefire.executeBattery(Surefire.java:215) + at org.apache.maven.surefire.Surefire.run(Surefire.java:163) + at org.apache.maven.surefire.Surefire.run(Surefire.java:87) + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) + at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) + at java.lang.reflect.Method.invoke(Method.java:585) + at org.apache.maven.surefire.SurefireBooter.runTestsInProcess(SurefireBooter.java:313) + at org.apache.maven.surefire.SurefireBooter.run(SurefireBooter.java:221) + at org.apache.maven.test.SurefirePlugin.execute(SurefirePlugin.java:371) + at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:412) + at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:534) + at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:475) + at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:454) + at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:306) + at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:273) + at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:140) + at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:322) + at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:115) + at org.apache.maven.cli.MavenCli.main(MavenCli.java:256) + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) + at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) + at java.lang.reflect.Method.invoke(Method.java:585) + at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315) + at org.codehaus.classworlds.Launcher.launch(Launcher.java:255) + at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430) + at org.codehaus.classworlds.Launcher.main(Launcher.java:375) + + + PerforceSyncCommand.command: /usr/local/bin/p4 + + + \ No newline at end of file diff --git a/Resources/Libraries/luaunit/junitxml/example-bamboo-2.xml b/Resources/Libraries/luaunit/junitxml/example-bamboo-2.xml new file mode 100644 index 0000000..02f628c --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/example-bamboo-2.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Resources/Libraries/luaunit/junitxml/example-jenkins.xml b/Resources/Libraries/luaunit/junitxml/example-jenkins.xml new file mode 100644 index 0000000..6690664 --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/example-jenkins.xml @@ -0,0 +1,29 @@ + + + + + + + + bla bla + + Detailed failure content + + + + + + Detailed error content + + + bla bla output bla bla + bla bla error bla bla + + + \ No newline at end of file diff --git a/Resources/Libraries/luaunit/junitxml/junit-apache-ant.xsd b/Resources/Libraries/luaunit/junitxml/junit-apache-ant.xsd new file mode 100644 index 0000000..71bdf60 --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/junit-apache-ant.xsd @@ -0,0 +1,206 @@ + + + + + JUnit test result schema for the Apache Ant JUnit and JUnitReport tasks +Copyright © 2011, Windy Road Technology Pty. Limited +The Apache Ant JUnit XML Schema is distributed under the terms of the GNU Lesser General Public License (LGPL) http://www.gnu.org/licenses/lgpl.html +Permission to waive conditions of this license may be requested from Windy Road Support (http://windyroad.org/support). + + + + + + + + + + Contains an aggregation of testsuite results + + + + + + + + + + Derived from testsuite/@name in the non-aggregated documents + + + + + Starts at '0' for the first testsuite and is incremented by 1 for each following testsuite + + + + + + + + + + + + Contains the results of exexuting a testsuite + + + + + Properties (e.g., environment settings) set during test execution + + + + + + + + + + + + + + + + + + + + + + + + Indicates that the test errored. An errored test is one that had an unanticipated problem. e.g., an unchecked throwable; or a problem with the implementation of the test. Contains as a text node relevant data for the error, e.g., a stack trace + + + + + + + The error message. e.g., if a java exception is thrown, the return value of getMessage() + + + + + The type of error that occured. e.g., if a java execption is thrown the full class name of the exception. + + + + + + + + + Indicates that the test failed. A failure is a test which the code has explicitly failed by using the mechanisms for that purpose. e.g., via an assertEquals. Contains as a text node relevant data for the failure, e.g., a stack trace + + + + + + + The message specified in the assert + + + + + The type of the assert. + + + + + + + + + + Name of the test method + + + + + Full class name for the class the test method is in. + + + + + Time taken (in seconds) to execute the test + + + + + + + Data that was written to standard out while the test was executed + + + + + + + + + + Data that was written to standard error while the test was executed + + + + + + + + + + + Full class name of the test for non-aggregated testsuite documents. Class name without the package for aggregated testsuites documents + + + + + + + + + + when the test was executed. Timezone may not be specified. + + + + + Host on which the tests were executed. 'localhost' should be used if the hostname cannot be determined. + + + + + + + + + + The total number of tests in the suite + + + + + The total number of tests in the suite that failed. A failure is a test which the code has explicitly failed by using the mechanisms for that purpose. e.g., via an assertEquals + + + + + The total number of tests in the suite that errorrd. An errored test is one that had an unanticipated problem. e.g., an unchecked throwable; or a problem with the implementation of the test. + + + + + Time taken (in seconds) to execute the tests in the suite + + + + + + + + + \ No newline at end of file diff --git a/Resources/Libraries/luaunit/junitxml/junit-jenkins.xsd b/Resources/Libraries/luaunit/junitxml/junit-jenkins.xsd new file mode 100644 index 0000000..a1d5ab0 --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/junit-jenkins.xsd @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Resources/Libraries/luaunit/junitxml/notes-junit-compatibility.txt b/Resources/Libraries/luaunit/junitxml/notes-junit-compatibility.txt new file mode 100644 index 0000000..ab92cff --- /dev/null +++ b/Resources/Libraries/luaunit/junitxml/notes-junit-compatibility.txt @@ -0,0 +1,34 @@ +Our source information: +https://github.com/windyroad/JUnit-Schema +* http://stackoverflow.com/questions/4922867/junit-xml-format-specification-that-hudson-supports +(Jenkins) +* https://github.com/jenkinsci/xunit-plugin/tree/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd +* http://help.catchsoftware.com/display/ET/JUnit+Format +* http://llg.cubic.org/docs/junit/ +* https://confluence.atlassian.com/bamboo/junit-parsing-in-bamboo-289277357.html +* http://nose2.readthedocs.io/en/latest/plugins/junitxml.html +* https://pzolee.blogs.balabit.com/2012/11/jenkins-vs-junit-xml-format/ +* https://www.relishapp.com/cucumber/cucumber/docs/formatters/junit-output-formatter +* http://stackoverflow.com/questions/11241781/python-unittests-in-jenkins +* https://github.com/xmlrunner/unittest-xml-reporting/tree/master/ + + + +Notes: +====== +Ant xml schema is much more restrictive than jenkins. + +Non allowed fields in ant, allowed in jenkins: + +testsuites: +- apache ant forbids all attributes. + +testsuite: +- apache ant forbids attributes: disabled, skipped +- apache ant allows elements system-out and system-err, that are in testcase for jenkins + +testcase: +- apache ant forbids attributes: assertions, status +- apache ant forbids elements: skipped, system-out, system-err + + -- cgit v1.1-26-g67d0