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 value; - - public static DestroyedLiteral of(Class value) { - return new DestroyedLiteral(value); - } - - private DestroyedLiteral(Class value) { - this.value = value; - } - - public Class 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 value; - - public static InitializedLiteral of(Class value) { - return new InitializedLiteral(value); - } - - private InitializedLiteral(Class value) { - this.value = value; - } - - public Class 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 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... stereotypes) { /** * - * @param qualifier + * @param stereotype * @return self */ public Builder addStereotype(Class 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 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 registerEjbInjectionPoint(InjectionPoint return new ResourceReferenceFactory() { @Override public ResourceReference createResource() { - return new SimpleResourceReference(resolveEjb(injectionPoint)); + return new SimpleResourceReference(ejbFactory.apply(injectionPoint)); } }; } - // only for Weld 2 - public Object resolveEjb(InjectionPoint injectionPoint) { - return ejbFactory.apply(injectionPoint); - } - } diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java b/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java index 2167916f..6a78dc10 100644 --- a/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java +++ b/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java @@ -49,7 +49,14 @@ public ResourceReferenceFactory registerPersistenceContextInjecti return new ResourceReferenceFactory() { @Override public ResourceReference createResource() { - return new SimpleResourceReference(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) context); + } + throw new IllegalStateException("Not an EntityManager instance: " + context); } }; } @@ -59,35 +66,18 @@ public ResourceReferenceFactory registerPersistenceUnitInj return new ResourceReferenceFactory() { @Override public ResourceReference createResource() { - return new SimpleResourceReference(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) 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() { } diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java b/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java index 2b284568..ecbe444d 100644 --- a/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java +++ b/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java @@ -49,8 +49,7 @@ public MockResourceInjectionServices(Map 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); @@ -83,11 +82,6 @@ public ResourceReferenceFactory registerResourceInjectionPoint(String jn throw new UnsupportedOperationException(); } - // only for Weld 2 - public Object resolveResource(String jndiName, String mappedName) { - throw new UnsupportedOperationException(); - } - @Override public void cleanup() { } diff --git a/junit4/pom.xml b/junit4/pom.xml index b584bea4..370c8e44 100644 --- a/junit4/pom.xml +++ b/junit4/pom.xml @@ -4,7 +4,7 @@ org.jboss.weld weld-junit-parent - 1.3.2-SNAPSHOT + 2.0.0-SNAPSHOT weld-junit4 @@ -43,29 +43,13 @@ test - - - - - - weld3 - - false - - env.WELD_JUNIT_PROFILE - weld3 - - - - - org.jboss.weld.module - weld-ejb - ${version.weld} - test - - - + + org.jboss.weld.module + weld-ejb + ${version.weld} + test + - + diff --git a/junit4/src/test/java/org/jboss/weld/junit4/bean/AddGloballyEnabledAlternativeTest.java b/junit4/src/test/java/org/jboss/weld/junit4/bean/AddGloballyEnabledAlternativeTest.java new file mode 100644 index 00000000..6ad149ae --- /dev/null +++ b/junit4/src/test/java/org/jboss/weld/junit4/bean/AddGloballyEnabledAlternativeTest.java @@ -0,0 +1,63 @@ +package org.jboss.weld.junit4.bean; + +import org.jboss.weld.junit.MockBean; +import org.jboss.weld.junit4.WeldInitiator; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Mockito; + +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.util.TypeLiteral; +import java.util.List; +import java.util.Set; + +/** + * Tests {@link org.jboss.weld.junit.MockBean} adding a bean that is a globally enabled alternative. + * + * @author Matej Novotny + */ +public class AddGloballyEnabledAlternativeTest { + + @Rule + public WeldInitiator weld = WeldInitiator.from(Bar.class) + .addBeans(createFooAlternativeBean(), createListBean()) + .build(); + + public static Bean createFooAlternativeBean() { + return MockBean.read(Foo.class) + .priority(3) + .alternative(true) + .addQualifier(Meaty.Literal.INSTANCE) + .build(); + } + + static Bean createListBean() { + return MockBean.builder() + .types(new TypeLiteral>() { + }.getType()) + .globallySelectedAlternative(2) + .creating( + // Mock object provided by Mockito + Mockito.when(Mockito.mock(List.class).get(0)).thenReturn("42").getMock()) + .build(); + } + + @Test + public void testAllBeansAreAddedAndCanBeSelected() { + Bar bar = weld.select(Bar.class).get(); + Assert.assertNotNull(bar.getFoo()); + Assert.assertNotNull(bar.getSomeList()); + Assert.assertEquals("42", bar.getSomeList().get(0)); + + // assert all of these are actually alternatives (enabled) + Set> beans = weld.getBeanManager().getBeans(Foo.class, Meaty.Literal.INSTANCE); + Assert.assertEquals(1, beans.size()); + Assert.assertTrue(beans.iterator().next().isAlternative()); + + beans = weld.getBeanManager().getBeans(new TypeLiteral>() { + }.getType()); + Assert.assertEquals(1, beans.size()); + Assert.assertTrue(beans.iterator().next().isAlternative()); + } +} diff --git a/junit4/src/test/java/org/jboss/weld/junit4/bean/Bar.java b/junit4/src/test/java/org/jboss/weld/junit4/bean/Bar.java new file mode 100644 index 00000000..84ba4b65 --- /dev/null +++ b/junit4/src/test/java/org/jboss/weld/junit4/bean/Bar.java @@ -0,0 +1,23 @@ +package org.jboss.weld.junit4.bean; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import java.util.List; + +@ApplicationScoped +public class Bar { + @Inject + @Meaty + private Foo foo; + + @Inject + private List someList; + + public List getSomeList() { + return someList; + } + + public Foo getFoo() { + return foo; + } +} diff --git a/junit4/src/test/java/org/jboss/weld/junit4/bean/Foo.java b/junit4/src/test/java/org/jboss/weld/junit4/bean/Foo.java new file mode 100644 index 00000000..436638cd --- /dev/null +++ b/junit4/src/test/java/org/jboss/weld/junit4/bean/Foo.java @@ -0,0 +1,7 @@ +package org.jboss.weld.junit4.bean; + +public class Foo { + public String ping() { + return "foo"; + } +} diff --git a/junit5/pom.xml b/junit5/pom.xml index 05454c84..bcda77c9 100644 --- a/junit5/pom.xml +++ b/junit5/pom.xml @@ -5,7 +5,7 @@ org.jboss.weld weld-junit-parent - 1.3.2-SNAPSHOT + 2.0.0-SNAPSHOT weld-junit5 @@ -48,30 +48,14 @@ hibernate-jpa-2.1-api test + + + org.jboss.weld.module + weld-ejb + ${version.weld} + test + - - - - weld3 - - false - - env.WELD_JUNIT_PROFILE - weld3 - - - - - org.jboss.weld.module - weld-ejb - ${version.weld} - test - - - - - - diff --git a/junit5/src/test/java/org/jboss/weld/junit5/bean/AddGloballyEnabledAlternativeTest.java b/junit5/src/test/java/org/jboss/weld/junit5/bean/AddGloballyEnabledAlternativeTest.java new file mode 100644 index 00000000..44352063 --- /dev/null +++ b/junit5/src/test/java/org/jboss/weld/junit5/bean/AddGloballyEnabledAlternativeTest.java @@ -0,0 +1,66 @@ +package org.jboss.weld.junit5.bean; + +import org.jboss.weld.junit.MockBean; +import org.jboss.weld.junit5.WeldInitiator; +import org.jboss.weld.junit5.WeldJunit5Extension; +import org.jboss.weld.junit5.WeldSetup; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mockito; + +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.util.TypeLiteral; + +import java.util.List; +import java.util.Set; + +/** + * Tests {@link org.jboss.weld.junit.MockBean} adding a bean that is a globally enabled alternative. + * + * @author Matej Novotny + */ +@ExtendWith(WeldJunit5Extension.class) +public class AddGloballyEnabledAlternativeTest { + + @WeldSetup + public WeldInitiator weld = WeldInitiator.from(Bar.class) + .addBeans(createFooAlternativeBean(), createListBean()) + .build(); + + public static Bean createFooAlternativeBean(){ + return MockBean.read(Foo.class) + .priority(3) + .alternative(true) + .addQualifier(Meaty.Literal.INSTANCE) + .build(); + } + + static Bean createListBean() { + return MockBean.builder() + .types(new TypeLiteral>() { + }.getType()) + .globallySelectedAlternative(2) + .creating( + // Mock object provided by Mockito + Mockito.when(Mockito.mock(List.class).get(0)).thenReturn("42").getMock()) + .build(); + } + + @Test + public void testAllBeansAreAddedAndCanBeSelected() { + Bar bar = weld.select(Bar.class).get(); + Assertions.assertNotNull(bar.getFoo()); + Assertions.assertNotNull(bar.getSomeList()); + Assertions.assertEquals("42", bar.getSomeList().get(0)); + + // assert all of these are actually alternatives (enabled) + Set> beans = weld.getBeanManager().getBeans(Foo.class, Meaty.Literal.INSTANCE); + Assertions.assertEquals(1, beans.size()); + Assertions.assertTrue(beans.iterator().next().isAlternative()); + + beans = weld.getBeanManager().getBeans(new TypeLiteral>(){}.getType()); + Assertions.assertEquals(1, beans.size()); + Assertions.assertTrue(beans.iterator().next().isAlternative()); + } +} diff --git a/junit5/src/test/java/org/jboss/weld/junit5/bean/Bar.java b/junit5/src/test/java/org/jboss/weld/junit5/bean/Bar.java new file mode 100644 index 00000000..b3b7e9a5 --- /dev/null +++ b/junit5/src/test/java/org/jboss/weld/junit5/bean/Bar.java @@ -0,0 +1,24 @@ +package org.jboss.weld.junit5.bean; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import java.util.List; + +@ApplicationScoped +public class Bar { + + @Inject + @Meaty + private Foo foo; + + @Inject + private List someList; + + public List getSomeList() { + return someList; + } + + public Foo getFoo() { + return foo; + } +} diff --git a/junit5/src/test/java/org/jboss/weld/junit5/bean/Foo.java b/junit5/src/test/java/org/jboss/weld/junit5/bean/Foo.java new file mode 100644 index 00000000..2032697b --- /dev/null +++ b/junit5/src/test/java/org/jboss/weld/junit5/bean/Foo.java @@ -0,0 +1,8 @@ +package org.jboss.weld.junit5.bean; + +public class Foo { + + public String ping() { + return "foo"; + } +} diff --git a/pom.xml b/pom.xml index 770182c3..a94d8913 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 weld-junit-parent pom - 1.3.2-SNAPSHOT + 2.0.0-SNAPSHOT Weld JUnit Extensions Set of JUnit extensions to enhance the testing of CDI components. @@ -42,8 +42,7 @@ 4.12 5.4.1 - 2.4.8.Final - 3.1.0.Final + 3.1.0.Final 2.25.1 1.0.0.Final 1.0.0.Final @@ -126,19 +125,6 @@ - - weld3 - - false - - env.WELD_JUNIT_PROFILE - weld3 - - - - ${version.weld3} - - spotbugs