You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/03/13 15:10:11 UTC

svn commit: r517702 - in /activemq/activemq-cpp/trunk/activemq-cpp/src: main/activemq/connector/stomp/commands/StompMessage.h test/activemq/connector/stomp/commands/MessageCommandTest.h

Author: tabish
Date: Tue Mar 13 07:10:10 2007
New Revision: 517702

URL: http://svn.apache.org/viewvc?view=rev&rev=517702
Log:
http://issues.apache.org/activemq/browse/AMQCPP-77

Make the Stomp Messages throw NoSuchElementExceptions for access to any of their getXXXProperty methods when the property doesn't exist.

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/commands/MessageCommandTest.h

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h?view=diff&rev=517702&r1=517701&r2=517702
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h Tue Mar 13 07:10:10 2007
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
- 
+
 #ifndef _ACTIVEMQ_CONNECTOR_STOMP_COMMANDS_STOMPMESSAGE_H_
 #define _ACTIVEMQ_CONNECTOR_STOMP_COMMANDS_STOMPMESSAGE_H_
 
@@ -43,14 +43,14 @@
     /**
      * Base class for Stomp Commands that represent the Active MQ message
      * types.  This class is templated and expects the Template type to be
-     * a cms::Message type, Message, TextMessage etc.  This class will 
+     * a cms::Message type, Message, TextMessage etc.  This class will
      * implement all the general cms:Message methods
-     * 
-     * This class implement AbsractCommand<StompCommnd> and the 
+     *
+     * This class implement AbsractCommand<StompCommnd> and the
      * ActiveMQMessage interface.
      */
     template<typename T>
-    class StompMessage : 
+    class StompMessage :
         public AbstractCommand< transport::Command >,
         public T,
         public core::ActiveMQMessage
@@ -59,10 +59,10 @@
 
         // Core API defined Acknowedge Handler.
         core::ActiveMQAckHandler* ackHandler;
-        
+
         // Cached Destination
         cms::Destination* dest;
-        
+
         // Cached Destination
         cms::Destination* replyTo;
 
@@ -74,36 +74,36 @@
             dest( NULL ),
             replyTo( NULL) {
         }
-            
-        StompMessage( StompFrame* frame ) : 
+
+        StompMessage( StompFrame* frame ) :
             AbstractCommand< transport::Command >( frame ),
             ackHandler( NULL ),
             dest( NULL ),
             replyTo( NULL )
