001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.proxy2.exception;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNull;
022
023import org.apache.commons.proxy2.util.AbstractTestCase;
024import org.junit.Test;
025
026public abstract class AbstractExceptionClassTestCase extends AbstractTestCase
027{
028    //**********************************************************************************************************************
029    // Fields
030    //**********************************************************************************************************************
031
032    private final Class<?> exceptionClass;
033
034    //**********************************************************************************************************************
035    // Constructors
036    //**********************************************************************************************************************
037
038    public AbstractExceptionClassTestCase(Class<?> exceptionClass)
039    {
040        this.exceptionClass = exceptionClass;
041    }
042
043    //**********************************************************************************************************************
044    // Other Methods
045    //**********************************************************************************************************************
046
047    @Test
048    public void testCauseOnlyConstructor() throws Exception
049    {
050        final Exception cause = new Exception();
051        Exception e = (Exception) exceptionClass.getConstructor(new Class[] { Throwable.class }).newInstance(
052                new Object[] { cause });
053        assertEquals(cause.toString(), e.getMessage());
054        assertEquals(cause, e.getCause());
055    }
056
057    @Test
058    public void testMessageAndCauseConstructor() throws Exception
059    {
060        final Exception cause = new Exception();
061        final String message = "message";
062        Exception e = (Exception) exceptionClass.getConstructor(new Class[] { String.class, Throwable.class })
063                .newInstance(new Object[] { message, cause });
064        assertEquals(message, e.getMessage());
065        assertEquals(cause, e.getCause());
066    }
067
068    @Test
069    public void testMessageOnlyConstructor() throws Exception
070    {
071        final String message = "message";
072        Exception e = (Exception) exceptionClass.getConstructor(new Class[] { String.class }).newInstance(
073                new Object[] { message });
074        assertEquals(message, e.getMessage());
075        assertNull(e.getCause());
076    }
077
078    @Test
079    public void testNoArgConstructor() throws Exception
080    {
081        Exception e = (Exception) exceptionClass.getConstructor(new Class[] {}).newInstance(new Object[] {});
082        assertNull(e.getMessage());
083        assertNull(e.getCause());
084    }
085}