You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ba...@apache.org on 2002/07/05 18:38:36 UTC
cvs commit: jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/exception NestableDelegateTestCase.java NestableExceptionTestCase.java NestableRuntimeExceptionTestCase.java
bayard 2002/07/05 09:38:36
Modified: lang/src/java/org/apache/commons/lang/exception
Nestable.java NestableDelegate.java
NestableException.java
NestableRuntimeException.java
lang/src/test/org/apache/commons/lang/exception
NestableDelegateTestCase.java
NestableExceptionTestCase.java
NestableRuntimeExceptionTestCase.java
Log:
Applying patch with extra methods for Nestable exceptions to get top and
bottom of the exception chain.
Submitted By: Steven Caswell
Revision Changes Path
1.3 +29 -0 jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/Nestable.java
Index: Nestable.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/Nestable.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- Nestable.java 5 Jul 2002 06:33:52 -0000 1.2
+++ Nestable.java 5 Jul 2002 16:38:36 -0000 1.3
@@ -63,6 +63,8 @@
*
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
+ * @version $Id$
*/
public interface Nestable
{
@@ -73,12 +75,39 @@
public Throwable getCause();
/**
+ * Returns the error message of this <code>Nestable</code>
+ *
+ * @return The error message.
+ */
+ public String getFirstMessage();
+
+ /**
+ * Returns the error message of the last <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the last nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getLastMessage();
+
+ /**
* Returns the error message of this and any nested
* <code>Throwable</code>.
*
* @return The error message.
*/
public String getMessage();
+
+ /**
+ * Returns the error message of the next <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the next nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getNextMessage();
/**
* Prints the stack trace of this exception to the specified print
1.3 +53 -0 jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/NestableDelegate.java
Index: NestableDelegate.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/NestableDelegate.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NestableDelegate.java 5 Jul 2002 06:33:52 -0000 1.2
+++ NestableDelegate.java 5 Jul 2002 16:38:36 -0000 1.3
@@ -66,6 +66,8 @@
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
+ * @version $Id$
*/
public class NestableDelegate
{
@@ -99,6 +101,33 @@
}
/**
+ * Returns the last message in the chain of <code>Throwable</code>s
+ * specified when this delegate was created.
+ */
+ public String getLastMessage()
+ {
+ String msg = null;
+ if(this.cause.getCause() == null)
+ {
+ return null;
+ }
+ Throwable t = this.cause.getCause();
+ while(t != null)
+ {
+ if(Nestable.class.isInstance(t))
+ {
+ t = ((Nestable) t).getCause();
+ }
+ else
+ {
+ msg = t.getMessage();
+ t = null;
+ }
+ }
+ return msg;
+ }
+
+ /**
* @param baseMsg The base message to use when creating the full
* message. Should be generally be called via
* <code>nestableHelper.getMessage(super.getMessage())</code>,
@@ -132,6 +161,30 @@
return (msg.length() > 0 ? msg.toString() : null);
}
+ /**
+ * Returns the next message in the chain of <code>Throwable</code>s
+ * specified when this delegate was created.
+ */
+ public String getNextMessage()
+ {
+ if(this.cause == null)
+ {
+ return null;
+ }
+ if(this.cause.getCause() == null)
+ {
+ return null;
+ }
+ if(Nestable.class.isInstance(this.cause.getCause()))
+ {
+ return ((Nestable) this.cause.getCause()).getFirstMessage();
+ }
+ else
+ {
+ return this.cause.getCause().getMessage();
+ }
+ }
+
/**
* Prints the stack trace of this exception the the standar error
* stream.
1.3 +39 -0 jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/NestableException.java
Index: NestableException.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/NestableException.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NestableException.java 5 Jul 2002 06:33:52 -0000 1.2
+++ NestableException.java 5 Jul 2002 16:38:36 -0000 1.3
@@ -124,6 +124,8 @@
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
+ * @version $Id$
*/
public class NestableException extends Exception implements Nestable
{
@@ -195,6 +197,29 @@
}
/**
+ * Returns the error message of this <code>Nestable</code>
+ *
+ * @return The error message.
+ */
+ public String getFirstMessage()
+ {
+ return super.getMessage();
+ }
+
+ /**
+ * Returns the error message of the last <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the last nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getLastMessage()
+ {
+ return delegate.getLastMessage();
+ }
+
+ /**
* @see org.apache.commons.lang.exception.Nestable#getMessage()
*/
public String getMessage()
@@ -222,6 +247,19 @@
}
/**
+ * Returns the error message of the next <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the next nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getNextMessage()
+ {
+ return delegate.getNextMessage();
+ }
+
+ /**
* Prints the stack trace of this exception the the standar error
* stream.
*/
@@ -255,4 +293,5 @@
{
super.printStackTrace(out);
}
+
}
1.3 +39 -0 jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/NestableRuntimeException.java
Index: NestableRuntimeException.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/java/org/apache/commons/lang/exception/NestableRuntimeException.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NestableRuntimeException.java 5 Jul 2002 06:33:52 -0000 1.2
+++ NestableRuntimeException.java 5 Jul 2002 16:38:36 -0000 1.3
@@ -70,6 +70,8 @@
* @author <a href="mailto:Rafal.Krzewski@e-point.pl">Rafal Krzewski</a>
* @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
* @author <a href="mailto:knielsen@apache.org">Kasper Nielsen</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
+ * @version $Id$
*/
public class NestableRuntimeException extends RuntimeException
implements Nestable
@@ -142,6 +144,29 @@
}
/**
+ * Returns the error message of this <code>Nestable</code>
+ *
+ * @return The error message.
+ */
+ public String getFirstMessage()
+ {
+ return super.getMessage();
+ }
+
+ /**
+ * Returns the error message of the last <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the last nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getLastMessage()
+ {
+ return delegate.getLastMessage();
+ }
+
+ /**
* @see org.apache.commons.lang.exception.Nestable#getMessage()
*/
public String getMessage()
@@ -169,6 +194,19 @@
}
/**
+ * Returns the error message of the next <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the next nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getNextMessage()
+ {
+ return delegate.getNextMessage();
+ }
+
+ /**
* Prints the stack trace of this exception the the standar error
* stream.
*/
@@ -202,4 +240,5 @@
{
super.printStackTrace(out);
}
+
}
1.5 +110 -1 jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java
Index: NestableDelegateTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- NestableDelegateTestCase.java 5 Jul 2002 07:03:39 -0000 1.4
+++ NestableDelegateTestCase.java 5 Jul 2002 16:38:36 -0000 1.5
@@ -63,8 +63,9 @@
/**
* Tests the org.apache.commons.lang.exception.NestableDelegate class.
*
- * @author <a href="mailto:stevencaswell@yahoo.com">Steven Caswell</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
+ * @version $Id$
*/
public class NestableDelegateTestCase extends junit.framework.TestCase
{
@@ -213,8 +214,44 @@
{
return new Exception("ThrowableNestable cause");
}
+
+ /**
+ * Returns the error message of this <code>Nestable</code>
+ *
+ * @return The error message.
+ */
+ public String getFirstMessage()
+ {
+ return "ThrowableNestable first message";
+ }
+
+ /**
+ * Returns the error message of the last <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the last nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getLastMessage()
+ {
+ return "ThrowableNestable last message";
+ }
/**
+ * Returns the error message of the next <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the next nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getNextMessage()
+ {
+ return "ThrowableNestable next message";
+ }
+
+ /**
* Prints the stack trace of this exception to the specified print
* writer. Includes inforamation from the exception--if
* any--which caused this exception.
@@ -270,6 +307,42 @@
}
/**
+ * Returns the error message of this <code>Nestable</code>
+ *
+ * @return The error message.
+ */
+ public String getFirstMessage()
+ {
+ return "ThrowableNestedNestable first message";
+ }
+
+ /**
+ * Returns the error message of the last <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the last nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getLastMessage()
+ {
+ return "ThrowableNestedNestable last message";
+ }
+
+ /**
+ * Returns the error message of the next <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the next nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getNextMessage()
+ {
+ return "ThrowableNestedNestable next message";
+ }
+
+ /**
* Prints the stack trace of this exception to the specified print
* writer. Includes inforamation from the exception--if
* any--which caused this exception.
@@ -320,6 +393,42 @@
return null;
}
+ /**
+ * Returns the error message of this <code>Nestable</code>
+ *
+ * @return The error message.
+ */
+ public String getFirstMessage()
+ {
+ return "non-throwable first message";
+ }
+
+ /**
+ * Returns the error message of the last <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the last nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getLastMessage()
+ {
+ return "non-throwable last message";
+ }
+
+ /**
+ * Returns the error message of the next <code>Throwable</code> in the
+ * nestable chain. Returns <code>null</code> if there is no nested
+ * <code>Throwable</code> or if the next nested <code>Throwable</code> was
+ * created without a message specified.
+ *
+ * @return The error message.
+ */
+ public String getNextMessage()
+ {
+ return "non-throwable next message";
+ }
+
/**
* Prints the stack trace of this exception to the specified print
* writer. Includes inforamation from the exception--if
1.3 +78 -4 jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java
Index: NestableExceptionTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NestableExceptionTestCase.java 5 Jul 2002 07:03:39 -0000 1.2
+++ NestableExceptionTestCase.java 5 Jul 2002 16:38:36 -0000 1.3
@@ -63,10 +63,16 @@
/**
* Tests the org.apache.commons.lang.exception.NestableException class.
*
- * @author <a href="mailto:stevencaswell@yahoo.com">Steven Caswell</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
+ * @version $Id$
*/
public class NestableExceptionTestCase extends junit.framework.TestCase
{
+ private Nestable[] nestables = new Nestable[9];
+ private String[] firstMessage = new String[nestables.length];
+ private String[] nextMessage = new String[nestables.length];
+ private String[] lastMessage = new String[nestables.length];
+
/**
* Construct a new instance of NestableExceptionTestCase with the specified name
*/
@@ -80,6 +86,50 @@
*/
public void setUp()
{
+ nestables[0] = new NestableException("one level");
+ firstMessage[0] = "one level";
+ nextMessage[0] = "null";
+ lastMessage[0] = "null";
+
+ nestables[1] = new NestableException(new Exception("two levels"));
+ firstMessage[1] = "null";
+ nextMessage[1] = "two levels";
+ lastMessage[1] = "two levels";
+
+ nestables[2] = new NestableException(new NestableException(new Exception("three levels")));
+ firstMessage[2] = "null";
+ nextMessage[2] = "null";
+ lastMessage[2] = "three levels";
+
+ nestables[3] = new NestableException("top level", new NestableException(new Exception("three levels")));
+ firstMessage[3] = "top level";
+ nextMessage[3] = "null";
+ lastMessage[3] = "three levels";
+
+ nestables[4] = new NestableException("top level", new NestableException("next level", new Exception("three levels")));
+ firstMessage[4] = "top level";
+ nextMessage[4] = "next level";
+ lastMessage[4] = "three levels";
+
+ nestables[5] = new NestableException("top level", new NestableException("next level", new Exception()));
+ firstMessage[5] = "top level";
+ nextMessage[5] = "next level";
+ lastMessage[5] = "null";
+
+ nestables[6] = new NestableException("top level", new NestableException());
+ firstMessage[6] = "top level";
+ nextMessage[6] = "null";
+ lastMessage[6] = "null";
+
+ nestables[7] = new NestableException(new NestableException("next level", new Exception()));
+ firstMessage[7] = "null";
+ nextMessage[7] = "next level";
+ lastMessage[7] = "null";
+
+ nestables[8] = new NestableException(new NestableException(new Exception()));
+ firstMessage[8] = "null";
+ nextMessage[8] = "null";
+ lastMessage[8] = "null";
}
public static Test suite()
@@ -97,6 +147,30 @@
/**
* Test the implementation
*/
+ public void testGetFirstMessage()
+ {
+ for(int i = 0; i < nestables.length; i++)
+ {
+ assertEquals("nestables " + i + " first message", firstMessage[i], ((nestables[i].getFirstMessage() == null) ? "null" : nestables[i].getFirstMessage()));
+ }
+ }
+
+ public void testGetNextMessage()
+ {
+ for(int i = 0; i < nestables.length; i++)
+ {
+ assertEquals("nestables " + i + " next message", nextMessage[i], ((nestables[i].getNextMessage() == null) ? "null" : nestables[i].getNextMessage()));
+ }
+ }
+
+ public void testGetLastMessage()
+ {
+ for(int i = 0; i < nestables.length; i++)
+ {
+ assertEquals("nestables " + i + " last message", lastMessage[i], ((nestables[i].getLastMessage() == null) ? "null" : nestables[i].getLastMessage()));
+ }
+ }
+
public void testNestableException()
{
NestableException ne1 = new NestableException();
@@ -105,8 +179,8 @@
NestableException ne2 = new NestableException("ne2");
assertNull("nestable exception(\"ne2\") cause is null", ne2.getCause());
- assertNotNull("nestable exception(\"ne2\") message is not null", ne2.getMessage());
- assertTrue("nestable exception(\"ne2\") message == ne2", ne2.getMessage().equals("ne2"));
+ assertNotNull("nestable exception(\"ne2\") message is not null", ne2.getMessage());
+ assertTrue("nestable exception(\"ne2\") message == ne2", ne2.getMessage().equals("ne2"));
NestableException ne3 = new NestableException(new Exception("ne3 exception"));
assertNotNull("nestable exception(new Exception(\"ne3 exception\") cause is not null",
@@ -142,7 +216,7 @@
NestableException ne7 = new NestableException("ne7o", new NestableException("ne7i", new Exception("ne7 exception")));
assertTrue("nextable exception(\"ne7o\", new NestableException(\"ne7i\", new Exception(\"ne7 exception\"))) message is ne7o: ne7i: ne7 exception",
- ne7.getMessage().equals("ne7o: ne7i: ne7 exception"));
+ ne7.getMessage().equals("ne7o: ne7i: ne7 exception"));
NestableException ne8 = new NestableException("ne8", new Exception("ne8 exception"));
ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
1.3 +75 -1 jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java
Index: NestableRuntimeExceptionTestCase.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/lang/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- NestableRuntimeExceptionTestCase.java 5 Jul 2002 07:03:39 -0000 1.2
+++ NestableRuntimeExceptionTestCase.java 5 Jul 2002 16:38:36 -0000 1.3
@@ -63,10 +63,16 @@
/**
* Tests the org.apache.commons.lang.exception.NestableRuntimeException class.
*
- * @author <a href="mailto:stevencaswell@yahoo.com">Steven Caswell</a>
+ * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
+ * @version $Id$
*/
public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
{
+ private Nestable[] nestables = new Nestable[9];
+ private String[] firstMessage = new String[nestables.length];
+ private String[] nextMessage = new String[nestables.length];
+ private String[] lastMessage = new String[nestables.length];
+
/**
* Construct a new instance of NestableRuntimeExceptionTestCase with the specified name
*/
@@ -80,6 +86,50 @@
*/
public void setUp()
{
+ nestables[0] = new NestableRuntimeException("one level");
+ firstMessage[0] = "one level";
+ nextMessage[0] = "null";
+ lastMessage[0] = "null";
+
+ nestables[1] = new NestableRuntimeException(new Exception("two levels"));
+ firstMessage[1] = "null";
+ nextMessage[1] = "two levels";
+ lastMessage[1] = "two levels";
+
+ nestables[2] = new NestableRuntimeException(new NestableRuntimeException(new Exception("three levels")));
+ firstMessage[2] = "null";
+ nextMessage[2] = "null";
+ lastMessage[2] = "three levels";
+
+ nestables[3] = new NestableRuntimeException("top level", new NestableRuntimeException(new Exception("three levels")));
+ firstMessage[3] = "top level";
+ nextMessage[3] = "null";
+ lastMessage[3] = "three levels";
+
+ nestables[4] = new NestableRuntimeException("top level", new NestableRuntimeException("next level", new Exception("three levels")));
+ firstMessage[4] = "top level";
+ nextMessage[4] = "next level";
+ lastMessage[4] = "three levels";
+
+ nestables[5] = new NestableRuntimeException("top level", new NestableRuntimeException("next level", new Exception()));
+ firstMessage[5] = "top level";
+ nextMessage[5] = "next level";
+ lastMessage[5] = "null";
+
+ nestables[6] = new NestableRuntimeException("top level", new NestableRuntimeException());
+ firstMessage[6] = "top level";
+ nextMessage[6] = "null";
+ lastMessage[6] = "null";
+
+ nestables[7] = new NestableRuntimeException(new NestableRuntimeException("next level", new Exception()));
+ firstMessage[7] = "null";
+ nextMessage[7] = "next level";
+ lastMessage[7] = "null";
+
+ nestables[8] = new NestableRuntimeException(new NestableRuntimeException(new Exception()));
+ firstMessage[8] = "null";
+ nextMessage[8] = "null";
+ lastMessage[8] = "null";
}
public static Test suite()
@@ -97,6 +147,30 @@
/**
* Test the implementation
*/
+ public void testGetFirstMessage()
+ {
+ for(int i = 0; i < nestables.length; i++)
+ {
+ assertEquals("nestables " + i + " first message", firstMessage[i], ((nestables[i].getFirstMessage() == null) ? "null" : nestables[i].getFirstMessage()));
+ }
+ }
+
+ public void testGetNextMessage()
+ {
+ for(int i = 0; i < nestables.length; i++)
+ {
+ assertEquals("nestables " + i + " next message", nextMessage[i], ((nestables[i].getNextMessage() == null) ? "null" : nestables[i].getNextMessage()));
+ }
+ }
+
+ public void testGetLastMessage()
+ {
+ for(int i = 0; i < nestables.length; i++)
+ {
+ assertEquals("nestables " + i + " last message", lastMessage[i], ((nestables[i].getLastMessage() == null) ? "null" : nestables[i].getLastMessage()));
+ }
+ }
+
public void testNestableRuntimeException()
{
NestableRuntimeException ne1 = new NestableRuntimeException();
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>