001package org.junit.internal;
002
003import org.hamcrest.Description;
004import org.hamcrest.Matcher;
005import org.hamcrest.SelfDescribing;
006import org.hamcrest.StringDescription;
007
008/**
009 * An exception class used to implement <i>assumptions</i> (state in which a given test
010 * is meaningful and should or should not be executed). A test for which an assumption
011 * fails should not generate a test case failure.
012 *
013 * @see org.junit.Assume
014 */
015public class AssumptionViolatedException extends RuntimeException implements SelfDescribing {
016    private static final long serialVersionUID = 2L;
017
018    /*
019     * We have to use the f prefix until the next major release to ensure
020     * serialization compatibility. 
021     * See https://github.com/junit-team/junit/issues/976
022     */
023    private final String fAssumption;
024    private final boolean fValueMatcher;
025    private final Object fValue;
026    private final Matcher<?> fMatcher;
027
028    /**
029     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
030     */
031    @Deprecated
032    public AssumptionViolatedException(String assumption, boolean hasValue, Object value, Matcher<?> matcher) {
033        this.fAssumption = assumption;
034        this.fValue = value;
035        this.fMatcher = matcher;
036        this.fValueMatcher = hasValue;
037
038        if (value instanceof Throwable) {
039          initCause((Throwable) value);
040        }
041    }
042
043    /**
044     * An assumption exception with the given <i>value</i> (String or
045     * Throwable) and an additional failing {@link Matcher}.
046     *
047     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
048     */
049    @Deprecated
050    public AssumptionViolatedException(Object value, Matcher<?> matcher) {
051        this(null, true, value, matcher);
052    }
053
054    /**
055     * An assumption exception with the given <i>value</i> (String or
056     * Throwable) and an additional failing {@link Matcher}.
057     *
058     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
059     */
060    @Deprecated
061    public AssumptionViolatedException(String assumption, Object value, Matcher<?> matcher) {
062        this(assumption, true, value, matcher);
063    }
064
065    /**
066     * An assumption exception with the given message only.
067     *
068     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
069     */
070    @Deprecated
071    public AssumptionViolatedException(String assumption) {
072        this(assumption, false, null, null);
073    }
074
075    /**
076     * An assumption exception with the given message and a cause.
077     *
078     * @deprecated Please use {@link org.junit.AssumptionViolatedException} instead.
079     */
080    @Deprecated
081    public AssumptionViolatedException(String assumption, Throwable e) {
082        this(assumption, false, null, null);
083        initCause(e);
084    }
085
086    @Override
087    public String getMessage() {
088        return StringDescription.asString(this);
089    }
090
091    public void describeTo(Description description) {
092        if (fAssumption != null) {
093            description.appendText(fAssumption);
094        }
095
096        if (fValueMatcher) {
097            // a value was passed in when this instance was constructed; print it
098            if (fAssumption != null) {
099                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}