Revision: 10584
Author:   rj...@google.com
Date:     Fri Aug 26 08:45:52 2011
Log:      Make UmbrellaException a bit more convenient to read, and test it.

Review at http://gwt-code-reviews.appspot.com/1532803

Review by: cromwell...@google.com
http://code.google.com/p/google-web-toolkit/source/detail?r=10584

Added:
/trunk/user/test/com/google/web/bindery/event/shared/UmbrellaExceptionTest.java
Modified:
 /trunk/user/src/com/google/web/bindery/event/shared/UmbrellaException.java

=======================================
--- /dev/null
+++ /trunk/user/test/com/google/web/bindery/event/shared/UmbrellaExceptionTest.java Fri Aug 26 08:45:52 2011
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2011 Google Inc.
+ *
+ * Licensed 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 com.google.web.bindery.event.shared;
+
+import junit.framework.TestCase;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Unit test for {@link #UmbrellaException}.
+ */
+public class UmbrellaExceptionTest extends TestCase {
+  public void testNone() {
+    // Why?
+    try {
+      throw new UmbrellaException(Collections.<Throwable> emptySet());
+    } catch (UmbrellaException e) {
+      assertNull(e.getCause());
+      assertNull(e.getMessage());
+    }
+  }
+
+  public void testOne() {
+    Set<Throwable> causes = new HashSet<Throwable>();
+    String message = "Just me";
+    RuntimeException theOne = new RuntimeException(message);
+    causes.add(theOne);
+
+    try {
+      throw new UmbrellaException(causes);
+    } catch (UmbrellaException e) {
+      assertSame(theOne, e.getCause());
+      assertEquals(UmbrellaException.ONE + message, e.getMessage());
+    }
+  }
+
+  public void testSome() {
+    Set<Throwable> causes = new HashSet<Throwable>();
+    String oneMessage = "one";
+    RuntimeException oneException = new RuntimeException(oneMessage);
+    causes.add(oneException);
+
+    String twoMessage = "two";
+    RuntimeException twoException = new RuntimeException(twoMessage);
+    causes.add(twoException);
+
+    try {
+      throw new UmbrellaException(causes);
+    } catch (UmbrellaException e) {
+ // A bit non-deterministic for a unit test, but I've checked both paths --
+      // rjrjr
+      if (e.getCause() == oneException) {
+        assertCauseMatchesFirstMessage(e, oneMessage, twoMessage);
+      } else if (e.getCause() == twoException) {
+        assertCauseMatchesFirstMessage(e, twoMessage, oneMessage);
+      } else {
+        fail("Expected one of the causes and its message");
+      }
+    }
+  }
+
+ private void assertCauseMatchesFirstMessage(UmbrellaException e, String firstMessage,
+      String otherMessage) {
+    assertTrue("Cause should be first message", e.getMessage().startsWith(
+        2 + UmbrellaException.MULTIPLE + firstMessage));
+ assertTrue("Should also see the other message", e.getMessage().contains(otherMessage));
+  }
+}
=======================================
--- /trunk/user/src/com/google/web/bindery/event/shared/UmbrellaException.java Mon Apr 18 16:25:25 2011 +++ /trunk/user/src/com/google/web/bindery/event/shared/UmbrellaException.java Fri Aug 26 08:45:52 2011
@@ -16,6 +16,7 @@
 package com.google.web.bindery.event.shared;

 import java.util.Collections;
+import java.util.Iterator;
 import java.util.Set;

 /**
@@ -26,15 +27,48 @@
  */
 public class UmbrellaException extends RuntimeException {

-  private static final String MSG =
- "One or more exceptions caught, see full set in UmbrellaException#getCauses";
+  // Visible for testing
+  static final String MULTIPLE = " exceptions caught: ";
+
+  // Visible for testing
+  static final String ONE = "Exception caught: ";
+
+  protected static Throwable makeCause(Set<Throwable> causes) {
+    Iterator<Throwable> iterator = causes.iterator();
+    if (!iterator.hasNext()) {
+      return null;
+    }
+
+    return iterator.next();
+  }
+
+  protected static String makeMessage(Set<Throwable> causes) {
+    int count = causes.size();
+    if (count == 0) {
+      return null;
+    }
+
+ StringBuilder b = new StringBuilder(count == 1 ? ONE : count + MULTIPLE);
+    boolean first = true;
+    for (Throwable t : causes) {
+      if (first) {
+        first = false;
+      } else {
+        b.append("; ");
+      }
+      b.append(t.getMessage());
+    }
+
+    return b.toString();
+  }
+
   /**
    * The causes of the exception.
    */
   private Set<Throwable> causes;

   public UmbrellaException(Set<Throwable> causes) {
- super(MSG, causes.size() == 0 ? null : causes.toArray(new Throwable[0])[0]);
+    super(makeMessage(causes), makeCause(causes));
     this.causes = causes;
   }

@@ -43,7 +77,7 @@
    */
   protected UmbrellaException() {
     // Can't delegate to the other constructor or GWT RPC gets cranky
-    super(MSG);
+    super(MULTIPLE);
     this.causes = Collections.<Throwable> emptySet();
   }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to