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.services.exceptionhandler;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.fail;
22  
23  import org.junit.Test;
24  import org.springframework.context.ApplicationContext;
25  
26  import ch.elca.el4j.core.context.ModuleApplicationContext;
27  import ch.elca.el4j.services.exceptionhandler.ContextExceptionHandlerInterceptor;
28  import ch.elca.el4j.services.exceptionhandler.MissingContextException;
29  
30  /**
31   * This class tests the context exception handler.
32   *
33   * <script type="text/javascript">printFileStatus
34   *   ("$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/tags/el4j_1_5_1/el4j/framework/modules/exception_handling/src/test/java/ch/elca/el4j/tests/services/exceptionhandler/ContextExceptionHandlerTest.java $",
35   *    "$Revision: 3038 $",
36   *    "$Date: 2008-06-09 12:08:34 +0200 (Mon, 09 Jun 2008) $",
37   *    "$Author: swismer $"
38   * );</script>
39   *
40   * @author Andreas Bur (ABU)
41   */
42  public class ContextExceptionHandlerTest{
43  
44  	/** The application context. */
45  	private ApplicationContext m_appContext;
46  	
47  	/** The bean that is guarded by the security facade. */
48  	private A m_a;
49  	
50  	/**
51  	 * Default constructor.
52  	 */
53  	public ContextExceptionHandlerTest() {
54  		m_appContext = new ModuleApplicationContext(
55  			"scenarios/services/exceptionhandler/"
56  			+ "contextExceptionHandlerTest.xml", false);
57  		m_a = (A) m_appContext.getBean("A");
58  	}
59  	
60  	/**
61  	 * Tests the context semantics. This test has to be driven manually. First
62  	 * a dialogue, then a log entry with stack trace and finally, again a
63  	 * dialogue is printed to the standard out.
64  	 */
65  	@Test
66  	public void testContextSemantics() {
67  		ContextExceptionHandlerInterceptor.setContext("gui");
68  		step(1, 1, 0);
69  		ContextExceptionHandlerInterceptor.setContext("batch");
70  		step(2, 0, 1);
71  		step(2, 0, 1);
72  	}
73  	
74  	/**
75  	 * Tests whether the situation with a missing context is handled correclty.
76  	 */
77  	@Test
78  	public void testMissingContext() {
79  		ContextExceptionHandlerInterceptor.setContext(null);
80  		// Checkstyle: EmptyBlock off
81  		try {
82  			step(1, 1, 0);
83  			fail("Expected a MissingContextException but didn't catch one.");
84  		} catch (MissingContextException mce) { }
85  		// Checkstyle: EmptyBlock on
86  	}
87  
88  	/**
89  	 * Performs a division operation and checks whether the correct method was
90  	 * invoked.
91  	 *
92  	 * @param call
93  	 *      The call's number used for displaying assertion failures.
94  	 *
95  	 * @param expectedGuiCalls
96  	 *      The expected number of GUI error handling code invocations.
97  	 *
98  	 * @param expectedBatchCalls
99  	 *      The expected number of batch error handling code invocations.
100 	 */
101 	private void step(int call, int expectedGuiCalls, int expectedBatchCalls) {
102 		MessageBoxExceptionHandler.s_numberOfHandleCalls = 0;
103 		LogExceptonHandler.s_numberOfHandleCalls = 0;
104 		
105 		int result = -1;
106 		try {
107 			result = m_a.div(1, 0);
108 		} catch (MissingContextException mce) {
109 			throw mce;
110 		} catch (Throwable t) {
111 			fail("Caught unexpected exception: " + t);
112 		}
113 		
114 		assertEquals("null was not transformed to 0.", 0, result);
115 		assertEquals("Used wrong context (" + call + ")",
116 				expectedGuiCalls,
117 				MessageBoxExceptionHandler.s_numberOfHandleCalls);
118 		assertEquals("Used wrong context (first call)",
119 				expectedBatchCalls, LogExceptonHandler.s_numberOfHandleCalls);
120 	}
121 }