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/01/02 18:51:49 UTC

svn commit: r491870 - in /incubator/activemq/activemq-cpp/trunk/activemq-cpp/src: main/activemq/connector/stomp/ main/activemq/connector/stomp/commands/ main/activemq/connector/stomp/marshal/ main/activemq/transport/ test/activemq/connector/stomp/ test...

Author: tabish
Date: Tue Jan  2 09:51:48 2007
New Revision: 491870

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

Added more unit testing for the Marshalling of message commmands and the read / write methods in the command.

Modified:
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshalable.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.cpp
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/CommandWriter.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompCommandWriterTest.h
    incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp Tue Jan  2 09:51:48 2007
@@ -41,7 +41,7 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////
-void StompCommandWriter::writeCommand( const Command* command ) 
+void StompCommandWriter::writeCommand( Command* command ) 
     throw ( transport::CommandIOException )
 {
     try

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.h Tue Jan  2 09:51:48 2007
@@ -78,7 +78,7 @@
          * @param os the target stream for the write.
          * @throws CommandIOException if a problem occurs during the write.
          */
-        virtual void writeCommand( const transport::Command* command ) 
+        virtual void writeCommand( transport::Command* command ) 
             throw ( transport::CommandIOException );
 
         /**

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h Tue Jan  2 09:51:48 2007
@@ -231,7 +231,7 @@
          * @throws MarshalException if the command is not
          * in a state that can be marshaled.
          */
-        virtual const StompFrame& marshal(void) const 
+        virtual const StompFrame& marshal(void)
             throw (marshal::MarshalException)
         {
             if( frame == NULL || !validate( *frame ) ){
@@ -275,30 +275,14 @@
          * be set
          */
         virtual void setBytes( const unsigned char* bytes, 
-                               const unsigned long long numBytes,
-                               const bool setContentLength = true )
+                               const unsigned long long numBytes )
         {
             getFrame().setBody( bytes, numBytes );
-            if( setContentLength )
-            {
-                setPropertyValue( 
-                    CommandConstants::toString( 
-                        CommandConstants::HEADER_CONTENTLENGTH),
-                    util::Long::toString( numBytes ) );
-            }
         }
         
-        virtual void setBytes( const std::vector<unsigned char>& bytes,
-                               const bool setContentLength = true )
+        virtual void setBytes( const std::vector<unsigned char>& bytes )
         {
             getFrame().getBody() = bytes;
-            if( setContentLength )
-            {
-                setPropertyValue( 
-                    CommandConstants::toString( 
-                        CommandConstants::HEADER_CONTENTLENGTH),
-                    util::Long::toString( bytes.size() ) );
-            }
         }
     };
     

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h Tue Jan  2 09:51:48 2007
@@ -106,12 +106,33 @@
         virtual void clearBody(){
             
             // Invoke base class's version.
-            StompMessage< cms::BytesMessage >::clearBody();
+            StompMessage<cms::BytesMessage>::clearBody();
             
             // Set the stream in write only mode.
             readOnly = false;
             
             outputStream.setBuffer( getBytes() );
+        }
+        
+        /**
+         * Marshals the command to a stomp frame.
+         * @returns the stomp frame representation of this
+         * command.
+         * @throws MarshalException if the command is not
+         * in a state that can be marshaled.
+         */
+        virtual const StompFrame& marshal(void)
+            throw (marshal::MarshalException)
+        {
+            // Before we send out the frame tag it with the content length
+            // as this is a bytes message and we can't ensure we have only
+            // a trailing NULL.
+            setPropertyValue( 
+                CommandConstants::toString( 
+                    CommandConstants::HEADER_CONTENTLENGTH),
+                util::Long::toString( getFrame().getBodyLength() ) );
+                        
+            return StompMessage<cms::BytesMessage>::marshal();
         }
         
         /**

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h Tue Jan  2 09:51:48 2007
@@ -74,7 +74,7 @@
          * @param msg The message buffer.
          */
         virtual void setText( const char* msg ) throw( cms::CMSException ) {
-            setBytes( (unsigned char*)msg, strlen(msg) + 1, false );
+            setBytes( (unsigned char*)msg, strlen(msg) + 1 );
         }
 
         /**
@@ -82,7 +82,7 @@
          * @param msg The message buffer.
          */
         virtual void setText( const std::string& msg ) throw( cms::CMSException ) {
-            setBytes( (unsigned char*)msg.c_str(), msg.length() + 1, false );
+            setBytes( (unsigned char*)msg.c_str(), msg.length() + 1 );
         }
 
     };

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshalable.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshalable.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshalable.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshalable.h Tue Jan  2 09:51:48 2007
@@ -39,7 +39,7 @@
          * @throws MarshalException if the command is not
          * in a state that can be marshalled.
          */
