KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > exception > NestableDelegateTestCase


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.exception;
17
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.EOFException JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintStream JavaDoc;
22 import java.io.PrintWriter JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27
28 /**
29  * Tests the org.apache.commons.lang.exception.NestableDelegate class.
30  *
31  * @author <a HREF="mailto:steven@caswell.name">Steven Caswell</a>
32  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
33  * @version $Id: NestableDelegateTestCase.java 161244 2005-04-14 06:16:36Z ggregory $
34  */

35 public class NestableDelegateTestCase extends junit.framework.TestCase {
36     private static final String JavaDoc CONSTRUCTOR_FAILED_MSG =
37     "The Nestable implementation passed to the NestableDelegate(Nestable) constructor must extend java.lang.Throwable";
38
39     private static final String JavaDoc PARTIAL_STACK_TRACE =
40         "ThrowableNestedNestable partial stack trace place-holder";
41
42     protected String JavaDoc lineSeparator;
43
44     /**
45      * Construct a new instance of NestableDelegateTestCase with the specified name
46      */

47     public NestableDelegateTestCase(String JavaDoc name)
48     {
49         super(name);
50     }
51
52     /**
53      * Set up instance variables required by this test case.
54      */

55     public void setUp()
56     {
57         lineSeparator = System.getProperty("line.separator");
58     }
59     
60     public static Test suite()
61     {
62         return new TestSuite(NestableDelegateTestCase.class);
63     }
64     
65     /**
66      * Tear down instance variables required by this test case.
67      */

68     public void tearDown()
69     {
70         lineSeparator = null;
71     }
72     
73     /**
74      * Test the implementation
75      */

76     public void testNestableDelegateConstructor()
77     {
78         String JavaDoc msg = null;
79         boolean constructorFailed = false;
80         try
81         {
82             NestableDelegate nonThrowableCause = new NestableDelegate(new NonThrowableNestable());
83         }
84         catch(IllegalArgumentException JavaDoc iae)
85         {
86             constructorFailed = true;
87             msg = iae.getMessage();
88         }
89         assertTrue("nestable delegate constructor with non-throwable cause failed == true", constructorFailed);
90         assertTrue("constructor failed exception msg == " + CONSTRUCTOR_FAILED_MSG,
91             msg.equals(CONSTRUCTOR_FAILED_MSG));
92
93         constructorFailed = false;
94         try
95         {
96             NestableDelegate nd1 = new NestableDelegate(new ThrowableNestable());
97         }
98         catch(IllegalArgumentException JavaDoc iae)
99         {
100             constructorFailed = true;
101         }
102         assertTrue("nestable delegate constructor with throwable cause failed == false", !constructorFailed);
103     }
104
105     public void testNestableDelegateGetMessage()
106     {
107         Nestable ne1 = new ThrowableNestable();
108         assertTrue("ThrowableNestable ne1 getMessage() == ThrowableNestable exception",
109             ne1.getMessage().equals("ThrowableNestable exception"));
110         NestableDelegate nd1 = new NestableDelegate(ne1);
111         assertTrue("nd1 getMessage() == " + ne1.getCause().getMessage(),
112             nd1.getMessage("base").equals("base: " + ne1.getCause().getMessage()));
113         
114         Nestable ne2 = new ThrowableNestedNestable(new Exception JavaDoc("nested exception 2"));
115         NestableDelegate nd2 = new NestableDelegate(ne2);
116         assertTrue("nd2 getMessage() == base: " + ne2.getCause().getMessage(),
117             nd2.getMessage("base").equals("base: " + ne2.getCause().getMessage()));
118     }
119
120     public void testNestableDelegateGetThrowableCount()
121     {
122         Nestable n = null;
123         NestableDelegate d = null;
124         
125         n = new NestableDelegateTester1();
126         d = new NestableDelegate(n);
127         doNestableDelegateGetThrowableCount(d, 1);
128         
129         n = new NestableDelegateTester1("level 1");
130         d = new NestableDelegate(n);
131         doNestableDelegateGetThrowableCount(d, 1);
132         
133         n = new NestableDelegateTester1(new Exception JavaDoc());
134         d = new NestableDelegate(n);
135         doNestableDelegateGetThrowableCount(d, 2);
136         
137         n = new NestableDelegateTester1(new Exception JavaDoc("level 2"));
138         d = new NestableDelegate(n);
139         doNestableDelegateGetThrowableCount(d, 2);
140         
141         n = new NestableDelegateTester1("level 1",
142                 new NestableDelegateTester2("level 2",
143                     new NestableDelegateTester1(
144                         new NestableDelegateTester2("level 4",
145                             new Exception JavaDoc("level 5")
146                         )
147                     )
148                 )
149             );
150         d = new NestableDelegate(n);
151         doNestableDelegateGetThrowableCount(d, 5);
152     }
153
154     private void doNestableDelegateGetThrowableCount(NestableDelegate d, int len)
155     {
156         // Compare the lengths
157
assertEquals("delegate length", len, d.getThrowableCount());
158     }
159     
160     public void testNestableDelegateGetMessages()
161     {
162         Nestable n = null;
163         NestableDelegate d = null;
164         String JavaDoc msgs[] = null;
165         
166         msgs = new String JavaDoc[1];
167         n = new NestableDelegateTester1();
168         d = new NestableDelegate(n);
169         doNestableDelegateGetMessages(d, msgs);
170         
171         msgs = new String JavaDoc[1];
172         msgs[0] = "level 1";
173         n = new NestableDelegateTester1(msgs[0]);
174         d = new NestableDelegate(n);
175         doNestableDelegateGetMessages(d, msgs);
176
177         msgs = new String JavaDoc[2];
178         n = new NestableDelegateTester1(new Exception JavaDoc());
179         d = new NestableDelegate(n);
180         doNestableDelegateGetMessages(d, msgs);
181
182         msgs = new String JavaDoc[2];
183         msgs[0] = null;
184         msgs[1] = "level 2";
185         n = new NestableDelegateTester1(new Exception JavaDoc(msgs[1]));
186         d = new NestableDelegate(n);
187         doNestableDelegateGetMessages(d, msgs);
188  
189         msgs = new String JavaDoc[5];
190         msgs[0] = "level 1";
191         msgs[1] = "level 2";
192         msgs[2] = null;
193         msgs[3] = "level 4";
194         msgs[4] = "level 5";
195         n = new NestableDelegateTester1(msgs[0],
196                 new NestableDelegateTester2(msgs[1],
197                     new NestableDelegateTester1(
198                         new NestableDelegateTester2(msgs[3],
199                             new Exception JavaDoc(msgs[4])
200                         )
201                     )
202                 )
203             );
204         d = new NestableDelegate(n);
205         doNestableDelegateGetMessages(d, msgs);
206     }
207
208     private void doNestableDelegateGetMessages(NestableDelegate d, String JavaDoc[] nMsgs)
209     {
210         // Compare the messages
211
String JavaDoc[] dMsgs = d.getMessages();
212         assertEquals("messages length", nMsgs.length, dMsgs.length);
213         for(int i = 0; i < nMsgs.length; i++)
214         {
215             assertEquals("message " + i, nMsgs[i], dMsgs[i]);
216         }
217     }
218
219     public void testNestableDelegateGetMessageN()
220     {
221         Nestable n = null;
222         NestableDelegate d = null;
223         String JavaDoc[] msgs = new String JavaDoc[5];
224         msgs[0] = "level 1";
225         msgs[1] = "level 2";
226         msgs[2] = null;
227         msgs[3] = "level 4";
228         msgs[4] = "level 5";
229         n = new NestableDelegateTester1(msgs[0],
230                 new NestableDelegateTester2(msgs[1],
231                     new NestableDelegateTester1(
232                         new NestableDelegateTester2(msgs[3],
233                             new Exception JavaDoc(msgs[4])
234                         )
235                     )
236                 )
237             );
238         d = new NestableDelegate(n);
239         for(int i = 0; i < msgs.length; i++)
240         {
241             assertEquals("message " + i, msgs[i], d.getMessage(i));
242         }
243         
244         // Test for index out of bounds
245
try
246         {
247             String JavaDoc msg = d.getMessage(-1);
248             fail("getMessage(-1) should have thrown IndexOutOfBoundsException");
249         }
250         catch(IndexOutOfBoundsException JavaDoc ioode)
251         {
252         }
253         try
254         {
255             String JavaDoc msg = d.getMessage(msgs.length + 100);
256             fail("getMessage(999) should have thrown IndexOutOfBoundsException");
257         }
258         catch(IndexOutOfBoundsException JavaDoc ioode)
259         {
260         }
261     }
262
263     public void testNestableDelegateGetThrowableN()
264     {
265         Nestable n = null;
266         NestableDelegate d = null;
267         String JavaDoc msgs[] = null;
268         Class JavaDoc[] throwables = null;
269         
270         msgs = new String JavaDoc[2];
271         msgs[0] = null;
272         msgs[1] = "level 2";
273         throwables = new Class JavaDoc[2];
274         throwables[0] = NestableDelegateTester1.class;
275         throwables[1] = Exception JavaDoc.class;
276         n = new NestableDelegateTester1(new Exception JavaDoc(msgs[1]));
277         d = new NestableDelegate(n);
278         doNestableDelegateGetThrowableN(d, throwables, msgs);
279  
280         msgs = new String JavaDoc[5];
281         msgs[0] = "level 1";
282         msgs[1] = "level 2";
283         msgs[2] = null;
284         msgs[3] = "level 4";
285         msgs[4] = "level 5";
286         throwables = new Class JavaDoc[5];
287         throwables[0] = NestableDelegateTester1.class;
288         throwables[1] = NestableDelegateTester2.class;
289         throwables[2] = NestableDelegateTester1.class;
290         throwables[3] = NestableDelegateTester2.class;
291         throwables[4] = Exception JavaDoc.class;
292         n = new NestableDelegateTester1(msgs[0],
293                 new NestableDelegateTester2(msgs[1],
294                     new NestableDelegateTester1(
295                         new NestableDelegateTester2(msgs[3],
296                             new Exception JavaDoc(msgs[4])
297                             )
298                         )
299                     )
300                 );
301         d = new NestableDelegate(n);
302         doNestableDelegateGetThrowableN(d, throwables, msgs);
303     }
304
305     private void doNestableDelegateGetThrowableN(NestableDelegate d, Class JavaDoc[] classes, String JavaDoc[] msgs)
306     {
307         Throwable JavaDoc t = null;
308         String JavaDoc msg = null;
309         
310         for(int i = 0; i < classes.length; i++)
311         {
312             t = d.getThrowable(i);
313             assertEquals("throwable class", classes[i], t.getClass());
314             if(Nestable.class.isInstance(t))
315             {
316                 msg = ((Nestable) t).getMessage(0);
317             }
318             else
319             {
320                 msg = t.getMessage();
321             }
322             assertEquals("throwable message", msgs[i], msg);
323         }
324         
325         // Test for index out of bounds
326
try
327         {
328             t = d.getThrowable(-1);
329             fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
330         }
331         catch(IndexOutOfBoundsException JavaDoc ioobe)
332         {
333         }
334         try
335         {
336             t = d.getThrowable(999);
337             fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
338         }
339         catch(IndexOutOfBoundsException JavaDoc ioobe)
340         {
341         }
342     }
343
344     public void testNestableDelegateGetThrowables()
345     {
346         Nestable n = null;
347         NestableDelegate d = null;
348         String JavaDoc msgs[] = null;
349         Class JavaDoc[] throwables = null;
350         
351         msgs = new String JavaDoc[2];
352         msgs[0] = null;
353         msgs[1] = "level 2";
354         throwables = new Class JavaDoc[2];
355         throwables[0] = NestableDelegateTester1.class;
356         throwables[1] = Exception JavaDoc.class;
357         n = new NestableDelegateTester1(new Exception JavaDoc(msgs[1]));
358         d = new NestableDelegate(n);
359         doNestableDelegateGetThrowables(d, throwables, msgs);
360  
361         msgs = new String JavaDoc[5];
362         msgs[0] = "level 1";
363         msgs[1] = "level 2";
364         msgs[2] = null;
365         msgs[3] = "level 4";
366         msgs[4] = "level 5";
367         throwables = new Class JavaDoc[5];
368         throwables[0] = NestableDelegateTester1.class;
369         throwables[1] = NestableDelegateTester2.class;
370         throwables[2] = NestableDelegateTester1.class;
371         throwables[3] = NestableDelegateTester2.class;
372         throwables[4] = Exception JavaDoc.class;
373         n = new NestableDelegateTester1(msgs[0],
374                 new NestableDelegateTester2(msgs[1],
375                     new NestableDelegateTester1(
376                         new NestableDelegateTester2(msgs[3],
377                             new Exception JavaDoc(msgs[4])
378                         )
379                     )
380                 )
381             );
382         d = new NestableDelegate(n);
383         doNestableDelegateGetThrowables(d, throwables, msgs);
384     }
385     
386     private void doNestableDelegateGetThrowables(NestableDelegate d, Class JavaDoc[] classes, String JavaDoc[] msgs)
387     {
388         Throwable JavaDoc[] throwables = null;
389         String JavaDoc msg = null;
390
391         throwables = d.getThrowables();
392         assertEquals("throwables length", classes.length, throwables.length);
393         for(int i = 0; i < classes.length; i++)
394         {
395             assertEquals("throwable class", classes[i], throwables[i].getClass());
396             Throwable JavaDoc t = throwables[i];
397             if(Nestable.class.isInstance(t))
398             {
399                 msg = ((Nestable) t).getMessage(0);
400             }
401             else
402             {
403                 msg = t.getMessage();
404             }
405             assertEquals("throwable message", msgs[i], msg);
406         }
407     }
408
409     public void testIndexOfThrowable()
410     {
411         Nestable n = null;
412         NestableDelegate d = null;
413         String JavaDoc msgs[] = null;
414         Class JavaDoc[] throwables = null;
415         
416         msgs = new String JavaDoc[5];
417         msgs[0] = "level 1";
418         msgs[1] = "level 2";
419         msgs[2] = null;
420         msgs[3] = "level 4";
421         msgs[4] = "level 5";
422         throwables = new Class JavaDoc[5];
423         throwables[0] = NestableDelegateTester1.class;
424         throwables[1] = NestableDelegateTester2.class;
425         throwables[2] = NestableDelegateTester1.class;
426         throwables[3] = NestableDelegateTester2.class;
427         throwables[4] = EOFException JavaDoc.class;
428         int[] indexes = {0, 1, 0, 1, 4};
429         n = new NestableDelegateTester1(msgs[0],
430                 new NestableDelegateTester2(msgs[1],
431                     new NestableDelegateTester1(
432                         new NestableDelegateTester2(msgs[3],
433                             new EOFException JavaDoc(msgs[4])
434                         )
435                     )
436                 )
437             );
438         d = new NestableDelegate(n);
439         for(int i = 0; i < throwables.length; i++)
440         {
441             doNestableDelegateIndexOfThrowable(d, throwables[i], 0, indexes[i], msgs[indexes[i]]);
442         }
443         doNestableDelegateIndexOfThrowable(d, NestableDelegateTester2.class, 2, 3, msgs[3]);
444         doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 1, 2, msgs[2]);
445         doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 3, -1, null);
446         doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 4, -1, null);
447         doNestableDelegateIndexOfThrowable(d, EOFException JavaDoc.class, 2, 4, msgs[4]);
448         doNestableDelegateIndexOfThrowable(d, IOException JavaDoc.class, 2, 4, msgs[4]);
449         doNestableDelegateIndexOfThrowable(d, Exception JavaDoc.class, 2, 2, msgs[2]);
450         doNestableDelegateIndexOfThrowable(d, Exception JavaDoc.class, 0, 0, msgs[0]);
451         doNestableDelegateIndexOfThrowable(d, java.util.Date JavaDoc.class, 0, -1, null);
452         doNestableDelegateIndexOfThrowable(d, null, 0, -1, null);
453         
454         // Test for index out of bounds
455
try
456         {
457             int index = d.indexOfThrowable(NestableDelegateTester1.class, -1);
458             fail("method should have thrown IndexOutOfBoundsException");
459         }
460         catch(IndexOutOfBoundsException JavaDoc iooob)
461         {
462         }
463         try
464         {
465             int index = d.indexOfThrowable(NestableDelegateTester1.class, 5);
466             fail("method should have thrown IndexOutOfBoundsException");
467         }
468         catch(IndexOutOfBoundsException JavaDoc iooob)
469         {
470         }
471     }
472
473     private void doNestableDelegateIndexOfThrowable(NestableDelegate d, Class JavaDoc type, int fromIndex, int expectedIndex, String JavaDoc expectedMsg)
474     {
475         Throwable JavaDoc t = null;
476         
477         int index = d.indexOfThrowable(type, fromIndex);
478         assertEquals("index of throwable " + (type == null ? "null" : type.getName()), expectedIndex, index);
479         if(expectedIndex > -1)
480         {
481             t = d.getThrowable(index);
482             if(expectedMsg != null)
483             {
484                 String JavaDoc msg = null;
485                 if(Nestable.class.isInstance(t))
486                 {
487                     msg = ((Nestable) t).getMessage(0);
488                 }
489                 else
490                 {
491                     msg = t.getMessage();
492                 }
493                 assertEquals("message of indexed throwable", expectedMsg, msg);
494             }
495         }
496     }
497     
498     public void testNestableDelegetePrintStackTrace()
499     {
500         int lineSepLen = lineSeparator.length();
501         int partialStackTraceLen = PARTIAL_STACK_TRACE.length();
502         Nestable ne3 = new ThrowableNestedNestable(new Exception JavaDoc("nested exception 3"));
503         NestableDelegate nd3 = new NestableDelegate(ne3);
504
505         ByteArrayOutputStream JavaDoc baos1 = new ByteArrayOutputStream JavaDoc();
506         PrintStream JavaDoc ps1 = new PrintStream JavaDoc(baos1);
507         nd3.printStackTrace(ps1);
508         String JavaDoc stack1 = baos1.toString();
509         assertTrue("stack trace startsWith", stack1.startsWith(PARTIAL_STACK_TRACE));
510
511         Nestable n = new NestableDelegateTester1("level 1",
512                 new NestableDelegateTester2("level 2",
513                     new NestableDelegateTester1(
514                         new NestableDelegateTester2("level 4",
515                             new Exception JavaDoc("level 5")
516                         )
517                     )
518                 )
519             );
520         NestableDelegate d = new NestableDelegate(n);
521         
522         // Only testing the flags for jdk1.3 and below
523
if (!ExceptionUtils.isThrowableNested()) {
524             NestableDelegate.topDown = true; NestableDelegate.trimStackFrames = true;
525             checkStackTrace(d, true, true, NestableDelegateTester1.class.getName()+": level 1", 24);
526             NestableDelegate.topDown = true; NestableDelegate.trimStackFrames = false;
527             checkStackTrace(d, true, false, NestableDelegateTester1.class.getName()+": level 1", 80);
528             NestableDelegate.topDown = false; NestableDelegate.trimStackFrames = true;
529             checkStackTrace(d, false, true, "java.lang.Exception: level 5", 24);
530             NestableDelegate.topDown = false; NestableDelegate.trimStackFrames = false;
531             checkStackTrace(d, false, false, "java.lang.Exception: level 5", 80);
532             NestableDelegate.topDown = true; NestableDelegate.trimStackFrames = true;
533         }
534     }
535     private void checkStackTrace(NestableDelegate d, boolean topDown, boolean trimStackFrames,
536             String JavaDoc startsWith, int expCount) {
537         ByteArrayOutputStream JavaDoc baos1 = new ByteArrayOutputStream JavaDoc();
538         PrintStream JavaDoc ps1 = new PrintStream JavaDoc(baos1);
539         d.printStackTrace(ps1);
540         String JavaDoc stack1 = baos1.toString();
541         int actCount = countLines(stack1);
542         assertTrue("topDown: "+topDown+", trimStackFrames: "+trimStackFrames+" startsWith",
543             stack1.startsWith(startsWith));
544         // test is unreliable, as count varies depending on JUnit version/where main method is
545
// assertEquals("topDown: "+topDown+", trimStackFrames: "+trimStackFrames+" lineCount",
546
// expCount, actCount);
547
}
548     private int countLines(String JavaDoc s) {
549         if (s == null) return 0;
550         
551         int i = 0, ndx = -1;
552         while ((ndx = s.indexOf("\n", ndx+1)) != -1) {
553             i++;
554         }
555         return i;
556     }
557     
558     public static void main(String JavaDoc args[])
559     {
560         TestRunner.run(suite());
561     }
562 }
563
564 /**
565  * Nestable and Throwable class which can be passed to the NestableDelegate
566  * constructor. Used for testing various methods which iterate through the
567  * nested causes.
568  */

