1package org.junit.internal;
2
3import org.hamcrest.Description;
4import org.hamcrest.Matcher;
5import org.hamcrest.SelfDescribing;
6import org.hamcrest.StringDescription;
7
8/**
9 * An exception class used to implement <i>assumptions</i> (state in which a given test
10 * is meaningful and should or should not be executed). A test for which an assumption
11 * fails should not generate a test case failure.
12 *
13 * @see org.junit.Assume
14 */
15public class AssumptionViolatedException extends RuntimeException implements SelfDescribing {
16    private static final long serialVersionUID = 2L;
17
18    /*
19     * We have to use the f prefix until the next major release to ensure
20     * serialization compatibility.
21     * See https://github.com/junit-team/junit/issues/976
22     */
23    private final String fAssumption;
24    private final boolean fValueMatcher;
25    private final Object fValue;
26    private final Matcher<?> fMatcher;
27
28    /**
29     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
30     */
31    @Deprecated
32    public AssumptionViolatedException(String assumption, boolean hasValue, Object value, Matcher<?> matcher) {
33        this.fAssumption = assumption;
34        this.fValue = value;
35        this.fMatcher = matcher;
36        this.fValueMatcher = hasValue;
37
38        if (value instanceof Throwable) {
39          initCause((Throwable) value);
40        }
41    }
42
43    /**
44     * An assumption exception with the given <i>value</i> (String or
45     * Throwable) and an additional failing {@link Matcher}.
46     *
47     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
48     */
49    @Deprecated
50    public AssumptionViolatedException(Object value, Matcher<?> matcher) {
51        this(null, true, value, matcher);
52    }
53
54    /**
55     * An assumption exception with the given <i>value</i> (String or
56     * Throwable) and an additional failing {@link Matcher}.
57     *
58     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
59     */
60    @Deprecated
61    public AssumptionViolatedException(String assumption, Object value, Matcher<?> matcher) {
62        this(assumption, true, value, matcher);
63    }
64
65    /**
66     * An assumption exception with the given message only.
67     *
68     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
69     */
70    @Deprecated
71    public AssumptionViolatedException(String assumption) {
72        this(assumption, false, null, null);
73    }
74
75    /**
76     * An assumption exception with the given message and a cause.
77     *
78     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
79     */
80    @Deprecated
81    public AssumptionViolatedException(String assumption, Throwable e) {
82        this(assumption, false, null, null);
83        initCause(e);
84    }
85
86    @Override
87    public String getMessage() {
88        return StringDescription.asString(this);
89    }
90
91    public void describeTo(Description description) {
92        if (fAssumption != null) {
93            description.appendText(fAssumption);
94        }
95
96        if (fValueMatcher) {
97            // a value was passed in when this instance was constructed; print it
98            if (fAssumption != null) {
99                description.appendText(": ");
100            }
101
102            description.appendText("got: ");
103            description.appendValue(fValue);
104
105            if (fMatcher != null) {
106                description.appendText(", expected: ");
107                description.appendDescriptionOf(fMatcher);
108            }
109        }
110    }
111}
112