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.mockito.Mockito.*;
21 import java.util.ArrayList;
22 import java.util.List;
24 import org.mockito.InOrder;
26 import junit.framework.TestCase;
29 * Test of the observer pattern implementation.
31 * @author Erik Brakkee
33 public class ObservableTest extends TestCase {
35 private static final int SUBSCRIBER_COUNT = 100;
37 private static final String UPDATE = "send";
39 private Integer _observed;
40 private Observable<Integer, String> _observable;
45 * @see junit.framework.TestCase#setUp()
48 protected void setUp() throws Exception {
50 _observed = new Integer(1);
51 _observable = new Observable<Integer, String>(_observed,
52 new DefaultObserverNotifier());
56 * Tests subscription and notification of one subscriber.
58 public void testOneObserver() {
59 final Observer mockObserver = mock(Observer.class);
60 InOrder order = inOrder(mockObserver);
62 long subscription = _observable.subscribe(mockObserver);
64 assertEquals(1, _observable.getObserverCount());
66 final String message = "hallo";
67 _observable.send(message);
69 order.verify(mockObserver).send(_observed, message);
70 verifyNoMoreInteractions(mockObserver);
72 _observable.unsubscribe(subscription);
73 assertEquals(0, _observable.getObserverCount());
75 _observable.send(message);
76 verifyNoMoreInteractions(mockObserver);
80 * Subscribes many susbcribers and sends notifications to subscribers.
81 * Verifies that unique subscription number are returned. Also verifies that
82 * the correct subscribers are notfied.
84 public void testManySubscribers() {
85 final int nsubscribers = SUBSCRIBER_COUNT;
86 final Observer[] mocks = new Observer[nsubscribers];
87 final InOrder[] order = new InOrder[nsubscribers];
89 List<Long> subscriptions = new ArrayList<Long>();
90 for (int i = 0; i < nsubscribers; i++) {
91 mocks[i] = mock(Observer.class);
92 order[i] = inOrder(mocks[i]);
93 long subscription = _observable.subscribe(mocks[i]);
94 assertTrue(subscriptions.add(subscription));
97 assertEquals(nsubscribers, _observable.getObserverCount());
99 final String message = "hallo";
101 _observable.send(message);
102 for (int i = 0; i < nsubscribers; i++) {
103 order[i].verify(mocks[i]).send(_observed, message);
106 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
107 _observable.unsubscribe(subscriptions.get(i));
109 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
110 _observable.getObserverCount());
112 final String message2 = "blabla";
114 _observable.send(message2);
115 for (int i = 0; i < nsubscribers / 2; i++) {
116 order[i].verify(mocks[i]).send(_observed, message2);
118 for (int i = nsubscribers/2; i < nsubscribers; i++) {
119 verifyNoMoreInteractions(mocks[i]);
125 * Subscribes and then unsubscribes with a wrong id. Verifies that
126 * IllegalArgumentException is thrown.
129 public void testUnsubscribeWithWrongSubscription() {
130 Observer<Integer, String> observer = mock(Observer.class);
132 long subscription = _observable.subscribe(observer);
134 assertEquals(1, _observable.getObserverCount());
137 _observable.unsubscribe(subscription + 1);
138 } catch (IllegalArgumentException e) {