initial version of support project with build support.
[utils] / test / org / wamblee / observer / ObservableTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.observer;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.jmock.Mock;
23 import org.jmock.cglib.MockObjectTestCase;
24
25 /**
26  * Test of the observer pattern implementation. 
27  */
28 public class ObservableTest extends MockObjectTestCase {
29     
30     private static final String UPDATE = "send";
31     
32
33     private Observable<ObservableTest, String> _observable; 
34   
35     /* (non-Javadoc)
36      * @see junit.framework.TestCase#setUp()
37      */
38     @Override
39     protected void setUp() throws Exception {
40         super.setUp();
41         
42         _observable = new Observable<ObservableTest, String>(this, new DefaultObserverNotifier());
43     }
44     
45     /**
46      * Tests subscription and notification of one subscriber.
47      */
48     public void testOneObserver() { 
49         Mock mockObserver = mock(Observer.class);
50         Observer<ObservableTest,String> observer = (Observer<ObservableTest,String>)mockObserver.proxy();        
51         long subscription = _observable.subscribe(observer);
52         
53         assertEquals(1, _observable.getObserverCount()); 
54         
55         String message = "hallo";
56         mockObserver.expects(once()).method(UPDATE).with(same(this), eq(message)); 
57        
58         _observable.send(message);
59         _observable.unsubscribe(subscription); 
60         assertEquals(0, _observable.getObserverCount()); 
61         
62         _observable.send(message);
63        
64     }
65    
66     
67     /**
68      * Subscribes many susbcribers and sends notifications to subscribers. 
69      * Verifies that unique subscription number are returned. 
70      * Also verifies that the correct subscribers are notfied. 
71      */
72     public void testManySubscribers() { 
73         int nsubscribers = 100; 
74         Mock[] mocks = new Mock[nsubscribers];
75  
76         List<Long> subscriptions = new ArrayList<Long>();
77         for (int i = 0; i < nsubscribers; i++) { 
78             Mock mockObserver = mock(Observer.class);
79             Observer<ObservableTest,String> observer = (Observer<ObservableTest,String>)mockObserver.proxy();        
80             long subscription = _observable.subscribe(observer);
81             
82             mocks[i] = mockObserver;
83             assertTrue( subscriptions.add(subscription));  
84         }
85         
86         assertEquals(nsubscribers, _observable.getObserverCount());
87        
88         String message = "hallo";
89         for (int i = 0; i < nsubscribers; i++) { 
90            mocks[i].expects(once()).method(UPDATE).with(same(this), eq(message));
91         }
92         
93         _observable.send(message);
94        
95         for (int i = nsubscribers/2; i < nsubscribers; i++) { 
96             _observable.unsubscribe(subscriptions.get(i));    
97         }
98         assertEquals(nsubscribers - ( nsubscribers - nsubscribers/2), _observable.getObserverCount());
99      
100         message = "blabla"; 
101         for (int i = 0; i < nsubscribers/2; i++) { 
102             mocks[i].expects(once()).method(UPDATE).with(same(this), eq(message));
103         }
104         _observable.send(message);
105     }
106
107 }