From 60980439707e6807974dbd0db098cd8dbe2d9633 Mon Sep 17 00:00:00 2001
From: Matej Novotny
Date: Thu, 11 Apr 2019 10:36:46 +0200
Subject: [PATCH] Swap master to CDI 2.0/Weld 3.1.
* 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.
---
.travis.yml | 3 -
junit-common/pom.xml | 2 +-
.../org/jboss/weld/junit/ContextImpl.java | 6 +-
.../jboss/weld/junit/DestroyedLiteral.java | 46 -------------
.../jboss/weld/junit/InitializedLiteral.java | 46 -------------
.../java/org/jboss/weld/junit/MockBean.java | 45 +++++++++++--
.../weld/junit/MockBeanWithPriority.java | 33 ++++++++++
.../weld/junit/MockEjbInjectionServices.java | 7 +-
.../weld/junit/MockJpaInjectionServices.java | 42 +++++-------
.../junit/MockResourceInjectionServices.java | 8 +--
junit4/pom.xml | 32 +++------
.../AddGloballyEnabledAlternativeTest.java | 63 ++++++++++++++++++
.../java/org/jboss/weld/junit4/bean/Bar.java | 23 +++++++
.../java/org/jboss/weld/junit4/bean/Foo.java | 7 ++
junit5/pom.xml | 32 +++------
.../AddGloballyEnabledAlternativeTest.java | 66 +++++++++++++++++++
.../java/org/jboss/weld/junit5/bean/Bar.java | 24 +++++++
.../java/org/jboss/weld/junit5/bean/Foo.java | 8 +++
pom.xml | 18 +----
19 files changed, 305 insertions(+), 206 deletions(-)
delete mode 100644 junit-common/src/main/java/org/jboss/weld/junit/DestroyedLiteral.java
delete mode 100644 junit-common/src/main/java/org/jboss/weld/junit/InitializedLiteral.java
create mode 100644 junit-common/src/main/java/org/jboss/weld/junit/MockBeanWithPriority.java
create mode 100644 junit4/src/test/java/org/jboss/weld/junit4/bean/AddGloballyEnabledAlternativeTest.java
create mode 100644 junit4/src/test/java/org/jboss/weld/junit4/bean/Bar.java
create mode 100644 junit4/src/test/java/org/jboss/weld/junit4/bean/Foo.java
create mode 100644 junit5/src/test/java/org/jboss/weld/junit5/bean/AddGloballyEnabledAlternativeTest.java
create mode 100644 junit5/src/test/java/org/jboss/weld/junit5/bean/Bar.java
create mode 100644 junit5/src/test/java/org/jboss/weld/junit5/bean/Foo.java
diff --git a/.travis.yml b/.travis.yml
index 01cc7e7a..e4bf80f4 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -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
diff --git a/junit-common/pom.xml b/junit-common/pom.xml
index f76f23d3..78bc2364 100644
--- a/junit-common/pom.xml
+++ b/junit-common/pom.xml
@@ -4,7 +4,7 @@
org.jboss.weld
weld-junit-parent
- 1.3.2-SNAPSHOT
+ 2.0.0-SNAPSHOT
weld-junit-common
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java b/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java
index 229d98a4..dd19df78 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java
@@ -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;
@@ -88,7 +90,7 @@ public void destroy(Contextual> contextual) {
public void activate() {
currentContext.set(new HashMap, ContextualInstance>>());
- beanManager.fireEvent(new Object(), InitializedLiteral.of(scope));
+ beanManager.fireEvent(new Object(), Initialized.Literal.of(scope));
}
public void deactivate() {
@@ -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));
}
/**
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/DestroyedLiteral.java b/junit-common/src/main/java/org/jboss/weld/junit/DestroyedLiteral.java
deleted file mode 100644
index a3fddc79..00000000
--- a/junit-common/src/main/java/org/jboss/weld/junit/DestroyedLiteral.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2018, Red Hat, Inc., and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jboss.weld.junit;
-
-import java.lang.annotation.Annotation;
-
-import javax.enterprise.context.Destroyed;
-import javax.enterprise.util.AnnotationLiteral;
-
-/**
- * Supports inline instantiation of the {@link Destroyed} qualifier.
- *
- * @author Martin Kouba
- */
-class DestroyedLiteral extends AnnotationLiteral implements Destroyed {
-
- private static final long serialVersionUID = 1L;
-
- private final Class extends Annotation> value;
-
- public static DestroyedLiteral of(Class extends Annotation> value) {
- return new DestroyedLiteral(value);
- }
-
- private DestroyedLiteral(Class extends Annotation> value) {
- this.value = value;
- }
-
- public Class extends Annotation> value() {
- return value;
- }
-}
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/InitializedLiteral.java b/junit-common/src/main/java/org/jboss/weld/junit/InitializedLiteral.java
deleted file mode 100644
index 7e9e7b59..00000000
--- a/junit-common/src/main/java/org/jboss/weld/junit/InitializedLiteral.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2018, Red Hat, Inc., and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jboss.weld.junit;
-
-import java.lang.annotation.Annotation;
-
-import javax.enterprise.context.Initialized;
-import javax.enterprise.util.AnnotationLiteral;
-
-/**
- * Supports inline instantiation of the {@link Initialized} qualifier.
- *
- * @author Martin Kouba
- */
-class InitializedLiteral extends AnnotationLiteral implements Initialized {
-
- private static final long serialVersionUID = 1L;
-
- private final Class extends Annotation> value;
-
- public static InitializedLiteral of(Class extends Annotation> value) {
- return new InitializedLiteral(value);
- }
-
- private InitializedLiteral(Class extends Annotation> value) {
- this.value = value;
- }
-
- public Class extends Annotation> value() {
- return value;
- }
-}
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java b/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
index f46b1b36..24a0e689 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
@@ -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;
@@ -136,7 +137,7 @@ public static Builder read(Class beanClass) {
private final Class> beanClass;
- private MockBean(Class> beanClass, Set> stereotypes, boolean alternative, boolean selectForSyntheticBeanArchive, String name,
+ protected MockBean(Class> beanClass, Set> stereotypes, boolean alternative, boolean selectForSyntheticBeanArchive, String name,
Set qualifiers, Set types, Class extends Annotation> scope, CreateFunction createCallback,
DestroyFunction destroyCallback) {
this.beanClass = beanClass;
@@ -325,6 +326,8 @@ public static class Builder {
private boolean selectForSyntheticBeanArchive;
+ private Integer priority;
+
private String name;
private Set qualifiers;
@@ -346,6 +349,7 @@ private Builder() {
this.types = new HashSet<>();
this.types.add(Object.class);
this.beanClass = WeldCDIExtension.class;
+ this.priority = null;
}
/**
@@ -447,6 +451,32 @@ public Builder 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 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 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.
*
@@ -455,7 +485,6 @@ public Builder alternative(boolean value) {
* selected for a bean archive. By default, all mock beans share the same bean class - {@code org.jboss.weld.junit.WeldCDIExtension}.
*
*
- * @param value
* @return self
* @see Bean#isAlternative()
* @see Weld#addAlternative(Class)
@@ -496,7 +525,7 @@ public Builder stereotypes(Class extends Annotation>... stereotypes) {
/**
*
- * @param qualifier
+ * @param stereotype
* @return self
*/
public Builder addStereotype(Class extends Annotation> stereotype) {
@@ -594,8 +623,14 @@ public MockBean 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);
+ }
}
}
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockBeanWithPriority.java b/junit-common/src/main/java/org/jboss/weld/junit/MockBeanWithPriority.java
new file mode 100644
index 00000000..6aca6ff3
--- /dev/null
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockBeanWithPriority.java
@@ -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 extends MockBean implements Prioritized {
+
+ private final int priority;
+
+ MockBeanWithPriority(Class> beanClass, Set> stereotypes, boolean alternative,
+ boolean selectForSyntheticBeanArchive, int priority, String name,
+ Set qualifiers, Set types, Class extends Annotation> scope,
+ CreateFunction createCallback, DestroyFunction 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;
+ }
+}
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java b/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java
index be0027b8..4a36e8fa 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java
@@ -48,14 +48,9 @@ public ResourceReferenceFactory