activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nmitt...@apache.org
Subject svn commit: r620291 - in /activemq/activemq-cpp/trunk/src/test/activemq/cmsutil: CmsTemplateTest.cpp CmsTemplateTest.h DummyConsumer.h DummySession.h MessageContext.h
Date Sun, 10 Feb 2008 16:38:22 GMT
Author: nmittler
Date: Sun Feb 10 08:38:21 2008
New Revision: 620291

URL: http://svn.apache.org/viewvc?rev=620291&view=rev
Log:
AMQCPP-152 - Adding tests for synchronous receive to CmsTemplate

Added:
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyConsumer.h
Modified:
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.cpp
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.h
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h
    activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/MessageContext.h

Modified: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.cpp?rev=620291&r1=620290&r2=620291&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.cpp Sun Feb 10 08:38:21
2008
@@ -201,3 +201,304 @@
         e.printStackTrace();
     }
 }
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplateTest::testReceive() {
+    
+    try {
+
+        MessageContext* messageContext = cf->getMessageContext();
+                
+        MySendListener listener;
+        messageContext->setSendListener(&listener);
+        
+        // First, test basics.
+        cms::Message* message = cmsTemplate->receive(); 
+        CPPUNIT_ASSERT( message != NULL );
+        const cms::Queue* q = dynamic_cast<const cms::Queue*>(listener.dest);
+        CPPUNIT_ASSERT(q != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"test", q->getQueueName());
+        CPPUNIT_ASSERT(listener.selector == "" );
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->isNoLocal(), listener.noLocal);
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->getReceiveTimeout(), listener.timeout);
+        delete message;        
+        
+        // Now change the destination type and verify that it changes.
+        cmsTemplate->setPubSubDomain(true);
+        message = cmsTemplate->receive(); 
+        CPPUNIT_ASSERT( message != NULL );
+        const cms::Topic* t = dynamic_cast<const cms::Topic*>(listener.dest);
+        CPPUNIT_ASSERT(t != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"test", t->getTopicName());
+        delete message;
+        
+        // Now change the destination name and verify that it changes.
+        cmsTemplate->setDefaultDestinationName("bob");
+        message = cmsTemplate->receive(); 
+        CPPUNIT_ASSERT( message != NULL );
+        t = dynamic_cast<const cms::Topic*>(listener.dest);
+        CPPUNIT_ASSERT(t != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"bob", t->getTopicName());
+        delete message;
+        
+        // Now change the timeout and verify that it changes.
+        cmsTemplate->setReceiveTimeout(1000);
+        message = cmsTemplate->receive(); 
+        CPPUNIT_ASSERT( message != NULL );
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->getReceiveTimeout(), listener.timeout);
+        delete message;
+        
+        // Now change the noLocal flag and verify that it changes.
+        cmsTemplate->setNoLocal(true);
+        message = cmsTemplate->receive(); 
+        CPPUNIT_ASSERT( message != NULL );
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->isNoLocal(), listener.noLocal);
+        delete message;
+        
+        // Now try a failure.
+        try {
+            
+            FailSendListener failListener;
+            messageContext->setSendListener(&failListener);
+            
+            // First, test basics.
+            cmsTemplate->receive(); 
+            CPPUNIT_FAIL("failed to throw expected exception");
+            
+        } catch( cms::CMSException& e) {
+            // Expected
+        }
+                
+    } catch( cms::CMSException& e) {
+        e.printStackTrace();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplateTest::testReceive_Destination() {
+    
+    try {
+
+        MessageContext* messageContext = cf->getMessageContext();
+                
+        MySendListener listener;
+        messageContext->setSendListener(&listener);
+        
+        // First, test basics.
+        activemq::connector::stomp::StompTopic myTopic("anothertopic");
+        cms::Message* message = cmsTemplate->receive(&myTopic); 
+        CPPUNIT_ASSERT( message != NULL );
+        CPPUNIT_ASSERT(&myTopic == listener.dest);
+        delete message;
+        
+        // Now try a failure.
+        try {
+            
+            FailSendListener failListener;
+            messageContext->setSendListener(&failListener);
+            
+            // First, test basics.
+            cmsTemplate->receive(&myTopic); 
+            CPPUNIT_FAIL("failed to throw expected exception");
+            
+        } catch( cms::CMSException& e) {
+            // Expected
+        }
+                
+    } catch( cms::CMSException& e) {
+        e.printStackTrace();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplateTest::testReceive_DestinationName() {
+    
+    try {
+
+        MessageContext* messageContext = cf->getMessageContext();
+                
+        MySendListener listener;
+        messageContext->setSendListener(&listener);
+        
+        // First, test basics.
+        std::string destName = "bob";
+        cms::Message* message = cmsTemplate->receive(destName); 
+        CPPUNIT_ASSERT( message != NULL );
+        const cms::Queue* q = dynamic_cast<const cms::Queue*>(listener.dest);
+        CPPUNIT_ASSERT(q != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"bob", q->getQueueName());
+        delete message;
+        
+        // Now try a failure.
+        try {
+            
+            FailSendListener failListener;
+            messageContext->setSendListener(&failListener);
+            
+            // First, test basics.
+            cmsTemplate->receive(destName); 
+            CPPUNIT_FAIL("failed to throw expected exception");
+            
+        } catch( cms::CMSException& e) {
+            // Expected
+        }
+                
+    } catch( cms::CMSException& e) {
+        e.printStackTrace();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplateTest::testReceiveSelected() {
+    
+    try {
+
+        MessageContext* messageContext = cf->getMessageContext();
+                
+        MySendListener listener;
+        messageContext->setSendListener(&listener);
+        
+        // First, test basics.
+        std::string selector = "yadda";
+        cms::Message* message = cmsTemplate->receiveSelected(selector); 
+        CPPUNIT_ASSERT( message != NULL );
+        const cms::Queue* q = dynamic_cast<const cms::Queue*>(listener.dest);
+        CPPUNIT_ASSERT(q != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"test", q->getQueueName());
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->isNoLocal(), listener.noLocal);
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->getReceiveTimeout(), listener.timeout);
+        CPPUNIT_ASSERT_EQUAL(selector, listener.selector);
+        delete message;        
+        
+        // Now change the destination type and verify that it changes.
+        selector = "blah";
+        cmsTemplate->setPubSubDomain(true);
+        message = cmsTemplate->receiveSelected(selector); 
+        CPPUNIT_ASSERT( message != NULL );
+        const cms::Topic* t = dynamic_cast<const cms::Topic*>(listener.dest);
+        CPPUNIT_ASSERT(t != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"test", t->getTopicName());
+        CPPUNIT_ASSERT_EQUAL(selector, listener.selector);
+        delete message;
+        
+        // Now change the destination name and verify that it changes.
+        cmsTemplate->setDefaultDestinationName("bob");
+        message = cmsTemplate->receiveSelected(selector); 
+        CPPUNIT_ASSERT( message != NULL );
+        t = dynamic_cast<const cms::Topic*>(listener.dest);
+        CPPUNIT_ASSERT(t != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"bob", t->getTopicName());
+        CPPUNIT_ASSERT_EQUAL(selector, listener.selector);
+        delete message;
+        
+        // Now change the timeout and verify that it changes.
+        cmsTemplate->setReceiveTimeout(1000);
+        message = cmsTemplate->receiveSelected(selector); 
+        CPPUNIT_ASSERT( message != NULL );
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->getReceiveTimeout(), listener.timeout);
+        CPPUNIT_ASSERT_EQUAL(selector, listener.selector);
+        delete message;
+        
+        // Now change the noLocal flag and verify that it changes.
+        cmsTemplate->setNoLocal(true);
+        message = cmsTemplate->receiveSelected(selector); 
+        CPPUNIT_ASSERT( message != NULL );
+        CPPUNIT_ASSERT_EQUAL(cmsTemplate->isNoLocal(), listener.noLocal);
+        CPPUNIT_ASSERT_EQUAL(selector, listener.selector);
+        delete message;
+        
+        // Now try a failure.
+        try {
+            
+            FailSendListener failListener;
+            messageContext->setSendListener(&failListener);
+            
+            // First, test basics.
+            cmsTemplate->receiveSelected(selector); 
+            CPPUNIT_FAIL("failed to throw expected exception");
+            
+        } catch( cms::CMSException& e) {
+            // Expected
+        }
+                
+    } catch( cms::CMSException& e) {
+        e.printStackTrace();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplateTest::testReceiveSelected_Destination() {
+    
+    try {
+
+        MessageContext* messageContext = cf->getMessageContext();
+                
+        MySendListener listener;
+        messageContext->setSendListener(&listener);
+        
+        // First, test basics.
+        std::string selector = "yadda";
+        activemq::connector::stomp::StompTopic myTopic("anothertopic");
+        cms::Message* message = cmsTemplate->receiveSelected(&myTopic, selector);

+        CPPUNIT_ASSERT( message != NULL );
+        CPPUNIT_ASSERT(&myTopic == listener.dest);
+        delete message;
+        
+        // Now try a failure.
+        try {
+            
+            FailSendListener failListener;
+            messageContext->setSendListener(&failListener);
+            
+            // First, test basics.
+            cmsTemplate->receiveSelected(&myTopic, selector); 
+            CPPUNIT_FAIL("failed to throw expected exception");
+            
+        } catch( cms::CMSException& e) {
+            // Expected
+        }
+                
+    } catch( cms::CMSException& e) {
+        e.printStackTrace();
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CmsTemplateTest::testReceiveSelected_DestinationName() {
+    
+    try {
+
+        MessageContext* messageContext = cf->getMessageContext();
+                
+        MySendListener listener;
+        messageContext->setSendListener(&listener);
+        
+        // First, test basics.
+        std::string selector = "yadda";
+        std::string destName = "bob";
+        cms::Message* message = cmsTemplate->receiveSelected(destName, selector); 
+        CPPUNIT_ASSERT( message != NULL );
+        const cms::Queue* q = dynamic_cast<const cms::Queue*>(listener.dest);
+        CPPUNIT_ASSERT(q != NULL);
+        CPPUNIT_ASSERT_EQUAL((std::string)"bob", q->getQueueName());
+        delete message;
+        
+        // Now try a failure.
+        try {
+            
+            FailSendListener failListener;
+            messageContext->setSendListener(&failListener);
+            
+            // First, test basics.
+            cmsTemplate->receiveSelected(destName, selector); 
+            CPPUNIT_FAIL("failed to throw expected exception");
+            
+        } catch( cms::CMSException& e) {
+            // Expected
+        }
+                
+    } catch( cms::CMSException& e) {
+        e.printStackTrace();
+    }
+}
+

Modified: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.h?rev=620291&r1=620290&r2=620291&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.h (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/CmsTemplateTest.h Sun Feb 10 08:38:21
2008
@@ -26,6 +26,7 @@
 #include <activemq/cmsutil/ProducerCallback.h>
 #include <activemq/cmsutil/DummyMessageCreator.h>
 #include <activemq/cmsutil/MessageContext.h>
+#include <activemq/exceptions/ActiveMQException.h>
 
 namespace activemq{
 namespace cmsutil{
@@ -38,6 +39,12 @@
         CPPUNIT_TEST( testExecuteSession );
         CPPUNIT_TEST( testExecuteProducer );
         CPPUNIT_TEST( testSend );
+        CPPUNIT_TEST( testReceive );
+        CPPUNIT_TEST( testReceive_Destination );
+        CPPUNIT_TEST( testReceive_DestinationName );
+        CPPUNIT_TEST( testReceiveSelected );
+        CPPUNIT_TEST( testReceiveSelected_Destination );
+        CPPUNIT_TEST( testReceiveSelected_DestinationName );        
         CPPUNIT_TEST_SUITE_END();               
              
 
@@ -52,6 +59,9 @@
             int deliveryMode;
             int priority;
             long long ttl;
+            std::string selector;
+            bool noLocal;
+            long long timeout;
                     
             MySendListener() {
                 dest = NULL;
@@ -64,13 +74,45 @@
             
             virtual void onSend(const cms::Destination* destination,
                 cms::Message* message, int deliveryMode, int priority, 
-                long long timeToLive) {
+                long long timeToLive) throw (cms::CMSException){
                 this->dest = destination;
                 this->message = message;
                 this->deliveryMode = deliveryMode;
                 this->priority = priority;
                 this->ttl = timeToLive;
             }
+            
+            virtual cms::Message* doReceive(const cms::Destination* dest, 
+                    const std::string& selector, 
+                    bool noLocal, 
+                    long long timeout) throw (cms::CMSException){
+                this->dest = dest;
+                this->selector = selector;
+                this->noLocal = noLocal;
+                this->timeout = timeout;
+                return new DummyMessage();
+            }
+        };
+        
+        class FailSendListener : public MessageContext::SendListener {
+        public:
+                    
+            FailSendListener() {
+            }
+            virtual ~FailSendListener(){}
+            
+            virtual void onSend(const cms::Destination* destination,
+                    cms::Message* message, int deliveryMode, int priority, 
+                    long long timeToLive) throw (cms::CMSException){
+                throw exceptions::ActiveMQException(__FILE__, __LINE__, "");
+            }
+            
+            virtual cms::Message* doReceive(const cms::Destination* dest, 
+                    const std::string& selector, 
+                    bool noLocal, 
+                    long long timeout) throw (cms::CMSException){
+                throw exceptions::ActiveMQException(__FILE__, __LINE__, "");
+            }
         };
         
         class MySessionCallback : public SessionCallback {
@@ -120,6 +162,12 @@
         void testExecuteSession();
         void testExecuteProducer();
         void testSend();
+        void testReceive();
+        void testReceive_Destination();
+        void testReceive_DestinationName();
+        void testReceiveSelected();
+        void testReceiveSelected_Destination();
+        void testReceiveSelected_DestinationName();
     };
 
 }}

Added: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyConsumer.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyConsumer.h?rev=620291&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyConsumer.h (added)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyConsumer.h Sun Feb 10 08:38:21
2008
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ACTIVEMQ_CMSUTIL_DUMMYCONSUMER_H_
+#define ACTIVEMQ_CMSUTIL_DUMMYCONSUMER_H_
+
+#include <cms/MessageConsumer.h>
+#include <activemq/cmsutil/MessageContext.h>
+
+namespace activemq {
+namespace cmsutil {
+
+    class DummyConsumer : public cms::MessageConsumer {  
+    private:
+        std::string selector;
+        cms::MessageListener* listener;
+        MessageContext* messageContext;
+        const cms::Destination* dest;
+        bool noLocal;
+        
+    public:
+
+        DummyConsumer(MessageContext* messageContext, const cms::Destination* dest, const
std::string& selector, bool noLocal) {
+            this->messageContext = messageContext;
+            this->selector = selector;
+            this->noLocal = noLocal;
+            this->dest = dest;
+            this->listener = NULL;
+        }
+        virtual ~DummyConsumer() {}
+              
+        virtual void close() throw( cms::CMSException ){}
+
+        virtual cms::Message* receive() throw ( cms::CMSException ) {
+            return messageContext->receive(dest, selector, noLocal, 0);
+        }
+
+        virtual cms::Message* receive( int millisecs ) throw ( cms::CMSException ) {
+            return messageContext->receive(dest, selector, noLocal, millisecs);
+        }
+
+        virtual cms::Message* receiveNoWait() throw ( cms::CMSException ) {
+            return messageContext->receive(dest, selector, noLocal, -1);
+        }
+
+        virtual void setMessageListener( cms::MessageListener* listener ) {
+            this->listener = listener;
+        }
+
+        virtual cms::MessageListener* getMessageListener() const {
+            return listener;
+        }
+
+        virtual std::string getMessageSelector() const 
+            throw ( cms::CMSException ) {
+            return selector;
+        }
+        
+    };
+    
+}}
+
+#endif /*ACTIVEMQ_CMSUTIL_DUMMYCONSUMER_H_*/

Modified: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h?rev=620291&r1=620290&r2=620291&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummySession.h Sun Feb 10 08:38:21
2008
@@ -22,6 +22,7 @@
 #include <activemq/connector/stomp/StompTopic.h>
 #include <activemq/connector/stomp/StompQueue.h>
 #include <activemq/cmsutil/DummyProducer.h>
+#include <activemq/cmsutil/DummyConsumer.h>
 
 namespace activemq {
 namespace cmsutil {
@@ -52,20 +53,25 @@
 
         virtual cms::MessageConsumer* createConsumer(
             const cms::Destination* destination )
-                throw ( cms::CMSException ) { return NULL; }
+                throw ( cms::CMSException ) {
+            
+            return new DummyConsumer(messageContext, destination, "", false);
+        }
 
         virtual cms::MessageConsumer* createConsumer( 
             const cms::Destination* destination,
             const std::string& selector )
                 throw ( cms::CMSException ) {
-            return NULL;
+            return new DummyConsumer(messageContext, destination, selector, false);
         }
         
         virtual cms::MessageConsumer* createConsumer( 
             const cms::Destination* destination,
             const std::string& selector,
             bool noLocal )
-                throw ( cms::CMSException ) { return NULL; }
+                throw ( cms::CMSException ) {
+            return new DummyConsumer(messageContext, destination, selector, noLocal);
+        }
 
         virtual cms::MessageConsumer* createDurableConsumer(
             const cms::Topic* destination,

Modified: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/MessageContext.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/MessageContext.h?rev=620291&r1=620290&r2=620291&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/MessageContext.h (original)
+++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/MessageContext.h Sun Feb 10 08:38:21
2008
@@ -35,7 +35,12 @@
             
             virtual void onSend(const cms::Destination* destination,
                 cms::Message* message, int deliveryMode, int priority, 
-                long long timeToLive) = 0;
+                long long timeToLive) throw (cms::CMSException)= 0;
+            
+            virtual cms::Message* doReceive(const cms::Destination* dest, 
+                    const std::string& selector, 
+                    bool noLocal, 
+                    long long timeout) throw (cms::CMSException) = 0;
         };
         
     private:
@@ -55,11 +60,23 @@
         
         void send(const cms::Destination* destination,
             cms::Message* message, int deliveryMode, int priority, 
-            long long timeToLive) {
+            long long timeToLive) throw (cms::CMSException){
             
             if( listener != NULL ) {
                 listener->onSend(destination, message, deliveryMode, priority, timeToLive);
             }
+        }
+        
+        cms::Message* receive(const cms::Destination* dest, 
+                const std::string& selector, 
+                bool noLocal, 
+                long long timeout) throw (cms::CMSException){
+            
+            if( listener != NULL ) {
+                return listener->doReceive(dest, selector, noLocal, timeout);
+            }
+            
+            return NULL;
         }
     };
 }}



Mime
View raw message