RestExceptionHandlerRegistry { public Optional<ExceptionHandlerBinding> findBindingFor(Class<? extends Throwable> exceptionClass) { if (exceptionHandlerBindings == null || exceptionHandlerBindings.isEmpty()) { return Optional.empty(); } int currentDistance = Integer.MAX_VALUE; ExceptionHandlerBinding closestBinding = null; for (ExceptionHandlerBinding binding : exceptionHandlerBindings) { int tempDistance = getDistanceBetweenExceptions(exceptionClass, binding.getExceptionClass()); if (tempDistance < currentDistance) { currentDistance = tempDistance; closestBinding = binding; if (currentDistance == 0) { break; } } } return Optional.ofNullable(closestBinding); } RestExceptionHandlerRegistry(Set<ExceptionHandlerBinding> bindings); Optional<ExceptionHandlerBinding> findBindingFor(Class<? extends Throwable> exceptionClass); static final String BEAN_NAME; }
@Test public void shouldReturnEmpty_IfBindingsNull() throws Exception { registry = new RestExceptionHandlerRegistry(null); Optional<ExceptionHandlerBinding> binding = registry.findBindingFor(IllegalStateException.class); assertThat(binding).isEmpty(); }
@Test public void shouldReturnEmpty_IfBindingsEmpty() throws Exception { registry = new RestExceptionHandlerRegistry(Sets.newHashSet()); Optional<ExceptionHandlerBinding> binding = registry.findBindingFor(IllegalStateException.class); assertThat(binding).isEmpty(); }
@Test public void shouldReturnEmpty_IfBindingNotFound() throws Exception { ExceptionHandlerBinding binding = ExceptionHandlerBinding.fromAnnotatedClass(IllegalStateExceptionHandler.class); registry = new RestExceptionHandlerRegistry(Sets.newHashSet(binding)); Optional<ExceptionHandlerBinding> foundBinding = registry.findBindingFor(IllegalArgumentException.class); assertThat(foundBinding).isEmpty(); }
@Test public void shouldReturnDirectMatchBinding() throws Exception { ExceptionHandlerBinding binding = ExceptionHandlerBinding.fromAnnotatedClass(IllegalStateExceptionHandler.class); registry = new RestExceptionHandlerRegistry(Sets.newHashSet(binding)); Optional<ExceptionHandlerBinding> foundBinding = registry.findBindingFor(IllegalStateException.class); assertThat(foundBinding).isPresent(); }
@Test public void shouldReturnDescendantMatchBinding() throws Exception { ExceptionHandlerBinding binding = ExceptionHandlerBinding.fromAnnotatedClass(IllegalStateExceptionHandler.class); registry = new RestExceptionHandlerRegistry(Sets.newHashSet(binding)); Optional<ExceptionHandlerBinding> foundBinding = registry.findBindingFor(IllegalComponentStateException.class); assertThat(foundBinding).isPresent(); }
@Test public void shouldReturnCloserMatchBinding() throws Exception { ExceptionHandlerBinding illegalStateBinding = ExceptionHandlerBinding.fromAnnotatedClass(IllegalStateExceptionHandler.class); ExceptionHandlerBinding runtimeBinding = ExceptionHandlerBinding.fromAnnotatedClass(RuntimeExceptionHandler.class); registry = new RestExceptionHandlerRegistry(Sets.newHashSet(illegalStateBinding, runtimeBinding)); Optional<ExceptionHandlerBinding> foundBinding = registry.findBindingFor(IllegalComponentStateException.class); assertThat(foundBinding) .isPresent() .contains(illegalStateBinding); }