-        {             
-            const std::string& destHeader = CommandConstants::toString( 
+        {
+            const std::string& destHeader = CommandConstants::toString(
                 CommandConstants::HEADER_DESTINATION );
-            const std::string& replyToHeader = CommandConstants::toString( 
+            const std::string& replyToHeader = CommandConstants::toString(
                 CommandConstants::HEADER_REPLYTO );
-                        
-            dest = CommandConstants::toDestination( 
+
+            dest = CommandConstants::toDestination(
                 getPropertyValue( destHeader, "" ) );
-            
+
             std::string replyToValue = getPropertyValue( replyToHeader, "null" );
-            if( replyToValue != "null" ) {                            
-                replyTo = CommandConstants::toDestination( replyToValue );                
-            }            
+            if( replyToValue != "null" ) {
+                replyTo = CommandConstants::toDestination( replyToValue );
+            }
         }
 
     	virtual ~StompMessage() {
-            
+
             if( dest != NULL ){
                 delete dest;
             }
-            
+
             if( replyTo != NULL ){
                 delete replyTo;
-            } 
+            }
         }
 
         /**
@@ -113,26 +113,26 @@
         virtual void clearBody(){
             getFrame().getBody().clear();
         }
-        
+
         /**
          * Clears the message properties.  Does not clear the body or
          * header values.
          */
         virtual void clearProperties(){
-            
+
             util::Properties& props = getFrame().getProperties();
             std::vector< std::pair< std::string, std::string > > propArray = props.toArray();
             for( unsigned int ix=0; ix<propArray.size(); ++ix ){
-                
+
                 const std::string& name = propArray[ix].first;
-                
+
                 // Only clear properties that aren't Stomp headers.
                 if( !CommandConstants::isStompHeader(name) ){
                     props.remove( name );
                 }
             }
         }
-        
+
         /**
          * Retrieves the propery names.
          * @return The complete set of property names currently in this
@@ -140,22 +140,22 @@
          */
         virtual std::vector<std::string> getPropertyNames() const{
             std::vector<std::string> names;
-            
+
             const util::Properties& props = getFrame().getProperties();
             std::vector< std::pair< std::string, std::string > > propArray = props.toArray();
             for( unsigned int ix=0; ix<propArray.size(); ++ix ){
-                
+
                 const std::string& name = propArray[ix].first;
-                
+
                 // Only clear properties that aren't Stomp headers.
                 if( !CommandConstants::isStompHeader(name) ){
                     names.push_back( name );
                 }
             }
-            
+
             return names;
         }
-        
+
         /**
          * Indicates whether or not a given property exists.
          * @param name The name of the property to look up.
@@ -165,94 +165,92 @@
             if( CommandConstants::isStompHeader( name ) ){
                 return false;
             }
-            
+
             return getFrame().getProperties().hasProperty( name );
-        }                
-        
-        virtual bool getBooleanProperty( const std::string& name ) const 
+        }
+
+        virtual bool getBooleanProperty( const std::string& name ) const
             throw( cms::CMSException ){
-            testProperty(name);
-            std::string value = getPropertyValue( name );
+            std::string value = getStrictPropertyValue<std::string>( name );
             return value == "true";
         }
-        
-        virtual unsigned char getByteProperty( const std::string& name ) const 
+
+        virtual unsigned char getByteProperty( const std::string& name ) const
             throw( cms::CMSException ){
             return getStrictPropertyValue<unsigned char>(name);
         }
-        
-        virtual double getDoubleProperty( const std::string& name ) const 
+
+        virtual double getDoubleProperty( const std::string& name ) const
             throw( cms::CMSException ){
             return getStrictPropertyValue<double>(name);
         }
-        
-        virtual float getFloatProperty( const std::string& name ) const 
+
+        virtual float getFloatProperty( const std::string& name ) const
             throw( cms::CMSException ){
             return getStrictPropertyValue<float>(name);
         }
-        
-        virtual int getIntProperty( const std::string& name ) const 
+
+        virtual int getIntProperty( const std::string& name ) const
             throw( cms::CMSException ){
             return getStrictPropertyValue<int>(name);
         }
-        
-        virtual long long getLongProperty( const std::string& name ) const 
+
+        virtual long long getLongProperty( const std::string& name ) const
             throw( cms::CMSException ){
             return getStrictPropertyValue<long long>(name);
         }
-        
-        virtual short getShortProperty( const std::string& name ) const 
+
+        virtual short getShortProperty( const std::string& name ) const
             throw( cms::CMSException ){
             return getStrictPropertyValue<short>(name);
         }
-        
-        virtual std::string getStringProperty( const std::string& name ) const 
+
+        virtual std::string getStringProperty( const std::string& name ) const
             throw( cms::CMSException ){
-            testProperty( name );
-            return getPropertyValue( name, "" );
+            return getStrictPropertyValue<std::string>(name);
         }
-        
+
         virtual void setBooleanProperty( const std::string& name,
             bool value ) throw( cms::CMSException ){
             testProperty( name );
-            
+
             std::string strvalue = value? "true" : "false";
             setPropertyValue( name, strvalue );
         }
-        
+
         virtual void setByteProperty( const std::string& name,
             unsigned char value ) throw( cms::CMSException ){
             setStrictPropertyValue<unsigned char>( name, value );
         }
-        
+
         virtual void setDoubleProperty( const std::string& name,
             double value ) throw( cms::CMSException ){
             setStrictPropertyValue<double>( name, value );
         }
-        
+
         virtual void setFloatProperty( const std::string& name,
             float value ) throw( cms::CMSException ){
             setStrictPropertyValue<float>( name, value );
         }
-        
+
         virtual void setIntProperty( const std::string& name,
             int value ) throw( cms::CMSException ){
             setStrictPropertyValue<int>( name, value );
         }
-        
+
         virtual void setLongProperty( const std::string& name,
             long long value ) throw( cms::CMSException ){
             setStrictPropertyValue<long long>( name, value );
         }
-        
+
         virtual void setShortProperty( const std::string& name,
             short value ) throw( cms::CMSException ){
             setStrictPropertyValue<short>( name, value );
         }
-        
+
         virtual void setStringProperty( const std::string& name,
             const std::string& value ) throw( cms::CMSException ){
-            testProperty( name );            
+            testProperty( name );
             setPropertyValue( name, value );
         }
 
@@ -261,8 +259,8 @@
          * @return string representation of the correlation Id
          */
         virtual std::string getCMSCorrelationId() const {
-            std::string correlationId = getPropertyValue( 
-                CommandConstants::toString( 
+            std::string correlationId = getPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_CORRELATIONID ), "null" );
             if( correlationId == "null" ){
                 return "";
@@ -275,14 +273,14 @@
          * @param correlationId String representing the correlation id.
          */
         virtual void setCMSCorrelationId(const std::string& correlationId) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_CORRELATIONID ) ,
                 correlationId );
         }
-        
+
         /**
-         * Acknowledges all consumed messages of the session 
+         * Acknowledges all consumed messages of the session
          * of this consumed message.
          * @throws CMSException
          */
@@ -295,18 +293,18 @@
          * @return DeliveryMode enumerated value.
          */
         virtual int getCMSDeliveryMode() const {
-            if(!getFrame().getProperties().hasProperty( 
-                   CommandConstants::toString( 
+            if(!getFrame().getProperties().hasProperty(
+                   CommandConstants::toString(
                        CommandConstants::HEADER_PERSISTENT ) ) ) {
                 return cms::DeliveryMode::PERSISTENT;
             }
-            
-            if( util::Boolean::parseBoolean( getPropertyValue( 
-                       CommandConstants::toString( 
+
+            if( util::Boolean::parseBoolean( getPropertyValue(
+                       CommandConstants::toString(
                            CommandConstants::HEADER_PERSISTENT ) ) ) == true ) {
                 return (int)cms::DeliveryMode::PERSISTENT;
             }
-            
+
             return cms::DeliveryMode::NON_PERSISTENT;
         }
 
@@ -315,18 +313,18 @@
          * @param mode DeliveryMode enumerated value.
          */
         virtual void setCMSDeliveryMode( int mode ) {
-            std::string persistant = "true"; 
-            
+            std::string persistant = "true";
+
             if( mode == (int)cms::DeliveryMode::NON_PERSISTENT ) {
                 persistant = "false";
             }
-            
-            setPropertyValue( 
-                CommandConstants::toString( 
+
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_PERSISTENT ) ,
-                persistant );            
+                persistant );
         }
-      
+
         /**
          * Gets the Destination for this Message
          * @return Destination object can be NULL
@@ -334,7 +332,7 @@
         virtual const cms::Destination* getCMSDestination() const{
             return dest;
         }
-              
+
         /**
          * Sets the Destination for this message
          * @param destination Destination Object
@@ -344,30 +342,30 @@
             {
                 delete dest;
                 dest = destination->clone();
-                setPropertyValue( 
-                    CommandConstants::toString( 
+                setPropertyValue(
+                    CommandConstants::toString(
                         CommandConstants::HEADER_DESTINATION ),
                     dest->toProviderString() );
             }
         }
-        
+
         /**
          * Gets the Expiration Time for this Message
          * @return time value
          */
         virtual long long getCMSExpiration() const {
-            return util::Long::parseLong( getPropertyValue( 
-                CommandConstants::toString( 
+            return util::Long::parseLong( getPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_EXPIRES ), "0" ) );
         }
-      
+
         /**
          * Sets the Expiration Time for this message
          * @param expireTime time value
          */
         virtual void setCMSExpiration( long long expireTime ) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_EXPIRES) ,
                 util::Long::toString( expireTime ) );
         }
@@ -377,8 +375,8 @@
          * @return time value
          */
         virtual std::string getCMSMessageId() const {
-            return getPropertyValue( 
-                CommandConstants::toString( 
+            return getPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_MESSAGEID ), "" );
         }
 
@@ -387,29 +385,29 @@
          * @param id time value
          */
         virtual void setCMSMessageId( const std::string& id ) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_MESSAGEID ),
                 id );
         }
-      
+
         /**
          * Gets the Priority Value for this Message
          * @return priority value
          */
         virtual int getCMSPriority() const {
-            return util::Integer::parseInt( getPropertyValue( 
-                CommandConstants::toString( 
+            return util::Integer::parseInt( getPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_JMSPRIORITY ), "0" ) );
         }
-      
+
         /**
          * Sets the Priority Value for this message
          * @param priority priority value
          */
         virtual void setCMSPriority( int priority ) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_JMSPRIORITY),
                 util::Integer::toString( priority ) );
         }
@@ -419,19 +417,19 @@
          * @return redelivered value
          */
         virtual bool getCMSRedelivered() const {
-            return util::Boolean::parseBoolean( getPropertyValue( 
-                CommandConstants::toString( 
-                    CommandConstants::HEADER_REDELIVERED ), 
+            return util::Boolean::parseBoolean( getPropertyValue(
+                CommandConstants::toString(
+                    CommandConstants::HEADER_REDELIVERED ),
                 "false" ) );
         }
-      
+
         /**
          * Sets the Redelivered Flag for this message
          * @param redelivered redelivered value
          */
         virtual void setCMSRedelivered( bool redelivered ) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_REDELIVERED ),
                 util::Boolean::toString( redelivered ) );
         }
