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 String UPDATE = "send";
33 private Observable<ObservableTest, String> _observable;
36 * @see junit.framework.TestCase#setUp()
39 protected void setUp() throws Exception {
42 _observable = new Observable<ObservableTest, String>(this, new DefaultObserverNotifier());
46 * Tests subscription and notification of one subscriber.
48 public void testOneObserver() {
49 Mock mockObserver = mock(Observer.class);
50 Observer<ObservableTest,String> observer = (Observer<ObservableTest,String>)mockObserver.proxy();
51 long subscription = _observable.subscribe(observer);
53 assertEquals(1, _observable.getObserverCount());
55 String message = "hallo";
56 mockObserver.expects(once()).method(UPDATE).with(same(this), eq(message));
58 _observable.send(message);
59 _observable.unsubscribe(subscription);
60 assertEquals(0, _observable.getObserverCount());
62 _observable.send(message);
68 * Subscribes many susbcribers and sends notifications to subscribers.
69 * Verifies that unique subscription number are returned.
70 * Also verifies that the correct subscribers are notfied.
72 public void testManySubscribers() {
73 int nsubscribers = 100;
74 Mock[] mocks = new Mock[nsubscribers];
76 List<Long> subscriptions = new ArrayList<Long>();
77 for (int i = 0; i < nsubscribers; i++) {
78 Mock mockObserver = mock(Observer.class);
79 Observer<ObservableTest,String> observer = (Observer<ObservableTest,String>)mockObserver.proxy();
80 long subscription = _observable.subscribe(observer);
82 mocks[i] = mockObserver;
83 assertTrue( subscriptions.add(subscription));
86 assertEquals(nsubscribers, _observable.getObserverCount());
88 String message = "hallo";
89 for (int i = 0; i < nsubscribers; i++) {
90 mocks[i].expects(once()).method(UPDATE).with(same(this), eq(message));
93 _observable.send(message);
95 for (int i = nsubscribers/2; i < nsubscribers; i++) {
96 _observable.unsubscribe(subscriptions.get(i));
98 assertEquals(nsubscribers - ( nsubscribers - nsubscribers/2), _observable.getObserverCount());
101 for (int i = 0; i < nsubscribers/2; i++) {
102 mocks[i].expects(once()).method(UPDATE).with(same(this), eq(message));
104 _observable.send(message);