Skip to content

Commit

Permalink
Swap master to CDI 2.0/Weld 3.1.
Browse files Browse the repository at this point in the history
* Swap to Weld 3, remove profiles, bump project version to 2.0.0-SNAPSHOT.
* Enhance MockBean.Builder, introduce MockBeanWithPriority, add tests.
* Simplify Travis matrix.
* Remove Literals which are now present in CDI API, simplify mocked services.
  • Loading branch information
manovotn authored Apr 11, 2019
1 parent 7edc3d9 commit 6098043
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 206 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ language: java
jdk:
- oraclejdk8
- openjdk8
env:
- WELD_JUNIT_PROFILE=weld2
- WELD_JUNIT_PROFILE=weld3
script: "WELD_JUNIT_DEBUG=spotbugs mvn verify -Dspotbugs.failOnError=true"
sudo: false
2 changes: 1 addition & 1 deletion junit-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-junit-parent</artifactId>
<version>1.3.2-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
</parent>

<artifactId>weld-junit-common</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.logging.Logger;

import javax.enterprise.context.ContextNotActiveException;
import javax.enterprise.context.Destroyed;
import javax.enterprise.context.Initialized;
import javax.enterprise.context.spi.Context;
import javax.enterprise.context.spi.Contextual;
import javax.enterprise.context.spi.CreationalContext;
Expand Down Expand Up @@ -88,7 +90,7 @@ public void destroy(Contextual<?> contextual) {

public void activate() {
currentContext.set(new HashMap<Contextual<?>, ContextualInstance<?>>());
beanManager.fireEvent(new Object(), InitializedLiteral.of(scope));
beanManager.fireEvent(new Object(), Initialized.Literal.of(scope));
}

public void deactivate() {
Expand All @@ -105,7 +107,7 @@ public void deactivate() {
}
ctx.clear();
currentContext.remove();
beanManager.fireEvent(new Object(), DestroyedLiteral.of(scope));
beanManager.fireEvent(new Object(), Destroyed.Literal.of(scope));
}

/**
Expand Down

This file was deleted.

This file was deleted.

45 changes: 40 additions & 5 deletions junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.InjectionPoint;
import javax.enterprise.inject.spi.PassivationCapable;
import javax.enterprise.inject.spi.Prioritized;
import javax.enterprise.inject.spi.Unmanaged;
import javax.enterprise.inject.spi.Unmanaged.UnmanagedInstance;
import javax.enterprise.util.AnnotationLiteral;
Expand Down Expand Up @@ -136,7 +137,7 @@ public static <T> Builder<T> read(Class<T> beanClass) {

private final Class<?> beanClass;

private 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 Down Expand Up @@ -325,6 +326,8 @@ public static class Builder<T> {

private boolean selectForSyntheticBeanArchive;

private Integer priority;

private String name;

private Set<Annotation> qualifiers;
Expand All @@ -346,6 +349,7 @@ private Builder() {
this.types = new HashSet<>();
this.types.add(Object.class);
this.beanClass = WeldCDIExtension.class;
this.priority = null;
}

/**
Expand Down Expand Up @@ -447,6 +451,32 @@ public Builder<T> alternative(boolean value) {
return this;
}

/**
* Programmatic equivalent to to putting {@link javax.annotation.Priority} annotation on a bean class.
* Allows for globally enabled alternatives.
*
* @param priority
* @return self
* @see Prioritized#getPriority()
*/
public Builder<T> priority(int priority) {
this.priority = priority;
return this;
}

/**
* This beans is a globally enabled alternative with a priority equal to its method parameter.
* Calling this method is a shortcut for {@link Builder#priority(int)} and {@link Builder#alternative(boolean)}
*
* @param priority
* @return self
*/
public Builder<T> globallySelectedAlternative(int priority) {
this.priority = priority;
this.alternative = true;
return this;
}

/**
* The bean is an alternative and should be automatically selected for the synthetic bean archive.
*
Expand All @@ -455,7 +485,6 @@ public Builder<T> alternative(boolean value) {
* selected for a bean archive. By default, all mock beans share the same bean class - {@code org.jboss.weld.junit.WeldCDIExtension}.
* </p>
*
* @param value
* @return self
* @see Bean#isAlternative()
* @see Weld#addAlternative(Class)
Expand Down Expand Up @@ -496,7 +525,7 @@ public Builder<T> stereotypes(Class<? extends Annotation>... stereotypes) {

/**
*
* @param qualifier
* @param stereotype
* @return self
*/
public Builder<T> addStereotype(Class<? extends Annotation> stereotype) {
Expand Down Expand Up @@ -594,8 +623,14 @@ public MockBean<T> build() {
}
}
}
return new MockBean<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, qualifiers, types, scope, createCallback,
destroyCallback);
// if given any priority, we will instead initialize MockBeanWithPriority
if (priority != null) {
return new MockBeanWithPriority<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, priority, name, qualifiers, types, scope, createCallback,
destroyCallback);
} else {
return new MockBean<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, qualifiers, types, scope, createCallback,
destroyCallback);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.jboss.weld.junit;

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

