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