From 8bf019b6750546460d7c7de37b31d14985af8ef3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 21 Jul 2011 07:15:26 +0000 Subject: [PATCH] fixed @ExceptionHandler exception type matching (ExceptionDepthComparator; SPR-8231) --- .../core/ExceptionDepthComparator.java | 4 +- .../core/ExceptionDepthComparatorTests.java | 105 ++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 org.springframework.core/src/test/java/org/springframework/core/ExceptionDepthComparatorTests.java diff --git a/org.springframework.core/src/main/java/org/springframework/core/ExceptionDepthComparator.java b/org.springframework.core/src/main/java/org/springframework/core/ExceptionDepthComparator.java index 2b8bff535e..c856b86a03 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/ExceptionDepthComparator.java +++ b/org.springframework.core/src/main/java/org/springframework/core/ExceptionDepthComparator.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -68,7 +68,7 @@ public class ExceptionDepthComparator implements Comparator foundClass = findClosestMatch(TargetException.class, SameDepthException.class); + assertEquals(TargetException.class, foundClass); + } + + @Test + public void sameDepthBeforeTarget() throws Exception { + Class foundClass = findClosestMatch(SameDepthException.class, TargetException.class); + assertEquals(TargetException.class, foundClass); + } + + @Test + public void lowestDepthBeforeTarget() throws Exception { + Class foundClass = findClosestMatch(LowestDepthException.class, TargetException.class); + assertEquals(TargetException.class, foundClass); + } + + @Test + public void targetBeforeLowestDepth() throws Exception { + Class foundClass = findClosestMatch(TargetException.class, LowestDepthException.class); + assertEquals(TargetException.class, foundClass); + } + + @Test + public void noDepthBeforeTarget() throws Exception { + Class foundClass = findClosestMatch(NoDepthException.class, TargetException.class); + assertEquals(TargetException.class, foundClass); + } + + @Test + public void noDepthBeforeHighestDepth() throws Exception { + Class foundClass = findClosestMatch(NoDepthException.class, HighestDepthException.class); + assertEquals(HighestDepthException.class, foundClass); + } + + @Test + public void highestDepthBeforeNoDepth() throws Exception { + Class foundClass = findClosestMatch(HighestDepthException.class, NoDepthException.class); + assertEquals(HighestDepthException.class, foundClass); + } + + @Test + public void highestDepthBeforeLowestDepth() throws Exception { + Class foundClass = findClosestMatch(HighestDepthException.class, LowestDepthException.class); + assertEquals(LowestDepthException.class, foundClass); + } + + @Test + public void lowestDepthBeforeHighestDepth() throws Exception { + Class foundClass = findClosestMatch(LowestDepthException.class, HighestDepthException.class); + assertEquals(LowestDepthException.class, foundClass); + } + + private Class findClosestMatch(Class... classes) { + return ExceptionDepthComparator.findClosestMatch(Arrays.asList(classes), new TargetException()); + } + + + public class HighestDepthException extends Throwable { + } + + public class LowestDepthException extends HighestDepthException { + } + + public class TargetException extends LowestDepthException { + } + + public class SameDepthException extends LowestDepthException { + } + + public class NoDepthException extends TargetException { + } + +}