1 //----------------------------------------------------------------------
2 //
3 // PerfectJPattern: "Design patterns are good but components are better!"
4 // TestAsynchronousSubject.java Copyright (c) 2009 Giovanni Azua Garcia
5 // bravegag@hotmail.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 3
10 // of the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, see <http://www.gnu.org/licenses/>.
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 * Test suite for {@link AsynchronousSubject} implementation.
36 *
37 * @author <a href="mailto:bravegag@hotmail.com">Giovanni Azua</a>
38 * @version $Revision: 1.0 $ $Date: Nov 17, 2007 9:57:59 PM $
39 */
40 public
41 class TestAsynchronousSubject
42 extends TestCase
43 {
44 //------------------------------------------------------------------------
45 // public
46 //------------------------------------------------------------------------
47 @SuppressWarnings("unchecked")
48 public void
49 testAsynchronousNotification()
50 {
51 // test references
52 TestStatusObserver myRealObserver1 = new TestStatusObserver();
53 TestStatusObserver myRealObserver2 = new TestStatusObserver();
54 FaultyStatusObserver myRealFaultyObserver = new FaultyStatusObserver();
55
56 // create several Observer instances
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 // create the asynchronous subject
65 ISubject<StatusData> mySubject = new AsynchronousSubject<
66 StatusData>();
67
68 // attach all observers
69 mySubject.attach(myFaultyObserver, myObserver1, myObserver2);
70
71 theCountDownLatch = new CountDownLatch(3);
72
73 // notify all observers with STARTED
74 mySubject.notifyObservers(StatusData.STARTED);
75
76 // wait for all Observer instances to finish processing the
77 // update event
78 try
79 {
80 theCountDownLatch.await();
81
82 // unavoidable without changing the AsynchronousSubject
83 // implementation... there is a time needed to modify the
84 // internal Subject list that is out of the Barrier time
85 Thread.sleep(1000);
86 }
87 catch (InterruptedException anException)
88 {
89 theLogger.error(anException.getMessage());
90
91 fail(anException.getMessage());
92 }
93
94 // by now the Faulty Observer must have been automatically detached
95 assertEquals("Faulty Observer must have been automatically detached",
96 2, mySubject.size());
97
98 // detach one observer
99 mySubject.detach(myObserver1);
100
101 // re-create CountDownLatch
102 theCountDownLatch = new CountDownLatch(1);
103
104 // notify remaining observers with COMPLETED
105 mySubject.notifyObservers(StatusData.COMPLETED);
106
107 // wait for all Observer instances to finish processing the
108 // update event
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 // assert that Event data received is correct
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 // assert that all notification threads are different
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 // inner classes
144 //------------------------------------------------------------------------
145 /**
146 * Example of concrete <code>IObserver<StatusData></code>
147 * implementation.
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 // members
184 //---------------------------------------------------------------------
185 private final List<String> theThreads = new ArrayList<String>();
186 private final List<StatusData> theStatus = new ArrayList<StatusData>();
187 }
188
189 //------------------------------------------------------------------------
190 /**
191 * Example of concrete <code>IObserver<StatusData></code>
192 * implementation.
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 // members
215 //------------------------------------------------------------------------
216 /**
217 * Provides logging services for this class.
218 */
219 private final Logger theLogger = LoggerFactory.getLogger(this.getClass());
220 private static CountDownLatch theCountDownLatch;
221 }