Author: scolebourne
Date: Fri Jan 6 17:26:35 2006
New Revision: 366623
URL: http://svn.apache.org/viewcvs?rev=366623&view=rev
Log:
New ExceptionUtils methods getMessage/getRootCauseMessage
Modified:
jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
Modified: jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt?rev=366623&r1=366622&r2=366623&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt (original)
+++ jakarta/commons/proper/lang/trunk/RELEASE-NOTES.txt Fri Jan 6 17:26:35 2006
@@ -50,7 +50,9 @@
NEW FEATURES IN 2.2:
- **** TO DO
+- New ExceptionUtils methods getMessage/getRootCauseMessage
+- New ExceptionUtils method setCause() [37574]
+
NEW FEATURES IN 2.1:
@@ -101,7 +103,6 @@
37111 Request for MutableBoolean implementation
36512 Enhanced Class.forName version
37596 DurationFormatUtils.formatDurationWords "11 <unit>s" gets converted to
"11 <unit>"
-37574 New ExceptionUtils method setCause()
BUG FIXES IN 2.1:
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/ExceptionUtils.java?rev=366623&r1=366622&r2=366623&view=diff
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/ExceptionUtils.java
Fri Jan 6 17:26:35 2006
@@ -28,6 +28,7 @@
import java.util.StringTokenizer;
import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.NullArgumentException;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
@@ -920,6 +921,43 @@
}
}
return list;
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets a short message summarising the exception.
+ * <p>
+ * The message returned is of the form
+ * {ClassNameWithoutPackage}: {ThrowableMessage}
+ *
+ * @param th the throwable to get a message for, null returns empty string
+ * @return the message, non-null
+ * @since Commons Lang 2.2
+ */
+ public static String getMessage(Throwable th) {
+ if (th == null) {
+ return "";
+ }
+ String clsName = ClassUtils.getShortClassName(th, null);
+ String msg = th.getMessage();
+ return clsName + ": " + StringUtils.defaultString(msg);
+ }
+
+ //-----------------------------------------------------------------------
+ /**
+ * Gets a short message summarising the root cause exception.
+ * <p>
+ * The message returned is of the form
+ * {ClassNameWithoutPackage}: {ThrowableMessage}
+ *
+ * @param th the throwable to get a message for, null returns empty string
+ * @return the message, non-null
+ * @since Commons Lang 2.2
+ */
+ public static String getRootCauseMessage(Throwable th) {
+ Throwable root = ExceptionUtils.getRootCause(th);
+ root = (root == null ? th : root);
+ return getMessage(root);
}
}
Modified:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
URL:
http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java?rev=366623&r1=366622&r2=366623&view=diff
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/ExceptionUtilsTestCase.java
Fri Jan 6 17:26:35 2006
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2005 The Apache Software Foundation.
+ * Copyright 2002-2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -397,7 +397,29 @@
} catch (IllegalArgumentException ex) {
}
}
-
+
+ public void test_getMessage_Throwable() {
+ Throwable th = null;
+ assertEquals("", ExceptionUtils.getMessage(th));
+
+ th = new IllegalArgumentException("Base");
+ assertEquals("IllegalArgumentException: Base",
ExceptionUtils.getMessage(th));
+
+ th = new ExceptionWithCause("Wrapper", th);
+ assertEquals("ExceptionUtilsTestCase.ExceptionWithCause: Wrapper",
ExceptionUtils.getMessage(th));
+ }
+
+ public void test_getRootCauseMessage_Throwable() {
+ Throwable th = null;
+ assertEquals("", ExceptionUtils.getRootCauseMessage(th));
+
+ th = new IllegalArgumentException("Base");
+ assertEquals("IllegalArgumentException: Base",
ExceptionUtils.getRootCauseMessage(th));
+
+ th = new ExceptionWithCause("Wrapper", th);
+ assertEquals("IllegalArgumentException: Base",
ExceptionUtils.getRootCauseMessage(th));
+ }
+
//-----------------------------------------------------------------------
/**
* Provides a method with a well known chained/nested exception
@@ -407,7 +429,13 @@
private static class ExceptionWithCause extends Exception {
private Throwable cause;
+ public ExceptionWithCause(String str, Throwable cause) {
+ super(str);
+ setCause(cause);
+ }
+
public ExceptionWithCause(Throwable cause) {
+ super();
setCause(cause);
}
@@ -429,5 +457,5 @@
public void getTargetException() {
}
}
-
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]