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: package org.apache.wicket.util;
018:
019: import java.lang.reflect.Constructor;
020:
021: import junit.framework.Assert;
022: import junit.framework.TestCase;
023:
024: /**
025: * Base class for testing exceptions in order to make
026: * sure that they achieve 100% test coverage. Assumes
027: * that exceptions implement all of the four standard
028: * consturctors.
029: *
030: * @author Chris Turner
031: */
032: public abstract class ExceptionTestBase extends TestCase {
033:
034: /**
035: * Create the test case.
036: *
037: * @param s The test name
038: */
039: protected ExceptionTestBase(String s) {
040: super (s);
041: }
042:
043: /**
044: * Return the name of the exception class to be tested.
045: *
046: * @return The name of the exception class
047: */
048: protected abstract String getExceptionClassName();
049:
050: /**
051: * Test the no argument constructor.
052: *
053: * @throws Exception If test fails
054: */
055: public final void testEmptyConstructor() throws Exception {
056: Class c = Class.forName(getExceptionClassName());
057: Constructor constructor = c.getConstructor((Class[]) null);
058: Exception e = (Exception) constructor
059: .newInstance((Object[]) null);
060: Assert.assertNotNull("Exception should be created", e);
061: Assert.assertNull(e.getMessage());
062: Assert.assertNull(e.getCause());
063: }
064:
065: /**
066: * Test the string message constructor.
067: *
068: * @throws Exception If test fails
069: */
070: public final void testMessageConstructor() throws Exception {
071: Class c = Class.forName(getExceptionClassName());
072: Constructor constructor = c
073: .getConstructor(new Class[] { String.class });
074: Exception e = (Exception) constructor
075: .newInstance(new Object[] { "test message" });
076: Assert.assertNotNull("Exception should be created", e);
077: Assert.assertEquals("test message", e.getMessage());
078: Assert.assertNull(e.getCause());
079: }
080:
081: /**
082: * Test the cause constructor.
083: *
084: * @throws Exception If test fails
085: */
086: public final void testCauseConstructor() throws Exception {
087: NullPointerException npe = new NullPointerException();
088: Class c = Class.forName(getExceptionClassName());
089: Constructor constructor = c
090: .getConstructor(new Class[] { Throwable.class });
091: Exception e = (Exception) constructor
092: .newInstance(new Object[] { npe });
093: Assert.assertNotNull("Exception should be created", e);
094: Assert.assertSame(npe, e.getCause());
095: }
096:
097: /**
098: * Test the message and cause constructor.
099: *
100: * @throws Exception If test fails
101: */
102: public final void testMessageAndCauseConstructor() throws Exception {
103: NullPointerException npe = new NullPointerException();
104: Class c = Class.forName(getExceptionClassName());
105: Constructor constructor = c.getConstructor(new Class[] {
106: String.class, Throwable.class });
107: Exception e = (Exception) constructor.newInstance(new Object[] {
108: "test message", npe });
109: Assert.assertNotNull("Exception should be created", e);
110: Assert.assertEquals("test message", e.getMessage());
111: Assert.assertSame(npe, e.getCause());
112: }
113:
114: }
|