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 static org.easymock.EasyMock.createControl;
20 import static org.easymock.EasyMock.createMock;
21 import static org.easymock.EasyMock.createStrictMock;
22 import static org.easymock.EasyMock.replay;
23 import static org.easymock.EasyMock.verify;
25 import java.util.ArrayList;
26 import java.util.List;
28 import junit.framework.TestCase;
30 import org.easymock.IMocksControl;
33 * Test of the observer pattern implementation.
35 * @author Erik Brakkee
37 public class ObservableTest extends TestCase {
39 private static final int SUBSCRIBER_COUNT = 100;
41 private static final String UPDATE = "send";
43 private Integer _observed;
44 private Observable<Integer, String> _observable;
49 * @see junit.framework.TestCase#setUp()
52 protected void setUp() throws Exception {
54 _observed = new Integer(1);
55 _observable = new Observable<Integer, String>(_observed,
56 new DefaultObserverNotifier());
60 * Tests subscription and notification of one subscriber.
62 public void testOneObserver() {
63 final Observer mockObserver = createStrictMock(Observer.class);
64 long subscription = _observable.subscribe(mockObserver);
66 assertEquals(1, _observable.getObserverCount());
68 final String message = "hallo";
69 mockObserver.send(_observed, message);
72 _observable.send(message);
75 _observable.unsubscribe(subscription);
76 assertEquals(0, _observable.getObserverCount());
78 _observable.send(message);
83 * Subscribes many susbcribers and sends notifications to subscribers.
84 * Verifies that unique subscription number are returned. Also verifies that
85 * the correct subscribers are notfied.
87 public void testManySubscribers() {
88 final int nsubscribers = SUBSCRIBER_COUNT;
89 final Observer[] mocks = new Observer[nsubscribers];
91 IMocksControl control = createControl();
93 List<Long> subscriptions = new ArrayList<Long>();
94 for (int i = 0; i < nsubscribers; i++) {
95 mocks[i] = control.createMock("mock" + i, Observer.class);
96 long subscription = _observable.subscribe(mocks[i]);
97 assertTrue(subscriptions.add(subscription));
100 assertEquals(nsubscribers, _observable.getObserverCount());
102 final String message = "hallo";
104 for (int i = 0; i < nsubscribers; i++) {
105 mocks[i].send(_observed, message);
109 _observable.send(message);
112 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
113 _observable.unsubscribe(subscriptions.get(i));
115 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
116 _observable.getObserverCount());
119 final String message2 = "blabla";
121 for (int i = 0; i < nsubscribers / 2; i++) {
122 mocks[i].send(_observed, message2);
126 _observable.send(message2);
131 * Subscribes and then unsubscribes with a wrong id. Verifies that
132 * IllegalArgumentException is thrown.
135 public void testUnsubscribeWithWrongSubscription() {
136 Observer<Integer, String> observer = createMock(Observer.class);
139 long subscription = _observable.subscribe(observer);
141 assertEquals(1, _observable.getObserverCount());
144 _observable.unsubscribe(subscription + 1);
145 } catch (IllegalArgumentException e) {