testcaseresultwriter.java

来自「sourcode about java basic」· Java 代码 · 共 65 行

JAVA
65
字号
package jsunit.java.source_core.net.jsunit.model;

import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class TestCaseResultWriter {
    public static final String
            TEST_CASE = "testCase",
            NAME = "name",
            TIME = "time",
            FAILURE = "failure",
            ERROR = "error",
            MESSAGE = "message";

    private TestCaseResult result;

    public TestCaseResultWriter(TestCaseResult result) {
        this.result = result;
    }

    public void addXmlTo(Element element) {
        element.addContent(createTestCaseElement());
    }

    public Element createTestCaseElement() {
        Element testCaseElement = new Element(TEST_CASE);
        try {
            testCaseElement.setAttribute(NAME, result.getFullyQualifiedName().replace('\u0000', ' '));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        testCaseElement.setAttribute(TIME, String.valueOf(result.getTime()));
        if (result.hadFailure()) {
            Element failureElement = new Element(FAILURE);
            try {
                failureElement.setText(result.getFailure().replace('\u0000', ' '));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            testCaseElement.addContent(failureElement);
        } else if (result.hadError()) {
            Element errorElement = new Element(ERROR);
            try {
                errorElement.setText(result.getError().replace('\u0000', ' '));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            testCaseElement.addContent(errorElement);
        }
        return testCaseElement;
    }

    public String getProblemSummary() {
        if (result.hadFailure())
            return result.getFullyQualifiedName() + " failed:\n" + result.getFailure();
        else if (result.hadError())
            return result.getFullyQualifiedName() + " had an error:\n" + result.getError();
        return null;
    }

    public String getXmlFragment() {
        return new XMLOutputter().outputString(createTestCaseElement());
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?