@@ -453,8 +451,8 @@
             {
                 delete replyTo;
                 replyTo = destination->clone();
-                setPropertyValue( 
-                    CommandConstants::toString( 
+                setPropertyValue(
+                    CommandConstants::toString(
                         CommandConstants::HEADER_REPLYTO ),
                     replyTo->toProviderString() );
             }
@@ -465,18 +463,18 @@
          * @return time stamp value
          */
         virtual long long getCMSTimeStamp() const {
-            return util::Long::parseLong( getPropertyValue( 
-                CommandConstants::toString( 
+            return util::Long::parseLong( getPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_TIMESTAMP ), "0" ) );
         }
-      
+
         /**
          * Sets the Time Stamp for this message
          * @param timeStamp time stamp value
          */
         virtual void setCMSTimeStamp( long long timeStamp ) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_TIMESTAMP ),
                 util::Long::toString( timeStamp ) );
         }
@@ -486,22 +484,22 @@
          * @return type value
          */
         virtual std::string getCMSMessageType() const {
-            std::string type = getPropertyValue( 
-                CommandConstants::toString( 
+            std::string type = getPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_TYPE ), "null" );
             if( type == "null" ){
                 return "";
             }
             return type;
         }
-      
+
         /**
          * Sets the CMS Message Type for this message
          * @param type type value
          */
         virtual void setCMSMessageType( const std::string& type ) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_TYPE ),
                 type );
         }
