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) 2006 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  package ch.elca.el4j.tests.remoting;
18  
19  import java.lang.reflect.InvocationTargetException;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.remoting.RemoteAccessException;
24  import org.junit.Test;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.fail;
28  
29  import ch.elca.el4j.tests.core.AbstractTest;
30  import ch.elca.el4j.tests.remoting.service.Calculator;
31  import ch.elca.el4j.tests.remoting.service.CalculatorException;
32  import ch.elca.el4j.tests.remoting.service.CalculatorOperation;
33  
34  /**
35   * This class is a test for the calculator.
36   *
37   * @svnLink $Revision: 4122 $;$Date: 2010-08-19 13:27:36 +0200 (Do, 19. Aug 2010) $;$Author: swrelca $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/tests/remoting/functional-tests/src/test/java/ch/elca/el4j/tests/remoting/AbstractCalculatorTest.java $
38   *
39   * @author Waraich Rashid (RWA)
40   */
41  
42  public abstract class AbstractCalculatorTest extends AbstractTest {
43  	/**
44  	 * Private logger.
45  	 */
46  	private static Logger s_logger = LoggerFactory.getLogger(
47  		AbstractCalculatorTest.class);
48  
49  	/**
50  	 * Instance of the calculator proxy.
51  	 */
52  	private Calculator m_calc;
53  	
54  	/**
55  	 * @return    the calculator object
56  	 */
57  	public Calculator getCalc() {
58  		if (m_calc == null) {
59  			m_calc = (Calculator) getApplicationContext().getBean("calculator");
60  		}
61  		return m_calc;
62  	}
63  	
64  	/**
65  	 * This test tests the area calculation method.
66  	 */
67  	@Test
68  	public void testAreaCalculation() {
69  		final double a = 2.3;
70  		final double b = 5.7;
71  		final double delta = 0.00000001;
72  		double result = getCalc().getArea(a, b);
73  		assertEquals("The area is not correctly calculated.", result, a * b, delta);
74  	}
75  	
76  	
77  	/**
78  	 * This test tests the exception handling.
79  	 * @throws Exception
80  	 */
81  	@Test
82  	public void testExceptionBehaviour() throws Throwable {
83  		try {
84  			getCalc().throwMeAnException();
85  			fail("No exception was thrown.");
86  		} catch (CalculatorException e) {
87  			s_logger.debug("Expected exception caught.", e);
88  		}
89  	}
90  	
91  	/**
92  	 * Tests if the protocol is able to handle enumerations.
93  	 */
94  	@Test
95  	public void testAbilityToHandleEnumerations() {
96  		// Checkstyle: MagicNumber off
97  		double result = getCalc().calculate(1.2, 2.5, CalculatorOperation.ADDITION);
98  		assertEquals(3.7, result, 0.1);
99  		result = getCalc().calculate(1.2, 2.5, CalculatorOperation.SUBTRACTION);
100 		assertEquals(-1.3, result, 0.1);
101 		//Checkstyle: MagicNumber on
102 	}
103 
104 }