View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
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   * Test case for exception chain converter.
36   * 
37   * @svnLink $Revision: 3875 $;$Date: 2009-08-04 14:35:53 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/core/aop/ExceptionChainConverterInterceptorTest.java $
38   *
39   * @author pos
40   */
41  public class ExceptionChainConverterInterceptorTest {
42  
43  	/**
44  	 * Test normal invocation usage
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  	 * Test usage with exception
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 	 * Test isolated cause conversion
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 	// to be able to call the protected method of class to test
125 	static class ExceptionChainConversionInterceptorChild extends
126 			ExceptionChainConversionInterceptor {
127 
128 		public Throwable callConvertCause(Throwable original) {
129 			return super.convertCause(original);
130 		}
131 
132 	}
133 
134 }