aboutsummaryrefslogtreecommitdiff
path: root/src/cz/crcs/ectester/common/test/Test.java
blob: 8bf9502838934e78c4689636bada051e41173a6c (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
package cz.crcs.ectester.common.test;

import static cz.crcs.ectester.common.test.Result.Value;

/**
 * An abstract test that can be run and has a Result.
 *
 * @author Jan Jancar johny@neuromancer.sk
 */
public abstract class Test implements Testable, Cloneable {
    protected boolean hasRun;
    protected boolean hasStarted;
    protected Result result;

    public Result getResult() {
        return result;
    }

    public boolean ok() {
        if (result == null) {
            return true;
        }
        return result.ok();
    }

    @Override
    public boolean error() {
        if (result == null) {
            return false;
        }
        return result.compareTo(Value.ERROR);
    }

    @Override
    public Object errorCause() {
        if (result == null || !result.compareTo(Value.ERROR)) {
            return null;
        }
        return result.getCause();
    }

    @Override
    public boolean hasRun() {
        return hasRun;
    }

    public boolean hasStarted() {
        return hasStarted;
    }

    @Override
    public void reset() {
        hasRun = false;
        hasStarted = false;
        result = null;
    }

    public abstract String getDescription();

    @Override
    public Test clone() throws CloneNotSupportedException {
        return (Test) super.clone();
    }

    @Override
    public void run() {
        if (hasRun)
            return;
        try {
            hasStarted = true;
            runSelf();
            hasRun = true;
        } catch (TestException e) {
            result = new Result(Value.ERROR, e);
            throw e;
        } catch (Exception e) {
            result = new Result(Value.ERROR, e);
            throw new TestException(e);
        }
    }

    protected abstract void runSelf();
}