Upgraded to jmock 2.4.0 from 1.1.0
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sat, 29 Mar 2008 15:58:28 +0000 (15:58 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sat, 29 Mar 2008 15:58:28 +0000 (15:58 +0000)
trunk/pom.xml
trunk/support/src/test/java/org/wamblee/observer/ObservableTest.java
trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java

index 092ca43ee75a94e49695fc75eccba13984e48449..3626101e9270eba61779c7fd311db4f1e5c4832b 100644 (file)
             <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>
index 1669fc00d788ebeaca97caddec736f7cee9ebe1e..8ea3e58799e9c96d55a5e92222fda10b1d1d6745 100644 (file)
@@ -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<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();
+       }
 }
index 556bac2af0f1140f541c978b68d8ae1e633b5172..9e25caaf8785291a3cbecf2a1872e659cbd18258 100644 (file)
@@ -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);