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