(no commit message)
[utils] / support / 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     /**
31      * 
32      */
33     private static final int SUBSCRIBER_COUNT = 100;
34
35     private static final String UPDATE = "send";
36
37     private Observable<ObservableTest, String> _observable;
38
39     /*
40      * (non-Javadoc)
41      * 
42      * @see junit.framework.TestCase#setUp()
43      */
44     @Override
45     protected void setUp() throws Exception {
46         super.setUp();
47
48         _observable = new Observable<ObservableTest, String>(this,
49                 new DefaultObserverNotifier());
50     }
51
52     /**
53      * Tests subscription and notification of one subscriber.
54      */
55     public void testOneObserver() {
56         Mock mockObserver = mock(Observer.class);
57         Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
58                 .proxy();
59         long subscription = _observable.subscribe(observer);
60
61         assertEquals(1, _observable.getObserverCount());
62
63         String message = "hallo";
64         mockObserver.expects(once()).method(UPDATE).with(same(this),
65                 eq(message));
66
67         _observable.send(message);
68         _observable.unsubscribe(subscription);
69         assertEquals(0, _observable.getObserverCount());
70
71         _observable.send(message);
72
73     }
74
75     /**
76      * Subscribes many susbcribers and sends notifications to subscribers.
77      * Verifies that unique subscription number are returned. Also verifies that
78      * the correct subscribers are notfied.
79      */
80     public void testManySubscribers() {
81         int nsubscribers = SUBSCRIBER_COUNT;
82         Mock[] mocks = new Mock[nsubscribers];
83
84         List<Long> subscriptions = new ArrayList<Long>();
85         for (int i = 0; i < nsubscribers; i++) {
86             Mock mockObserver = mock(Observer.class);
87             Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
88                     .proxy();
89             long subscription = _observable.subscribe(observer);
90
91             mocks[i] = mockObserver;
92             assertTrue(subscriptions.add(subscription));
93         }
94
95         assertEquals(nsubscribers, _observable.getObserverCount());
96
97         String message = "hallo";
98         for (int i = 0; i < nsubscribers; i++) {
99             mocks[i].expects(once()).method(UPDATE).with(same(this),
100                     eq(message));
101         }
102
103         _observable.send(message);
104
105         for (int i = nsubscribers / 2; i < nsubscribers; i++) {
106             _observable.unsubscribe(subscriptions.get(i));
107         }
108         assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
109                 _observable.getObserverCount());
110
111         message = "blabla";
112         for (int i = 0; i < nsubscribers / 2; i++) {
113             mocks[i].expects(once()).method(UPDATE).with(same(this),
114                     eq(message));
115         }
116         _observable.send(message);
117     }
118
119 }