qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject [4/6] qpid-jms git commit: Use the MockProvider and remove some redundant tests, also adds some more to cover expected exception cases.
Date Mon, 09 Feb 2015 21:50:57 GMT
Use the MockProvider and remove some redundant tests, also adds some
more to cover expected exception cases.

Project: http://git-wip-us.apache.org/repos/asf/qpid-jms/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-jms/commit/c3b6d6a7
Tree: http://git-wip-us.apache.org/repos/asf/qpid-jms/tree/c3b6d6a7
Diff: http://git-wip-us.apache.org/repos/asf/qpid-jms/diff/c3b6d6a7

Branch: refs/heads/master
Commit: c3b6d6a768635eece08f97f3ca9c2edd1a2caac7
Parents: 1a8b2f6
Author: Timothy Bish <tabish121@gmail.com>
Authored: Mon Feb 9 16:12:05 2015 -0500
Committer: Timothy Bish <tabish121@gmail.com>
Committed: Mon Feb 9 16:12:05 2015 -0500

----------------------------------------------------------------------
 .../org/apache/qpid/jms/JmsConnectionTest.java  | 177 ++++++++-----------
 .../qpid/jms/JmsConnectionTestSupport.java      | 120 ++-----------
 2 files changed, 90 insertions(+), 207 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/c3b6d6a7/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTest.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTest.java b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTest.java
index f025159..331809b 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTest.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTest.java
@@ -23,62 +23,64 @@ import static org.junit.Assert.assertTrue;
 import java.io.IOException;
 import java.net.URI;
 
+import javax.jms.IllegalStateException;
+import javax.jms.InvalidClientIDException;
 import javax.jms.JMSException;
-import javax.jms.Session;
 
 import org.apache.qpid.jms.message.JmsInboundMessageDispatch;
-import org.apache.qpid.jms.meta.JmsConnectionInfo;
-import org.apache.qpid.jms.meta.JmsResource;
-import org.apache.qpid.jms.provider.Provider;
-import org.apache.qpid.jms.provider.ProviderFuture;
+import org.apache.qpid.jms.provider.mock.MockProvider;
+import org.apache.qpid.jms.provider.mock.MockProviderFactory;
 import org.apache.qpid.jms.util.IdGenerator;
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Test;
-import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Test basic functionality around JmsConnection
  */
 public class JmsConnectionTest {
 
-    private static final Logger LOG = LoggerFactory.getLogger(JmsConnectionTest.class);
-
-    private final Provider provider = Mockito.mock(Provider.class);
     private final IdGenerator clientIdGenerator = new IdGenerator();
 
+    private MockProvider provider;
+    private JmsConnection connection;
+
+    @Before
+    public void setUp() throws Exception {
+        provider = (MockProvider) MockProviderFactory.create(new URI("mock://localhost"));
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (connection != null) {
+            connection.close();
+        }
+    }
+
     @Test(expected=JMSException.class)
     public void testJmsConnectionThrowsJMSExceptionProviderStartFails() throws JMSException,
IllegalStateException, IOException {
-        Mockito.doThrow(IOException.class).when(provider).start();
+        provider.getConfiguration().setFailOnStart(true);
         new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
     }
 
     @Test
     public void testStateAfterCreate() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
 
         assertFalse(connection.isStarted());
         assertFalse(connection.isClosed());
         assertFalse(connection.isConnected());
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testSetClientIdWithNull() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.setClientID(null);
-    }
-
     @Test
     public void testGetConnectionId() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
         assertEquals("ID:TEST:1", connection.getConnectionId().toString());
     }
 
     @Test
     public void testAddConnectionListener() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
         JmsConnectionListener listener = new JmsConnectionListener() {
 
             @Override
@@ -109,104 +111,81 @@ public class JmsConnectionTest {
 
     @Test
     public void testConnectionStart() throws JMSException, IOException {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
 
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                LOG.debug("Handling provider create call");
-                if (args[0] instanceof JmsConnectionInfo) {
-                    ProviderFuture request = (ProviderFuture) args[1];
-                    request.onSuccess();
-                }
-                return null;
-            }
-        }).when(provider).create(Mockito.any(JmsResource.class), Mockito.any(ProviderFuture.class));
-
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                LOG.debug("Handling provider destroy call");
-                if (args[0] instanceof JmsConnectionInfo) {
-                    ProviderFuture request = (ProviderFuture) args[1];
-                    request.onSuccess();
-                }
-                return null;
-            }
-        }).when(provider).destroy(Mockito.any(JmsResource.class), Mockito.any(ProviderFuture.class));
-
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
         assertFalse(connection.isConnected());
         connection.start();
         assertTrue(connection.isConnected());
-        connection.close();
     }
 
