1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.perfectjpattern.core.behavioral.observer;
22
23 import java.util.*;
24 import java.util.concurrent.*;
25
26 import junit.framework.*;
27
28 import org.perfectjpattern.core.api.behavioral.observer.*;
29 import org.perfectjpattern.core.behavioral.observer.data.*;
30 import org.perfectjpattern.core.structural.proxy.*;
31 import org.slf4j.*;
32
33
34
35
36
37
38
39
40 public
41 class TestAsynchronousSubject
42 extends TestCase
43 {
44
45
46
47 @SuppressWarnings("unchecked")
48 public void
49 testAsynchronousNotification()
50 {
51
52 TestStatusObserver myRealObserver1 = new TestStatusObserver();
53 TestStatusObserver myRealObserver2 = new TestStatusObserver();
54 FaultyStatusObserver myRealFaultyObserver = new FaultyStatusObserver();
55
56
57 IObserver<StatusData> myObserver1 = new SynchronizedProxy<
58 IObserver>(IObserver.class, myRealObserver1).getComponent();
59 IObserver<StatusData> myObserver2 = new SynchronizedProxy<
60 IObserver>(IObserver.class, myRealObserver2).getComponent();
61 IObserver<StatusData> myFaultyObserver = new SynchronizedProxy<
62 IObserver>(IObserver.class, myRealFaultyObserver).getComponent();
63
64
65 ISubject<StatusData> mySubject = new AsynchronousSubject<
66 StatusData>();
67
68
69 mySubject.attach(myFaultyObserver, myObserver1, myObserver2);
70
71 theCountDownLatch = new CountDownLatch(3);
72
73
74 mySubject.notifyObservers(StatusData.STARTED);
75
76
77
78 try
79 {
80 theCountDownLatch.await();
81
82
83
84
85 Thread.sleep(1000);
86 }
87 catch (InterruptedException anException)
88 {
89 theLogger.error(anException.getMessage());
90
91 fail(anException.getMessage());
92 }
93
94
95 assertEquals("Faulty Observer must have been automatically detached",
96 2, mySubject.size());
97
98
99 mySubject.detach(myObserver1);
100
101
102 theCountDownLatch = new CountDownLatch(1);
103
104
105 mySubject.notifyObservers(StatusData.COMPLETED);
106
107
108
109 try
110 {
111 theCountDownLatch.await();
112 }
113 catch (InterruptedException anException)
114 {
115 theLogger.error(anException.getMessage());
116
117 fail(anException.getMessage());
118 }
119
120
121 assertEquals("myObserver1 did not receive the right events.", 1,
122 myRealObserver1.getStatus().size());
123 assertEquals("myObserver1 did not receive the right events.",
124 StatusData.STARTED, myRealObserver1.getStatus().get(0));
125
126 assertEquals("myObserver2 did not receive the right events.", 2,
127 myRealObserver2.getStatus().size());
128 assertEquals("myObserver2 did not receive the right events.",
129 StatusData.STARTED, myRealObserver2.getStatus().get(0));
130 assertEquals("myObserver2 did not receive the right events.",
131 StatusData.COMPLETED, myRealObserver2.getStatus().get(1));
132
133
134 Set<String> myThreads = new TreeSet<String>();
135 myThreads.addAll(myRealObserver1.getThreads());
136 myThreads.addAll(myRealObserver2.getThreads());
137
138 assertEquals("The number of different threads being used was " +
139 "different than expected.", 3, myThreads.size());
140 }
141
142
143
144
145
146
147
148
149 private static final
150 class TestStatusObserver
151 implements IObserver<StatusData>
152 {
153
154 public void
155 update(StatusData aStatusData)
156 {
157 try
158 {
159 theStatus.add(aStatusData);
160 theThreads.add(Thread.currentThread().getName());
161 }
162 finally
163 {
164 theCountDownLatch.countDown();
165 }
166 }
167
168
169 public final List<StatusData>
170 getStatus()
171 {
172 return theStatus;
173 }
174
175
176 public final List<String>
177 getThreads()
178 {
179 return theThreads;
180 }
181
182
183
184
185 private final List<String> theThreads = new ArrayList<String>();
186 private final List<StatusData> theStatus = new ArrayList<StatusData>();
187 }
188
189
190
191
192
193
194 private static final
195 class FaultyStatusObserver
196 implements IObserver<StatusData>
197 {
198
199 public void
200 update(StatusData aStatusData)
201 {
202 try
203 {
204 throw new RuntimeException("I am faulty, remember?");
205 }
206 finally
207 {
208 theCountDownLatch.countDown();
209 }
210 }
211 }
212
213
214
215
216
217
218
219 private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
220 private static CountDownLatch theCountDownLatch;
221 }