001: /*
002: * @(#)IChainableExceptionUTestI.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.util.throwable.v1;
028:
029: import java.io.PrintStream;
030: import java.io.PrintWriter;
031: import java.io.StringWriter;
032: import java.io.ByteArrayOutputStream;
033: import org.easymock.EasyMock;
034: import org.easymock.MockControl;
035: import junit.framework.Test;
036: import junit.framework.TestCase;
037: import junit.framework.TestSuite;
038: import net.sourceforge.groboutils.junit.v1.iftc.*;
039:
040: /**
041: * Tests the IChainableException interface.
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @version $Date: 2003/05/06 05:35:02 $
045: * @since March 17, 2002
046: */
047: public class IChainableExceptionUTestI extends InterfaceTestCase {
048:
049: //-------------------------------------------------------------------------
050: // Due to the way exceptions work, most of the logic is involved in the
051: // constructor. Hence, we'll use an alternative pattern to the standard
052: // ImplFactory, where it will return a factory (below) that will create
053: // exceptions using the correct constructor.
054:
055: public static interface IChainableExceptionFactory {
056: public IChainableException createException();
057:
058: public IChainableException createException(String message);
059:
060: public IChainableException createException(Throwable cause);
061:
062: public IChainableException createException(String message,
063: Throwable cause);
064:
065: public IChainableException createException(Throwable cause,
066: String message);
067: }
068:
069: //-------------------------------------------------------------------------
070: // Standard JUnit Class-specific declarations
071:
072: private static final Class THIS_CLASS = IChainableExceptionUTestI.class;
073:
074: public IChainableExceptionUTestI(String name, ImplFactory f) {
075: super (name, IChainableExceptionFactory.class, f);
076: }
077:
078: //-------------------------------------------------------------------------
079: // setup
080:
081: /**
082: *
083: * @exception Exception thrown under any exceptional condition.
084: */
085: protected void setUp() throws Exception {
086: super .setUp();
087:
088: // set ourself up
089: }
090:
091: private IChainableExceptionFactory createChainableExceptionFactory() {
092: return (IChainableExceptionFactory) createImplObject();
093: }
094:
095: protected IChainableException createException() {
096: IChainableException ce = createChainableExceptionFactory()
097: .createException();
098: assertIsRealChainableException(ce);
099: return ce;
100: }
101:
102: protected IChainableException createException(String message) {
103: IChainableException ce = createChainableExceptionFactory()
104: .createException(message);
105: assertIsRealChainableException(ce);
106: return ce;
107: }
108:
109: protected IChainableException createException(Throwable cause) {
110: IChainableException ce = createChainableExceptionFactory()
111: .createException(cause);
112: assertIsRealChainableException(ce);
113: return ce;
114: }
115:
116: protected IChainableException createException(String message,
117: Throwable cause) {
118: IChainableException ce = createChainableExceptionFactory()
119: .createException(message, cause);
120: assertIsRealChainableException(ce);
121: return ce;
122: }
123:
124: protected IChainableException createException(Throwable cause,
125: String message) {
126: IChainableException ce = createChainableExceptionFactory()
127: .createException(cause, message);
128: assertIsRealChainableException(ce);
129: return ce;
130: }
131:
132: //-------------------------------------------------------------------------
133: // Tests
134:
135: public void testEmptyConstructor1() {
136: IChainableException ce = createException();
137: assertNull("Cause is not null.", ce.getCause());
138: }
139:
140: public void testEmptyConstructor2() {
141: IChainableException ce = createException();
142: Throwable t = new Throwable();
143: ce.initCause(t);
144: assertEquals("Cause is not right.", t, ce.getCause());
145: }
146:
147: public void testEmptyConstructor3() {
148: IChainableException ce = createException();
149: try {
150: ce.initCause((Throwable) ce);
151: fail("Did not throw IllegalArgumentException.");
152: } catch (IllegalArgumentException iae) {
153: // check exception
154: }
155: }
156:
157: public void testCauseConstructor1() {
158: Throwable t = null;
159: IChainableException ce = createException(t);
160: assertNull("Cause is not null.", ce.getCause());
161: }
162:
163: public void testCauseConstructor2() {
164: Throwable t = new Throwable();
165: IChainableException ce = createException(t);
166: assertEquals("Cause is not right.", t, ce.getCause());
167: }
168:
169: public void testCauseConstructor3() {
170: Throwable t = new Throwable();
171: IChainableException ce = createException(t);
172: try {
173: ce.initCause(t);
174: fail("Did not throw IllegalStateException.");
175: } catch (IllegalStateException ise) {
176: // check exception
177: }
178: }
179:
180: public void testCauseConstructor4() {
181: IChainableException ce = createException((Throwable) null);
182: try {
183: ce.initCause(null);
184: fail("Did not throw IllegalStateException.");
185: } catch (IllegalStateException ise) {
186: // check exception
187: }
188: }
189:
190: public void testCauseConstructor5() {
191: IChainableException ce = createException((Throwable) null);
192: try {
193: ce.initCause(new Throwable());
194: fail("Did not throw IllegalStateException.");
195: } catch (IllegalStateException ise) {
196: // check exception
197: }
198: }
199:
200: public void testCauseConstructor6() {
201: IChainableException ce = createException(new Throwable());
202: try {
203: ce.initCause(null);
204: fail("Did not throw IllegalStateException.");
205: } catch (IllegalStateException ise) {
206: // check exception
207: }
208: }
209:
210: public void testCauseConstructor7() {
211: IChainableException ce = createException(new Throwable());
212: try {
213: // order is important here - already set the throwable, so that
214: // will be thrown first.
215: ce.initCause((Throwable) ce);
216: fail("Did not throw IllegalStateException.");
217: } catch (IllegalStateException iae) {
218: // check exception
219: }
220: }
221:
222: //-------------------------------------------------------------------------
223: // Helpers
224:
225: private void assertIsRealChainableException(IChainableException ce) {
226: assertNotNull(ce);
227: assertTrue("Class under test (" + ce.getClass()
228: + ") is not an exception.", ce instanceof Throwable);
229: }
230:
231: //-------------------------------------------------------------------------
232: // Standard JUnit declarations
233:
234: public static InterfaceTestSuite suite() {
235: InterfaceTestSuite suite = new InterfaceTestSuite(THIS_CLASS);
236:
237: return suite;
238: }
239:
240: public static void main(String[] args) {
241: String[] name = { THIS_CLASS.getName() };
242:
243: // junit.textui.TestRunner.main( name );
244: // junit.swingui.TestRunner.main( name );
245:
246: junit.textui.TestRunner.main(name);
247: }
248:
249: /**
250: *
251: * @exception Exception thrown under any exceptional condition.
252: */
253: protected void tearDown() throws Exception {
254: // tear ourself down
255:
256: super.tearDown();
257: }
258: }
|