Skip to content

Commit

Permalink
Harmonize NoUniqueBeanDefinitionException message
Browse files Browse the repository at this point in the history
This commit makes sure that the programmatic exception that is thrown
by the cache abstraction uses the same message structure as a default
message produced by NoUniqueBeanDefinitionException.

Closes gh-33305
  • Loading branch information
snicoll committed Aug 2, 2024
1 parent 29dce74 commit 0a2611b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ void multipleCacheManagerBeans() {
}
catch (NoUniqueBeanDefinitionException ex) {
assertThat(ex.getMessage()).contains(
"no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]");
"no CacheResolver specified and expected single matching CacheManager but found 2: cm1,cm2");
assertThat(ex.getNumberOfBeansFound()).isEqualTo(2);
assertThat(ex.getBeanNamesFound()).containsExactly("cm1", "cm2");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -29,6 +29,7 @@
* multiple matching candidates have been found when only one matching bean was expected.
*
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.2.1
* @see BeanFactory#getBean(Class)
*/
Expand All @@ -41,6 +42,19 @@ public class NoUniqueBeanDefinitionException extends NoSuchBeanDefinitionExcepti
private final Collection<String> beanNamesFound;


/**
* Create a new {@code NoUniqueBeanDefinitionException}.
* @param type required type of the non-unique bean
* @param beanNamesFound the names of all matching beans (as a Collection)
* @param message detailed message describing the problem
* @since 6.2
*/
public NoUniqueBeanDefinitionException(Class<?> type, Collection<String> beanNamesFound, String message) {
super(type, message);
this.numberOfBeansFound = beanNamesFound.size();
this.beanNamesFound = new ArrayList<>(beanNamesFound);
}

/**
* Create a new {@code NoUniqueBeanDefinitionException}.
* @param type required type of the non-unique bean
Expand All @@ -59,10 +73,8 @@ public NoUniqueBeanDefinitionException(Class<?> type, int numberOfBeansFound, St
* @param beanNamesFound the names of all matching beans (as a Collection)
*/
public NoUniqueBeanDefinitionException(Class<?> type, Collection<String> beanNamesFound) {
super(type, "expected single matching bean but found " + beanNamesFound.size() + ": " +
this(type, beanNamesFound, "expected single matching bean but found " + beanNamesFound.size() + ": " +
StringUtils.collectionToCommaDelimitedString(beanNamesFound));
this.numberOfBeansFound = beanNamesFound.size();
this.beanNamesFound = new ArrayList<>(beanNamesFound);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,22 @@ public void afterSingletonsInstantiated() {
setCacheManager(this.beanFactory.getBean(CacheManager.class));
}
catch (NoUniqueBeanDefinitionException ex) {
StringBuilder message = new StringBuilder("no CacheResolver specified and expected a single CacheManager bean, but found ");
message.append(ex.getNumberOfBeansFound());
if (ex.getBeanNamesFound() != null) {
message.append(": [").append(StringUtils.collectionToCommaDelimitedString(ex.getBeanNamesFound())).append("]");
int numberOfBeansFound = ex.getNumberOfBeansFound();
Collection<String> beanNamesFound = ex.getBeanNamesFound();

StringBuilder message = new StringBuilder("no CacheResolver specified and expected single matching CacheManager but found ");
message.append(numberOfBeansFound);
if (beanNamesFound != null) {
message.append(": ").append(StringUtils.collectionToCommaDelimitedString(beanNamesFound));
}
String exceptionMessage = message.toString();

if (beanNamesFound != null) {
throw new NoUniqueBeanDefinitionException(CacheManager.class, beanNamesFound, exceptionMessage);
}
else {
throw new NoUniqueBeanDefinitionException(CacheManager.class, numberOfBeansFound, exceptionMessage);
}
message.append(" - mark one as primary or declare a specific CacheManager to use.");
throw new NoUniqueBeanDefinitionException(CacheManager.class, ex.getNumberOfBeansFound(), message.toString());
}
catch (NoSuchBeanDefinitionException ex) {
throw new NoSuchBeanDefinitionException(CacheManager.class, "no CacheResolver specified - "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ void multipleCacheManagerBeans() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(MultiCacheManagerConfig.class);
assertThatThrownBy(ctx::refresh)
.isInstanceOf(NoUniqueBeanDefinitionException.class)
.hasMessageContaining("no CacheResolver specified and expected a single CacheManager bean, but found 2: [cm1,cm2]")
.hasNoCause();
.isInstanceOfSatisfying(NoUniqueBeanDefinitionException.class, ex -> {
assertThat(ex.getMessage()).contains(
"no CacheResolver specified and expected single matching CacheManager but found 2: cm1,cm2");
assertThat(ex.getNumberOfBeansFound()).isEqualTo(2);
assertThat(ex.getBeanNamesFound()).containsExactly("cm1", "cm2");
}).hasNoCause();
}

@Test
Expand Down

0 comments on commit 0a2611b

Please sign in to comment.