569 class NestableDelegateTester1 extends Exception JavaDoc implements Nestable
570 {
571     private Throwable JavaDoc cause = null;
572
573     public NestableDelegateTester1()
574     {
575         super();
576     }
577
578     public NestableDelegateTester1(String JavaDoc reason, Throwable JavaDoc cause)
579     {
580         super(reason);
581         this.cause = cause;
582     }
583     
584     public NestableDelegateTester1(String JavaDoc reason)
585     {
586         super(reason);
587     }
588     
589     public NestableDelegateTester1(Throwable JavaDoc cause)
590     {
591         super();
592         this.cause = cause;
593     }
594     
595     /**
596      * @see Nestable#getThrowables()
597      * Returns zero-length <code>Throwable</code> array for this test.
598      */

599     public Throwable JavaDoc[] getThrowables()
600     {
601         return new Throwable JavaDoc[0];
602     }
603     
604     /**
605      * @see Nestable#getMessages()
606      * Returns zero-length String array for this test.
607      */

608     public String JavaDoc[] getMessages()
609     {
610         return new String JavaDoc[0];
611     }
612     
613     /**
614      * @see Nestable#indexOfThrowable(Class)
615      * Returns -1 for this test.
616      */

617     public int indexOfThrowable(Class JavaDoc type)
618     {
619         return -1;
620     }
621     
622     /**
623      * @see Nestable#getThrowable(int)
624      * Returns <code>null</code> for this test.
625      */

626     public Throwable JavaDoc getThrowable(int index)
627     {
628         return null;
629     }
630     
631     /**
632      * @see Nestable#getThrowableCount()
633      * Returns 1 for this test.
634      */

635     public int getThrowableCount()
636     {
637         return 1;
638     }
639     
640     /**
641      * @see Nestable#getCause()
642      */

643     public Throwable JavaDoc getCause()
644     {
645         return cause;
646     }
647     
648     /**
649      * Empty method to satisfy the implemented interface. Does nothing
650      * in this test.
651      *
652      * @param out The writer to use.
653      */

654     public void printPartialStackTrace(PrintWriter JavaDoc out)
655     {
656         super.printStackTrace(out);
657     }
658     
659     /**
660      * @see Nestable#getMessage(int)
661      */

662     public String JavaDoc getMessage(int index)
663     {
664         if(index == 0)
665         {
666             return super.getMessage();
667         }
668         else
669         {
670             return "";
671         }
672     }
673     
674     /**
675      * @see Nestable#indexOfThrowable(Class, int)
676      * Returns -1 for this test.
677      */

678     public int indexOfThrowable(Class JavaDoc type, int fromIndex)
679     {
680         return -1;
681     }
682     
683 }
684
685 /**
686  * Nestable and Throwable class which can be passed to the NestableDelegate
687  * constructor. Used for testing various methods which iterate through the
688  * nested causes.
689  */

