Author: nmittler Date: Sun Feb 3 17:18:23 2008 New Revision: 618143 URL: http://svn.apache.org/viewvc?rev=618143&view=rev Log: AMQCPP-152 - Adding tests for CmsTemplate Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CachedProducer.h activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyProducer.h Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h 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 Added: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CachedProducer.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CachedProducer.h?rev=618143&view=auto ============================================================================== --- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CachedProducer.h (added) +++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CachedProducer.h Sun Feb 3 17:18:23 2008 @@ -0,0 +1,115 @@ +/* + * 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_CACHEDPRODUCER_H_ +#define ACTIVEMQ_CMSUTIL_CACHEDPRODUCER_H_ + +#include + +namespace activemq { +namespace cmsutil { + + /** + * A cached message roducer contained within a pooled session. + */ + class CachedProducer : public cms::MessageProducer { + private: + + cms::MessageProducer* producer; + + public: + + CachedProducer( cms::MessageProducer* producer ) { + this->producer = producer; + } + + virtual ~CachedProducer() {} + + /** + * Does nothing - the real producer resource will be closed + * by the lifecycle manager. + */ + virtual void close() throw( cms::CMSException ){ + // Do nothing. + } + + virtual void send( cms::Message* message ) throw ( cms::CMSException ){ + producer->send(message); + } + + virtual void send( cms::Message* message, int deliveryMode, int priority, + long long timeToLive) throw ( cms::CMSException ){ + producer->send(message, deliveryMode, priority, timeToLive); + } + + + virtual void send( const cms::Destination* destination, + cms::Message* message ) throw ( cms::CMSException ){ + producer->send(destination, message); + } + + virtual void send( const cms::Destination* destination, + cms::Message* message, int deliveryMode, int priority, + long long timeToLive) throw ( cms::CMSException ){ + producer->send(destination, message, deliveryMode, priority, timeToLive); + } + + virtual void setDeliveryMode( int mode ) { + producer->setDeliveryMode(mode); + } + + virtual int getDeliveryMode() const { + return producer->getDeliveryMode(); + } + + virtual void setDisableMessageID( bool value ) { + producer->setDisableMessageID(value); + } + + virtual bool getDisableMessageID() const { + return producer->getDisableMessageID(); + } + + virtual void setDisableMessageTimeStamp( bool value ) { + producer->setDisableMessageTimeStamp(value); + } + + virtual bool getDisableMessageTimeStamp() const { + return producer->getDisableMessageTimeStamp(); + } + + virtual void setPriority( int priority ) { + producer->setPriority(priority); + } + + virtual int getPriority() const { + return producer->getPriority(); + } + + virtual void setTimeToLive( long long time ) { + producer->setTimeToLive(time); + } + + virtual long long getTimeToLive() const { + return producer->getTimeToLive(); + } + + }; + +}} + +#endif /*ACTIVEMQ_CMSUTIL_CACHEDPRODUCER_H_*/ Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp?rev=618143&r1=618142&r2=618143&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp (original) +++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.cpp Sun Feb 3 17:18:23 2008 @@ -255,7 +255,12 @@ } AMQ_CATCH_NOTHROW( cms::CMSException ) - delete producer; + // Destroy if it's not a cached producer. + CachedProducer* cachedProducer = dynamic_cast(producer); + if( cachedProducer == NULL ) { + delete producer; + } + producer = NULL; } Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h?rev=618143&r1=618142&r2=618143&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h (original) +++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/CmsTemplate.h Sun Feb 3 17:18:23 2008 @@ -181,22 +181,56 @@ virtual void init() throw (cms::CMSException, decaf::lang::exceptions::IllegalStateException); + /** + * Sets the destination object to be used by default for send/receive operations. + * If no default destination is provided, the defaultDestinationName + * property is used to resolve this default destination for send/receive + * operations. + * + * @param defaultDestination + * the default destination + */ virtual void setDefaultDestination(cms::Destination* defaultDestination) { this->defaultDestination = defaultDestination; } + /** + * Retrieves the default destination to be used for send/receive operations. + * @return the default destination. Const version of this method. + */ virtual const cms::Destination* getDefaultDestination() const { return this->defaultDestination; } + /** + * Retrieves the default destination to be used for send/receive operations. + * @return the default destination. Non-const version of this method. + */ virtual cms::Destination* getDefaultDestination() { return this->defaultDestination; } + /** + * Sets the name of the default destination to be used from send/receive operations. + * Calling this method will set the defaultDestination to NULL. + * The destination type (topic/queue) is determined by the + * pubSubDomain property. + * + * @param defaultDestinationName + * the name of the destination for send/receive to by default. + */ virtual void setDefaultDestinationName(const std::string& defaultDestinationName) { + this->defaultDestination = NULL; this->defaultDestinationName = defaultDestinationName; } + /** + * Gets the name of the default destination to be used for send/receive operations. + * The destination type (topic/queue) is determined by the + * pubSubDomain property. + * + * @return the default name of the destination for send/receive operations. + */ virtual const std::string getDefaultDestinationName() const { return this->defaultDestinationName; } Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp?rev=618143&r1=618142&r2=618143&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp (original) +++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.cpp Sun Feb 3 17:18:23 2008 @@ -49,23 +49,33 @@ try { + if( destination == NULL ) { + throw ActiveMQException(__FILE__, __LINE__, "destination is NULL"); + } + std::string destName = getUniqueDestName(destination); // Check the cache - add it if necessary. - cms::MessageProducer* p = producerCache.getValue(destName); - if( p == NULL ) { + CachedProducer* cachedProducer = NULL; + try { + cachedProducer = producerCache.getValue(destName); + } catch( decaf::lang::exceptions::NoSuchElementException& e ) { - // No producer exists for this destination - create it. - p = session->createProducer(destination); - - // Add it to the cache. - producerCache.setValue(destName, p); + // No producer exists for this destination - start by creating + // a new producer resource. + cms::MessageProducer* p = session->createProducer(destination); - // Add the producer to the resource lifecycle manager. + // Add the producer resource to the resource lifecycle manager. pool->getResourceLifecycleManager()->addMessageProducer(p); + + // Create the cached producer wrapper. + cachedProducer = new CachedProducer(p); + + // Add it to the cache. + producerCache.setValue(destName, cachedProducer); } - return p; + return cachedProducer; } AMQ_CATCH_RETHROW( ActiveMQException ) AMQ_CATCHALL_THROW( ActiveMQException ) Modified: activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h?rev=618143&r1=618142&r2=618143&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h (original) +++ activemq/activemq-cpp/trunk/src/main/activemq/cmsutil/PooledSession.h Sun Feb 3 17:18:23 2008 @@ -20,6 +20,7 @@ #include #include +#include namespace activemq { namespace cmsutil { @@ -38,7 +39,7 @@ cms::Session* session; - decaf::util::Map producerCache; + decaf::util::Map producerCache; public: 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=618143&r1=618142&r2=618143&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 3 17:18:23 2008 @@ -43,27 +43,53 @@ //////////////////////////////////////////////////////////////////////////////// void CmsTemplateTest::testExecuteSession() { - cmsTemplate->setSessionAcknowledgeMode(cms::Session::CLIENT_ACKNOWLEDGE); - - // Test basics. - MySessionCallback sessionCallback; - cmsTemplate->execute(&sessionCallback); - CPPUNIT_ASSERT(sessionCallback.session != NULL); - CPPUNIT_ASSERT(sessionCallback.ackMode == cms::Session::CLIENT_ACKNOWLEDGE); + try { + cmsTemplate->setSessionAcknowledgeMode(cms::Session::CLIENT_ACKNOWLEDGE); + + // Test basics. + MySessionCallback sessionCallback; + cmsTemplate->execute(&sessionCallback); + CPPUNIT_ASSERT(sessionCallback.session != NULL); + CPPUNIT_ASSERT(sessionCallback.ackMode == cms::Session::CLIENT_ACKNOWLEDGE); + + // Try again and make sure we get the same session + MySessionCallback sessionCallback2; + cmsTemplate->execute(&sessionCallback2); + CPPUNIT_ASSERT(sessionCallback2.session == sessionCallback.session); + CPPUNIT_ASSERT(sessionCallback2.ackMode == cms::Session::CLIENT_ACKNOWLEDGE); + + // Now try different ack mode and make sure we have a different session. + cmsTemplate->setSessionAcknowledgeMode(cms::Session::AUTO_ACKNOWLEDGE); + MySessionCallback sessionCallback3; + cmsTemplate->execute(&sessionCallback3); + CPPUNIT_ASSERT(sessionCallback3.session != NULL); + CPPUNIT_ASSERT(sessionCallback3.session != sessionCallback.session); + CPPUNIT_ASSERT(sessionCallback3.ackMode == cms::Session::AUTO_ACKNOWLEDGE); + + } catch( cms::CMSException& e) { + e.printStackTrace(); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void CmsTemplateTest::testExecuteProducer() { - // Try again and make sure we get the same session - MySessionCallback sessionCallback2; - cmsTemplate->execute(&sessionCallback2); - CPPUNIT_ASSERT(sessionCallback2.session == sessionCallback.session); - CPPUNIT_ASSERT(sessionCallback2.ackMode == cms::Session::CLIENT_ACKNOWLEDGE); + try { + + // Test basics. + MyProducerCallback callback; + cmsTemplate->execute(&callback); + CPPUNIT_ASSERT(callback.session != NULL); + CPPUNIT_ASSERT(callback.producer != NULL); - // Now try different ack mode and make sure we have a different session. - cmsTemplate->setSessionAcknowledgeMode(cms::Session::AUTO_ACKNOWLEDGE); - MySessionCallback sessionCallback3; - cmsTemplate->execute(&sessionCallback3); - CPPUNIT_ASSERT(sessionCallback3.session != NULL); - CPPUNIT_ASSERT(sessionCallback3.session != sessionCallback.session); - CPPUNIT_ASSERT(sessionCallback3.ackMode == cms::Session::AUTO_ACKNOWLEDGE); + // Try again and make sure we have the same producer + MyProducerCallback callback2; + cmsTemplate->execute(&callback2); + CPPUNIT_ASSERT(callback2.session == callback.session); + CPPUNIT_ASSERT(callback2.producer == callback.producer); + + } 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=618143&r1=618142&r2=618143&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 3 17:18:23 2008 @@ -34,6 +34,7 @@ { CPPUNIT_TEST_SUITE( CmsTemplateTest ); CPPUNIT_TEST( testExecuteSession ); + CPPUNIT_TEST( testExecuteProducer ); CPPUNIT_TEST_SUITE_END(); @@ -85,6 +86,7 @@ virtual void tearDown(); void testExecuteSession(); + void testExecuteProducer(); }; }} Added: activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyProducer.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyProducer.h?rev=618143&view=auto ============================================================================== --- activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyProducer.h (added) +++ activemq/activemq-cpp/trunk/src/test/activemq/cmsutil/DummyProducer.h Sun Feb 3 17:18:23 2008 @@ -0,0 +1,210 @@ +/* + * 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_DUMMYPRODUCER_H_ +#define ACTIVEMQ_CMSUTIL_DUMMYPRODUCER_H_ + +#include + +namespace activemq { +namespace cmsutil { + + class DummyProducer : public cms::MessageProducer { + private: + int deliveryMode; + bool disableMessageId; + bool disableMessageTimestamp; + int priority; + long long ttl; + + public: + + DummyProducer() { + deliveryMode = 1; + disableMessageId = false; + disableMessageTimestamp = false; + priority = 4; + ttl = 0L; + } + virtual ~DummyProducer() {} + + virtual void close() throw( cms::CMSException ){} + + /** + * Sends the message to the default producer destination, but does + * not take ownership of the message, caller must still destroy it. + * Uses default values for deliveryMode, priority, and time to live. + * + * @param message + * The message to be sent. + * @throws cms::CMSException + */ + virtual void send( cms::Message* message ) throw ( cms::CMSException ){ + } + + /** + * Sends the message to the default producer destination, but does + * not take ownership of the message, caller must still destroy it. + * + * @param message + * The message to be sent. + * @param deliveryMode + * The delivery mode to be used. + * @param priority + * The priority for this message. + * @param timeToLive + * The time to live value for this message in milliseconds. + * @throws cms::CMSException + */ + virtual void send( cms::Message* message, int deliveryMode, int priority, + long long timeToLive) throw ( cms::CMSException ){ + } + + /** + * Sends the message to the designated destination, but does + * not take ownership of the message, caller must still destroy it. + * Uses default values for deliveryMode, priority, and time to live. + * + * @param destination + * The destination on which to send the message + * @param message + * the message to be sent. + * @throws cms::CMSException + */ + virtual void send( const cms::Destination* destination, + cms::Message* message ) throw ( cms::CMSException ){} + + /** + * Sends the message to the designated destination, but does + * not take ownership of the message, caller must still destroy it. + * + * @param destination + * The destination on which to send the message + * @param message + * The message to be sent. + * @param deliveryMode + * The delivery mode to be used. + * @param priority + * The priority for this message. + * @param timeToLive + * The time to live value for this message in milliseconds. + * @throws cms::CMSException + */ + virtual void send( const cms::Destination* destination, + cms::Message* message, int deliveryMode, int priority, + long long timeToLive) throw ( cms::CMSException ){} + + /** + * Sets the delivery mode for this Producer + * + * @param mode + * The DeliveryMode + */ + virtual void setDeliveryMode( int mode ) { + this->deliveryMode = mode; + } + + /** + * Gets the delivery mode for this Producer + * + * @return The DeliveryMode + */ + virtual int getDeliveryMode() const { + return deliveryMode; + } + + /** + * Sets if Message Ids are disbled for this Producer + * + * @param value + * boolean indicating enable / disable (true / false) + */ + virtual void setDisableMessageID( bool value ) { + disableMessageId = value; + } + + /** + * Gets if Message Ids are disbled for this Producer + * + * @return boolean indicating enable / disable (true / false) + */ + virtual bool getDisableMessageID() const { + return disableMessageId; + } + + /** + * Sets if Message Time Stamps are disbled for this Producer + * @param value - boolean indicating enable / disable (true / false) + */ + virtual void setDisableMessageTimeStamp( bool value ) { + disableMessageTimestamp = value; + } + + /** + * Gets if Message Time Stamps are disbled for this Producer + * + * @return boolean indicating enable / disable (true / false) + */ + virtual bool getDisableMessageTimeStamp() const { + return disableMessageTimestamp; + } + + /** + * Sets the Priority that this Producers sends messages at + * + * @param priority + * int value for Priority level + */ + virtual void setPriority( int priority ) { + this->priority = priority; + } + + /** + * Gets the Priority level that this producer sends messages at + * + * @return int based priority level + */ + virtual int getPriority() const { + return priority; + } + + /** + * Sets the Time to Live that this Producers sends messages with. This + * value will be used if the time to live is not specified via the + * send method. + * + * @param time + * default time to live value in milliseconds + */ + virtual void setTimeToLive( long long time ) { + ttl = time; + } + + /** + * Gets the Time to Live that this producer sends messages with + * + * @return Time to live value in milliseconds + */ + virtual long long getTimeToLive() const { + return ttl; + } + + }; + +}} + +#endif /*ACTIVEMQ_CMSUTIL_DUMMYPRODUCER_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=618143&r1=618142&r2=618143&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 3 17:18:23 2008 @@ -21,6 +21,7 @@ #include #include #include +#include namespace activemq { namespace cmsutil { @@ -70,7 +71,7 @@ throw ( cms::CMSException ) { return NULL; } virtual cms::MessageProducer* createProducer( const cms::Destination* destination ) - throw ( cms::CMSException ) { return NULL; } + throw ( cms::CMSException ) { return new DummyProducer(); } virtual cms::Queue* createQueue( const std::string& queueName ) throw ( cms::CMSException ) {