-        virtual const StompFrame& marshal(void) const 
+        virtual const StompFrame& marshal(void)
             throw ( marshal::MarshalException ) = 0;
 
     };

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.cpp
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.cpp?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.cpp (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.cpp Tue Jan  2 09:51:48 2007
@@ -89,13 +89,13 @@
 }
 
 ////////////////////////////////////////////////////////////////////////////////      
-const StompFrame& Marshaler::marshal( const transport::Command* command )
+const StompFrame& Marshaler::marshal( transport::Command* command )
     throw ( MarshalException )
 {
     try
     {
-        const Marshalable* marshalable = 
-            dynamic_cast<const Marshalable*>(command);
+        Marshalable* marshalable = 
+            dynamic_cast<Marshalable*>(command);
 
         // Easy, just get the frame from the command
         if(marshalable != NULL)

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/marshal/Marshaler.h Tue Jan  2 09:51:48 2007
@@ -57,7 +57,7 @@
          * @throws MarshalException
          */
         virtual const StompFrame& marshal( 
-            const transport::Command* command )
+            transport::Command* command )
                 throw ( MarshalException );
 
     };

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/CommandWriter.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/CommandWriter.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/CommandWriter.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/CommandWriter.h Tue Jan  2 09:51:48 2007
@@ -41,7 +41,7 @@
          * @param command the command to write.
          * @throws CommandIOException if a problem occurs during the write.
          */
-        virtual void writeCommand( const Command* command ) 
+        virtual void writeCommand( Command* command ) 
             throw ( CommandIOException ) = 0;
 
     };

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompCommandWriterTest.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompCommandWriterTest.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompCommandWriterTest.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompCommandWriterTest.h Tue Jan  2 09:51:48 2007
@@ -22,13 +22,16 @@
 #include <cppunit/extensions/HelperMacros.h>
 
 #include <activemq/connector/stomp/StompCommandWriter.h>
+#include <activemq/connector/stomp/StompCommandReader.h>
 #include <activemq/transport/Command.h>
 #include <activemq/connector/stomp/commands/ConnectedCommand.h>
 #include <activemq/connector/stomp/commands/TextMessageCommand.h>
 #include <activemq/connector/stomp/commands/BytesMessageCommand.h>
 #include <activemq/connector/stomp/StompTopic.h>
+#include <activemq/connector/stomp/commands/CommandConstants.h>
 
 #include <activemq/io/ByteArrayOutputStream.h>
+#include <activemq/io/ByteArrayInputStream.h>
 #include <algorithm>
 
 namespace activemq{
@@ -39,8 +42,51 @@
     {
         CPPUNIT_TEST_SUITE( StompCommandWriterTest );
         CPPUNIT_TEST( test );
+        CPPUNIT_TEST( testWriteAndReads );
         CPPUNIT_TEST_SUITE_END();
 
+        class MyBytesMessageCommand : public commands::BytesMessageCommand {
+        public:
+
+            virtual const StompFrame& marshal(void)
+                throw (marshal::MarshalException)
+            {
+                const StompFrame& frame = 
+                    commands::BytesMessageCommand::marshal();
+
+                // Before we send out the frame tag it with the content length
+                // as this is a bytes message and we can't ensure we have only
+                // a trailing NULL.
+                const_cast< StompFrame* >( &frame )->setCommand(
+                    commands::CommandConstants::toString( 
+                        commands::CommandConstants::MESSAGE ) );
+
+                return frame;
+            }
+
+        };
+
+        class MyTextMessageCommand : public commands::TextMessageCommand {
+        public:
+
+            virtual const StompFrame& marshal(void)
+                throw (marshal::MarshalException)
+            {
+                const StompFrame& frame = 
+                    commands::TextMessageCommand::marshal();
+
+                // Before we send out the frame tag it with the content length
+                // as this is a bytes message and we can't ensure we have only
+                // a trailing NULL.
+                const_cast< StompFrame* >( &frame )->setCommand(
+                    commands::CommandConstants::toString( 
+                        commands::CommandConstants::MESSAGE ) );
+
+                return frame;
+            }
+
+        };
+
     public:
     
     	StompCommandWriterTest() {}
@@ -114,8 +160,78 @@
                             boStream.getByteArray() ) );
         }
 
