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 018 package org.apache.commons.proxy.exception; 019 020 import junit.framework.TestCase; 021 022 /** 023 * @author James Carman 024 * @since 1.0 025 */ 026 public abstract class AbstractExceptionClassTestCase extends TestCase 027 { 028 private final Class exceptionClass; 029 030 public AbstractExceptionClassTestCase( Class exceptionClass ) 031 { 032 this.exceptionClass = exceptionClass; 033 } 034 035 public void testNoArgConstructor() throws Exception 036 { 037 Exception e = ( Exception )exceptionClass.getConstructor( new Class[] {} ).newInstance( new Object[] {} ); 038 assertNull( e.getMessage() ); 039 assertNull( e.getCause() ); 040 } 041 042 public void testMessageOnlyConstructor() throws Exception 043 { 044 final String message = "message"; 045 Exception e = ( Exception )exceptionClass.getConstructor( new Class[] { String.class } ).newInstance( new Object[] { message } ); 046 assertEquals( message, e.getMessage() ); 047 assertNull( e.getCause() ); 048 } 049 050 public void testCauseOnlyConstructor() throws Exception 051 { 052 final Exception cause = new Exception(); 053 Exception e = ( Exception )exceptionClass.getConstructor( new Class[] { Throwable.class } ).newInstance( new Object[] { cause } ); 054 assertEquals( cause.toString(), e.getMessage() ); 055 assertEquals( cause, e.getCause() ); 056 } 057 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 } ).newInstance( new Object[] { message, cause } ); 063 assertEquals( message, e.getMessage() ); 064 assertEquals( cause, e.getCause() ); 065 } 066 }