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  
18  package ch.elca.el4j.tests.core.implicitcontextpassing;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotSame;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.fail;
24  
25  import org.junit.Before;
26  import org.junit.Test;
27  import org.springframework.context.ApplicationContext;
28  
29  import ch.elca.el4j.core.context.ModuleApplicationContext;
30  import ch.elca.el4j.core.contextpassing.ImplicitContextPassingRegistry;
31  import ch.elca.el4j.tests.remoting.service.Calculator;
32  
33  // Checkstyle: MagicNumber off
34  // Checkstyle: EmptyBlock off
35  
36  /**
37   * This integration test checks correctness of the Implicit context passer.
38   *
39   * @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/tests/core/core_implicit_context_passing_test/src/test/java/ch/elca/el4j/tests/core/implicitcontextpassing/ImplicitContextPassingIntegrationTest.java $
40   *
41   * @author David Stefan (DST)
42   */
43  
44  public class ImplicitContextPassingIntegrationTest {
45  
46  	/**
47  	 * Location where config files can be found.
48  	 * The order of the config files is crucial. If the client is started before
49  	 * the server, it obviously won't work.
50  	 */
51  	private static final String[] CONFIG_LOCATION
52  		= new String[]{
53  			"classpath:scenarios/implicitcontextpassing/rmi-server-config.xml",
54  			"classpath:scenarios/implicitcontextpassing/rmi-client-config.xml"
55  		};
56  
57  	/**
58  	 * ApplicationContext object.
59  	 */
60  	private ApplicationContext m_appContext;
61  
62  	/**
63  	 * ImplicitContextPassingRegistry for client.
64  	 */
65  	private ImplicitContextPassingRegistry m_clientRegistry;
66  
67  	/**
68  	 * ImplicitContextPassingRegistry for server.
69  	 */
70  	private ImplicitContextPassingRegistry m_serverRegistry;
71  	
72  	/**
73  	 * Implicit context passer for client.
74  	 */
75  	private ImplicitContextPassTester m_clientPasserA;
76  
77  	/**
78  	 * Implicit context passer for server.
79  	 */
80  	private ImplicitContextPassTester m_serverPasserA;
81  	
82  	/**
83  	 * Inteface of service provided by server.
84  	 */
85  	private Calculator m_calc;
86  
87  	/**
88  	 * {@inheritDoc}
89  	 */
90  	@Before
91  	public void setUp() {
92  
93  		m_appContext = new ModuleApplicationContext(CONFIG_LOCATION, true);
94  		m_clientRegistry = (ImplicitContextPassingRegistry) m_appContext
95  			.getBean("clientImplicitContextPassingRegistry");
96  
97  		m_serverRegistry = (ImplicitContextPassingRegistry) m_appContext
98  			.getBean("serverImplicitContextPassingRegistry");
99  		
100 		m_serverPasserA = (ImplicitContextPassTester) m_appContext
101 			.getBean("serverPasserA");
102 		
103 		m_clientPasserA = (ImplicitContextPassTester) m_appContext
104 			.getBean("clientPasserA");
105 		
106 		m_calc = (Calculator) m_appContext.getBean("calculator");
107 	}
108 
109 	/**
110 	 * This test simulates registration with contextPasser of client beeing
111 	 * null.
112 	 */
113 	@Test
114 	public void testClientContextPasserIsNull() {
115 		try {
116 			m_clientRegistry.registerImplicitContextPasser(null);
117 			fail("Passing null should raise an exception");
118 		} catch (NullPointerException e) {
119 			// should raise a nullpointer exception
120 		}
121 	}
122 
123 	/**
124 	 * This test simulates registration with contextPasser of server beeing
125 	 * null.
126 	 */
127 	@Test
128 	public void testServerContextPasserIsNull() {
129 		try {
130 			m_serverRegistry.registerImplicitContextPasser(null);
131 			fail("Passing null should raise an exception");
132 		} catch (NullPointerException e) {
133 			// should raise a nullpointer exception
134 		}
135 	}
136 
137 	/**
138 	 * This test simulates assignment to passer of a registry beeing null .
139 	 */
140 	@Test
141 	public void testRegistryIsNull() {
142 		ImplicitContextPasserImplA passerA = new ImplicitContextPasserImplA();
143 		try {
144 			passerA.setImplicitContextPassingRegistry(null);
145 			fail("Passing null should raise an exception");
146 		} catch (RuntimeException e) {
147 			// should raise a runtime exception
148 		}
149 	}
150 
151 	/**
152 	 * This tests simulates context passing with one passer on each side,
153 	 * passing an integer.
154 	 */
155 	@Test
156 	public void testOnePasserIntegerData() {
157 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.INT_TEST);
158 		m_calc.getArea(0.65, 0.78);
159 		assertEquals(m_clientPasserA.getTestData(),
160 			m_serverPasserA.getReceivedData());
161 		// Reset data for next test
162 		m_serverPasserA.setDataToUse(ImplicitContextPasserImplA.RESET);
163 	}
164 	
165 	/**
166 	 * This tests simulates context passing with one passer on each side,
167 	 * passing a float.
168 	 */
169 	@Test
170 	public void testOnePasserFloatData() {
171 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.FLOAT_TEST);
172 		m_calc.getArea(0.65, 0.78);
173 		assertEquals(m_clientPasserA.getTestData(),
174 			m_serverPasserA.getReceivedData());
175 		// Reset data for next test
176 		m_serverPasserA.setDataToUse(ImplicitContextPasserImplA.RESET);
177 
178 	}
179 	
180 	/**
181 	 * This tests simulates context passing with one passer on each side,
182 	 * passing an double.
183 	 */
184 	@Test
185 	public void testOnePasserDoubleData() {
186 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.DOUBLE_TEST);
187 		m_calc.getArea(0.65, 0.78);
188 		assertEquals(m_clientPasserA.getTestData(),
189 			m_serverPasserA.getReceivedData());
190 		// Reset data for next test
191 		m_serverPasserA.setDataToUse(ImplicitContextPasserImplA.RESET);
192 
193 	}
194 	
195 	/**
196 	 * This tests simulates context passing with one passer on each side,
197 	 * passing a list.
198 	 */
199 	@Test
200 	public void testOnePasserListData() {
201 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.LIST_TEST);
202 		m_calc.getArea(0.65, 0.78);
203 		assertEquals(m_clientPasserA.getTestData(),
204 			m_serverPasserA.getReceivedData());
205 		// Reset data for next test
206 		m_serverPasserA.setDataToUse(ImplicitContextPasserImplA.RESET);
207 	}
208 
209 	/**
210 	 * This tests simulates context passing with one passer on each side,
211 	 * passing an empty list.
212 	 */
213 	@Test
214 	public void testOnePasserEmptyListData() {
215 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.NULL_LIST_TEST);
216 		m_calc.getArea(0.65, 0.78);
217 		assertEquals(m_clientPasserA.getTestData(),
218 			m_serverPasserA.getReceivedData());
219 		// Reset data for next test
220 		m_serverPasserA.setDataToUse(ImplicitContextPasserImplA.RESET);
221 	}
222 	
223 	/**
224 	* This test checks if context is NOT passed if service method isn't called.
225 	*/
226 	@Test
227 	public void testServiceNotCalled() {
228 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.RESET);
229 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.DOUBLE_TEST);
230 		assertNull(m_serverPasserA.getReceivedData());
231 		// Reset data for next test
232 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.RESET);
233 	}
234 	
235 	/**
236 	 * This tests simulates context passing with two passers on each side,
237 	 * passing a double and a list.
238 	 */
239 	@Test
240 	public void testTwoPasser() {
241 		
242 		ImplicitContextPasserImplB serverPasserB = (ImplicitContextPasserImplB)
243 			m_appContext.getBean("serverPasserB");
244 	
245 		ImplicitContextPasserImplB clientPasserB = (ImplicitContextPasserImplB)
246 			m_appContext.getBean("clientPasserB");
247 		
248 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.DOUBLE_TEST);
249 		clientPasserB.setDataToUse(ImplicitContextPassTester.LIST_TEST);
250 		m_calc.getArea(0.65, 0.78);
251 		assertEquals(m_clientPasserA.getTestData(), m_serverPasserA
252 			.getReceivedData());
253 		assertEquals(clientPasserB.getTestData(), serverPasserB
254 			.getReceivedData());
255 	}
256 	
257 	/**
258 	 * This tests simulates context passing with two passers on client side
259 	 * and one on server side, passing a double and a list.
260 	 */
261 	@Test
262 	public void testTwoAndOnePasser() {
263 		
264 		ImplicitContextPasserImplB clientPasserB = (ImplicitContextPasserImplB)
265 			m_appContext.getBean("clientPasserB");
266 		
267 		m_clientPasserA.setDataToUse(ImplicitContextPasserImplA.DOUBLE_TEST);
268 		clientPasserB.setDataToUse(ImplicitContextPassTester.DOUBLE_TEST);
269 		m_calc.getArea(0.65, 0.78);
270 		assertEquals(m_clientPasserA.getTestData(), m_serverPasserA
271 			.getReceivedData());
272 		assertNotSame(clientPasserB.getTestData(), m_serverPasserA
273 			.getReceivedData());
274 	}
275 	
276 }
277 // Checkstyle: EmptyBlock on
278 // Checkstyle: MagicNumber on