summaryrefslogtreecommitdiff
path: root/java/common/src
diff options
context:
space:
mode:
authorMartin Ritchie <ritchiem@apache.org>2008-11-06 09:56:03 +0000
committerMartin Ritchie <ritchiem@apache.org>2008-11-06 09:56:03 +0000
commit340a73dbd9f9797d48b3007ab3b9fa8de21bd6ee (patch)
treeaec1b91061f7100848f1be49d5a0d1bceac229e1 /java/common/src
parent23b0950928a190f439f9cb54dcb6984eb6a47a51 (diff)
downloadqpid-python-340a73dbd9f9797d48b3007ab3b9fa8de21bd6ee.tar.gz
QPID-1434 : Add ability to rethrow AMQExceptions
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@711820 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common/src')
-rw-r--r--java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java128
1 files changed, 128 insertions, 0 deletions
diff --git a/java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java b/java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java
new file mode 100644
index 0000000000..853dc85592
--- /dev/null
+++ b/java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java
@@ -0,0 +1,128 @@
+/*
+ *
+ * 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;
+
+import junit.framework.TestCase;
+import org.apache.qpid.protocol.AMQConstant;
+import org.apache.qpid.framing.AMQFrameDecodingException;
+
+/**
+ * This test is to ensure that when an AMQException is rethrown that the specified exception is correctly wrapped up.
+ *
+ * There are three cases:
+ * Re-throwing an AMQException
+ * Re-throwing a Subclass of AMQException
+ * Re-throwing a Subclass of AMQException that does not have the default AMQException constructor which will force the
+ * creation of an AMQException.
+ */
+public class AMQExceptionTest extends TestCase
+{
+ /**
+ * Test that an AMQException will be correctly created and rethrown.
+ */
+ public void testRethrowGeneric()
+ {
+ AMQException test = new AMQException(AMQConstant.ACCESS_REFUSED, "refused", new RuntimeException());
+
+ try
+ {
+ reThrowException(test);
+ }
+ catch (AMQException e)
+ {
+ assertEquals("Exception not of correct class", AMQException.class, e.getClass());
+ }
+ }
+
+ /**
+ * Test that a subclass of AMQException that has the default constructor will be correctly created and rethrown.
+ */
+ public void testRethrowAMQESubclass()
+ {
+ AMQFrameDecodingException test = new AMQFrameDecodingException(AMQConstant.INTERNAL_ERROR,
+ "Error",
+ new Exception());
+ try
+ {
+ reThrowException(test);
+ }
+ catch (AMQException e)
+ {
+ assertEquals("Exception not of correct class", AMQFrameDecodingException.class, e.getClass());
+ }
+ }
+
+ /**
+ * Test that a subclass of AMQException that doesnot have the default constructor will be correctly rethrown as an
+ * AMQException
+ */
+ public void testRethrowAMQESubclassNoConstructor()
+ {
+ AMQExceptionSubclass test = new AMQExceptionSubclass("Invalid Argument Exception");
+ try
+ {
+ reThrowException(test);
+ }
+ catch (AMQException e)
+ {
+ assertEquals("Exception not of correct class", AMQException.class, e.getClass());
+ }
+ }
+
+ /**
+ * Private method to rethrown and validate the basic values of the rethrown
+ * @param test Exception to rethrow
+ * @throws AMQException the rethrown exception
+ */
+ private void reThrowException(AMQException test) throws AMQException
+ {
+ try
+ {
+ test.rethrow();
+ }
+ catch (AMQException amqe)
+ {
+ assertEquals("Error code does not match.", test.getErrorCode(), amqe.getErrorCode());
+ assertTrue("Exception message does not start as expected.", amqe.getMessage().startsWith(test.getMessage()));
+ assertEquals("Test Exception is not set as the cause", test, amqe.getCause());
+ assertEquals("Cause is not correct", test.getCause(), amqe.getCause().getCause());
+ throw amqe;
+ }
+ catch (Throwable e)
+ {
+ fail("Throwable recieved when AMQException expected:" + e);
+ }
+
+ }
+
+ /**
+ * Private class that extends AMQException but does not have a default exception.
+ */
+ private class AMQExceptionSubclass extends AMQException
+ {
+
+ public AMQExceptionSubclass(String msg)
+ {
+ super(null, msg, null);
+ }
+ }
+}
+