From aad050ba479b1b0099cc105e1953ea837780219a Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 2 Oct 2013 11:17:39 -0400 Subject: [PATCH] AMQP-334 Fix Nested Cause Classification Consider Foo caused by Bar caused by Baz. If Bar is categorized TRUE and Baz categorized FALSE, classiy() should return TRUE (hit on Bar), but it returned FALSE. The early exit from the cause traversal was not taken because we were always testing against the top level throwable (Bar). Add a test to verify this scenario; test against the cause on each iteration through the loop. --- .../classify/BinaryExceptionClassifier.java | 2 +- .../BinaryExceptionClassifierTests.java | 47 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/classify/BinaryExceptionClassifier.java b/src/main/java/org/springframework/classify/BinaryExceptionClassifier.java index a326f76..2f374e4 100644 --- a/src/main/java/org/springframework/classify/BinaryExceptionClassifier.java +++ b/src/main/java/org/springframework/classify/BinaryExceptionClassifier.java @@ -110,7 +110,7 @@ public class BinaryExceptionClassifier extends SubclassClassifier> set = Collections + .> singleton(IllegalStateException.class); + BinaryExceptionClassifier binaryExceptionClassifier = new BinaryExceptionClassifier(set); + binaryExceptionClassifier.setTraverseCauses(true); + assertTrue(binaryExceptionClassifier.classify(new RuntimeException(new FooException("Foo")))); + } + + @Test + public void testClassifySubclassMatchInCauseFalse() { + Map, Boolean> map = new HashMap, Boolean>(); + map.put(IllegalStateException.class, true); + map.put(BarException.class, false); + BinaryExceptionClassifier binaryExceptionClassifier = new BinaryExceptionClassifier(map, true); + binaryExceptionClassifier.setTraverseCauses(true); + assertTrue(binaryExceptionClassifier.classify(new RuntimeException(new FooException("Foo", new BarException())))); + assertTrue(((Map) new DirectFieldAccessor(binaryExceptionClassifier).getPropertyValue("classified")) + .containsKey(FooException.class)); + } + @Test public void testTypesProvidedInConstructor() { classifier = new BinaryExceptionClassifier(Collections @@ -86,4 +111,26 @@ public class BinaryExceptionClassifierTests { classifier.setTraverseCauses(true); assertFalse(classifier.classify(new RuntimeException(new RuntimeException(new IllegalStateException("Foo"))))); } + + @SuppressWarnings("serial") + private class FooException extends IllegalStateException { + + private FooException(String s) { + super(s); + } + + private FooException(String s, Throwable t) { + super(s, t); + } + + } + + @SuppressWarnings("serial") + private class BarException extends RuntimeException { + + private BarException() { + super(); + } + + } }