@@ -516,26 +514,26 @@
         virtual void setAckHandler( core::ActiveMQAckHandler* handler ) {
             this->ackHandler = handler;
         }
-        
+
         /**
          * Gets the number of times this message has been redelivered.
          * @return redelivery count
          */
         virtual int getRedeliveryCount() const {
-            return util::Integer::parseInt( getPropertyValue( 
-                CommandConstants::toString( 
+            return util::Integer::parseInt( getPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_REDELIVERYCOUNT ),
                 "0" ) );
         }
-        
+
         /**
-         * Sets the count of the number of times this message has been 
+         * Sets the count of the number of times this message has been
          * redelivered
          * @param count redelivery count
          */
         virtual void setRedeliveryCount( int count ) {
-            setPropertyValue( 
-                CommandConstants::toString( 
+            setPropertyValue(
+                CommandConstants::toString(
                     CommandConstants::HEADER_REDELIVERYCOUNT ),
                 util::Integer::toString( count ) );
         }
@@ -554,8 +552,8 @@
             return false;
         }
 
-    protected:   
-        
+    protected:
+
         /**
          * Checks to see if the given property has the name of a
          * pre-defined header.  If so, throws an exception.
@@ -563,11 +561,11 @@
         virtual void testProperty( const std::string& name ) const
             throw( cms::CMSException ){
             if( CommandConstants::isStompHeader( name ) ){
-                throw exceptions::IllegalArgumentException( __FILE__, __LINE__, 
+                throw exceptions::IllegalArgumentException( __FILE__, __LINE__,
                     "searching for property with name of pre-defined header" );
             }
         }
-        
+
         /**
          * Attempts to get a property from the frame's property
          * map.
@@ -575,43 +573,45 @@
         template <typename TYPE>
         TYPE getStrictPropertyValue( const std::string& name ) const
             throw( cms::CMSException ){
-                
+
             testProperty( name );
-            
-            const char* strProp = getPropertyValue(name);
-            if( strProp == NULL ){
-                throw exceptions::NoSuchElementException( __FILE__, __LINE__, 
+
+            if( !getProperties().hasProperty( name ) ){
+                throw exceptions::NoSuchElementException( 
+                    __FILE__, __LINE__,
                     "property not available in message" );
             }
-            
+
+            const char* strProp = getPropertyValue( name );
             std::istringstream stream( strProp );
             TYPE value;
             stream >> value;
-            
+
             if( stream.fail() ){
-                throw exceptions::RuntimeException( __FILE__, __LINE__, 
+                throw exceptions::RuntimeException( 
+                    __FILE__, __LINE__,
                     "Error extracting property from string" );
             }
-            
+
             return value;
         }
-        
+
         /**
          * Attempts to set the property in the frame.  If an error occurs or
          * the property name is that of a pre-defined header, an exception
          * is thrown.
          */
         template <typename TYPE>
-        void setStrictPropertyValue( const std::string& name, TYPE value ) 
+        void setStrictPropertyValue( const std::string& name, TYPE value )
             throw( cms::CMSException ){
             testProperty( name );
-            
+
             std::ostringstream stream;
             stream << value;
-            
+
             setPropertyValue( name, stream.str() );
         }
-        
+
         /**
          * Inheritors are required to override this method to init the
          * frame with data appropriate for the command type.
@@ -624,14 +624,14 @@
         }
 
         /**
-         * Inheritors are required to override this method to validate 
+         * Inheritors are required to override this method to validate
          * the passed stomp frame before it is marshalled or unmarshaled
          * @param frame Frame to validate
          * @returns true if frame is valid
          */
         virtual bool validate( const StompFrame& frame ) const
         {
-            if(frame.getCommand() == 
+            if(frame.getCommand() ==
                CommandConstants::toString( CommandConstants::SEND ) )
             {
                 if(frame.getProperties().hasProperty(
@@ -641,7 +641,7 @@
                     return true;
                 }
             }
-            else if( frame.getCommand() == 
+            else if( frame.getCommand() ==
                      CommandConstants::toString( CommandConstants::MESSAGE ) )
             {
                 if(frame.getProperties().hasProperty(

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/commands/MessageCommandTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/commands/MessageCommandTest.h?view=diff&rev=517702&r1=517701&r2=517702
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/commands/MessageCommandTest.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/commands/MessageCommandTest.h Tue Mar 13 07:10:10 2007
@@ -220,10 +220,17 @@
             // Test getting a string property that doesn't exist. 
             try {
                 std::string str = cmd.getStringProperty( "text" );
-            } catch( cms::CMSException& e){
                 CPPUNIT_ASSERT(false);
+            } catch( cms::CMSException& e){
             }
             
+            // Test getting a bool property that doesn't exist. 
+            try {
+                bool value = cmd.getBooleanProperty( "text" );
+                CPPUNIT_ASSERT(false);
+            } catch( cms::CMSException& e){
+            }
+
             // Test accessing non-existent property
             try{
                 cmd.getIntProperty("string");