ff37ccb9bfeb1e72aea95826c5fcf473f86b5a4a
[utils] / support / general / src / test / java / 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 static org.easymock.EasyMock.createControl;
20 import static org.easymock.EasyMock.createMock;
21 import static org.easymock.EasyMock.createStrictMock;
22 import static org.easymock.EasyMock.replay;
23 import static org.easymock.EasyMock.verify;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import junit.framework.TestCase;
29
30 import org.easymock.IMocksControl;
31
32 /**
33  * Test of the observer pattern implementation.
34  * 
35  * @author Erik Brakkee
36  */
37 public class ObservableTest extends TestCase {
38
39         private static final int SUBSCRIBER_COUNT = 100;
40
41         private static final String UPDATE = "send";
42
43         private Integer _observed;
44         private Observable<Integer, String> _observable;
45
46         /*
47          * (non-Javadoc)
48          * 
49          * @see junit.framework.TestCase#setUp()
50          */
51         @Override
52         protected void setUp() throws Exception {
53                 super.setUp();
54                 _observed = new Integer(1);
55                 _observable = new Observable<Integer, String>(_observed,
56                                 new DefaultObserverNotifier());
57         }
58
59         /**
60          * Tests subscription and notification of one subscriber.
61          */
62         public void testOneObserver() {
63                 final Observer mockObserver = createStrictMock(Observer.class);
64                 long subscription = _observable.subscribe(mockObserver);
65
66                 assertEquals(1, _observable.getObserverCount());
67
68                 final String message = "hallo";
69                 mockObserver.send(_observed, message);
70                 replay(mockObserver);
71
72                 _observable.send(message);
73                 verify(mockObserver);
74
75                 _observable.unsubscribe(subscription);
76                 assertEquals(0, _observable.getObserverCount());
77
78                 _observable.send(message);
79
80         }
81
82         /**
83          * Subscribes many susbcribers and sends notifications to subscribers.
84          * Verifies that unique subscription number are returned. Also verifies that
85          * the correct subscribers are notfied.
86          */
87         public void testManySubscribers() {
88                 final int nsubscribers = SUBSCRIBER_COUNT;
89                 final Observer[] mocks = new Observer[nsubscribers];
90
91                 IMocksControl control = createControl();
92
93                 List<Long> subscriptions = new ArrayList<Long>();
94                 for (int i = 0; i < nsubscribers; i++) {
95                         mocks[i] = control.createMock("mock" + i, Observer.class);
96                         long subscription = _observable.subscribe(mocks[i]);
97                         assertTrue(subscriptions.add(subscription));
98                 }
99
100                 assertEquals(nsubscribers, _observable.getObserverCount());
101
102                 final String message = "hallo";
103
104                 for (int i = 0; i < nsubscribers; i++) {
105                         mocks[i].send(_observed, message);
106                 }
107                 control.replay();
108
109                 _observable.send(message);
110                 control.verify();
111
112                 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
113                         _observable.unsubscribe(subscriptions.get(i));
114                 }
115                 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
116                                 _observable.getObserverCount());
117
118                 control.reset();
119                 final String message2 = "blabla";
120
121                 for (int i = 0; i < nsubscribers / 2; i++) {
122                         mocks[i].send(_observed, message2);
123                 }
124                 control.replay();
125
126                 _observable.send(message2);
127                 control.verify();
128         }
129
130         /**
131          * Subscribes and then unsubscribes with a wrong id. Verifies that
132          * IllegalArgumentException is thrown.
133          * 
134          */
135         public void testUnsubscribeWithWrongSubscription() {
136                 Observer<Integer, String> observer = createMock(Observer.class);
137                 replay(observer);
138                 
139                 long subscription = _observable.subscribe(observer);
140
141                 assertEquals(1, _observable.getObserverCount());
142
143                 try {
144                         _observable.unsubscribe(subscription + 1);
145                 } catch (IllegalArgumentException e) {
146                         return; // ok
147                 }
148                 fail();
149         }
150 }