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 * @author Erik Brakkee
30 public class ObservableTest extends MockObjectTestCase {
32 private static final int SUBSCRIBER_COUNT = 100;
34 private static final String UPDATE = "send";
36 private Observable<ObservableTest, String> _observable;
41 * @see junit.framework.TestCase#setUp()
44 protected void setUp() throws Exception {
47 _observable = new Observable<ObservableTest, String>(this,
48 new DefaultObserverNotifier());
52 * Tests subscription and notification of one subscriber.
54 public void testOneObserver() {
55 Mock mockObserver = mock(Observer.class);
56 Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
58 long subscription = _observable.subscribe(observer);
60 assertEquals(1, _observable.getObserverCount());
62 String message = "hallo";
63 mockObserver.expects(once()).method(UPDATE).with(same(this),
66 _observable.send(message);
67 _observable.unsubscribe(subscription);
68 assertEquals(0, _observable.getObserverCount());
70 _observable.send(message);
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.
79 public void testManySubscribers() {
80 int nsubscribers = SUBSCRIBER_COUNT;
81 Mock[] mocks = new Mock[nsubscribers];
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
88 long subscription = _observable.subscribe(observer);
90 mocks[i] = mockObserver;
91 assertTrue(subscriptions.add(subscription));
94 assertEquals(nsubscribers, _observable.getObserverCount());
96 String message = "hallo";
97 for (int i = 0; i < nsubscribers; i++) {
98 mocks[i].expects(once()).method(UPDATE).with(same(this),
102 _observable.send(message);
104 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
105 _observable.unsubscribe(subscriptions.get(i));
107 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
108 _observable.getObserverCount());
111 for (int i = 0; i < nsubscribers / 2; i++) {
112 mocks[i].expects(once()).method(UPDATE).with(same(this),
115 _observable.send(message);
119 * Subscribes and then unsubscribes with a wrong id. Verifies that
120 * IllegalArgumentException is thrown.
123 public void testUnsubscribeWithWrongSubscription() {
124 Mock mockObserver = mock(Observer.class);
125 Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
127 long subscription = _observable.subscribe(observer);
129 assertEquals(1, _observable.getObserverCount());
132 _observable.unsubscribe(subscription + 1);
133 } catch (IllegalArgumentException e) {