690 class NestableDelegateTester2 extends Throwable JavaDoc implements Nestable
691 {
692     private Throwable JavaDoc cause = null;
693
694     public NestableDelegateTester2()
695     {
696         super();
697     }
698     
699     public NestableDelegateTester2(String JavaDoc reason, Throwable JavaDoc cause)
700     {
701         super(reason);
702         this.cause = cause;
703     }
704     
705     public NestableDelegateTester2(String JavaDoc reason)
706     {
707         super(reason);
708     }
709     
710     public NestableDelegateTester2(Throwable JavaDoc cause)
711     {
712         super();
713         this.cause = cause;
714     }
715     
716     /**
717      * @see Nestable#getThrowables()
718      * Returns zero-length <code>Throwable</code> array for this test.
719      */

720     public Throwable JavaDoc[] getThrowables()
721     {
722         return new Throwable JavaDoc[0];
723     }
724     
725     /**
726      * @see Nestable#getMessages()
727      * Returns zero-length String array for this test.
728      */

729     public String JavaDoc[] getMessages()
730     {
731         return new String JavaDoc[0];
732     }
733     
734     /**
735      * @see Nestable#indexOfThrowable(Class)
736      * Returns -1 for this test.
737      */

738     public int indexOfThrowable(Class JavaDoc type)
739     {
740         return -1;
741     }
742     
743     /**
744      * @see Nestable#getThrowable(int)
745      * Returns <code>null</code> for this test.
746      */

747     public Throwable JavaDoc getThrowable(int index)
748     {
749         return null;
750     }
751     
752     /**
753      * @see Nestable#getThrowableCount()
754      * Returns 1 for this test.
755      *
756      * @return 1
757      */

758     public int getThrowableCount()
759     {
760         return 1;
761     }
762     
763     /**
764      * @see Nestable#getCause()
765      */

766     public Throwable JavaDoc getCause()
767     {
768         return cause;
769     }
770     
771     /**
772      * Empty method to satisfy the implemented interface. Does nothing
773      * in this test.
774      *
775      * @param out The writer to use.
776      */

777     public void printPartialStackTrace(PrintWriter JavaDoc out)
778     {
779         super.printStackTrace(out);
780     }
781     
782     /**
783      * @see Nestable#getMessage(int)
784      */

785     public String JavaDoc getMessage(int index)
786     {
787         if(index == 0)
788         {
789             return super.getMessage();
790         }
791         else
792         {
793             return "";
794         }
795     }
796     
797     /**
798      * @see Nestable#indexOfThrowable(Class, int)
799      * Returns -1 for this test.
800      */

801     public int indexOfThrowable(Class JavaDoc type, int fromIndex)
802     {
803         return -1;
804     }
805     
806 }
807
808 /**
809  * Used to test that the constructor passes when passed a throwable cause
810  * And, the NestableDelegate.getMessage() returns the message from underlying
811  * nestable (which also has to be a Throwable).
812  */

