1 /*
2 * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3 * the spring framework, http://el4j.sf.net
4 * Copyright (C) 2010 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.core.context.junit4;
18
19 import org.junit.runners.model.InitializationError;
20 import org.springframework.test.context.ContextConfiguration;
21 import org.springframework.test.context.ContextLoader;
22 import org.springframework.test.context.TestContext;
23 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24 import org.springframework.test.context.support.AbstractTestExecutionListener;
25
26 import ch.elca.el4j.core.context.ModuleApplicationContext;
27 import ch.elca.el4j.tests.core.ModuleTestContextLoader;
28
29 /**
30 *
31 * JUnit4ClassRunner which enables the use of the <code>ExtendedContextConfiguration</code>.
32 *
33 * To be used in conjunction with the {@link ModuleTestContextLoader}.
34 * It is an error to use this ClassRunner with a {@link ContextConfiguration#loader} not
35 * set to a subtype of {@link ModuleTestContextLoader}.
36 *
37 * @svnLink $Revision: 4253 $;$Date: 2010-12-21 11:08:04 +0100 (Di, 21. Dez 2010) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/test/java/ch/elca/el4j/tests/core/context/junit4/EL4JJunit4ClassRunner.java $
38 *
39 * @author Simon Stelling (SST)
40 */
41 public class EL4JJunit4ClassRunner extends SpringJUnit4ClassRunner {
42
43 /**
44 * Constructor which places the tested class in a thread-local variable of ModuleTestContextLoader so
45 * the ContextLoader can investigate the <code>@ExtendedContextConfiguration</code> annotation and
46 * configure the {@link ModuleApplicationContext} appropriately.
47 * @param testClass the test class
48 * @throws InitializationError
49 */
50 public EL4JJunit4ClassRunner(Class<?> testClass) throws InitializationError {
51 super(testClass);
52
53 // allow ModuleTestContextLoader to detect @ExtendedContextConfiguration annotations
54 // on the test class.
55 ContextConfiguration contextConfiguration = testClass.getAnnotation(ContextConfiguration.class);
56 Class<? extends ContextLoader> loader = contextConfiguration.loader();
57 if (!ModuleTestContextLoader.class.isAssignableFrom(loader)) {
58 throw new InitializationError("EL4JJunit4ClassRunner used without ModuleTestContextLoader. "
59 + "Consider setting the 'loader' attribute of the @ContextConfiguration "
60 + "to ModuleTestContextLoader.class");
61 }
62
63 ModuleTestContextLoader.setTestedClass(testClass);
64
65 /* Let Spring close the application context by marking it dirty.
66 *
67 * There are two reasons why closing the context is wanted:
68 * 1. Be friendly to the database: release all db connections from the connection pool
69 * 2. There is an application context cache inside the TestContext that checks equality
70 * only using the include and not the exclude locations.
71 */
72 getTestContextManager().registerTestExecutionListeners(new AbstractTestExecutionListener() {
73 @Override
74 public void afterTestClass(TestContext testContext) throws Exception {
75 testContext.markApplicationContextDirty();
76 }
77 });
78 }
79 }