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