/**
* 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)}.
*
* @author Matej Novotny
*/
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);
if (priority <= 0) {
throw new IllegalArgumentException("MockBean cannot have priority equal or lower than 0!");
}
this.priority = priority;
}

@Override
public int getPriority() {
return this.priority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ public ResourceReferenceFactory<Object> registerEjbInjectionPoint(InjectionPoint
return new ResourceReferenceFactory<Object>() {
@Override
public ResourceReference<Object> createResource() {
return new SimpleResourceReference<Object>(resolveEjb(injectionPoint));
return new SimpleResourceReference<Object>(ejbFactory.apply(injectionPoint));
}
};
}

// only for Weld 2
public Object resolveEjb(InjectionPoint injectionPoint) {
return ejbFactory.apply(injectionPoint);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ public ResourceReferenceFactory<EntityManager> registerPersistenceContextInjecti
return new ResourceReferenceFactory<EntityManager>() {
@Override
public ResourceReference<EntityManager> createResource() {
return new SimpleResourceReference<EntityManager>(resolvePersistenceContext(injectionPoint));
if (persistenceContextFactory == null) {
throw new IllegalStateException("Persistent context factory not set, cannot resolve injection point: " + injectionPoint);
}
Object context = persistenceContextFactory.apply(injectionPoint);
if (context == null || context instanceof EntityManager) {
return new SimpleResourceReference<EntityManager>((EntityManager) context);
}
throw new IllegalStateException("Not an EntityManager instance: " + context);
}
};
}
Expand All @@ -59,35 +66,18 @@ public ResourceReferenceFactory<EntityManagerFactory> registerPersistenceUnitInj
return new ResourceReferenceFactory<EntityManagerFactory>() {
@Override
public ResourceReference<EntityManagerFactory> createResource() {
return new SimpleResourceReference<EntityManagerFactory>(resolvePersistenceUnit(injectionPoint));
if (persistenceUnitFactory == null) {
throw new IllegalStateException("Persistent unit factory not set, cannot resolve injection point: " + injectionPoint);
}
Object unit = persistenceUnitFactory.apply(injectionPoint);
if (unit == null || unit instanceof EntityManagerFactory) {
return new SimpleResourceReference<EntityManagerFactory>((EntityManagerFactory) unit);
}
throw new IllegalStateException("Not an EntityManagerFactory instance: " + unit);
}
};
}

// only for Weld 2
public EntityManager resolvePersistenceContext(InjectionPoint injectionPoint) {
if (persistenceContextFactory == null) {
throw new IllegalStateException("Persistent context factory not set, cannot resolve injection point: " + injectionPoint);
}
Object context = persistenceContextFactory.apply(injectionPoint);
if (context == null || context instanceof EntityManager) {
return (EntityManager) context;
}
throw new IllegalStateException("Not an EntityManager instance: " + context);
}

// only for Weld 2
public EntityManagerFactory resolvePersistenceUnit(InjectionPoint injectionPoint) {
if (persistenceUnitFactory == null) {
throw new IllegalStateException("Persistent unit factory not set, cannot resolve injection point: " + injectionPoint);
}
Object unit = persistenceUnitFactory.apply(injectionPoint);
if (unit == null || unit instanceof EntityManagerFactory) {
return (EntityManagerFactory) unit;
}
throw new IllegalStateException("Not an EntityManagerFactory instance: " + unit);
}

@Override
public void cleanup() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public MockResourceInjectionServices(Map<String, Object> resources) {
this.resources = ImmutableMap.copyOf(resources);
}

// only for Weld 2
public Object resolveResource(InjectionPoint injectionPoint) {
private Object resolveResource(InjectionPoint injectionPoint) {
Resource resource = getResourceAnnotation(injectionPoint);
if (resource == null) {
throw new IllegalArgumentException("No @Resource annotation found on " + injectionPoint);
Expand Down Expand Up @@ -83,11 +82,6 @@ public ResourceReferenceFactory<Object> registerResourceInjectionPoint(String jn
throw new UnsupportedOperationException();
}

// only for Weld 2
public Object resolveResource(String jndiName, String mappedName) {
throw new UnsupportedOperationException();
}

@Override
public void cleanup() {
}
Expand Down
Loading

0 comments on commit 6098043

Please sign in to comment.