diff options
author | Martin Ritchie <ritchiem@apache.org> | 2009-02-13 11:19:38 +0000 |
---|---|---|
committer | Martin Ritchie <ritchiem@apache.org> | 2009-02-13 11:19:38 +0000 |
commit | 64d9444c6da714ed7831fa122dfdaaa2ddda3b4a (patch) | |
tree | 8244782b5dcdefa87b4b26cdcddd3ecffdf5e04d | |
parent | 571225ebdad7e9777d6949596189867d4fc62d93 (diff) | |
download | qpid-python-64d9444c6da714ed7831fa122dfdaaa2ddda3b4a.tar.gz |
QPID-1629 : Fully test MessageHandles before move
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@744074 13f79535-47bb-0310-9956-ffa450edef68
4 files changed, 372 insertions, 4 deletions
diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/InMemoryMessageHandle.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/InMemoryMessageHandle.java index 2a7c90a81e..1092f67d94 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/InMemoryMessageHandle.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/InMemoryMessageHandle.java @@ -75,7 +75,12 @@ public class InMemoryMessageHandle implements AMQMessageHandle public ContentChunk getContentChunk(StoreContext context, int index) throws AMQException, IllegalArgumentException { - if (index > _contentBodies.size() - 1) + if(_contentBodies == null) + { + throw new RuntimeException("No ContentBody has been set"); + } + + if (index > _contentBodies.size() - 1 || index < 0) { throw new IllegalArgumentException("Index " + index + " out of valid range 0 to " + (_contentBodies.size() - 1)); diff --git a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/WeakReferenceMessageHandle.java b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/WeakReferenceMessageHandle.java index 3ed8b0e55c..804d2c2131 100644 --- a/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/WeakReferenceMessageHandle.java +++ b/qpid/java/broker/src/main/java/org/apache/qpid/server/queue/WeakReferenceMessageHandle.java @@ -109,7 +109,12 @@ public class WeakReferenceMessageHandle implements AMQMessageHandle public ContentChunk getContentChunk(StoreContext context, int index) throws AMQException, IllegalArgumentException { - if (index > _contentBodies.size() - 1) + if(_contentBodies == null) + { + throw new RuntimeException("No ContentBody has been set"); + } + + if (index > _contentBodies.size() - 1 || index < 0) { throw new IllegalArgumentException("Index " + index + " out of valid range 0 to " + (_contentBodies.size() - 1)); @@ -197,8 +202,7 @@ public class WeakReferenceMessageHandle implements AMQMessageHandle final long arrivalTime = System.currentTimeMillis(); - - MessageMetaData mmd = new MessageMetaData(publishBody, contentHeaderBody, _contentBodies.size(), arrivalTime); + MessageMetaData mmd = new MessageMetaData(publishBody, contentHeaderBody, _contentBodies == null ? 0 : _contentBodies.size(), arrivalTime); _messageStore.storeMessageMetaData(storeContext, _messageId, mmd); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/InMemoryMessageHandleTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/InMemoryMessageHandleTest.java new file mode 100644 index 0000000000..cac84c01b4 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/InMemoryMessageHandleTest.java @@ -0,0 +1,311 @@ +/* + * + * 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. + * + */ +package org.apache.qpid.server.queue; + +import junit.framework.TestCase; +import org.apache.qpid.AMQException; +import org.apache.qpid.framing.BasicContentHeaderProperties; +import org.apache.qpid.framing.ContentHeaderBody; +import org.apache.qpid.framing.ContentHeaderProperties; +import org.apache.qpid.framing.abstraction.ContentChunk; +import org.apache.qpid.framing.abstraction.MessagePublishInfo; +import org.apache.qpid.framing.abstraction.MessagePublishInfoImpl; + +public class InMemoryMessageHandleTest extends TestCase +{ + AMQMessageHandle _handle; + + protected AMQMessageHandle newHandle(Long id) + { + return new InMemoryMessageHandle(id); + } + + public void testMessageID() + { + Long id = 1L; + _handle = newHandle(id); + + assertEquals("Message not set value", id, _handle.getMessageId()); + } + + public void testInvalidContentChunk() + { + _handle = newHandle(1L); + + try + { + _handle.getContentChunk(null, 0); + fail("getContentChunk should not succeed"); + } + catch (RuntimeException e) + { + assertTrue(e.getMessage().equals("No ContentBody has been set")); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + ContentChunk cc = new MockContentChunk(null, 100); + + try + { + _handle.addContentBodyFrame(null, cc, false); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + _handle.getContentChunk(null, -1); + fail("getContentChunk should not succeed"); + } + catch (IllegalArgumentException e) + { + assertTrue(e.getMessage().contains("out of valid range")); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + _handle.getContentChunk(null, 1); + fail("getContentChunk should not succeed"); + } + catch (IllegalArgumentException e) + { + assertTrue(e.getMessage().contains("out of valid range")); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + } + + public void testAddSingleContentChunk() + { + + _handle = newHandle(1L); + + ContentChunk cc = new MockContentChunk(null, 100); + + try + { + _handle.addContentBodyFrame(null, cc, true); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + assertEquals("Incorrect body count", 1, _handle.getBodyCount(null)); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + assertEquals("Incorrect ContentChunk returned.", cc, _handle.getContentChunk(null, 0)); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + cc = new MockContentChunk(null, 100); + + try + { + _handle.addContentBodyFrame(null, cc, true); + fail("Exception should prevent adding two final chunks"); + } + catch (UnsupportedOperationException e) + { + //normal path + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + } + + public void testAddMultipleContentChunk() + { + + _handle = newHandle(1L); + + ContentChunk cc = new MockContentChunk(null, 100); + + try + { + _handle.addContentBodyFrame(null, cc, false); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + assertEquals("Incorrect body count", 1, _handle.getBodyCount(null)); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + assertEquals("Incorrect ContentChunk returned.", cc, _handle.getContentChunk(null, 0)); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + cc = new MockContentChunk(null, 100); + + try + { + _handle.addContentBodyFrame(null, cc, true); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + assertEquals("Incorrect body count", 2, _handle.getBodyCount(null)); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + try + { + assertEquals("Incorrect ContentChunk returned.", cc, _handle.getContentChunk(null, 1)); + } + catch (AMQException e) + { + fail("AMQException thrown:" + e.getMessage()); + } + + } + + // todo Move test to QueueEntry +// public void testRedelivered() +// { +// _handle = newHandle(1L); +// +// assertFalse("New message should not be redelivered", _handle.isRedelivered()); +// +// _handle.setRedelivered(true); +// +// assertTrue("New message should not be redelivered", _handle.isRedelivered()); +// } + + public void testInitialArrivalTime() + { + _handle = newHandle(1L); + + assertEquals("Initial Arrival time should be 0L", 0L, _handle.getArrivalTime()); + } + + public void testSetPublishAndContentHeaderBody_WithBody() + { + _handle = newHandle(1L); + + MessagePublishInfo mpi = new MessagePublishInfoImpl(); + int bodySize = 100; + + ContentHeaderBody chb = new ContentHeaderBody(0, 0, new BasicContentHeaderProperties(), bodySize); + + try + { + _handle.setPublishAndContentHeaderBody(null, mpi, chb); + + assertEquals("BodySize not returned correctly. ", bodySize, _handle.getBodySize(null)); + } + catch (AMQException e) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + public void testSetPublishAndContentHeaderBody_Empty() + { + _handle = newHandle(1L); + + MessagePublishInfo mpi = new MessagePublishInfoImpl(); + int bodySize = 0; + + BasicContentHeaderProperties props = new BasicContentHeaderProperties(); + + props.setAppId("HandleTest"); + + ContentHeaderBody chb = new ContentHeaderBody(0, 0, props, bodySize); + + try + { + _handle.setPublishAndContentHeaderBody(null, mpi, chb); + + assertEquals("BodySize not returned correctly. ", bodySize, _handle.getBodySize(null)); + + ContentHeaderBody retreived_chb = _handle.getContentHeaderBody(null); + + ContentHeaderProperties chp = retreived_chb.properties; + + assertEquals("ContentHeaderBody not correct", chb, retreived_chb); + + assertEquals("AppID not correctly retreived", "HandleTest", + ((BasicContentHeaderProperties) chp).getAppIdAsString()); + + MessagePublishInfo retreived_mpi = _handle.getMessagePublishInfo(null); + + assertEquals("MessagePublishInfo not correct", mpi, retreived_mpi); + + + } + catch (AMQException e) + { + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + public void testIsPersistent() + { + _handle = newHandle(1L); + + assertFalse(_handle.isPersistent()); + } + +} diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/WeakMessageHandleTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/WeakMessageHandleTest.java new file mode 100644 index 0000000000..c6e7e2ebe2 --- /dev/null +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/WeakMessageHandleTest.java @@ -0,0 +1,48 @@ +/* + * + * 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. + * + */ +package org.apache.qpid.server.queue; + +import org.apache.qpid.server.store.MemoryMessageStore; + +public class WeakMessageHandleTest extends InMemoryMessageHandleTest +{ + private MemoryMessageStore _messageStore; + + public void setUp() + { + _messageStore = new MemoryMessageStore(); + _messageStore.configure(); + } + + protected AMQMessageHandle newHandle(Long id) + { + return new WeakReferenceMessageHandle(id, _messageStore); + } + + @Override + public void testIsPersistent() + { + _handle = newHandle(1L); + assertTrue(_handle.isPersistent()); + } + + +} |