813 class ThrowableNestable extends Throwable JavaDoc implements Nestable
814 {
815     private Throwable JavaDoc cause = new Exception JavaDoc("ThrowableNestable cause");
816
817     /**
818      * @see Nestable#getThrowableCount()
819      * Returns 1 for this test.
820      */

821     public int getThrowableCount()
822     {
823         return 1;
824     }
825     
826     /**
827      * @see Nestable#getMessage()
828      * Returns the hard-coded string "ThrowableNestable exception" for this
829      * test.
830      */

831     public String JavaDoc getMessage()
832     {
833         return "ThrowableNestable exception";
834     }
835
836     /**
837      * @see Nestable#getMessage(int)
838      * Returns the hard-coded string "ThrowableNestable exception" for this
839      * test.
840      */

841     public String JavaDoc getMessage(int index)
842     {
843         return getMessage();
844     }
845
846     /**
847      * @see Nestable#getMessages()
848      * Returns single-element string array with "ThrowableNestable exception".
849      */

850     public String JavaDoc[] getMessages()
851     {
852         String JavaDoc msgs[] = new String JavaDoc[1];
853         msgs[0] = getMessage();
854         return msgs;
855     }
856     
857     /**
858      * @see Nestable#getCause()
859      */

860     public Throwable JavaDoc getCause()
861     {
862         return cause;
863     }
864
865     /**
866      * @see Nestable#printStackTrace(PrintWriter)
867      * Empty method to satisfy the implemented interface. Does nothing
868      * in this test.
869      */

870     public void printStackTrace(PrintWriter JavaDoc out)
871     {
872     }
873     
874     /**
875      * @see Nestable#printPartialStackTrace(PrintWriter)
876      * Empty method to satisfy the implemented interface. Does nothing
877      * in this test.
878      */

879     public void printPartialStackTrace(PrintWriter JavaDoc out)
880     {
881     }
882     
883     /**
884      * @see Nestable#getThrowable(int)
885      */

886     public Throwable JavaDoc getThrowable(int index)
887     {
888         return cause;
889     }
890     
891     /**
892      * @see Nestable#getThrowables()
893      */

894     public Throwable JavaDoc[] getThrowables()
895     {
896         Throwable JavaDoc throwables[] = new Throwable JavaDoc[1];
897         throwables[0] = cause;
898         return throwables;
899     }
900     
901     /**
902      * @see Nestable#indexOfThrowable(Class)
903      */

904     public int indexOfThrowable(Class JavaDoc type)
905     {
906         if(Exception JavaDoc.class.isInstance(type))
907         {
908             return 0;
909         }
910         return -1;
911     }
912     
913     /**
914      * @see Nestable#indexOfThrowable(Class,int)
915      */

916     public int indexOfThrowable(Class JavaDoc type, int fromIndex)
917     {
918         return indexOfThrowable(type);
919     }
920     
921 }
922
923 /**
924  * Nestable and Throwable class which takes in a 'cause' object.
925  * Returns a message wrapping the 'cause' message
926  * Prints a fixed stack trace and partial stack trace.
927  */

