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 {
33 private static final int SUBSCRIBER_COUNT = 100;
35 private static final String UPDATE = "send";
37 private Observable<ObservableTest, String> _observable;
42 * @see junit.framework.TestCase#setUp()
45 protected void setUp() throws Exception {
48 _observable = new Observable<ObservableTest, String>(this,
49 new DefaultObserverNotifier());
53 * Tests subscription and notification of one subscriber.
55 public void testOneObserver() {
56 Mock mockObserver = mock(Observer.class);
57 Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
59 long subscription = _observable.subscribe(observer);
61 assertEquals(1, _observable.getObserverCount());
63 String message = "hallo";
64 mockObserver.expects(once()).method(UPDATE).with(same(this),
67 _observable.send(message);
68 _observable.unsubscribe(subscription);
69 assertEquals(0, _observable.getObserverCount());
71 _observable.send(message);
76 * Subscribes many susbcribers and sends notifications to subscribers.
77 * Verifies that unique subscription number are returned. Also verifies that
78 * the correct subscribers are notfied.
80 public void testManySubscribers() {
81 int nsubscribers = SUBSCRIBER_COUNT;
82 Mock[] mocks = new Mock[nsubscribers];
84 List<Long> subscriptions = new ArrayList<Long>();
85 for (int i = 0; i < nsubscribers; i++) {
86 Mock mockObserver = mock(Observer.class);
87 Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) mockObserver
89 long subscription = _observable.subscribe(observer);
91 mocks[i] = mockObserver;
92 assertTrue(subscriptions.add(subscription));
95 assertEquals(nsubscribers, _observable.getObserverCount());
97 String message = "hallo";
98 for (int i = 0; i < nsubscribers; i++) {
99 mocks[i].expects(once()).method(UPDATE).with(same(this),
103 _observable.send(message);
105 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
106 _observable.unsubscribe(subscriptions.get(i));
108 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
109 _observable.getObserverCount());
112 for (int i = 0; i < nsubscribers / 2; i++) {
113 mocks[i].expects(once()).method(UPDATE).with(same(this),
116 _observable.send(message);