X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Forg%2Fwamblee%2Fobserver%2FObservableTest.java;fp=test%2Forg%2Fwamblee%2Fobserver%2FObservableTest.java;h=b2b7bdd5b13ca2fb5744278a2e15a620e91d4de9;hb=dd05ce755afaefa6d3c64c6febf7d242b25b36c0;hp=0000000000000000000000000000000000000000;hpb=bc8798f6191bcb9b8ed6fc20994474fafd01a0b8;p=utils diff --git a/test/org/wamblee/observer/ObservableTest.java b/test/org/wamblee/observer/ObservableTest.java new file mode 100644 index 00000000..b2b7bdd5 --- /dev/null +++ b/test/org/wamblee/observer/ObservableTest.java @@ -0,0 +1,107 @@ +/* + * Copyright 2005 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wamblee.observer; + +import java.util.ArrayList; +import java.util.List; + +import org.jmock.Mock; +import org.jmock.cglib.MockObjectTestCase; + +/** + * Test of the observer pattern implementation. + */ +public class ObservableTest extends MockObjectTestCase { + + private static final String UPDATE = "send"; + + + private Observable _observable; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + + _observable = new Observable(this, new DefaultObserverNotifier()); + } + + /** + * Tests subscription and notification of one subscriber. + */ + public void testOneObserver() { + Mock mockObserver = mock(Observer.class); + Observer observer = (Observer)mockObserver.proxy(); + long subscription = _observable.subscribe(observer); + + assertEquals(1, _observable.getObserverCount()); + + String message = "hallo"; + mockObserver.expects(once()).method(UPDATE).with(same(this), eq(message)); + + _observable.send(message); + _observable.unsubscribe(subscription); + assertEquals(0, _observable.getObserverCount()); + + _observable.send(message); + + } + + + /** + * Subscribes many susbcribers and sends notifications to subscribers. + * Verifies that unique subscription number are returned. + * Also verifies that the correct subscribers are notfied. + */ + public void testManySubscribers() { + int nsubscribers = 100; + Mock[] mocks = new Mock[nsubscribers]; + + List subscriptions = new ArrayList(); + for (int i = 0; i < nsubscribers; i++) { + Mock mockObserver = mock(Observer.class); + Observer observer = (Observer)mockObserver.proxy(); + long subscription = _observable.subscribe(observer); + + mocks[i] = mockObserver; + assertTrue( subscriptions.add(subscription)); + } + + assertEquals(nsubscribers, _observable.getObserverCount()); + + String message = "hallo"; + for (int i = 0; i < nsubscribers; i++) { + mocks[i].expects(once()).method(UPDATE).with(same(this), eq(message)); + } + + _observable.send(message); + + for (int i = nsubscribers/2; i < nsubscribers; i++) { + _observable.unsubscribe(subscriptions.get(i)); + } + assertEquals(nsubscribers - ( nsubscribers - nsubscribers/2), _observable.getObserverCount()); + + message = "blabla"; + for (int i = 0; i < nsubscribers/2; i++) { + mocks[i].expects(once()).method(UPDATE).with(same(this), eq(message)); + } + _observable.send(message); + } + +}