928 class ThrowableNestedNestable extends Throwable JavaDoc implements Nestable
929 {
930     private Throwable JavaDoc cause = null;
931     
932     public ThrowableNestedNestable(Throwable JavaDoc cause)
933     {
934         this.cause = cause;
935     }
936     
937     /**
938      * @see Nestable#getThrowableCount()
939      * Returns 1 for this test.
940      */

941     public int getThrowableCount()
942     {
943         return 1;
944     }
945     
946     /**
947      * @see Nestable#getMessage()
948      * For this test, returns "ThrowableNestable exception (" appended to the
949      * message of the cause specified in the constructor.
950      */

951     public String JavaDoc getMessage()
952     {
953         return "ThrowableNestedNestable exception (" + cause.getMessage() + ")";
954     }
955
956     /**
957      * @see Nestable#getMessage(int)
958      * For this test, returns "ThrowableNestable exception (" appended to the
959      * message of the cause specified in the constructor.
960      */

961     public String JavaDoc getMessage(int index)
962     {
963         return "ThrowableNestedNestable exception (" + cause.getMessage() + ")";
964     }
965     
966     /**
967      * @see Nestable#getMessages()
968      * For this test, returns a single-element string array containing
969      * "ThrowableNestable exception (" appended to the
970      * message of the cause specified in the constructor.
971      */

972     public String JavaDoc[] getMessages()
973     {
974         String JavaDoc[] msgs = new String JavaDoc[1];
975         msgs[0] = "ThrowableNestedNestable exception (" + cause.getMessage() + ")";
976         return msgs;
977     }
978     
979     /**
980      * @see Nestable#getCause()
981      */

982     public Throwable JavaDoc getCause()
983     {
984         return cause;
985     }
986     
987     /**
988      * @see Nestable#printStackTrace(PrintWriter)
989      * For this test, writes the string
990      * "ThrowableNestedNestable stack trace place-holder" to the print writer.
991      */

992     public void printStackTrace(PrintWriter JavaDoc out)
993     {
994         out.println("ThrowableNestedNestable stack trace place-holder");
995     }
996     
997     /**
998      * @see Nestable#printPartialStackTrace(PrintWriter)
999      * For this test, writes the string
1000     * "ThrowableNestedNestable partial stack trace place-holder" to the print
1001     * writer.
1002     */

1003    public void printPartialStackTrace(PrintWriter JavaDoc out)
1004    {
1005        out.println("ThrowableNestedNestable partial stack trace place-holder");
1006    }
1007    
1008    /**
1009     * @see Nestable#getThrowable(int)
1010     */

1011    public Throwable JavaDoc getThrowable(int index)
1012    {
1013        return cause;
1014    }
1015    
1016    /**
1017     * @see Nestable#getThrowableS()
1018     */

1019    public Throwable JavaDoc[] getThrowables()
1020    {
1021        Throwable JavaDoc throwables[] = new Throwable JavaDoc[1];
1022        throwables[0] = cause;
1023        return throwables;
1024    }
1025    
1026    /**
1027     * @see Nestable#indexOfThrowable(Class)
1028     */

1029    public int indexOfThrowable(Class JavaDoc type)
1030    {
1031        if(Exception JavaDoc.class.isInstance(type))
1032        {
1033            return 0;
1034        }
1035        return -1;
1036    }
1037    
1038    /**
1039     * @see Nestable#indexOfThrowable(Class, int)
1040     */

1041    public int indexOfThrowable(Class JavaDoc type, int fromIndex)
1042    {
1043        return indexOfThrowable(type);
1044    }
1045    
1046}
1047
1048/**
1049 * Used to test that the constructor fails when passed a non-throwable cause
1050 */

