001 package org.springframework.richclient.exceptionhandling.delegation;
002
003 import junit.framework.TestCase;
004
005 import java.util.ArrayList;
006 import java.util.List;
007 import java.util.LinkedList;
008
009 import org.springframework.richclient.exceptionhandling.delegation.ChainInspectingExceptionHandlerDelegate.ChainPart;
010
011 /**
012 * @author Geoffrey De Smet
013 */
014 public class DelegatingExceptionHandlerTests extends TestCase {
015
016 public void testSimpleDelegation() {
017 DelegatingExceptionHandler delegatingExceptionHandler = new DelegatingExceptionHandler();
018 List<ExceptionHandlerDelegate> delegateList = new LinkedList<ExceptionHandlerDelegate>();
019 ExceptionHandlerCounter illegalArgumentCounter = new ExceptionHandlerCounter();
020 delegateList.add(new SimpleExceptionHandlerDelegate(IllegalArgumentException.class, illegalArgumentCounter));
021 ExceptionHandlerCounter nullPointerCounter = new ExceptionHandlerCounter();
022 delegateList.add(new SimpleExceptionHandlerDelegate(NullPointerException.class, nullPointerCounter));
023 ExceptionHandlerCounter runtimeCounter = new ExceptionHandlerCounter();
024 delegateList.add(new SimpleExceptionHandlerDelegate(RuntimeException.class, runtimeCounter));
025 delegatingExceptionHandler.setDelegateList(delegateList);
026
027 delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new NullPointerException());
028 delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new RuntimeException());
029 // NumberFormatException extends IllegalArgumentException
030 delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new NumberFormatException());
031 // IllegalStateException extends RuntimeException
032 delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new IllegalStateException());
033 delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new IllegalArgumentException());
034 delegatingExceptionHandler.uncaughtException(Thread.currentThread(), new NullPointerException());
035
036 assertEquals(2, illegalArgumentCounter.getCounter());
037 assertEquals(2, nullPointerCounter.getCounter());
038 assertEquals(2, runtimeCounter.getCounter());
039 }
040
041 public void testSimplePurgedDelegation() {
042 DelegatingExceptionHandler delegatingExceptionHandler = new DelegatingExceptionHandler();
043 List<ExceptionHandlerDelegate> delegateList = new LinkedList<ExceptionHandlerDelegate>();
044 ExceptionHandlerCounter nullPointerCounter = new ExceptionHandlerCounter(NullPointerException.class);
045 SimpleExceptionHandlerDelegate delegate = new SimpleExceptionHandlerDelegate(NullPointerException.class, nullPointerCounter);
046 delegate.setExceptionPurger(new DefaultExceptionPurger(null, IllegalArgumentException.class));
047 delegateList.add(delegate);
048 ExceptionHandlerCounter runtimeCounter = new ExceptionHandlerCounter();
049 delegateList.add(new SimpleExceptionHandlerDelegate(RuntimeException.class, runtimeCounter));
050 delegatingExceptionHandler.setDelegateList(delegateList);
051
052 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
053 new NullPointerException()); // ok
054 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
055 new IllegalArgumentException(new NullPointerException())); // ok
056 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
057 new IllegalArgumentException(new IllegalStateException(new NullPointerException()))); // not ok
058 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
059 new IllegalArgumentException(new IllegalArgumentException(new NullPointerException()))); // ok
060 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
061 new IllegalArgumentException()); // not ok
062
063 assertEquals(3, nullPointerCounter.getCounter());
064 assertEquals(2, runtimeCounter.getCounter());
065 }
066
067 public void testChainInpstectingDelegation() {
068 DelegatingExceptionHandler delegatingExceptionHandler = new DelegatingExceptionHandler();
069 List<ExceptionHandlerDelegate> delegateList = new LinkedList<ExceptionHandlerDelegate>();
070 List<ChainPart> chainPartList = new ArrayList<ChainPart>();
071 chainPartList.add(new ChainPart(IllegalArgumentException.class, 0, 2));
072 chainPartList.add(new ChainPart(IllegalStateException.class, 1));
073 ExceptionHandlerCounter chainCounter = new ExceptionHandlerCounter();
074 delegateList.add(new ChainInspectingExceptionHandlerDelegate(chainPartList, chainCounter));
075 List<ChainPart> cornerChainPartList = new ArrayList<ChainPart>();
076 cornerChainPartList.add(new ChainPart(NumberFormatException.class));
077 ExceptionHandlerCounter cornerCounter = new ExceptionHandlerCounter();
078 delegateList.add(new ChainInspectingExceptionHandlerDelegate(cornerChainPartList, cornerCounter));
079 ExceptionHandlerCounter runtimeCounter = new ExceptionHandlerCounter();
080 delegateList.add(new SimpleExceptionHandlerDelegate(RuntimeException.class, runtimeCounter));
081 delegatingExceptionHandler.setDelegateList(delegateList);
082
083
084 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
085 new IllegalArgumentException(
086 new RuntimeException(new IllegalStateException()))); // chainCounter
087 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
088 new RuntimeException(new IllegalArgumentException(
089 new RuntimeException(new IllegalStateException())))); // chainCounter
090 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
091 new RuntimeException(new RuntimeException(new IllegalArgumentException(
092 new RuntimeException(new IllegalStateException()))))); // chainCounter
093 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
094 new RuntimeException(new RuntimeException(new RuntimeException(new IllegalArgumentException(
095 new RuntimeException(new IllegalStateException())))))); // runtimeCounter
096 delegatingExceptionHandler.uncaughtException(Thread.currentThread(),
097 new IllegalArgumentException(
098 new IllegalStateException())); // runtimeCounter
099 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 }