Skip to content

Commit

Permalink
Introduce annotation for changing bean discovery mode in synthetic ar…
Browse files Browse the repository at this point in the history
…chive
  • Loading branch information
manovotn committed Nov 1, 2023
1 parent 91b7b3d commit 22a0231
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
10 changes: 10 additions & 0 deletions junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static void scanForRequiredBeanClasses(List<Class<?>> testClasses, Weld weld, bo
Set<Class<?>> foundClasses = new HashSet<>();
Set<Type> excludedBeanTypes = new HashSet<>();
Set<Class<?>> excludedBeanClasses = new HashSet<>();
boolean syntheticArchiveDiscoverySet = false;

while (!classesToProcess.isEmpty()) {

Expand Down Expand Up @@ -198,6 +199,15 @@ static void scanForRequiredBeanClasses(List<Class<?>> testClasses, Weld weld, bo
.distinct()
.forEach(excludedBeanClasses::add);

// discovery mode can only be set once; we use the first annotation we find
if (!syntheticArchiveDiscoverySet) {
Optional<SetBeanDiscoveryMode> annotation = AnnotationSupport.findAnnotation(currClass, SetBeanDiscoveryMode.class);
if (annotation.isPresent()) {
syntheticArchiveDiscoverySet = true;
weld.setBeanDiscoveryMode(annotation.get().value());
}
}

}

for (Class<?> foundClass : foundClasses) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jboss.weld.junit5.auto;

import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Sets discovery mode for Weld SE synthetic bean archive. Valid options are {@link BeanDiscoveryMode#ALL} and
* {@link BeanDiscoveryMode#ANNOTATED}.
* <p>
* Starting with Weld 5 (CDI 4), the default value is {@code ANNOTATED}. Applications can leverage this annotation to
* enforce same behavior as older Weld versions where synthetic archives used discovery mode {@code ALL}.
* <p>
* This annotation is equal to invocation of {@link org.jboss.weld.environment.se.Weld#setBeanDiscoveryMode(BeanDiscoveryMode)}.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface SetBeanDiscoveryMode {

BeanDiscoveryMode value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
* @see ExcludeBean
* @see ExcludeBeanClasses
* @see EnableAutoWeld
* @see SetBeanDiscoveryMode
* @see WeldJunitEnricher
*/
public class WeldJunit5AutoExtension extends WeldJunit5Extension {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.jboss.weld.junit5.auto;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.junit5.auto.discovery.WithBeanDefiningAnnotation;
import org.jboss.weld.junit5.auto.discovery.WithoutBeanDefiningAnnotation;
import org.junit.jupiter.api.Test;

@EnableAutoWeld
@AddPackages(value = WithBeanDefiningAnnotation.class, recursively = false)
@SetBeanDiscoveryMode(BeanDiscoveryMode.ALL)
public class SetDiscoveryModeAllTest {

@Inject
private WithBeanDefiningAnnotation standardBean;

@Inject
Instance<Object> instance;

@Test
void testDiscoveryModeWasChanged() {
// bean with bean defining annotation is normally resolvable
assertNotNull(standardBean);
// bean without bean defining annotation is discovered as well because we set discovery to ALL
assertTrue(instance.select(WithoutBeanDefiningAnnotation.class).isResolvable());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jboss.weld.junit5.auto.discovery;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class WithBeanDefiningAnnotation {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jboss.weld.junit5.auto.discovery;

// intentionally doesn't have any bean defining annotation
public class WithoutBeanDefiningAnnotation {
}

0 comments on commit 22a0231

Please sign in to comment.