Author: rgreig
Date: Thu Oct 12 11:19:32 2006
New Revision: 463363

URL: http://svn.apache.org/viewvc?view=rev&rev=463363
Log:
QPID-2. JMSBytesMessage.readXXX methods now test the appropriate number of 
bytes are available in the message and throw the appropriate exception if not. 
Unit test updated to check behaviour of readXXX methods.

Modified:
    
incubator/qpid/trunk/qpid/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java
    
incubator/qpid/trunk/qpid/java/client/test/src/org/apache/qpid/client/message/TestBytesMessage.java

Modified: 
incubator/qpid/trunk/qpid/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java?view=diff&rev=463363&r1=463362&r2=463363
==============================================================================
--- 
incubator/qpid/trunk/qpid/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java
 (original)
+++ 
incubator/qpid/trunk/qpid/java/client/src/org/apache/qpid/client/message/JMSBytesMessage.java
 Thu Oct 12 11:19:32 2006
@@ -25,6 +25,7 @@
 import javax.jms.JMSException;
 import javax.jms.MessageNotReadableException;
 import javax.jms.MessageNotWriteableException;
+import javax.jms.MessageEOFException;
 import java.io.*;
 import java.nio.charset.Charset;
 import java.nio.charset.CharacterCodingException;
@@ -141,6 +142,19 @@
         }
     }
 
+    /**
+     * Check that there is at least a certain number of bytes available to read
+     * @param len the number of bytes
+     * @throws MessageEOFException if there are less than len bytes available 
to read
+     */
+    private void checkAvailable(int len) throws MessageEOFException
+    {
+        if (_data.remaining() < len)
+        {
+            throw new MessageEOFException("Unable to read " + len + " bytes");
+        }
+    }
+
     private void checkWritable() throws MessageNotWriteableException
     {
         if (_readable)
@@ -152,66 +166,84 @@
     public boolean readBoolean() throws JMSException
     {
         checkReadable();
+        checkAvailable(1);
         return _data.get() != 0;
     }
 
     public byte readByte() throws JMSException
     {
         checkReadable();
+        checkAvailable(1);
         return _data.get();
     }
 
     public int readUnsignedByte() throws JMSException
     {
         checkReadable();
+        checkAvailable(1);
         return _data.getUnsigned();
     }
 
     public short readShort() throws JMSException
     {
         checkReadable();
+        checkAvailable(2);
         return _data.getShort();
     }
 
     public int readUnsignedShort() throws JMSException
     {
         checkReadable();
+        checkAvailable(2);
         return _data.getUnsignedShort();
     }
 
+    /**
+     * Note that this method reads a unicode character as two bytes from the 
stream
+     * @return the character read from the stream
+     * @throws JMSException
+     */
     public char readChar() throws JMSException
     {
         checkReadable();
+        checkAvailable(2);
         return _data.getChar();
     }
 
     public int readInt() throws JMSException
     {
         checkReadable();
+        checkAvailable(4);
         return _data.getInt();
     }
 
     public long readLong() throws JMSException
     {
         checkReadable();
+        checkAvailable(8);
         return _data.getLong();
     }
 
     public float readFloat() throws JMSException
     {
         checkReadable();
+        checkAvailable(4);
         return _data.getFloat();
     }
 
     public double readDouble() throws JMSException
     {
         checkReadable();
+        checkAvailable(8);
         return _data.getDouble();
     }
 
     public String readUTF() throws JMSException
     {
         checkReadable();
+        // we check only for one byte since theoretically the string could be 
only a
+        // single byte when using UTF-8 encoding
+        checkAvailable(1);
         try
         {
             return _data.getString(Charset.forName("UTF-8").newDecoder());
@@ -232,8 +264,15 @@
         }
         checkReadable();
         int count = (_data.remaining() >= bytes.length ? bytes.length : 
_data.remaining());
-        _data.get(bytes, 0, count);
-        return count;
+        if (count == 0)
+        {
+            return -1;
+        }
+        else
+        {
+            _data.get(bytes, 0, count);
+            return count;
+        }
     }
 
     public int readBytes(byte[] bytes, int maxLength) throws JMSException
@@ -248,8 +287,15 @@
         }
         checkReadable();
         int count = (_data.remaining() >= maxLength ? maxLength : 
_data.remaining());
-        _data.get(bytes, 0, count);
-        return count;
+        if (count == 0)
+        {
+            return -1;
+        }
+        else
+        {
+            _data.get(bytes, 0, count);
+            return count;
+        }
     }
 
     public void writeBoolean(boolean b) throws JMSException

