Author: andyhot
Date: Sun Nov  8 16:03:59 2009
New Revision: 833891

URL: http://svn.apache.org/viewvc?rev=833891&view=rev
Log:
TAPESTRY-2570: Fixed NPE issue when trying to report an exception involving an 
instance that returns null for toString() + move around some variables 
definition to improve readability

Added:
    
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/exception/
    
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/exception/TestExceptionAnalyzer.java
Modified:
    
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/exception/ExceptionAnalyzer.java

Modified: 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/exception/ExceptionAnalyzer.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/exception/ExceptionAnalyzer.java?rev=833891&r1=833890&r2=833891&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/exception/ExceptionAnalyzer.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/util/exception/ExceptionAnalyzer.java
 Sun Nov  8 16:03:59 2009
@@ -101,30 +101,15 @@
     }
 
     protected Throwable buildDescription(Throwable exception)
-    {
-        BeanInfo info;
-        Class exceptionClass;
-        ExceptionProperty property;
-        PropertyDescriptor[] descriptors;
-        PropertyDescriptor descriptor;
-        Throwable next = null;
-        int i;
-        Object value;
-        Method method;
-        ExceptionProperty[] properties;
-        ExceptionDescription description;
-        String stringValue;
-        String message;
-        String[] stackTrace = null;
-
+    {   
         propertyDescriptions.clear();
-
-        message = exception.getMessage();
-        exceptionClass = exception.getClass();
+        
+        Class exceptionClass = exception.getClass();
 
         // Get properties, ignoring those in Throwable and higher
         // (including the 'message' property).
 
+        BeanInfo info;
         try
         {
             info = Introspector.getBeanInfo(exceptionClass, Throwable.class);
@@ -134,13 +119,16 @@
             return null;
         }
 
-        descriptors = info.getPropertyDescriptors();
+        Object value;
+        Throwable next = null;
+        String message = exception.getMessage();
+        PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
 
-        for (i = 0; i < descriptors.length; i++)
+        for (int i = 0; i < descriptors.length; i++)
         {
-            descriptor = descriptors[i];
+               PropertyDescriptor descriptor = descriptors[i];
 
-            method = descriptor.getReadMethod();
+            Method method = descriptor.getReadMethod();
             if (method == null)
                 continue;
 
@@ -175,29 +163,35 @@
                 continue;
             }
 
-            stringValue = value.toString().trim();
+            String stringValue = value.toString();
+            
+            if (stringValue == null)
+               continue;
+            
+            stringValue = stringValue.trim();            
 
             if (stringValue.length() == 0)
                 continue;
 
-            property = new ExceptionProperty(descriptor.getDisplayName(), 
value);
+            ExceptionProperty property = new 
ExceptionProperty(descriptor.getDisplayName(), value);
 
             propertyDescriptions.add(property);
         }
 
         // If exhaustive, or in the deepest exception (where there's no next)
         // the extract the stack trace.
-
+        String[] stackTrace = null;
+        
         if (next == null || exhaustive)
             stackTrace = getStackTrace(exception);
 
         // Would be nice to sort the properties here.
 
-        properties = new ExceptionProperty[propertyDescriptions.size()];
+        ExceptionProperty[] properties = new 
ExceptionProperty[propertyDescriptions.size()];
 
         ExceptionProperty[] propArray = (ExceptionProperty[]) 
propertyDescriptions.toArray(properties);
 
-        description = new ExceptionDescription(exceptionClass.getName(), 
message, propArray, stackTrace);
+        ExceptionDescription description = new 
ExceptionDescription(exceptionClass.getName(), message, propArray, stackTrace);
 
         exceptionDescriptions.add(description);
 

Added: 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/exception/TestExceptionAnalyzer.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/exception/TestExceptionAnalyzer.java?rev=833891&view=auto
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/exception/TestExceptionAnalyzer.java
 (added)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/util/exception/TestExceptionAnalyzer.java
 Sun Nov  8 16:03:59 2009
@@ -0,0 +1,52 @@
+package org.apache.tapestry.util.exception;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import org.apache.hivemind.ApplicationRuntimeException;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+...@test
+public class TestExceptionAnalyzer extends Assert {
+       
+       public void test_report_exception() {
+               ApplicationRuntimeException ex = new 
ApplicationRuntimeException("TAPerrorSTRY");               
+               
+               ByteArrayOutputStream stream = new ByteArrayOutputStream();
+               
+               new ExceptionAnalyzer().reportException(ex, new 
PrintStream(stream));
+               
+               String output = stream.toString();
+                       
+               
assertTrue(output.contains("org.apache.hivemind.ApplicationRuntimeException"));
+               assertTrue(output.contains("TAPerrorSTRY"));
+       }
+       
+       // test for TAPESTRY-2570
+       public void test_with_null_tostring_exception() {
+               Exception dummyException = new Exception("TAPerrorSTRY"){
+                       public Object getDummy() {
+                               return new Object(){
+                                       @Override
+                                       public String toString() {
+                                               return null;
+                                       }
+                               };
+                       }
+               };
+               ApplicationRuntimeException ex = new 
ApplicationRuntimeException(dummyException);
+               
+               ByteArrayOutputStream stream = new ByteArrayOutputStream();
+               
+               new ExceptionAnalyzer().reportException(ex, new 
PrintStream(stream));
+               
+               String output = stream.toString();
+               
+               
assertTrue(output.contains("org.apache.hivemind.ApplicationRuntimeException"));
+               assertTrue(output.contains("TAPerrorSTRY"));
+               
+       }
+       
+       
+}


Reply via email to