Skip to content

Commit

Permalink
Adding method for simplified bean-resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Oct 10, 2023
1 parent ae00fce commit fdc09b7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ private static <T, V extends Annotation> T getCDIBean(final BeanManager beanMana
}

private static List<Bean<?>> sortByPriority(final List<Bean<?>> toBeSorted) {
if (toBeSorted.size() < 2) {
if (toBeSorted.size() < 2)
return toBeSorted;
}
final List<PriorityComparator> wrapperList = new ArrayList<>();
for (final Bean<?> element : toBeSorted) {
wrapperList.add(new PriorityComparator(element));
Expand Down Expand Up @@ -120,6 +119,20 @@ public static <T, V extends Annotation> T resolveBeanOrThrowIllegalStateExceptio
() -> new IllegalStateException("Portal-532: " + createErrorMessage(beanClass, annotationClass)));
}

/**
* Shorthand for calling
* {@link #resolveBeanOrThrowIllegalStateException(Class, Class)} with
* {@code null} for the parameter annotation.
*
* @param beanClass identifying the type to be loaded, must not be null
* @param <T>
*
* @return the found bean, or {@link Optional#empty()} if none could be found
*/
public static <T> T resolveRequiredBean(final Class<T> beanClass) {
return resolveBeanOrThrowIllegalStateException(beanClass, null);
}

/**
* Helper method for resolving the beanTypes according to the given identifier.
*
Expand Down Expand Up @@ -155,9 +168,8 @@ public static <T, V extends Annotation> Set<Bean<?>> resolveBeanTypes(final Bean
*/
private static <T, V extends Annotation> void checkBeanTypesFound(final Class<T> beanClass,
final Class<V> annotationClass, final Set<Bean<?>> beanTypes) {
if (beanTypes.isEmpty()) {
if (beanTypes.isEmpty())
throw new IllegalArgumentException(createErrorMessage(beanClass, annotationClass));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static de.cuioss.portal.core.cdi.PortalBeanManager.resolveBean;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -78,4 +79,11 @@ void shouldThrowExceptionOnZeroBeansFound() {
});
}

@Test
void shouldReturnSimpleRequiredWithoutQualifier() {
final var check = PortalBeanManager.resolveRequiredBean(TestBeanWithoutQualifier.class);
assertNotNull(check);
assertEquals(TestBeanWithQualifier.MESSAGE, check.getInitMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
* <p>
* Additional configuration can be added by using {@link #configuration()}
* </p>
* <p>
* For details on usage, see {@link PortalTestConfiguration}
* </p>
*
* @author Oliver Wolff
*/
Expand Down

0 comments on commit fdc09b7

Please sign in to comment.