1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
38
39
40
41
42 public abstract class AbstractCalculatorTest extends AbstractTest {
43
44
45
46 private static Logger s_logger = LoggerFactory.getLogger(
47 AbstractCalculatorTest.class);
48
49
50
51
52 private Calculator m_calc;
53
54
55
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
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
79
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
93
94 @Test
95 public void testAbilityToHandleEnumerations() {
96
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
102 }
103
104 }