aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/reader/output/TextOutputWriter.java
blob: 7f9cdae80e7051616b74e24c7b7ab4320b997219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cz.crcs.ectester.reader.output;

import cz.crcs.ectester.reader.Util;
import cz.crcs.ectester.reader.response.Response;
import cz.crcs.ectester.reader.test.Test;

import java.io.PrintStream;

/**
 * @author Jan Jancar johny@neuromancer.sk
 */
public class TextOutputWriter implements OutputWriter {
    private PrintStream output;

    public TextOutputWriter(PrintStream output) {
        this.output = output;
    }

    @Override
    public void begin() {
    }

    private String testPrefix(Test t) {
        return String.format("%-4s", t.getResult() == Test.Result.SUCCESS ? "OK" : "NOK");
    }

    private String responseSuffix(Response r) {
        StringBuilder suffix = new StringBuilder();
        for (int j = 0; j < r.getNumSW(); ++j) {
            short sw = r.getSW(j);
            if (sw != 0) {
                suffix.append(" ").append(Util.getSWString(sw));
            }
        }
        if (suffix.length() == 0) {
            suffix.append(" [").append(Util.getSW(r.getNaturalSW())).append("]");
        }
        return String.format("%4d ms : %s", r.getDuration() / 1000000, suffix);
    }

    @Override
    public void outputResponse(Response r) {
        String out = "";
        out += String.format("%-70s:", r.getDescription()) + " : ";
        out += responseSuffix(r);
        output.println(out);
        output.flush();
    }

    private String testString(Test t) {
        if (!t.hasRun())
            return null;

        StringBuilder out = new StringBuilder();
        if (t instanceof Test.Simple) {
            Test.Simple test = (Test.Simple) t;
            out.append(String.format("%-70s:", testPrefix(t) + " : " + test.getDescription())).append(" : ");
            out.append(responseSuffix(test.getResponse()));
        } else if (t instanceof Test.Compound) {
            Test.Compound test = (Test.Compound) t;
            Test[] tests = test.getTests();
            for (int i = 0; i < tests.length; ++i) {
                if (i == 0) {
                    out.append(" /- ");
                } else if (i == tests.length - 1) {
                    out.append(" \\- ");
                } else {
                    out.append(" |  ");
                }
                out.append(testString(tests[i])).append(System.lineSeparator());
            }
            out.append(String.format("%-70s:", testPrefix(t) + " : " + test.getDescription()));
        }
        return out.toString();
    }

    @Override
    public void outputTest(Test t) {
        output.println(testString(t));
        output.flush();
    }

    @Override
    public void end() {
    }
}