-    };
+        void testWriteAndReads() {
+            
+            io::ByteArrayOutputStream boStream;
+            io::ByteArrayInputStream biStream;
+
+            StompCommandWriter writer( &boStream );
+            StompCommandReader reader( &biStream );
+            
+            MyTextMessageCommand textCommand;
+            MyBytesMessageCommand bytesCommand;
 
+            StompTopic topic2("a");
+            bytesCommand.setCMSDestination( &topic2 );
+            bytesCommand.setCMSMessageId( "123" );
+            textCommand.setCMSDestination( &topic2 );
+            textCommand.setCMSMessageId( "123" );
+
+            textCommand.setText("This is a TextMessage");
+
+            const int testInt1 = 45678;
+            const int testInt2 = 42;
+            
+            const std::string testStr1 = "Test String 1";
+            const std::string testStr2 = "Test String 2";
+            
+            const bool testBool1 = true;
+            const bool testBool2 = false;
+
+            bytesCommand.writeInt( testInt1 );
+            bytesCommand.writeInt( testInt2 );
+            
+            bytesCommand.writeString( testStr1 );
+            bytesCommand.writeUTF( testStr2 );
+            
+            bytesCommand.writeBoolean( testBool1 );
+            bytesCommand.writeBoolean( testBool2 );
+
+            writer.writeCommand( &textCommand );
+            writer.writeCommand( &bytesCommand );
+
+            // Copy output Command to the Input Stream
+            biStream.setByteArray( boStream.getByteArray(), 
+                                   boStream.getByteArraySize() );
+
+            commands::TextMessageCommand* textMessage = 
+                dynamic_cast< commands::TextMessageCommand* >( 
+                    reader.readCommand() );
+
+            CPPUNIT_ASSERT( textMessage != NULL );
+            CPPUNIT_ASSERT( textMessage->getText() != "" );
+            std::string text = textMessage->getText();
+            CPPUNIT_ASSERT( text == "This is a TextMessage" );
+
+            commands::BytesMessageCommand* bytesMessage = 
+                dynamic_cast< commands::BytesMessageCommand* >( 
+                    reader.readCommand() );
+
+            CPPUNIT_ASSERT( bytesMessage != NULL );
+
+            CPPUNIT_ASSERT( bytesMessage->readInt() == testInt1 );
+            CPPUNIT_ASSERT( bytesMessage->readInt() == testInt2 );
+            CPPUNIT_ASSERT( bytesMessage->readString() == testStr1 );
+            CPPUNIT_ASSERT( bytesMessage->readUTF() == testStr2 );
+            CPPUNIT_ASSERT( bytesMessage->readBoolean() == testBool1 );
+            CPPUNIT_ASSERT( bytesMessage->readBoolean() == testBool2 );
+
+            delete bytesMessage;
+            delete textMessage;
+        }
+        
+    };
+    
 }}}
 
 #endif /*_ACTIVEMQ_CONNECTOR_STOMP_STOMPCOMMANDWRITERTEST_H_*/

Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h
URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h?view=diff&rev=491870&r1=491869&r2=491870
==============================================================================
--- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h (original)
+++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/transport/IOTransportTest.h Tue Jan  2 09:51:48 2007
@@ -157,7 +157,7 @@
                 return outputStream;
             }
 
-            virtual void writeCommand( const Command* command ) 
+            virtual void writeCommand( Command* command ) 
                 throw (CommandIOException)
             {
                 try{