From 2c302d97359f19396a862f2a5ea481f775c74e31 Mon Sep 17 00:00:00 2001 From: erik Date: Sat, 29 Mar 2008 15:58:28 +0000 Subject: [PATCH] Upgraded to jmock 2.4.0 from 1.1.0 --- trunk/pom.xml | 42 ++-- .../org/wamblee/observer/ObservableTest.java | 233 +++++++++--------- .../java/org/wamblee/test/SpringTestCase.java | 4 +- 3 files changed, 147 insertions(+), 132 deletions(-) diff --git a/trunk/pom.xml b/trunk/pom.xml index 092ca43e..3626101e 100644 --- a/trunk/pom.xml +++ b/trunk/pom.xml @@ -32,16 +32,23 @@ test - jmock - jmock-cglib - 1.1.0 + org.jmock + jmock + ${jmock.version} + test + + + org.jmock + jmock-junit4 + ${jmock.version} + test + + + + org.jmock + jmock-legacy + ${jmock.version} test - - - cglib - cglib-full - - @@ -176,37 +183,37 @@ org.springframework spring-beans - ${springversion} + ${spring.version} org.springframework spring-web - ${springversion} + ${spring.version} org.springframework spring-jms - ${springversion} + ${spring.version} org.springframework spring-context - ${springversion} + ${spring.version} org.springframework spring-hibernate3 - ${springversion} + ${spring.version} org.springframework spring-jpa - ${springversion} + ${spring.version} org.springframework spring-aop - ${springversion} + ${spring.version} @@ -438,7 +445,8 @@ - 2.0.8 + 2.0.8 + 2.4.0 diff --git a/trunk/support/src/test/java/org/wamblee/observer/ObservableTest.java b/trunk/support/src/test/java/org/wamblee/observer/ObservableTest.java index 1669fc00..8ea3e587 100644 --- a/trunk/support/src/test/java/org/wamblee/observer/ObservableTest.java +++ b/trunk/support/src/test/java/org/wamblee/observer/ObservableTest.java @@ -19,121 +19,130 @@ package org.wamblee.observer; import java.util.ArrayList; import java.util.List; -import org.jmock.Mock; -import org.jmock.cglib.MockObjectTestCase; +import junit.framework.TestCase; + +import org.jmock.Expectations; +import org.jmock.Mockery; /** * 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; + private Mockery _context; + + /* + * (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()); + _context = new Mockery(); + + } + + /** + * Tests subscription and notification of one subscriber. + */ + public void testOneObserver() { + final Observer observer = _context.mock(Observer.class); + long subscription = _observable.subscribe(observer); + + assertEquals(1, _observable.getObserverCount()); + + final String message = "hallo"; + _context.checking(new Expectations() { + { + one(observer).send(_observed, message); + } + }); + + _observable.send(message); + _context.assertIsSatisfied(); + _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]; + + List subscriptions = new ArrayList(); + for (int i = 0; i < nsubscribers; i++) { + mocks[i] = _context.mock(Observer.class, "" + i); + long subscription = _observable.subscribe(mocks[i]); + assertTrue(subscriptions.add(subscription)); + } + + assertEquals(nsubscribers, _observable.getObserverCount()); + + final String message = "hallo"; + _context.checking(new Expectations() { + { + for (int i = 0; i < nsubscribers; i++) { + one(mocks[i]).send(_observed, message); + } + } + }); + + _observable.send(message); + _context.assertIsSatisfied(); + + for (int i = nsubscribers / 2; i < nsubscribers; i++) { + _observable.unsubscribe(subscriptions.get(i)); + } + assertEquals(nsubscribers - (nsubscribers - nsubscribers / 2), + _observable.getObserverCount()); + + final String message2 = "blabla"; + _context.checking(new Expectations() { + { + for (int i = 0; i < nsubscribers / 2; i++) { + one(mocks[i]).send(_observed, message2); + } + } + }); + _observable.send(message2); + _context.assertIsSatisfied(); + } + + /** + * Subscribes and then unsubscribes with a wrong id. Verifies that + * IllegalArgumentException is thrown. + * + */ + public void testUnsubscribeWithWrongSubscription() { + Observer observer = _context.mock(Observer.class); + long subscription = _observable.subscribe(observer); + + assertEquals(1, _observable.getObserverCount()); + + try { + _observable.unsubscribe(subscription + 1); + } catch (IllegalArgumentException e) { + return; // ok + } + fail(); + } } diff --git a/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java b/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java index 556bac2a..9e25caaf 100644 --- a/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java +++ b/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java @@ -21,7 +21,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -42,7 +41,6 @@ import org.dbunit.dataset.IDataSet; import org.dbunit.dataset.filter.ITableFilter; import org.dbunit.operation.DatabaseOperation; import org.hibernate.SessionFactory; -import org.jmock.cglib.MockObjectTestCase; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -66,7 +64,7 @@ import org.wamblee.persistence.hibernate.HibernateMappingFiles; /** * Test case support class for spring tests. */ -public class SpringTestCase extends MockObjectTestCase { +public class SpringTestCase extends TestCase { private static final Log LOG = LogFactory.getLog(SpringTestCase.class); -- 2.31.1