Skip to content

Commit

Permalink
Update Weld parent, apply formatter/impsort changes
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotn committed Nov 1, 2023
1 parent 22a0231 commit 6e431d7
Show file tree
Hide file tree
Showing 140 changed files with 975 additions and 829 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/.settings/
/.idea
*.iml
.cache

Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Set;
import java.util.function.Function;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.annotation.Resource;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Dependent;
Expand All @@ -50,6 +49,8 @@
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.inject.WeldInstance;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
*
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
Expand Down Expand Up @@ -89,8 +90,10 @@ public static Weld createWeld() {

protected volatile WeldContainer container;

protected AbstractWeldInitiator(Weld weld, List<Object> instancesToInject, Set<Class<? extends Annotation>> scopesToActivate, Set<Bean<?>> beans,
Map<String, Object> resources, Function<InjectionPoint, Object> ejbFactory, Function<InjectionPoint, Object> persistenceUnitFactory,
protected AbstractWeldInitiator(Weld weld, List<Object> instancesToInject,
Set<Class<? extends Annotation>> scopesToActivate, Set<Bean<?>> beans,
Map<String, Object> resources, Function<InjectionPoint, Object> ejbFactory,
Function<InjectionPoint, Object> persistenceUnitFactory,
Function<InjectionPoint, Object> persistenceContextFactory) {
this.instancesToInject = new ArrayList<>();
for (Object instance : instancesToInject) {
Expand Down Expand Up @@ -141,7 +144,9 @@ protected ToInject createToInject(Object instanceToInject) {
* Injects the given non-contextual instance immediately. The returned {@link AutoCloseable} should be used
* to release the creational context once the injected beans are no longer needed.
*
* <p>Example:
* <p>
* Example:
*
* <pre>{@code
* try (AutoCloseable contextReleaser = injectNonContextual(this)) {
* // do some things with the injected instances
Expand Down Expand Up @@ -229,7 +234,8 @@ public Event<Object> event() {
// We need to use reflection due to some compatibility issues
Method eventMethod = container.getClass().getMethod("event");
return (Event<Object>) eventMethod.invoke(container);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
throw new IllegalStateException("Cannot invoke WeldContainer.event() method using reflection", e);
}
}
Expand All @@ -255,7 +261,8 @@ public void shutdown() {

/**
*
* @return <code>true</code> if the container was initialized completely and is not shut down yet, <code>false</code> otherwise
* @return <code>true</code> if the container was initialized completely and is not shut down yet, <code>false</code>
* otherwise
*/
public boolean isRunning() {
return (container != null) && container.isRunning();
Expand Down Expand Up @@ -316,7 +323,8 @@ void inject() {
CreationalContext<Object> ctx = beanManager.createCreationalContext(null);
@SuppressWarnings("unchecked")
InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) beanManager
.getInjectionTargetFactory(beanManager.createAnnotatedType(instance.getClass())).createInjectionTarget(null);
.getInjectionTargetFactory(beanManager.createAnnotatedType(instance.getClass()))
.createInjectionTarget(null);
injectionTarget.inject(instance, ctx);
creationalContext = ctx;
}
Expand Down Expand Up @@ -357,7 +365,8 @@ public AbstractBuilder(Weld weld) {
}

/**
* Activate and deactivate contexts for the given normal scopes for the lifetime of the initialized Weld container, by default for each test method
* Activate and deactivate contexts for the given normal scopes for the lifetime of the initialized Weld container, by
* default for each test method
* execution.
* <p>
* {@link ApplicationScoped} is ignored as it is always active.
Expand Down Expand Up @@ -393,7 +402,8 @@ protected Function<InjectionPoint, Object> getPersistenceUnitFactory() {
}

/**
* Instructs the initiator to inject the given non-contextual instance once the container is started, i.e. during test execution.
* Instructs the initiator to inject the given non-contextual instance once the container is started, i.e. during test
* execution.
*
* <p>
* This method could be used e.g. to inject a test class instance:
Expand All @@ -416,7 +426,8 @@ protected Function<InjectionPoint, Object> getPersistenceUnitFactory() {
* </pre>
*
* <p>
* Injected {@link Dependent} bean instances are destroyed after the test execution. However, the lifecycle of the non-contextual instance is not
* Injected {@link Dependent} bean instances are destroyed after the test execution. However, the lifecycle of the
* non-contextual instance is not
* managed by the container and all injected references will be invalid after the test execution.
* </p>
*
Expand Down Expand Up @@ -507,15 +518,17 @@ public T setPersistenceContextFactory(Function<InjectionPoint, Object> persisten

protected abstract T self();

protected abstract I build(Weld weld, List<Object> instancesToInject, Set<Class<? extends Annotation>> scopesToActivate, Set<Bean<?>> beans);
protected abstract I build(Weld weld, List<Object> instancesToInject, Set<Class<? extends Annotation>> scopesToActivate,
Set<Bean<?>> beans);

/**
*
* @return a new initiator instance
*/
public I build() {
return build(weld, instancesToInject.isEmpty() ? Collections.emptyList() : new ArrayList<>(instancesToInject),
scopesToActivate.isEmpty() ? Collections.<Class<? extends Annotation>> emptySet() : new HashSet<>(scopesToActivate),
scopesToActivate.isEmpty() ? Collections.<Class<? extends Annotation>> emptySet()
: new HashSet<>(scopesToActivate),
beans.isEmpty() ? Collections.<Bean<?>> emptySet() : new HashSet<>(beans));
}

Expand Down
50 changes: 33 additions & 17 deletions junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* @author Martin Kouba
*
* @param <T>
* See also {@code WeldInitiator.Builder#addBean(Bean)} method.
* See also {@code WeldInitiator.Builder#addBean(Bean)} method.
* @since 1.1
*/
public class MockBean<T> implements Bean<T>, PassivationCapable {
Expand All @@ -76,7 +76,8 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
* </ul>
*
* <p>
* Note that {@link Builder#creating(Object)} or {@link Builder#create(CreateFunction)} must be always set. Otherwise, an {@link IllegalStateException} is
* Note that {@link Builder#creating(Object)} or {@link Builder#create(CreateFunction)} must be always set. Otherwise, an
* {@link IllegalStateException} is
* thrown during {@link Builder#build()} invocation.
* <p>
*
Expand All @@ -87,7 +88,8 @@ public static <T> Builder<T> builder() {
}

/**
* A convenient method to create a {@link Bean} with default values (see also {@link #builder()}). Additionaly, the specified bean types are added to the
* A convenient method to create a {@link Bean} with default values (see also {@link #builder()}). Additionaly, the
* specified bean types are added to the
* set of bean types and {@link Bean#create(CreationalContext)} will always return the specified bean instance.
*
* @param beanInstance
Expand All @@ -101,10 +103,12 @@ public static <T> Bean<T> of(T beanInstance, Type... beanTypes) {
/**
* A convenient method to create a {@link Builder} initialized from the specified bean class.
* <p>
* Note that the container may not be started yet and so it is not possible to use CDI SPI. Instead, we try to simulate the default bean discovery.
* Note that the container may not be started yet and so it is not possible to use CDI SPI. Instead, we try to simulate the
* default bean discovery.
* </p>
* <p>
* By default, {@link Unmanaged} is used to create/destroy the bean instance. However, it is possible to override this behavior.
* By default, {@link Unmanaged} is used to create/destroy the bean instance. However, it is possible to override this
* behavior.
* </p>
*
* @param beanClass
Expand Down Expand Up @@ -138,7 +142,8 @@ public static <T> Builder<T> read(Class<T> beanClass) {

private final Class<?> beanClass;

protected MockBean(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean selectForSyntheticBeanArchive, String name,
protected MockBean(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative,
boolean selectForSyntheticBeanArchive, String name,
Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope, CreateFunction<T> createCallback,
DestroyFunction<T> destroyCallback) {
this.beanClass = beanClass;
Expand All @@ -151,7 +156,8 @@ protected MockBean(Class<?> beanClass, Set<Class<? extends Annotation>> stereoty
this.scope = scope;
this.createCallback = createCallback;
this.destroyCallback = destroyCallback;
this.id = new StringBuilder().append(MockBean.class.getName()).append("_").append(SEQUENCE.incrementAndGet()).toString();
this.id = new StringBuilder().append(MockBean.class.getName()).append("_").append(SEQUENCE.incrementAndGet())
.toString();
}

@Override
Expand Down Expand Up @@ -207,7 +213,8 @@ public boolean isAlternative() {
}

/**
* Initiator should use {@link #getBeanClass()} to select the alternative. Also the alternative should only be selected if {@link #isAlternative()} also
* Initiator should use {@link #getBeanClass()} to select the alternative. Also the alternative should only be selected if
* {@link #isAlternative()} also
* returns <code>true</code>.
*
* @return <code>true</code> if the initiator should select the bean for the synthetic bean archive
Expand Down Expand Up @@ -252,7 +259,8 @@ private static <T> Builder<T> readInternal(Class<T> beanClass) {
}
if (!scopes.isEmpty()) {
if (scopes.size() > 1) {
throw new IllegalStateException("At most one scope may be specifie [beanClass: " + beanClass + ", scopes: " + scopes + "]");
throw new IllegalStateException(
"At most one scope may be specifie [beanClass: " + beanClass + ", scopes: " + scopes + "]");
}
builder.scope(scopes.iterator().next().annotationType());
}
Expand Down Expand Up @@ -292,7 +300,8 @@ private static Set<Annotation> getStereotypes(AnnotatedElement element) {
private static Set<Annotation> getScopes(AnnotatedElement element) {
Set<Annotation> scopes = new HashSet<>();
for (Annotation annotation : element.getAnnotations()) {
if (annotation.annotationType().isAnnotationPresent(Scope.class) || annotation.annotationType().isAnnotationPresent(NormalScope.class)) {
if (annotation.annotationType().isAnnotationPresent(Scope.class)
|| annotation.annotationType().isAnnotationPresent(NormalScope.class)) {
scopes.add(annotation);
}
}
Expand Down Expand Up @@ -477,8 +486,10 @@ public Builder<T> globallySelectedAlternative(int priority) {
* The bean is an alternative and should be automatically selected for the synthetic bean archive.
*
* <p>
* Users are encouraged to specify {@link #beanClass(Class)} when using this method. The bean class is used to determine which alternative beans are
* selected for a bean archive. By default, all mock beans share the same bean class - {@code org.jboss.weld.junit.WeldCDIExtension}.
* Users are encouraged to specify {@link #beanClass(Class)} when using this method. The bean class is used to determine
* which alternative beans are
* selected for a bean archive. By default, all mock beans share the same bean class -
* {@code org.jboss.weld.junit.WeldCDIExtension}.
* </p>
*
* @return self
Expand All @@ -493,7 +504,8 @@ public Builder<T> selectedAlternative() {
}

/**
* The bean has the given bean class, is an alternative and should be automatically selected for the synthetic bean archive.
* The bean has the given bean class, is an alternative and should be automatically selected for the synthetic bean
* archive.
*
* @param beanClass
* @return self
Expand Down Expand Up @@ -544,7 +556,8 @@ public Builder<T> creating(final T instance) {
* Use {@link Unmanaged} to create/destroy the bean instance.
*
* <p>
* NOTE: {@link CreationalContext#toString()} is used as a key in a map and therefore must be unique for the lifetime of a bean instance. Weld
* NOTE: {@link CreationalContext#toString()} is used as a key in a map and therefore must be unique for the lifetime of
* a bean instance. Weld
* implementation fulfills this requirement.
* </p>
*
Expand All @@ -565,7 +578,8 @@ public Builder<T> useUnmanaged(Class<T> beanClass) {
if (unmanagedInstance != null) {
if (!unmanagedInstance.get().equals(o)) {
throw new IllegalStateException(
"Unmanaged instance [" + unmanagedInstance.get() + "] is not equal to the bean instance to be destroyed: " + o);
"Unmanaged instance [" + unmanagedInstance.get()
+ "] is not equal to the bean instance to be destroyed: " + o);
}
unmanagedInstance.preDestroy().dispose();
}
Expand Down Expand Up @@ -622,10 +636,12 @@ public MockBean<T> build() {
}
// if given any priority, we will instead initialize MockBeanWithPriority
if (priority != null) {
return new MockBeanWithPriority<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, priority, name, normalizedQualfiers, types, scope, createCallback,
return new MockBeanWithPriority<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, priority,
name, normalizedQualfiers, types, scope, createCallback,
destroyCallback);
} else {
return new MockBean<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, normalizedQualfiers, types, scope, createCallback,
return new MockBean<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name,
normalizedQualfiers, types, scope, createCallback,
destroyCallback);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package org.jboss.weld.junit;

import jakarta.enterprise.inject.spi.Prioritized;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Set;

import jakarta.enterprise.inject.spi.Prioritized;

/**
* A subclass of {@link MockBean} implementing {@link Prioritized} hence allowing for globally enabled alternatives.
* Used instead of {@link MockBean} if user specified {@link MockBean.Builder#priority(int)}.
Expand All @@ -16,10 +17,11 @@ class MockBeanWithPriority<T> extends MockBean<T> implements Prioritized {
private final int priority;

MockBeanWithPriority(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative,
boolean selectForSyntheticBeanArchive, int priority, String name,
Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope,
CreateFunction<T> createCallback, DestroyFunction<T> destroyCallback) {
super(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, qualifiers, types, scope, createCallback, destroyCallback);
boolean selectForSyntheticBeanArchive, int priority, String name,
Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope,
CreateFunction<T> createCallback, DestroyFunction<T> destroyCallback) {
super(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, qualifiers, types, scope,
createCallback, destroyCallback);
if (priority <= 0) {
throw new IllegalArgumentException("MockBean cannot have priority equal or lower than 0!");
}
Expand Down
Loading

0 comments on commit 6e431d7

Please sign in to comment.