1   package org.springframework.richclient.exceptionhandling.delegation;
2   
3   import junit.framework.TestCase;
4   
5   import java.util.ArrayList;
6   import java.util.List;
7   import java.util.LinkedList;
8   
9   import org.springframework.richclient.exceptionhandling.delegation.ChainInspectingExceptionHandlerDelegate.ChainPart;
10  
11  /**
12   * @author Geoffrey De Smet
13   */
14  public class DelegatingExceptionHandlerTests extends TestCase {
15  
16      public void testSimpleDelegation() {
17          DelegatingExceptionHandler delegatingExceptionHandler = new DelegatingExceptionHandler();
18          List<ExceptionHandlerDelegate> delegateList = new LinkedList<ExceptionHandlerDelegate>();
19          ExceptionHandlerCounter illegalArgumentCounter = new ExceptionHandlerCounter();
20          delegateList.add(new SimpleExceptionHandlerDelegate(IllegalArgumentException.class, illegalArgumentCounter));
21          ExceptionHandlerCounter nullPointerCounter = new ExceptionHandlerCounter();
22          delegateList.add(new SimpleExceptionHandlerDelegate(NullPointerException.class, nullPointerCounter));
23          ExceptionHandlerCounter runtimeCounter = new ExceptionHandlerCounter();
24          delegateList.add(new SimpleExceptionHandlerDelegate(RuntimeException.class, runtimeCounter));
25          delegatingExceptionHandler.setDelegateList(delegateList);
26  
27          delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new NullPointerException());
28          delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new RuntimeException());
29          // NumberFormatException extends IllegalArgumentException
30          delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new NumberFormatException());
31          // IllegalStateException extends RuntimeException
32          delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new IllegalStateException());
33          delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new IllegalArgumentException());
34          delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new NullPointerException());
35  
36          assertEquals(2, illegalArgumentCounter.getCounter());
37          assertEquals(2, nullPointerCounter.getCounter());
38          assertEquals(2, runtimeCounter.getCounter());
39      }
40  
41      public void testSimplePurgedDelegation() {
42          DelegatingExceptionHandler delegatingExceptionHandler = new DelegatingExceptionHandler();
43          List<ExceptionHandlerDelegate> delegateList = new LinkedList<ExceptionHandlerDelegate>();
44          ExceptionHandlerCounter nullPointerCounter = new ExceptionHandlerCounter(NullPointerException.class);
45          SimpleExceptionHandlerDelegate delegate = new SimpleExceptionHandlerDelegate(NullPointerException.class, nullPointerCounter);
46          delegate.setExceptionPurger(new DefaultExceptionPurger(null, IllegalArgumentException.class));
47          delegateList.add(delegate);
48          ExceptionHandlerCounter runtimeCounter = new ExceptionHandlerCounter();
49          delegateList.add(new SimpleExceptionHandlerDelegate(RuntimeException.class, runtimeCounter));
50          delegatingExceptionHandler.setDelegateList(delegateList);
51  
52          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
53                  new NullPointerException()); // ok
54          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
55                  new IllegalArgumentException(new NullPointerException())); // ok
56          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
57                  new IllegalArgumentException(new IllegalStateException(new NullPointerException()))); // not ok
58          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
59                  new IllegalArgumentException(new IllegalArgumentException(new NullPointerException()))); // ok
60          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
61                  new IllegalArgumentException()); // not ok
62  
63          assertEquals(3, nullPointerCounter.getCounter());
64          assertEquals(2, runtimeCounter.getCounter());
65      }
66      
67      public void testChainInpstectingDelegation() {
68          DelegatingExceptionHandler delegatingExceptionHandler = new DelegatingExceptionHandler();
69          List<ExceptionHandlerDelegate> delegateList = new LinkedList<ExceptionHandlerDelegate>();
70          List<ChainPart> chainPartList = new ArrayList<ChainPart>();
71          chainPartList.add(new ChainPart(IllegalArgumentException.class, 0, 2));
72          chainPartList.add(new ChainPart(IllegalStateException.class, 1));
73          ExceptionHandlerCounter chainCounter = new ExceptionHandlerCounter();
74          delegateList.add(new ChainInspectingExceptionHandlerDelegate(chainPartList, chainCounter));
75          List<ChainPart> cornerChainPartList = new ArrayList<ChainPart>();
76          cornerChainPartList.add(new ChainPart(NumberFormatException.class));
77          ExceptionHandlerCounter cornerCounter = new ExceptionHandlerCounter();
78          delegateList.add(new ChainInspectingExceptionHandlerDelegate(cornerChainPartList, cornerCounter));
79          ExceptionHandlerCounter runtimeCounter = new ExceptionHandlerCounter();
80          delegateList.add(new SimpleExceptionHandlerDelegate(RuntimeException.class, runtimeCounter));
81          delegatingExceptionHandler.setDelegateList(delegateList);
82  
83  
84          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
85                  new IllegalArgumentException(
86                          new RuntimeException(new IllegalStateException()))); // chainCounter
87          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
88                  new RuntimeException(new IllegalArgumentException(
89                          new RuntimeException(new IllegalStateException())))); // chainCounter
90          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
91                  new RuntimeException(new RuntimeException(new IllegalArgumentException(
92                          new RuntimeException(new IllegalStateException()))))); // chainCounter
93          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
94                  new RuntimeException(new RuntimeException(new RuntimeException(new IllegalArgumentException(
95                          new RuntimeException(new IllegalStateException())))))); // runtimeCounter
96          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
97                  new IllegalArgumentException(
98                          new IllegalStateException())); // runtimeCounter
99          delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
100                 new IllegalArgumentException(
101                         new RuntimeException(new RuntimeException(new IllegalStateException())))); // runtimeCounter
102         delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
103                 new IllegalStateException(
104                         new RuntimeException(new IllegalArgumentException()))); // runtimeCounter
105         delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
106                 new NumberFormatException()); // cornerCounter
107         delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
108                 new RuntimeException(new NumberFormatException())); // cornerCounter
109 
110         assertEquals(3, chainCounter.getCounter());
111         assertEquals(2, cornerCounter.getCounter());
112         assertEquals(4, runtimeCounter.getCounter());
113     }
114     
115     public static class ExceptionHandlerCounter implements Thread.UncaughtExceptionHandler {
116 
117         private int counter = 0;
118         private Class filterClass;
119 
120         private ExceptionHandlerCounter() {}
121 
122         private ExceptionHandlerCounter(Class filterClass) {
123             this.filterClass = filterClass;
124         }
125 
126         public int getCounter() {
127             return counter;
128         }
129 
130         public void uncaughtException(Thread t, Throwable e) {
131             if (filterClass == null || filterClass.isInstance(e)) {
132                 counter++;
133             }
134         }
135     }
136 
137 
138 
139 }