<scope>test</scope>
</dependency>
<dependency>
- <groupId>jmock</groupId>
- <artifactId>jmock-cglib</artifactId>
- <version>1.1.0</version>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock</artifactId>
+ <version>${jmock.version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock-junit4</artifactId>
+ <version>${jmock.version}</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>org.jmock</groupId>
+ <artifactId>jmock-legacy</artifactId>
+ <version>${jmock.version}</version>
<scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>cglib</groupId>
- <artifactId>cglib-full</artifactId>
- </exclusion>
- </exclusions>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
- <version>${springversion}</version>
+ <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
- <version>${springversion}</version>
+ <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
- <version>${springversion}</version>
+ <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
- <version>${springversion}</version>
+ <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-hibernate3</artifactId>
- <version>${springversion}</version>
+ <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jpa</artifactId>
- <version>${springversion}</version>
+ <version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
- <version>${springversion}</version>
+ <version>${spring.version}</version>
</dependency>
<!-- should be possible to remove the dependence on log4j -->
</repositories>
<properties>
- <springversion>2.0.8</springversion>
+ <spring.version>2.0.8</spring.version>
+ <jmock.version>2.4.0</jmock.version>
</properties>
</project>
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<ObservableTest, String> _observable;
-
- /*
- * (non-Javadoc)
- *
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- _observable = new Observable<ObservableTest, String>(this,
- new DefaultObserverNotifier());
- }
-
- /**
- * Tests subscription and notification of one subscriber.
- */
- public void testOneObserver() {
- Mock mockObserver = mock(Observer.class);
- Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) 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<Long> subscriptions = new ArrayList<Long>();
- for (int i = 0; i < nsubscribers; i++) {
- Mock mockObserver = mock(Observer.class);
- Observer<ObservableTest, String> observer = (Observer<ObservableTest, String>) 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<ObservableTest, String> observer = (Observer<ObservableTest, String>) 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<Integer, String> _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<Integer, String>(_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<Long> subscriptions = new ArrayList<Long>();
+ 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<Integer, String> 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();
+ }
}