Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce annotation for changing bean discovery mode in synthetic archive #159

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
}
Loading