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;
28 * Test of the observer pattern implementation.
30 * @author Erik Brakkee
32 public class ObservableTest extends TestCase {
33 private static final int SUBSCRIBER_COUNT = 100;
34 private static final String UPDATE = "send";
35 private Integer observed;
36 private Observable<Integer, String> observable;
41 * @see junit.framework.TestCase#setUp()
44 protected void setUp() throws Exception {
46 observed = new Integer(1);
47 observable = new Observable<Integer, String>(observed,
48 new DefaultObserverNotifier());
52 * Tests subscription and notification of one subscriber.
54 public void testOneObserver() {
55 final Observer mockObserver = mock(Observer.class);
56 InOrder order = inOrder(mockObserver);
58 long subscription = observable.subscribe(mockObserver);
60 assertEquals(1, observable.getObserverCount());
62 final String message = "hallo";
63 observable.send(message);
65 order.verify(mockObserver).send(observed, message);
66 verifyNoMoreInteractions(mockObserver);
68 observable.unsubscribe(subscription);
69 assertEquals(0, observable.getObserverCount());
71 observable.send(message);
72 verifyNoMoreInteractions(mockObserver);
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 final int nsubscribers = SUBSCRIBER_COUNT;
82 final Observer[] mocks = new Observer[nsubscribers];
83 final InOrder[] order = new InOrder[nsubscribers];
85 List<Long> subscriptions = new ArrayList<Long>();
87 for (int i = 0; i < nsubscribers; i++) {
88 mocks[i] = mock(Observer.class);
89 order[i] = inOrder(mocks[i]);
91 long subscription = observable.subscribe(mocks[i]);
92 assertTrue(subscriptions.add(subscription));
95 assertEquals(nsubscribers, observable.getObserverCount());
97 final String message = "hallo";
99 observable.send(message);
101 for (int i = 0; i < nsubscribers; i++) {
102 order[i].verify(mocks[i]).send(observed, message);
105 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
106 observable.unsubscribe(subscriptions.get(i));
109 assertEquals(nsubscribers - (nsubscribers - (nsubscribers / 2)),
110 observable.getObserverCount());
112 final String message2 = "blabla";
114 observable.send(message2);
116 for (int i = 0; i < (nsubscribers / 2); i++) {
117 order[i].verify(mocks[i]).send(observed, message2);
120 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
121 verifyNoMoreInteractions(mocks[i]);
126 * Subscribes and then unsubscribes with a wrong id. Verifies that
127 * IllegalArgumentException is thrown.
130 public void testUnsubscribeWithWrongSubscription() {
131 Observer<Integer, String> observer = mock(Observer.class);
133 long subscription = observable.subscribe(observer);
135 assertEquals(1, observable.getObserverCount());
138 observable.unsubscribe(subscription + 1);
139 } catch (IllegalArgumentException e) {