001: /*
002: * @(#)ChainableExceptionHelperUTest.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:
039: /**
040: * Tests the ChainableExceptionHelper class.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version $Date: 2003/02/10 22:52:42 $
044: * @since March 17, 2002
045: */
046: public class ChainableExceptionHelperUTest extends TestCase {
047: //-------------------------------------------------------------------------
048: // Standard JUnit Class-specific declarations
049:
050: private static final Class THIS_CLASS = ChainableExceptionHelperUTest.class;
051:
052: public ChainableExceptionHelperUTest(String name) {
053: super (name);
054: }
055:
056: //-------------------------------------------------------------------------
057: // setup
058:
059: /**
060: *
061: * @exception Exception thrown under any exceptional condition.
062: */
063: protected void setUp() throws Exception {
064: super .setUp();
065:
066: // set ourself up
067: }
068:
069: //-------------------------------------------------------------------------
070: // Tests
071:
072: public void testConstructor1a() {
073: Throwable source = new Throwable();
074: new ChainableExceptionHelper(source);
075: }
076:
077: public void testConstructor1b() {
078: try {
079: new ChainableExceptionHelper(null);
080: fail("Did not throw IllegalArgumentException");
081: } catch (IllegalArgumentException iae) {
082: // test exception?
083: }
084: }
085:
086: public void testConstructor2a() {
087: Throwable source = new Throwable();
088: Throwable cause = new Throwable();
089: new ChainableExceptionHelper(source, null);
090: }
091:
092: public void testConstructor2b() {
093: Throwable source = new Throwable();
094: Throwable cause = new Throwable();
095: new ChainableExceptionHelper(source, cause);
096: }
097:
098: public void testConstructor2c() {
099: Throwable source = new Throwable();
100: Throwable cause = new Throwable();
101: try {
102: new ChainableExceptionHelper(null, null);
103: fail("Did not throw IllegalArgumentException");
104: } catch (IllegalArgumentException iae) {
105: // test exception?
106: }
107: }
108:
109: public void testConstructor2d() {
110: Throwable source = new Throwable();
111: Throwable cause = new Throwable();
112: try {
113: new ChainableExceptionHelper(null, cause);
114: fail("Did not throw IllegalArgumentException");
115: } catch (IllegalArgumentException iae) {
116: // test exception?
117: }
118: }
119:
120: public void testConstructor2e() {
121: Throwable source = new Throwable();
122: Throwable cause = new Throwable();
123: try {
124: new ChainableExceptionHelper(source, source);
125: fail("Did not throw IllegalArgumentException");
126: } catch (IllegalArgumentException iae) {
127: // test exception?
128: }
129: }
130:
131: public void testGetCause1() {
132: Throwable source = new Throwable();
133: Throwable cause = new Throwable();
134: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
135: source);
136:
137: assertNull("Does not have a null cause.", ceh.getCause());
138: }
139:
140: public void testGetCause2() {
141: Throwable source = new Throwable();
142: Throwable cause = new Throwable();
143: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
144: source, null);
145:
146: assertNull("Does not have a null cause.", ceh.getCause());
147: }
148:
149: public void testGetCause3() {
150: Throwable source = new Throwable();
151: Throwable cause = new Throwable();
152: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
153: source, cause);
154:
155: assertEquals("Does not have right cause.", ceh.getCause(),
156: cause);
157: }
158:
159: public void testInitCause1() {
160: Throwable source = new Throwable();
161: Throwable cause = new Throwable();
162: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
163: source);
164:
165: assertEquals("Did not return right exception.", ceh
166: .initCause(cause), source);
167: }
168:
169: public void testInitCause2() {
170: Throwable source = new Throwable();
171: Throwable cause = new Throwable();
172: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
173: source);
174:
175: assertEquals("Did not return right exception.", ceh
176: .initCause(null), source);
177: }
178:
179: public void testInitCause3() {
180: Throwable source = new Throwable();
181: Throwable cause = new Throwable();
182: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
183: source, cause);
184:
185: try {
186: ceh.initCause(cause);
187: fail("Did not throw IllegalStateException");
188: } catch (IllegalStateException ise) {
189: // test exception?
190: }
191: }
192:
193: public void testInitGetCause1() {
194: Throwable source = new Throwable();
195: Throwable cause = new Throwable();
196: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
197: source);
198:
199: ceh.initCause(cause);
200: assertEquals("Did not return right cause.", ceh.getCause(),
201: cause);
202: }
203:
204: public void testInitGetCause2() {
205: Throwable source = new Throwable();
206: Throwable cause = new Throwable();
207: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
208: source);
209:
210: ceh.initCause(null);
211: assertNull("Did not return right cause.", ceh.getCause());
212: }
213:
214: public void testInitGetCause3() {
215: Throwable source = new Throwable();
216: Throwable cause = new Throwable();
217: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
218: source, cause);
219:
220: assertEquals("Did not return right cause.", ceh.getCause(),
221: cause);
222: }
223:
224: public void testInitGetCause4() {
225: Throwable source = new Throwable();
226: Throwable cause = new Throwable();
227: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
228: source, null);
229:
230: assertNull("Did not return right cause.", ceh.getCause());
231: }
232:
233: public void testPrintStackTrace1() {
234: Throwable source = new Throwable();
235: Throwable cause = new Throwable();
236: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
237: source);
238:
239: StringWriter sw = new StringWriter();
240: ceh.printStackTrace(new PrintWriter(sw, true));
241:
242: // check sw result.
243: assertTrue("Not a valid length", sw.toString().length() > 0);
244: }
245:
246: public void testPrintStackTrace2() {
247: Throwable source = new Throwable();
248: Throwable cause = new Throwable();
249: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
250: source);
251:
252: ByteArrayOutputStream baos = new ByteArrayOutputStream();
253: ceh.printStackTrace(new PrintStream(baos, true));
254:
255: // check sw result.
256: assertTrue("Not a valid length", baos.toString().length() > 0);
257: }
258:
259: public void testPrintStackTrace3() {
260: Throwable source = new Throwable();
261: Throwable cause = new Throwable();
262: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
263: source);
264:
265: ceh.initCause(cause);
266: StringWriter sw = new StringWriter();
267: ceh.printStackTrace(new PrintWriter(sw, true));
268:
269: // check sw result.
270: assertTrue("Not a valid length", sw.toString().length() > 0);
271: }
272:
273: public void testPrintStackTrace4() {
274: Throwable source = new Throwable();
275: Throwable cause = new Throwable();
276: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
277: source);
278:
279: ceh.initCause(cause);
280: ByteArrayOutputStream baos = new ByteArrayOutputStream();
281: ceh.printStackTrace(new PrintStream(baos, true));
282:
283: // check sw result.
284: assertTrue("Not a valid length", baos.toString().length() > 0);
285: }
286:
287: public void testInitGetCause5() {
288: Throwable source = new Throwable();
289: Throwable cause = new Throwable();
290: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
291: source);
292:
293: ceh.initCause(null);
294: StringWriter sw = new StringWriter();
295: ceh.printStackTrace(new PrintWriter(sw, true));
296:
297: // check sw result.
298: assertTrue("Not a valid length", sw.toString().length() > 0);
299: }
300:
301: public void testInitGetCause6() {
302: Throwable source = new Throwable();
303: Throwable cause = new Throwable();
304: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
305: source);
306:
307: ceh.initCause(null);
308: ByteArrayOutputStream baos = new ByteArrayOutputStream();
309: ceh.printStackTrace(new PrintStream(baos, true));
310:
311: // check sw result.
312: assertTrue("Not a valid length", baos.toString().length() > 0);
313: }
314:
315: public void testInitGetCause7() {
316: Throwable source = new Throwable();
317: Throwable cause = new Throwable();
318: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
319: source, cause);
320:
321: StringWriter sw = new StringWriter();
322: ceh.printStackTrace(new PrintWriter(sw, true));
323:
324: // check sw result.
325: assertTrue("Not a valid length", sw.toString().length() > 0);
326: }
327:
328: public void testInitGetCause8() {
329: Throwable source = new Throwable();
330: Throwable cause = new Throwable();
331: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
332: source, cause);
333:
334: ByteArrayOutputStream baos = new ByteArrayOutputStream();
335: ceh.printStackTrace(new PrintStream(baos, true));
336:
337: // check sw result.
338: assertTrue("Not a valid length", baos.toString().length() > 0);
339: }
340:
341: public void testInitGetCause9() {
342: Throwable source = new Throwable();
343: Throwable cause = new Throwable();
344: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
345: source, null);
346:
347: StringWriter sw = new StringWriter();
348: ceh.printStackTrace(new PrintWriter(sw, true));
349:
350: // check sw result.
351: assertTrue("Not a valid length", sw.toString().length() > 0);
352: }
353:
354: public void testInitGetCause10() {
355: Throwable source = new Throwable();
356: Throwable cause = new Throwable();
357: ChainableExceptionHelper ceh = new ChainableExceptionHelper(
358: source, null);
359:
360: ByteArrayOutputStream baos = new ByteArrayOutputStream();
361: ceh.printStackTrace(new PrintStream(baos, true));
362:
363: // check sw result.
364: assertTrue("Not a valid length", baos.toString().length() > 0);
365: }
366:
367: //-------------------------------------------------------------------------
368: // Helpers
369:
370: //-------------------------------------------------------------------------
371: // Standard JUnit declarations
372:
373: public static Test suite() {
374: TestSuite suite = new TestSuite(THIS_CLASS);
375:
376: // Test the implementation's interface conformity.
377: /*
378: suite.addTest( IxUTestI.suite(
379: new ImplementationCreator[] {
380: new ImplementationCreator() {
381: public Object createImplementedObject() {
382: // XXXXXXXXXXXXXXXXXXXXXXXX
383: }
384: },
385: } ) );
386: */
387:
388: return suite;
389: }
390:
391: public static void main(String[] args) {
392: String[] name = { THIS_CLASS.getName() };
393:
394: // junit.textui.TestRunner.main( name );
395: // junit.swingui.TestRunner.main( name );
396:
397: junit.textui.TestRunner.main(name);
398: }
399:
400: /**
401: *
402: * @exception Exception thrown under any exceptional condition.
403: */
404: protected void tearDown() throws Exception {
405: // tear ourself down
406:
407: super.tearDown();
408: }
409: }
|