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.
16 package org.wamblee.observer;
18 import junit.framework.TestCase;
20 import org.mockito.InOrder;
21 import static org.mockito.Mockito.*;
23 import java.util.ArrayList;
24 import java.util.List;
27 * Test of the observer pattern implementation.
29 * @author Erik Brakkee
31 public class ObservableTest extends TestCase {
32 private static final int SUBSCRIBER_COUNT = 100;
33 private static final String UPDATE = "send";
34 private Integer observed;
35 private Observable<Integer, String> observable;
40 * @see junit.framework.TestCase#setUp()
43 protected void setUp() throws Exception {
45 observed = new Integer(1);
46 observable = new Observable<Integer, String>(observed,
47 new DefaultObserverNotifier());
51 * Tests subscription and notification of one subscriber.
53 public void testOneObserver() {
54 final Observer mockObserver = mock(Observer.class);
55 InOrder order = inOrder(mockObserver);
57 long subscription = observable.subscribe(mockObserver);
59 assertEquals(1, observable.getObserverCount());
61 final String message = "hallo";
62 observable.send(message);
64 order.verify(mockObserver).send(observed, message);
65 verifyNoMoreInteractions(mockObserver);
67 observable.unsubscribe(subscription);
68 assertEquals(0, observable.getObserverCount());
70 observable.send(message);
71 verifyNoMoreInteractions(mockObserver);
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 final int nsubscribers = SUBSCRIBER_COUNT;
81 final Observer[] mocks = new Observer[nsubscribers];
82 final InOrder[] order = new InOrder[nsubscribers];
84 List<Long> subscriptions = new ArrayList<Long>();
86 for (int i = 0; i < nsubscribers; i++) {
87 mocks[i] = mock(Observer.class);
88 order[i] = inOrder(mocks[i]);
90 long subscription = observable.subscribe(mocks[i]);
91 assertTrue(subscriptions.add(subscription));
94 assertEquals(nsubscribers, observable.getObserverCount());
96 final String message = "hallo";
98 observable.send(message);
100 for (int i = 0; i < nsubscribers; i++) {
101 order[i].verify(mocks[i]).send(observed, message);
104 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
105 observable.unsubscribe(subscriptions.get(i));
108 assertEquals(nsubscribers - (nsubscribers - (nsubscribers / 2)),
109 observable.getObserverCount());
111 final String message2 = "blabla";
113 observable.send(message2);
115 for (int i = 0; i < (nsubscribers / 2); i++) {
116 order[i].verify(mocks[i]).send(observed, message2);
119 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
120 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) {