1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package ch.elca.el4j.tests.core.aop;
19
20 import static org.easymock.EasyMock.createMock;
21 import static org.easymock.EasyMock.expect;
22 import static org.easymock.EasyMock.replay;
23 import static org.easymock.EasyMock.verify;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import org.aopalliance.intercept.MethodInvocation;
30 import org.junit.Test;
31
32 import ch.elca.el4j.core.aop.ExceptionChainConversionInterceptor;
33
34
35
36
37
38
39
40
41 public class ExceptionChainConverterInterceptorTest {
42
43
44
45
46 @Test
47 public void testBasicInterceptor() {
48 MethodInvocation methodInvocation = (MethodInvocation) createMock(MethodInvocation.class);
49
50 String myFancyObject = "alsdkjfasljfaslödjfasdo034023740";
51
52 try {
53 expect(methodInvocation.proceed()).andReturn(myFancyObject);
54 } catch (Throwable e1) {
55 fail();
56 }
57
58 replay(methodInvocation);
59
60 ExceptionChainConversionInterceptor ecci = new ExceptionChainConversionInterceptor();
61
62 Object result = null;
63 try {
64 result = ecci.invoke(methodInvocation);
65 } catch (Throwable e) {
66 fail();
67 }
68 assertEquals(result, myFancyObject);
69 verify(methodInvocation);
70 }
71
72
73
74
75 @Test
76 public void testInterceptorWithException() {
77 MethodInvocation methodInvocation = (MethodInvocation) createMock(MethodInvocation.class);
78
79 try {
80 expect(methodInvocation.proceed()).andThrow(
81 new IllegalArgumentException("ttt", m_testThrowable));
82 } catch (Throwable e1) {
83 }
84
85 replay(methodInvocation);
86
87 ExceptionChainConversionInterceptor ecci = new ExceptionChainConversionInterceptor();
88
89 try {
90 ecci.invoke(methodInvocation);
91 } catch (Throwable t) {
92 checkThrowableIsCorrectlyHandled(t.getCause());
93 assertTrue(t instanceof IllegalArgumentException);
94 assertTrue(t.getMessage().equals("ttt"));
95 verify(methodInvocation);
96 return;
97 }
98 fail();
99 }
100
101
102
103
104 @Test
105 public void testConvertCause() {
106 ExceptionChainConversionInterceptorChild ecci = new ExceptionChainConversionInterceptorChild();
107
108 ecci.callConvertCause(null);
109
110 Throwable result = ecci.callConvertCause(m_testThrowable);
111
112 checkThrowableIsCorrectlyHandled(result);
113 }
114
115 Throwable m_testThrowable = new Throwable("t", new Exception("t2",
116 new Exception("t3", new RuntimeException("t4", null))));
117
118 private void checkThrowableIsCorrectlyHandled(Throwable result) {
119 assertFalse(result.getCause() instanceof Exception);
120 assertFalse(result.getCause().getCause() instanceof Exception);
121 assertFalse(result.getCause().getCause().getCause() instanceof RuntimeException);
122 }
123
124
125 static class ExceptionChainConversionInterceptorChild extends
126 ExceptionChainConversionInterceptor {
127
128 public Throwable callConvertCause(Throwable original) {
129 return super.convertCause(original);
130 }
131
132 }
133
134 }