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.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 exception that has no
024 * {@link Throwable#getCause() cause}.
025 * 
026 * @author rwoo
027 * 
028 * @param <T>
029 *            an exception subclass
030 */
031public class ExceptionNoCauseMatcher<T extends Exception> extends
032        BaseMatcher<T> {
033
034    /*
035     * (non-Javadoc)
036     * 
037     * @see org.hamcrest.Matcher#matches(java.lang.Object)
038     */
039    public boolean matches(Object obj) {
040        if (!(obj instanceof Exception))
041            return false;
042
043        Exception exception = (Exception) obj;
044
045        return exception.getCause() == null;
046    }
047
048    /*
049     * (non-Javadoc)
050     * 
051     * @see org.hamcrest.SelfDescribing#describeTo(org.hamcrest.Description)
052     */
053    public void describeTo(Description description) {
054        description.appendText("has no cause");
055    }
056
057}