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 junit.framework.TestCase;
24 import org.jmock.Expectations;
25 import org.jmock.Mockery;
28 * Test of the observer pattern implementation.
30 * @author Erik Brakkee
32 public class ObservableTest extends TestCase {
34 private static final int SUBSCRIBER_COUNT = 100;
36 private static final String UPDATE = "send";
38 private Integer _observed;
39 private Observable<Integer, String> _observable;
40 private Mockery _context;
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());
53 _context = new Mockery();
58 * Tests subscription and notification of one subscriber.
60 public void testOneObserver() {
61 final Observer observer = _context.mock(Observer.class);
62 long subscription = _observable.subscribe(observer);
64 assertEquals(1, _observable.getObserverCount());
66 final String message = "hallo";
67 _context.checking(new Expectations() {
69 one(observer).send(_observed, message);
73 _observable.send(message);
74 _context.assertIsSatisfied();
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 List<Long> subscriptions = new ArrayList<Long>();
92 for (int i = 0; i < nsubscribers; i++) {
93 mocks[i] = _context.mock(Observer.class, "" + i);
94 long subscription = _observable.subscribe(mocks[i]);
95 assertTrue(subscriptions.add(subscription));
98 assertEquals(nsubscribers, _observable.getObserverCount());
100 final String message = "hallo";
101 _context.checking(new Expectations() {
103 for (int i = 0; i < nsubscribers; i++) {
104 one(mocks[i]).send(_observed, message);
109 _observable.send(message);
110 _context.assertIsSatisfied();
112 for (int i = nsubscribers / 2; i < nsubscribers; i++) {
113 _observable.unsubscribe(subscriptions.get(i));
115 assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2),
116 _observable.getObserverCount());
118 final String message2 = "blabla";
119 _context.checking(new Expectations() {
121 for (int i = 0; i < nsubscribers / 2; i++) {
122 one(mocks[i]).send(_observed, message2);
126 _observable.send(message2);
127 _context.assertIsSatisfied();
131 * Subscribes and then unsubscribes with a wrong id. Verifies that
132 * IllegalArgumentException is thrown.
135 public void testUnsubscribeWithWrongSubscription() {
136 Observer<Integer, String> observer = _context.mock(Observer.class);
137 long subscription = _observable.subscribe(observer);
139 assertEquals(1, _observable.getObserverCount());
142 _observable.unsubscribe(subscription + 1);
143 } catch (IllegalArgumentException e) {