X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fobserver%2FObservableTest.java;h=ff37ccb9bfeb1e72aea95826c5fcf473f86b5a4a;hb=96d8a3fb8ba278dc93fcc32d1c37ce6bd8b6726e;hp=08e5a2aa4f220336afd2b79b4163d1325d614ceb;hpb=fe85d022196c8578bb549945260a6e3c2017c33c;p=utils diff --git a/support/src/test/java/org/wamblee/observer/ObservableTest.java b/support/src/test/java/org/wamblee/observer/ObservableTest.java index 08e5a2aa..ff37ccb9 100644 --- a/support/src/test/java/org/wamblee/observer/ObservableTest.java +++ b/support/src/test/java/org/wamblee/observer/ObservableTest.java @@ -16,122 +16,135 @@ package org.wamblee.observer; +import static org.easymock.EasyMock.createControl; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + import java.util.ArrayList; import java.util.List; -import org.jmock.Mock; -import org.jmock.cglib.MockObjectTestCase; +import junit.framework.TestCase; + +import org.easymock.IMocksControl; /** * Test of the observer pattern implementation. + * + * @author Erik Brakkee */ -public class ObservableTest extends MockObjectTestCase { - - private static final int SUBSCRIBER_COUNT = 100; - - 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 = SUBSCRIBER_COUNT; - 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); - } - - /** - * Subscribes and then unsubscribes with a wrong id. Verifies that - * IllegalArgumentException is thrown. - * - */ - public void testUnsubscribeWithWrongSubscription() { - Mock mockObserver = mock(Observer.class); - Observer observer = (Observer) mockObserver - .proxy(); - long subscription = _observable.subscribe(observer); - - assertEquals(1, _observable.getObserverCount()); - - try { - _observable.unsubscribe(subscription + 1); - } catch (IllegalArgumentException e) { - return; // ok - } - fail(); - } - +public class ObservableTest extends TestCase { + + private static final int SUBSCRIBER_COUNT = 100; + + private static final String UPDATE = "send"; + + private Integer _observed; + private Observable _observable; + + /* + * (non-Javadoc) + * + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + _observed = new Integer(1); + _observable = new Observable(_observed, + new DefaultObserverNotifier()); + } + + /** + * Tests subscription and notification of one subscriber. + */ + public void testOneObserver() { + final Observer mockObserver = createStrictMock(Observer.class); + long subscription = _observable.subscribe(mockObserver); + + assertEquals(1, _observable.getObserverCount()); + + final String message = "hallo"; + mockObserver.send(_observed, message); + replay(mockObserver); + + _observable.send(message); + verify(mockObserver); + + _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() { + final int nsubscribers = SUBSCRIBER_COUNT; + final Observer[] mocks = new Observer[nsubscribers]; + + IMocksControl control = createControl(); + + List subscriptions = new ArrayList(); + for (int i = 0; i < nsubscribers; i++) { + mocks[i] = control.createMock("mock" + i, Observer.class); + long subscription = _observable.subscribe(mocks[i]); + assertTrue(subscriptions.add(subscription)); + } + + assertEquals(nsubscribers, _observable.getObserverCount()); + + final String message = "hallo"; + + for (int i = 0; i < nsubscribers; i++) { + mocks[i].send(_observed, message); + } + control.replay(); + + _observable.send(message); + control.verify(); + + for (int i = nsubscribers / 2; i < nsubscribers; i++) { + _observable.unsubscribe(subscriptions.get(i)); + } + assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2), + _observable.getObserverCount()); + + control.reset(); + final String message2 = "blabla"; + + for (int i = 0; i < nsubscribers / 2; i++) { + mocks[i].send(_observed, message2); + } + control.replay(); + + _observable.send(message2); + control.verify(); + } + + /** + * Subscribes and then unsubscribes with a wrong id. Verifies that + * IllegalArgumentException is thrown. + * + */ + public void testUnsubscribeWithWrongSubscription() { + Observer observer = createMock(Observer.class); + replay(observer); + + long subscription = _observable.subscribe(observer); + + assertEquals(1, _observable.getObserverCount()); + + try { + _observable.unsubscribe(subscription + 1); + } catch (IllegalArgumentException e) { + return; // ok + } + fail(); + } }