1051class NonThrowableNestable implements Nestable
1052{
1053    /**
1054     * @see Nestable#getThrowableCount()
1055     * Returns 1 for this test.
1056     */

1057    public int getThrowableCount()
1058    {
1059        return 1;
1060    }
1061    
1062    /**
1063     * @see Nestable#getMessage()
1064     * Returns the string "non-throwable" for this test.
1065     */

1066    public String JavaDoc getMessage()
1067    {
1068        return "non-throwable";
1069    }
1070
1071    /**
1072     * @see Nestable#getMessage(int)
1073     * Returns the string "non-throwable" for this test.
1074     */

1075    public String JavaDoc getMessage(int index)
1076    {
1077        return "non-throwable";
1078    }
1079    
1080    /**
1081     * @see Nestable#getMessage()
1082     * Returns a single-element array containing the string "non-throwable" for
1083     * this test.
1084     */

1085    public String JavaDoc[] getMessages()
1086    {
1087        String JavaDoc[] msgs = new String JavaDoc[1];
1088        msgs[0] = "non-throwable";
1089        return msgs;
1090    }
1091    
1092    /**
1093     * @see Nestable#getCause()
1094     * Returns <code>null</code> for this test.
1095     */

1096    public Throwable JavaDoc getCause()
1097    {
1098        return null;
1099    }
1100    
1101    /**
1102     * @see Nestable#printStackTrace(PrintWriter)
1103     * Empty method to satisfy the implemented interface. Does nothing
1104     * in this test.
1105     */

1106    public void printStackTrace(PrintWriter JavaDoc out)
1107    {
1108    }
1109    
1110    /**
1111     * @see Nestable#printStackTrace(PrintStream)
1112     * Empty method to satisfy the implemented interface. Does nothing
1113     * in this test.
1114     */

1115    public void printStackTrace(PrintStream JavaDoc out)
1116    {
1117    }
1118    
1119    /**
1120     * @see Nestable#printPartialStackTrace(PrintWriter)
1121     * Empty method to satisfy the implemented interface. Does nothing
1122     * in this test.
1123     */

1124    public void printPartialStackTrace(PrintWriter JavaDoc out)
1125    {
1126    }
1127    
1128
1129    /**
1130     * @see Nestable#getThrowable(int)
1131     * Returns <code>null</code> for this test.
1132     */

1133    public Throwable JavaDoc getThrowable(int index)
1134    {
1135        return null;
1136    }
1137    
1138    /**
1139     * @see Nestable#getThrowables()
1140     * Returns zero-length <code>Throwable</code> array.
1141     */

1142    public Throwable JavaDoc[] getThrowables()
1143    {
1144        return new Throwable JavaDoc[0];
1145    }
1146    
1147    /**
1148     * @see Nestable#indexOfThrowable(Class)
1149     * Returns -1 for this test.
1150     */

1151    public int indexOfThrowable(Class JavaDoc type)
1152    {
1153        return -1;
1154    }
1155    
1156    /**
1157     * @see Nestable#indexOfThrowable(Class, int)
1158     * Returns -1 for this test.
1159     */

1160    public int indexOfThrowable(Class JavaDoc type, int fromIndex)
1161    {
1162        return -1;
1163    }
1164    
1165}
1166
Popular Tags