Skip to content

Commit 1917327

Browse files
committedNov 13, 2013
Adding unit test to ensure that other modules are refreshed on stopping
a module - TRUNK-4134
1 parent aec4e86 commit 1917327

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed
 

‎web/src/test/java/org/openmrs/web/test/TestContextLoader.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
package org.openmrs.web.test;
1515

1616
import org.springframework.context.ConfigurableApplicationContext;
17-
import org.springframework.context.support.ClassPathXmlApplicationContext;
17+
import org.springframework.mock.web.MockServletContext;
1818
import org.springframework.test.context.support.AbstractContextLoader;
19+
import org.springframework.web.context.WebApplicationContext;
20+
import org.springframework.web.context.support.XmlWebApplicationContext;
1921

2022
/**
2123
* The only reason why i created this class is to be able to create an application context which can
@@ -29,7 +31,13 @@ public TestContextLoader() {
2931

3032
@Override
3133
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
32-
return new ClassPathXmlApplicationContext(locations);
34+
XmlWebApplicationContext context = new XmlWebApplicationContext();
35+
context.setConfigLocations(locations);
36+
MockServletContext sc = new MockServletContext();
37+
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
38+
context.setServletContext(sc);
39+
context.refresh();
40+
return context;
3341
}
3442

3543
@Override

‎web/src/test/java/org/openmrs/web/test/WebModuleActivatorTest.java

+39-1
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@
1717

1818
import org.junit.Test;
1919
import org.openmrs.module.BaseModuleActivatorTest;
20+
import org.openmrs.module.Module;
21+
import org.openmrs.module.ModuleFactory;
2022
import org.openmrs.module.ModuleUtil;
23+
import org.openmrs.module.web.WebModuleUtil;
2124
import org.springframework.context.support.AbstractRefreshableApplicationContext;
2225
import org.springframework.test.annotation.NotTransactional;
2326
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.web.context.support.XmlWebApplicationContext;
2428

2529
/**
2630
* ModuleActivator tests that need refreshing the spring application context. The only reason why i
2731
* did not put these in the api projects's ModuleActivatorTest is because when the spring
2832
* application context is refreshed, classes that the module references which are not in the api but
2933
* web, will lead to ClassNotFoundException s, hence preventing the refresh. If you want to try this
30-
* out, just put these tests in ModuleActivatorTest
34+
* out, just put these tests in ModuleActivatorTest NOTE: The way we start, stop, unload, etc,
35+
* modules is copied from ModuleListController
3136
*/
3237
@ContextConfiguration(locations = { "classpath*:webModuleApplicationContext.xml" }, inheritLocations = true, loader = TestContextLoader.class)
3338
public class WebModuleActivatorTest extends BaseModuleActivatorTest {
@@ -46,4 +51,37 @@ public void shouldCallWillRefreshContextAndContextRefreshedOnRefresh() throws Ex
4651
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE2_ID) == 1);
4752
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE3_ID) == 1);
4853
}
54+
55+
@Test
56+
@NotTransactional
57+
public void shouldRefreshOtherModulesOnStoppingModule() {
58+
59+
//When OpenMRS is running and you stop a module:
60+
// willRefreshContext() and contextRefreshed() methods get called for ONLY the started modules' activators EXCLUDING the stopped module
61+
// willStop() and stopped() methods get called for ONLY the stopped module's activator
62+
63+
Module module = ModuleFactory.getModuleById(MODULE3_ID);
64+
ModuleFactory.stopModule(module);
65+
WebModuleUtil.stopModule(module, ((XmlWebApplicationContext) applicationContext).getServletContext());
Has conversations. Original line has conversations.
66+
67+
//module3 should have stopped
68+
assertTrue(moduleTestData.getWillStopCallCount(MODULE3_ID) == 1);
69+
assertTrue(moduleTestData.getStoppedCallCount(MODULE3_ID) == 1);
70+
71+
//module1 and module2 should not stop
72+
assertTrue(moduleTestData.getWillStopCallCount(MODULE1_ID) == 0);
73+
assertTrue(moduleTestData.getStoppedCallCount(MODULE1_ID) == 0);
74+
assertTrue(moduleTestData.getWillStopCallCount(MODULE2_ID) == 0);
75+
assertTrue(moduleTestData.getStoppedCallCount(MODULE2_ID) == 0);
76+
77+
//module3 should not refresh
78+
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE3_ID) == 0);
79+
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE3_ID) == 0);
80+
81+
//module1 and module2 should refresh
82+
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE1_ID) == 1);
83+
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE2_ID) == 1);
84+
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE1_ID) == 1);
85+
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE2_ID) == 1);
86+
}
4987
}

0 commit comments

Comments
 (0)
Please sign in to comment.