Skip to content

Commit b83b4ec

Browse files
committedNov 7, 2013
Initial work for testing module life cycle - TRUNK-4134
1 parent 9902022 commit b83b4ec

File tree

5 files changed

+297
-0
lines changed

5 files changed

+297
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.openmrs.module;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import org.junit.Before;
6+
import org.junit.Ignore;
7+
import org.junit.Test;
8+
import org.openmrs.test.BaseContextSensitiveTest;
9+
import org.openmrs.test.SkipBaseSetup;
10+
import org.openmrs.test.StartModule;
11+
12+
@SkipBaseSetup
13+
@StartModule({ "org/openmrs/module/include/test3-1.0-SNAPSHOT.omod", "org/openmrs/module/include/test1-1.0-SNAPSHOT.omod",
14+
"org/openmrs/module/include/test2-1.0-SNAPSHOT.omod" })
15+
public class ModuleActivatorTest extends BaseContextSensitiveTest {
16+
17+
private static final String MODULE1_ID = "test1";
18+
19+
private static final String MODULE2_ID = "test2";
20+
21+
private static final String MODULE3_ID = "test3";
22+
23+
ModuleTestData moduleTestData;
24+
25+
@Before
26+
public void beforeEachTest() {
27+
moduleTestData = ModuleTestData.getInstance();
28+
}
29+
30+
@Test
31+
public void shouldCallWillStartOnStartup() throws Exception {
32+
assertTrue(moduleTestData.getWillStartCallCount(MODULE1_ID) == 1);
33+
assertTrue(moduleTestData.getWillStartCallCount(MODULE2_ID) == 1);
34+
assertTrue(moduleTestData.getWillStartCallCount(MODULE3_ID) == 1);
35+
}
36+
Has conversations. Original line has conversations.
37+
@Test
38+
@Ignore("This is work in progress. So not yet finished this")
39+
public void shouldCallWillRefreshContextOnStartup() throws Exception {
40+
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE1_ID) == 1);
41+
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE2_ID) == 1);
42+
assertTrue(moduleTestData.getWillRefreshContextCallCount(MODULE3_ID) == 1);
43+
}
44+
45+
@Test
46+
@Ignore("This is work in progress. So not yet looked into why this fails")
47+
public void shouldCallStartedOnStartup() throws Exception {
48+
assertTrue(moduleTestData.getStartedCallCount(MODULE1_ID) == 1);
49+
assertTrue(moduleTestData.getStartedCallCount(MODULE2_ID) == 1);
50+
assertTrue(moduleTestData.getStartedCallCount(MODULE3_ID) == 1);
51+
}
52+
53+
@Test
54+
public void shouldStartModulesInOrder() throws Exception {
55+
//module test2 depends on test1
56+
//while test3 depends on test2
57+
assertTrue(moduleTestData.getWillStartCallTime(MODULE1_ID) <= moduleTestData.getWillStartCallTime(MODULE2_ID));
Has conversations. Original line has conversations.
58+
assertTrue(moduleTestData.getWillStartCallTime(MODULE2_ID) <= moduleTestData.getWillStartCallTime(MODULE3_ID));
59+
}
60+
61+
@Test
62+
public void shouldCallWillStopAndStoppedOnlyForStoppedModule() throws Exception {
63+
ModuleFactory.stopModule(ModuleFactory.getModuleById(MODULE3_ID));
64+
65+
//should have called willStop() for only module test3
66+
assertTrue(moduleTestData.getWillStopCallCount(MODULE3_ID) == 1);
67+
assertTrue(moduleTestData.getWillStopCallCount(MODULE1_ID) == 0);
68+
assertTrue(moduleTestData.getWillStopCallCount(MODULE2_ID) == 0);
69+
70+
//should have called stopped() for only module test3
71+
assertTrue(moduleTestData.getStoppedCallCount(MODULE3_ID) == 1);
72+
assertTrue(moduleTestData.getStoppedCallCount(MODULE1_ID) == 0);
73+
assertTrue(moduleTestData.getStoppedCallCount(MODULE2_ID) == 0);
Has conversations. Original line has conversations.
74+
}
75+
76+
@Test
77+
public void shouldCallWillStopAndStoppedOnShutdown() throws Exception {
78+
ModuleUtil.shutdown();
79+
80+
//should have called willStop()
81+
assertTrue(moduleTestData.getWillStopCallCount(MODULE1_ID) == 1);
82+
assertTrue(moduleTestData.getWillStopCallCount(MODULE2_ID) == 1);
83+
assertTrue(moduleTestData.getWillStopCallCount(MODULE3_ID) == 1);
84+
85+
//should have called stopped()
86+
assertTrue(moduleTestData.getStoppedCallCount(MODULE1_ID) == 1);
87+
assertTrue(moduleTestData.getStoppedCallCount(MODULE2_ID) == 1);
88+
assertTrue(moduleTestData.getStoppedCallCount(MODULE3_ID) == 1);
89+
90+
//willStop() should have been called before stopped()
91+
assertTrue(moduleTestData.getWillStopCallTime(MODULE1_ID) <= moduleTestData.getStoppedCallTime(MODULE1_ID));
92+
assertTrue(moduleTestData.getWillStopCallTime(MODULE2_ID) <= moduleTestData.getStoppedCallTime(MODULE2_ID));
93+
assertTrue(moduleTestData.getWillStopCallTime(MODULE3_ID) <= moduleTestData.getStoppedCallTime(MODULE3_ID));
94+
}
95+
96+
private void init() {
97+
moduleTestData.init(MODULE1_ID);
98+
moduleTestData.init(MODULE2_ID);
99+
moduleTestData.init(MODULE3_ID);
100+
}
101+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
package org.openmrs.module;
2+
3+
import java.util.Date;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class ModuleTestData {
Has conversations. Original line has conversations.
8+
9+
private Map<String, Integer> willRefreshContextCallCount = new HashMap<String, Integer>();
10+
11+
private Map<String, Integer> contextRefreshedCallCount = new HashMap<String, Integer>();
12+
13+
private Map<String, Integer> willStartCallCount = new HashMap<String, Integer>();
14+
15+
private Map<String, Integer> startedCallCount = new HashMap<String, Integer>();
16+
17+
private Map<String, Integer> willStopCallCount = new HashMap<String, Integer>();
18+
19+
private Map<String, Integer> stoppedCallCount = new HashMap<String, Integer>();
20+
21+
private Map<String, Long> willRefreshContextCallTime = new HashMap<String, Long>();
22+
23+
private Map<String, Long> contextRefreshedCallTime = new HashMap<String, Long>();
Has conversations. Original line has conversations.
24+
25+
private Map<String, Long> willStartCallTime = new HashMap<String, Long>();
26+
27+
private Map<String, Long> startedCallTime = new HashMap<String, Long>();
28+
29+
private Map<String, Long> willStopCallTime = new HashMap<String, Long>();
30+
31+
private Map<String, Long> stoppedCallTime = new HashMap<String, Long>();
32+
33+
private ModuleTestData() {
34+
35+
}
36+
37+
private static class ModuleTestDataHolder {
38+
39+
private static ModuleTestData INSTANCE = null;
40+
}
41+
42+
public static ModuleTestData getInstance() {
43+
if (ModuleTestDataHolder.INSTANCE == null)
44+
ModuleTestDataHolder.INSTANCE = new ModuleTestData();
45+
46+
return ModuleTestDataHolder.INSTANCE;
47+
}
48+
49+
public void init(String moduleId) {
50+
willRefreshContextCallCount.put(moduleId, 0);
51+
contextRefreshedCallCount.put(moduleId, 0);
52+
willStartCallCount.put(moduleId, 0);
53+
startedCallCount.put(moduleId, 0);
54+
willStopCallCount.put(moduleId, 0);
55+
stoppedCallCount.put(moduleId, 0);
56+
57+
willRefreshContextCallTime.put(moduleId, 0l);
58+
contextRefreshedCallTime.put(moduleId, 0l);
59+
willStartCallTime.put(moduleId, 0l);
60+
startedCallTime.put(moduleId, 0l);
61+
willStopCallTime.put(moduleId, 0l);
62+
stoppedCallTime.put(moduleId, 0l);
63+
}
64+
65+
public Integer getWillRefreshContextCallCount(String moduleId) {
66+
Integer count = willRefreshContextCallCount.get(moduleId);
67+
if (count == null) {
68+
count = 0;
69+
}
70+
return count;
71+
}
72+
73+
public Integer getContextRefreshedCallCount(String moduleId) {
74+
Integer count = contextRefreshedCallCount.get(moduleId);
75+
if (count == null) {
76+
count = 0;
77+
}
78+
return count;
79+
}
80+
81+
public Integer getWillStartCallCount(String moduleId) {
82+
Integer count = willStartCallCount.get(moduleId);
83+
if (count == null) {
84+
count = 0;
85+
}
86+
return count;
87+
}
88+
89+
public Integer getStartedCallCount(String moduleId) {
90+
Integer count = startedCallCount.get(moduleId);
91+
if (count == null) {
92+
count = 0;
93+
}
94+
return count;
95+
}
96+
97+
public Integer getWillStopCallCount(String moduleId) {
98+
Integer count = willStopCallCount.get(moduleId);
99+
if (count == null) {
100+
count = 0;
101+
}
102+
return count;
103+
}
104+
105+
public Integer getStoppedCallCount(String moduleId) {
106+
Integer count = stoppedCallCount.get(moduleId);
107+
if (count == null) {
108+
count = 0;
109+
}
110+
return count;
111+
}
112+
113+
public void willRefreshContext(String moduleId) {
114+
willRefreshContextCallTime.put(moduleId, new Date().getTime());
115+
116+
Integer count = willRefreshContextCallCount.get(moduleId);
117+
if (count == null) {
118+
count = 0;
119+
}
120+
willRefreshContextCallCount.put(moduleId, count + 1);
121+
}
122+
123+
public void contextRefreshed(String moduleId) {
124+
contextRefreshedCallTime.put(moduleId, new Date().getTime());
125+
126+
Integer count = contextRefreshedCallCount.get(moduleId);
127+
if (count == null) {
128+
count = 0;
129+
}
130+
contextRefreshedCallCount.put(moduleId, count + 1);
131+
}
132+
133+
public void willStart(String moduleId) {
134+
willStartCallTime.put(moduleId, new Date().getTime());
135+
136+
Integer count = willStartCallCount.get(moduleId);
137+
if (count == null) {
138+
count = 0;
139+
}
140+
willStartCallCount.put(moduleId, count + 1);
141+
}
142+
143+
public void started(String moduleId) {
144+
startedCallTime.put(moduleId, new Date().getTime());
145+
146+
Integer count = startedCallCount.get(moduleId);
147+
if (count == null) {
148+
count = 0;
149+
}
150+
startedCallCount.put(moduleId, count + 1);
151+
}
152+
153+
public void willStop(String moduleId) {
154+
willStopCallTime.put(moduleId, new Date().getTime());
155+
156+
Integer count = willStopCallCount.get(moduleId);
157+
if (count == null) {
158+
count = 0;
159+
}
160+
willStopCallCount.put(moduleId, count + 1);
161+
}
162+
163+
public void stopped(String moduleId) {
164+
stoppedCallTime.put(moduleId, new Date().getTime());
165+
166+
Integer count = stoppedCallCount.get(moduleId);
167+
if (count == null) {
168+
count = 0;
169+
}
170+
stoppedCallCount.put(moduleId, count + 1);
171+
}
172+
173+
public Long getWillRefreshContextCallTime(String moduleId) {
174+
return willRefreshContextCallTime.get(moduleId);
175+
}
176+
177+
public Long getContextRefreshedCallTime(String moduleId) {
178+
return contextRefreshedCallTime.get(moduleId);
179+
}
180+
181+
public Long getWillStartCallTime(String moduleId) {
182+
return willStartCallTime.get(moduleId);
183+
}
184+
185+
public Long getStartedCallTime(String moduleId) {
186+
return startedCallTime.get(moduleId);
187+
}
188+
189+
public Long getWillStopCallTime(String moduleId) {
190+
return willStopCallTime.get(moduleId);
191+
}
192+
193+
public Long getStoppedCallTime(String moduleId) {
194+
return stoppedCallTime.get(moduleId);
195+
}
196+
}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)
Please sign in to comment.