Clover Coverage Report - perfectjpattern(Aggregated)
Coverage timestamp: Sat Feb 28 2009 14:35:07 CET
40   221   7   8
0   120   0.17   1.67
5     1.4  
3    
2.2% of code in this file is excluded from these metrics.
 
  TestAsynchronousSubject       Line # 41 32 0% 3 4 87.9% 0.8787879
  TestAsynchronousSubject.TestStatusObserver       Line # 150 6 0% 3 0 100% 1.0
  TestAsynchronousSubject.FaultyStatusObserver       Line # 195 2 25% 1 0 100% 1.0
 
  (1)
 
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  1 toggle @SuppressWarnings("unchecked")
48    public void
49    testAsynchronousNotification()
50    {
51    // test references
52  1 TestStatusObserver myRealObserver1 = new TestStatusObserver();
53  1 TestStatusObserver myRealObserver2 = new TestStatusObserver();
54  1 FaultyStatusObserver myRealFaultyObserver = new FaultyStatusObserver();
55   
56    // create several Observer instances
57  1 IObserver<StatusData> myObserver1 = new SynchronizedProxy<
58    IObserver>(IObserver.class, myRealObserver1).getComponent();
59  1 IObserver<StatusData> myObserver2 = new SynchronizedProxy<
60    IObserver>(IObserver.class, myRealObserver2).getComponent();
61  1 IObserver<StatusData> myFaultyObserver = new SynchronizedProxy<
62    IObserver>(IObserver.class, myRealFaultyObserver).getComponent();
63   
64    // create the asynchronous subject
65  1 ISubject<StatusData> mySubject = new AsynchronousSubject<
66    StatusData>();
67   
68    // attach all observers
69  1 mySubject.attach(myFaultyObserver, myObserver1, myObserver2);
70   
71  1 theCountDownLatch = new CountDownLatch(3);
72   
73    // notify all observers with STARTED
74  1 mySubject.notifyObservers(StatusData.STARTED);
75   
76    // wait for all Observer instances to finish processing the
77    // update event
78  1 try
79    {
80  1 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  1 Thread.sleep(1000);
86    }
87    catch (InterruptedException anException)
88    {
89  0 theLogger.error(anException.getMessage());
90   
91  0 fail(anException.getMessage());
92    }
93   
94    // by now the Faulty Observer must have been automatically detached
95  1 assertEquals("Faulty Observer must have been automatically detached",
96    2, mySubject.size());
97   
98    // detach one observer
99  1 mySubject.detach(myObserver1);
100   
101    // re-create CountDownLatch
102  1 theCountDownLatch = new CountDownLatch(1);
103   
104    // notify remaining observers with COMPLETED
105  1 mySubject.notifyObservers(StatusData.COMPLETED);
106   
107    // wait for all Observer instances to finish processing the
108    // update event
109  1 try
110    {
111  1 theCountDownLatch.await();
112    }
113    catch (InterruptedException anException)
114    {
115  0 theLogger.error(anException.getMessage());
116   
117  0 fail(anException.getMessage());
118    }
119   
120    // assert that Event data received is correct
121  1 assertEquals("myObserver1 did not receive the right events.", 1,
122    myRealObserver1.getStatus().size());
123  1 assertEquals("myObserver1 did not receive the right events.",
124    StatusData.STARTED, myRealObserver1.getStatus().get(0));
125   
126  1 assertEquals("myObserver2 did not receive the right events.", 2,
127    myRealObserver2.getStatus().size());
128  1 assertEquals("myObserver2 did not receive the right events.",
129    StatusData.STARTED, myRealObserver2.getStatus().get(0));
130  1 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  1 Set<String> myThreads = new TreeSet<String>();
135  1 myThreads.addAll(myRealObserver1.getThreads());
136  1 myThreads.addAll(myRealObserver2.getThreads());
137   
138  1 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&lt;StatusData&gt;</code>
147    * implementation.
148    */
149    private static final
 
150    class TestStatusObserver
151    implements IObserver<StatusData>
152    {
153    //---------------------------------------------------------------------
 
154  3 toggle public void
155    update(StatusData aStatusData)
156    {
157  3 try
158    {
159  3 theStatus.add(aStatusData);
160  3 theThreads.add(Thread.currentThread().getName());
161    }
162    finally
163    {
164  3 theCountDownLatch.countDown();
165    }
166    }
167   
168    //---------------------------------------------------------------------
 
169  5 toggle public final List<StatusData>
170    getStatus()
171    {
172  5 return theStatus;
173    }
174   
175    //---------------------------------------------------------------------
 
176  2 toggle public final List<String>
177    getThreads()
178    {
179  2 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&lt;StatusData&gt;</code>
192    * implementation.
193    */
194    private static final
 
195    class FaultyStatusObserver
196    implements IObserver<StatusData>
197    {
198    //---------------------------------------------------------------------
 
199  1 toggle public void
200    update(StatusData aStatusData)
201    {
202  1 try
203    {
204    throw new RuntimeException("I am faulty, remember?");
205    }
206    finally
207    {
208  1 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    }