2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.wamblee.observer;
19 import java.util.ArrayList;
20 import java.util.List;
22 import org.jmock.Mock;
23 import org.jmock.cglib.MockObjectTestCase;
26 * Test of the observer pattern implementation.
28 public class ObservableTest extends MockObjectTestCase {
30 private static final int SUBSCRIBER_COUNT = 100;
32 private static final String UPDATE = "send";
34 private Observable<ObservableTest, String> _observable;
39 * @see junit.framework.TestCase#setUp()
42 protected void setUp() throws Exception {
45 _observable = new Observable<ObservableTest, String>(this,
46 new DefaultObserverNotifier());
50 * Tests subscription and notification of one subscriber.
52 public void testOneObserver() {
53 Mock mockObserver = mock(Observer.class);
54 Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
56 long subscription = _observable.subscribe(observer);
58 assertEquals(1, _observable.getObserverCount());
60 String message = "hallo";
61 mockObserver.expects(once()).method(UPDATE).with(same(this),
64 _observable.send(message);
65 _observable.unsubscribe(subscription);
66 assertEquals(0, _observable.getObserverCount());
68 _observable.send(message);
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.
77 public void testManySubscribers() {
78 int nsubscribers = SUBSCRIBER_COUNT;
79 Mock[] mocks = new Mock[nsubscribers];
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
86 long subscription = _observable.subscribe(observer);
88 mocks[i] = mockObserver;
89 assertTrue(subscriptions.add(subscription));
92 assertEquals(nsubscribers, _observable.getObserverCount());
94 String message = "hallo";
95 for (int i = 0; i < nsubscribers; i++) {
96 mocks[i].expects(once()).method(UPDATE).with(same(this),
100 _observable.send(message);
102 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
103 _observable.unsubscribe(subscriptions.get(i));
105 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
106 _observable.getObserverCount());
109 for (int i = 0; i < nsubscribers / 2; i++) {
110 mocks[i].expects(once()).method(UPDATE).with(same(this),
113 _observable.send(message);
117 * Subscribes and then unsubscribes with a wrong id. Verifies that
118 * IllegalArgumentException is thrown.
121 public void testUnsubscribeWithWrongSubscription() {
122 Mock mockObserver = mock(Observer.class);
123 Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
125 long subscription = _observable.subscribe(observer);
127 assertEquals(1, _observable.getObserverCount());
130 _observable.unsubscribe(subscription + 1);
131 } catch (IllegalArgumentException e) {