1 package com.notuvy.singleapp;
2
3 import com.notuvy.singleapp.multiuser.MultiUserFixture;
4 import com.notuvy.singleapp.multiuser.MultiUserSingleAppEnforcer;
5 import org.apache.commons.lang.StringUtils;
6 import org.junit.*;
7
8 import java.io.Serializable;
9 import java.util.concurrent.atomic.AtomicInteger;
10
11
12
13
14
15
16
17 public class TestSingletonApplicationLauncher {
18
19
20
21
22 static {
23 System.setProperty("log4j.configuration", "singleapp_log4j.xml");
24 }
25
26 private static final int PORT = 48382;
27
28
29
30
31
32
33
34
35
36 String message = "Uninitialized";
37 private static final MultiUserFixture fixture = new MultiUserFixture();
38
39
40
41
42
43 abstract class App {
44 final String fIdentifier;
45 String fFromPreempt = "";
46 final AtomicInteger fFlag = new AtomicInteger(0);
47
48 App(String pIdentifier) {
49 fIdentifier = pIdentifier;
50 }
51
52 public void run() {
53 message = fIdentifier;
54 if (StringUtils.isNotBlank(fFromPreempt)) {
55 message = message + fFromPreempt;
56 }
57 }
58
59 @Override
60 public String toString() {
61 return String.format("App[%s]", fIdentifier);
62 }
63
64 void waitForDefer() {
65 int value = fFlag.decrementAndGet();
66
67 while (value != 0) {
68 try {
69 Thread.sleep(50);
70 } catch (InterruptedException e) {
71
72 }
73 value = fFlag.intValue();
74 }
75 }
76 }
77
78 class DeferApp extends App implements Deferable {
79 DeferApp(String pIdentifier) {
80 super(pIdentifier);
81 }
82
83 public Serializable sendFromDeferred() {
84 return fIdentifier;
85 }
86
87 public void recvFromDeferred(Object pObject) {
88 message = message + String.format(", (deferred %s)", pObject);
89 fFlag.incrementAndGet();
90 }
91
92 }
93
94 class PreemptApp extends App implements Preemptable {
95 PreemptApp(String pIdentifier) {
96 super(pIdentifier);
97 }
98
99 public Serializable sendToPreemptor() {
100 return fIdentifier;
101 }
102
103 public void recvFromPreempted(Object pObject) {
104 fFromPreempt = String.format(", (preempting %s)", pObject);
105 }
106
107 public void gracefulExitForPreempt() {
108 }
109 }
110
111 @Before
112 public void initialize() {
113 message = "Initial";
114 }
115
116 @After
117 public void stopCurrentRegulator() {
118 fixture.terminate();
119
120 System.setProperty(MultiUserSingleAppEnforcer.TEST_USER_NAME, "");
121 }
122
123 @BeforeClass
124 public static void setupThis() {
125
126 fixture.terminate();
127 }
128
129
130
131
132
133
134 @Test
135 public void testDeferSameUser() throws InterruptedException {
136 Assert.assertEquals("Initial", message);
137
138 DeferApp app1 = new DeferApp("one");
139 SingletonApplicationLauncher first = SingletonApplicationLauncher.onPort(PORT).setSingleUser();
140 first.launchDefer(app1);
141 Assert.assertEquals("one", message);
142
143 SingletonApplicationLauncher.onPort(PORT).setSingleUser().launchDefer(new DeferApp("two"));
144 app1.waitForDefer();
145 Assert.assertEquals("one, (deferred two)", message);
146
147 SingletonApplicationLauncher.onPort(PORT).setSingleUser().launchDefer(new DeferApp("three"));
148 app1.waitForDefer();
149 Assert.assertEquals("one, (deferred two), (deferred three)", message);
150
151 first.shutdown();
152 }
153
154 @Test
155 public void testDeferDifferentUser() throws InterruptedException {
156 Assert.assertEquals("Initial", message);
157
158 SingletonApplicationLauncher.onPort(PORT).launchDefer(new DeferApp("one"));
159 Assert.assertEquals("one", message);
160
161 System.setProperty(MultiUserSingleAppEnforcer.TEST_USER_NAME, "johndoe");
162 DeferApp app4 = new DeferApp("four");
163 SingletonApplicationLauncher.onPort(PORT).launchDefer(app4);
164 Thread.sleep(100);
165 Assert.assertEquals("four", message);
166
167 SingletonApplicationLauncher.onPort(PORT).launchDefer(new DeferApp("five"));
168 app4.waitForDefer();
169 Assert.assertEquals("four, (deferred five)", message);
170 }
171
172
173 @Test
174 public void testPreemptSameUser() {
175 Assert.assertEquals("Initial", message);
176
177 SingletonApplicationLauncher.onPort(PORT).setSingleUser().setAllowSystemExit(false).setAllowSystemExit(false).launchPreempt(new PreemptApp("one"));
178 Assert.assertEquals("one", message);
179
180 SingletonApplicationLauncher.onPort(PORT).setSingleUser().setAllowSystemExit(false).setAllowSystemExit(false).launchPreempt(new PreemptApp("two"));
181 Assert.assertEquals("two, (preempting one)", message);
182
183 SingletonApplicationLauncher last = SingletonApplicationLauncher.onPort(PORT).setSingleUser().setAllowSystemExit(false).setAllowSystemExit(false);
184 last.launchPreempt(new PreemptApp("three"));
185 Assert.assertEquals("three, (preempting two)", message);
186
187 last.shutdown();
188 }
189
190 @Test
191 public void testPreemptDifferentUser() {
192 Assert.assertEquals("Initial", message);
193
194 SingletonApplicationLauncher.onPort(PORT).setAllowSystemExit(false).launchPreempt(new PreemptApp("one"));
195 Assert.assertEquals("one", message);
196
197 System.setProperty(MultiUserSingleAppEnforcer.TEST_USER_NAME, "johndoe");
198 SingletonApplicationLauncher.onPort(PORT).setAllowSystemExit(false).launchPreempt(new PreemptApp("two"));
199 Assert.assertEquals("two", message);
200
201 SingletonApplicationLauncher.onPort(PORT).setAllowSystemExit(false).launchPreempt(new PreemptApp("three"));
202 Assert.assertEquals("three, (preempting two)", message);
203
204 SingletonApplicationLauncher.onPort(PORT).setAllowSystemExit(false).launchPreempt(new PreemptApp("four"));
205 Assert.assertEquals("four, (preempting three)", message);
206 }
207
208
209 }