Skip to content

Commit

Permalink
Adding unit test to ensure that other modules are refreshed on stopping
Browse files Browse the repository at this point in the history
a module - TRUNK-4134
  • Loading branch information
dkayiwa committed Nov 13, 2013
1 parent aec4e86 commit 1917327
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
12 changes: 10 additions & 2 deletions web/src/test/java/org/openmrs/web/test/TestContextLoader.java
Expand Up @@ -14,8 +14,10 @@
package org.openmrs.web.test;

import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.support.AbstractContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;

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

@Override
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
return new ClassPathXmlApplicationContext(locations);
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(locations);
MockServletContext sc = new MockServletContext();
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
context.setServletContext(sc);
context.refresh();
return context;
}

@Override
Expand Down
Expand Up @@ -17,17 +17,22 @@

import org.junit.Test;
import org.openmrs.module.BaseModuleActivatorTest;
import org.openmrs.module.Module;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.ModuleUtil;
import org.openmrs.module.web.WebModuleUtil;
import org.springframework.context.support.AbstractRefreshableApplicationContext;
import org.springframework.test.annotation.NotTransactional;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.web.context.support.XmlWebApplicationContext;

/**
* ModuleActivator tests that need refreshing the spring application context. The only reason why i
* did not put these in the api projects's ModuleActivatorTest is because when the spring
* application context is refreshed, classes that the module references which are not in the api but
* web, will lead to ClassNotFoundException s, hence preventing the refresh. If you want to try this
* out, just put these tests in ModuleActivatorTest
* out, just put these tests in ModuleActivatorTest NOTE: The way we start, stop, unload, etc,
* modules is copied from ModuleListController
*/
@ContextConfiguration(locations = { "classpath*:webModuleApplicationContext.xml" }, inheritLocations = true, loader = TestContextLoader.class)
public class WebModuleActivatorTest extends BaseModuleActivatorTest {
Expand All @@ -46,4 +51,37 @@ public void shouldCallWillRefreshContextAndContextRefreshedOnRefresh() throws Ex
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE2_ID) == 1);
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE3_ID) == 1);
}

@Test
@NotTransactional
public void shouldRefreshOtherModulesOnStoppingModule() {

//When OpenMRS is running and you stop a module:
// willRefreshContext() and contextRefreshed() methods get called for ONLY the started modules' activators EXCLUDING the stopped module
// willStop() and stopped() methods get called for ONLY the stopped module's activator

Module module = ModuleFactory.getModuleById(MODULE3_ID);
ModuleFactory.stopModule(module);
WebModuleUtil.stopModule(module, ((XmlWebApplicationContext) applicationContext).getServletContext());

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 18, 2013

Member

Why are you calling stopModule here twice? WebModuleUtil.stopModule should be enough

This comment has been minimized.

Copy link
@dkayiwa

dkayiwa Nov 18, 2013

Author Member

Because ModuleListController calls both the above methods. Reason being that they do different things on stopping a module. :)

This comment has been minimized.

Copy link
@wluyima

wluyima Nov 18, 2013

Member

I see why, you can ignore my comment


//module3 should have stopped
assertTrue(moduleTestData.getWillStopCallCount(MODULE3_ID) == 1);
assertTrue(moduleTestData.getStoppedCallCount(MODULE3_ID) == 1);

//module1 and module2 should not stop
assertTrue(moduleTestData.getWillStopCallCount(MODULE1_ID) == 0);
assertTrue(moduleTestData.getStoppedCallCount(MODULE1_ID) == 0);
assertTrue(moduleTestData.getWillStopCallCount(MODULE2_ID) == 0);
assertTrue(moduleTestData.getStoppedCallCount(MODULE2_ID) == 0);

//module3 should not refresh
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE3_ID) == 0);
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE3_ID) == 0);

//module1 and module2 should refresh
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE1_ID) == 1);
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE2_ID) == 1);
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE1_ID) == 1);
assertTrue(moduleTestData.getContextRefreshedCallCount(MODULE2_ID) == 1);
}
}

0 comments on commit 1917327

Please sign in to comment.