001/**
002 * Copyright (C) 2011 rwoo@gmx.de
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *         http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.googlecode.catchexception.throwable.apis.internal.hamcrest;
017
018import org.hamcrest.BaseMatcher;
019import org.hamcrest.Description;
020import org.hamcrest.Matcher;
021
022/**
023 * Creates a {@link Matcher matcher} that matches an throwable that has no {@link Throwable#getCause() cause}.
024 * 
025 * @author rwoo
026 * 
027 * @param <T>
028 *            an throwable subclass
029 */
030public class ThrowableNoCauseMatcher<T extends Throwable> extends BaseMatcher<T> {
031
032    /*
033     * (non-Javadoc)
034     * 
035     * @see org.hamcrest.Matcher#matches(java.lang.Object)
036     */
037    @Override
038    public boolean matches(Object obj) {
039        if (!(obj instanceof Throwable))
040            return false;
041
042        Throwable throwable = (Throwable) obj;
043
044        return throwable.getCause() == null;
045    }
046
047    /*
048     * (non-Javadoc)
049     * 
050     * @see org.hamcrest.SelfDescribing#describeTo(org.hamcrest.Description)
051     */
052    @Override
053    public void describeTo(Description description) {
054        description.appendText("has no cause");
055    }
056
057}