Modified: 
incubator/qpid/trunk/qpid/java/client/test/src/org/apache/qpid/client/message/TestBytesMessage.java
URL: 
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/java/client/test/src/org/apache/qpid/client/message/TestBytesMessage.java?view=diff&rev=463363&r1=463362&r2=463363
==============================================================================
--- 
incubator/qpid/trunk/qpid/java/client/test/src/org/apache/qpid/client/message/TestBytesMessage.java
 (original)
+++ 
incubator/qpid/trunk/qpid/java/client/test/src/org/apache/qpid/client/message/TestBytesMessage.java
 Thu Oct 12 11:19:32 2006
@@ -23,6 +23,7 @@
 
 import javax.jms.MessageNotReadableException;
 import javax.jms.MessageNotWriteableException;
+import javax.jms.MessageEOFException;
 
 public class TestBytesMessage
 {
@@ -83,6 +84,209 @@
     {
         JMSBytesMessage bm = new JMSBytesMessage();
         bm.writeObject(null);
+    }
+
+    @Test
+    public void testReadBoolean() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeBoolean(true);
+        bm.reset();
+        boolean result = bm.readBoolean();
+        Assert.assertTrue(result);        
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFByte() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeByte((byte)1);
+        bm.reset();
+        bm.readByte();
+        // should throw
+        bm.readByte();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFUnsignedByte() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeByte((byte)1);
+        bm.reset();
+        bm.readByte();
+        // should throw
+        bm.readUnsignedByte();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFBoolean() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeBoolean(true);
+        bm.reset();
+        bm.readBoolean();
+        // should throw
+        bm.readBoolean();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFChar() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeChar('A');
+        bm.reset();
+        bm.readChar();
+        // should throw
+        bm.readChar();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFDouble() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeDouble(1.3d);
+        bm.reset();
+        bm.readDouble();
+        // should throw
+        bm.readDouble();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFFloat() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeFloat(1.3f);
+        bm.reset();
+        bm.readFloat();
+        // should throw
+        bm.readFloat();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFInt() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeInt(99);
+        bm.reset();
+        bm.readInt();
+        // should throw
+        bm.readInt();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFLong() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeLong(4L);
+        bm.reset();
+        bm.readLong();
+        // should throw
+        bm.readLong();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFShort() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeShort((short)4);
+        bm.reset();
+        bm.readShort();
+        // should throw
+        bm.readShort();
+    }
+
+    @Test(expected=MessageEOFException.class)
+    public void testEOFUnsignedShort() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeShort((short)4);
+        bm.reset();
+        bm.readUnsignedShort();
+        // should throw
+        bm.readUnsignedShort();
+    }
+
+    /**
+     * Tests that the readBytes() method populates the passed in array
+     * correctly
+     * @throws Exception
+     */
+    @Test
+    public void testReadBytes() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeByte((byte)3);
+        bm.writeByte((byte)4);
+        bm.reset();
+        byte[] result = new byte[2];
+        int count = bm.readBytes(result);
+        Assert.assertEquals((byte)3, result[0]);
+        Assert.assertEquals((byte)4, result[1]);
+        Assert.assertEquals(2, count);
+    }
+
+    @Test
+    public void testReadBytesEOF() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeByte((byte)3);
+        bm.writeByte((byte)4);
+        bm.reset();
+        byte[] result = new byte[2];
+        bm.readBytes(result);
+        int count = bm.readBytes(result);
+        Assert.assertEquals(-1, count);
+    }
+
+    @Test
+    public void testReadBytesWithLargerArray() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeByte((byte)3);
+        bm.writeByte((byte)4);
+        bm.reset();
+        byte[] result = new byte[3];
+        int count = bm.readBytes(result);
+        Assert.assertEquals(2, count);
+        Assert.assertEquals((byte)3, result[0]);
+        Assert.assertEquals((byte)4, result[1]);
+        Assert.assertEquals((byte)0, result[2]);
+    }
+
+    @Test
+    public void testReadBytesWithCount() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.writeByte((byte)3);
+        bm.writeByte((byte)4);
+        bm.writeByte((byte)5);
+        bm.reset();
+        byte[] result = new byte[3];
+        int count = bm.readBytes(result, 2);
+        Assert.assertEquals(2, count);
+        Assert.assertEquals((byte)3, result[0]);
+        Assert.assertEquals((byte)4, result[1]);
+        Assert.assertEquals((byte)0, result[2]);
+    }
+
+    @Test
+    public void testToBodyString() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        final String testText = "This is a test";
+        bm.writeUTF(testText);
+        bm.reset();
+        String result = bm.toBodyString();
+        Assert.assertEquals(testText, result);
+    }
+
+    @Test
+    public void testToBodyStringWithNull() throws Exception
+    {
+        JMSBytesMessage bm = new JMSBytesMessage();
+        bm.reset();
+        String result = bm.toBodyString();
+        Assert.assertNull(result);
     }
 
     public static junit.framework.Test suite()


Reply via email to