-    //---------- Test methods fail after connection closed -------------------//
+    @Test
+    public void testConnectionStartAndStop() throws JMSException, IOException {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testSetClientIdAfterClose() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.setClientID("test-Id");
+        assertFalse(connection.isConnected());
+        connection.start();
+        assertTrue(connection.isConnected());
+        connection.stop();
+        assertTrue(connection.isConnected());
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testStartCalledAfterClose() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.start();
+    @Test(timeout=30000, expected=InvalidClientIDException.class)
+    public void testSetClientIDFromNull() throws JMSException, IOException {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        assertFalse(connection.isConnected());
+        connection.setClientID("");
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testStopCalledAfterClose() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.stop();
+    @Test(timeout=30000, expected=InvalidClientIDException.class)
+    public void testSetClientIDFromEmptyString() throws JMSException, IOException {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        assertFalse(connection.isConnected());
+        connection.setClientID(null);
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testSetExceptionListenerAfterClose() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.setExceptionListener(null);
+    @Test(timeout=30000, expected=IllegalStateException.class)
+    public void testSetClientIDFailsOnSecondCall() throws JMSException, IOException {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+
+        assertFalse(connection.isConnected());
+        connection.setClientID("TEST-ID");
+        assertTrue(connection.isConnected());
+        connection.setClientID("TEST-ID");
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testFetExceptionListenerAfterClose() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.getExceptionListener();
+    @Test(timeout=30000, expected=IllegalStateException.class)
+    public void testSetClientIDFailsAfterStart() throws JMSException, IOException {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+
+        assertFalse(connection.isConnected());
+        connection.start();
+        assertTrue(connection.isConnected());
+        connection.setClientID("TEST-ID");
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testCreateConnectionConsumerForTopicAfterClose() throws JMSException {
-        JmsDestination destination = new JmsTopic("test");
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.createConnectionConsumer(destination, null, null, 0);
+    //----- Currently these are unimplemented, these will fail after that ----//
+
+    @Test(timeout=30000, expected=JMSException.class)
+    public void testCreateConnectionConsumer() throws Exception {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        connection.createConnectionConsumer((JmsDestination) new JmsTopic(), "", null, 1);
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testCreateConnectionConsumerForQueueAfterClose() throws JMSException {
-        JmsDestination destination = new JmsQueue("test");
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.createConnectionConsumer(destination, null, null, 0);
+    @Test(timeout=30000, expected=JMSException.class)
+    public void testCreateConnectionTopicConsumer() throws Exception {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        connection.createConnectionConsumer(new JmsTopic(), "", null, 1);
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testCreateTopicSessionAfterClose() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
+    @Test(timeout=30000, expected=JMSException.class)
+    public void testCreateConnectionQueueConsumer() throws Exception {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        connection.createConnectionConsumer(new JmsQueue(), "", null, 1);
     }
 
-    @Test(expected=javax.jms.IllegalStateException.class)
-    public void testCreateQueueSessionAfterClose() throws JMSException {
-        JmsConnection connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
-        connection.close();
-        connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+    @Test(timeout=30000, expected=JMSException.class)
+    public void testCreateDurableConnectionConsumer() throws Exception {
+        connection = new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        connection.createDurableConnectionConsumer(new JmsTopic(), "id", "", null, 1);
     }
 }

http://git-wip-us.apache.org/repos/asf/qpid-jms/blob/c3b6d6a7/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTestSupport.java
----------------------------------------------------------------------
diff --git a/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTestSupport.java
b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTestSupport.java
index a4a4ef5..20c7807 100644
--- a/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTestSupport.java
+++ b/qpid-jms-client/src/test/java/org/apache/qpid/jms/JmsConnectionTestSupport.java
@@ -18,22 +18,13 @@ package org.apache.qpid.jms;
 
 import java.net.URI;
 
-import org.apache.qpid.jms.message.facade.defaults.JmsDefaultMessageFactory;
-import org.apache.qpid.jms.meta.JmsConnectionInfo;
-import org.apache.qpid.jms.meta.JmsResource;
-import org.apache.qpid.jms.meta.JmsSessionId;
 import org.apache.qpid.jms.provider.Provider;
-import org.apache.qpid.jms.provider.ProviderFuture;
 import org.apache.qpid.jms.provider.ProviderListener;
+import org.apache.qpid.jms.provider.mock.MockProviderFactory;
+import org.apache.qpid.jms.provider.mock.MockProviderListener;
 import org.apache.qpid.jms.test.QpidJmsTestCase;
 import org.apache.qpid.jms.util.IdGenerator;
 import org.junit.After;
-import org.junit.Before;
-import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.stubbing.Answer;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Base for tests that require a JmsConnection that is created using a
@@ -41,122 +32,35 @@ import org.slf4j.LoggerFactory;
  */
 public class JmsConnectionTestSupport extends QpidJmsTestCase {
 
-    private static final Logger LOG = LoggerFactory.getLogger(JmsConnectionTestSupport.class);
-
-    private final Provider provider = Mockito.mock(Provider.class);
     private final IdGenerator clientIdGenerator = new IdGenerator();
 
+    protected final MockProviderFactory mockFactory = new MockProviderFactory();
+
     protected JmsConnection connection;
     protected JmsTopicConnection topicConnection;
     protected JmsQueueConnection queueConnection;
     protected ProviderListener providerListener;
 
-    private void createMockProvider() throws Exception {
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                if (args[0] instanceof JmsConnectionInfo) {
-                    providerListener.onConnectionEstablished(new URI("vm://localhost"));
-                }
-                LOG.trace("Handling provider create resource: {}", args[0]);
-                ProviderFuture request = (ProviderFuture) args[1];
-                request.onSuccess();
-                return null;
-            }
-        }).when(provider).create(Mockito.any(JmsResource.class), Mockito.any(ProviderFuture.class));
-
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                if (args[0] instanceof JmsResource) {
-                    LOG.trace("Handling provider start resource: {}", args[0]);
-                    ProviderFuture request = (ProviderFuture) args[1];
-                    request.onSuccess();
-                }
-                return null;
-            }
-        }).when(provider).start(Mockito.any(JmsResource.class), Mockito.any(ProviderFuture.class));
-
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                LOG.trace("Handling provider recover: {}", args[0]);
-                ProviderFuture request = (ProviderFuture) args[1];
-                request.onSuccess();
-                return null;
-            }
-        }).when(provider).recover(Mockito.any(JmsSessionId.class), Mockito.any(ProviderFuture.class));
+    private Provider createMockProvider() throws Exception {
+        return mockFactory.createProvider(new URI("mock://localhost")).setEventListener(new
MockProviderListener() {
 
-        Mockito.doAnswer(new Answer<Object>() {
             @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                LOG.trace("Handling provider session acknowledge: {}", args[0]);
-                ProviderFuture request = (ProviderFuture) args[1];
-                request.onSuccess();
-                return null;
+            public void whenProviderListenerSet(Provider provider, ProviderListener listener)
{
+                providerListener = listener;
             }
-        }).when(provider).acknowledge(Mockito.any(JmsSessionId.class), Mockito.any(ProviderFuture.class));
-
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                if (args[0] instanceof JmsResource) {
-                    LOG.trace("Handling provider destroy resource: {}", args[0]);
-                    ProviderFuture request = (ProviderFuture) args[1];
-                    request.onSuccess();
-                }
-                return null;
-            }
-        }).when(provider).destroy(Mockito.any(JmsResource.class), Mockito.any(ProviderFuture.class));
-
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                if (args[0] instanceof ProviderListener) {
-                    providerListener = (ProviderListener) args[0];
-                }
-                return null;
-            }
-        }).when(provider).setProviderListener(Mockito.any(ProviderListener.class));
-
-        Mockito.doAnswer(new Answer<Object>() {
-            @Override
-            public Object answer(InvocationOnMock invocation) throws Throwable {
-                Object[] args = invocation.getArguments();
-                LOG.trace("Handling provider destroy resource: {}", args[0]);
-                ProviderFuture request = (ProviderFuture) args[1];
-                request.onSuccess();
-                return null;
-            }
-        }).when(provider).unsubscribe(Mockito.anyString(), Mockito.any(ProviderFuture.class));
-
-        Mockito.when(provider.getProviderListener()).thenReturn(providerListener);
-        Mockito.when(provider.getMessageFactory()).thenReturn(new JmsDefaultMessageFactory());
+        });
     }
 
     protected JmsConnection createConnectionToMockProvider() throws Exception {
-        return new JmsConnection("ID:TEST:1", provider, clientIdGenerator);
+        return new JmsConnection("ID:TEST:1", createMockProvider(), clientIdGenerator);
     }
 
     protected JmsQueueConnection createQueueConnectionToMockProvider() throws Exception {
-        return new JmsQueueConnection("ID:TEST:1", provider, clientIdGenerator);
+        return new JmsQueueConnection("ID:TEST:1", createMockProvider(), clientIdGenerator);
     }
 
     protected JmsTopicConnection createTopicConnectionToMockProvider() throws Exception {
-        return new JmsTopicConnection("ID:TEST:1", provider, clientIdGenerator);
-    }
-
-    @Override
-    @Before
-    public void setUp() throws Exception {
-        super.setUp();
-        createMockProvider();
+        return new JmsTopicConnection("ID:TEST:1", createMockProvider(), clientIdGenerator);
     }
 
     @Override


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org


Mime
View raw message