details: https://code.openbravo.com/erp/devel/pi/rev/aebfddd2027d changeset: 36010:aebfddd2027d user: Carlos Aristu <carlos.aristu <at> openbravo.com> date: Thu Jun 06 10:34:38 2019 +0200 summary: fixes bug 41033: ImportEntryPreProcessors with qualifiers are not executed
As part of the refactor to provide the ImportEntryBuilder[1], the injection of the import entry preprocessors was changed. As the instances of that class are created outside of the Weld scope, the preprocessors are injected using "WeldUtils.getInstances()" method instead of using @Inject. The problem here is that "WeldUtils.getInstances()" looked for all beans of an specific type that does not have a qualifier. For this reason, import entry preprocessors with qualifiers were not being found and, therefore, not executed by the ImportEntryBuilder before creating the import entry. To fix this problem now "WeldUtils.getInstances()" has been changed in order to look for any bean of a particular type (regardless of whether it is qualified or not). This is done by specifying the @Any qualifier to the "BeanManager.getBeans()" method. Thus, now WeldUtils.getInstances() behaves exactly in the same way as when using @Inject together with @Any when we are inside the scope of Weld. [1] https://code.openbravo.com/erp/devel/pi/rev/90c48060859cc70935add00e77e8065dedb197cc details: https://code.openbravo.com/erp/devel/pi/rev/3f43ae2410c8 changeset: 36011:3f43ae2410c8 user: Carlos Aristu <carlos.aristu <at> openbravo.com> date: Thu Jun 06 10:58:49 2019 +0200 summary: related to issue 41033: code improvements - Use Any AnnotationLiteral (available since CDI 2.0) - Use diamond operator - Remove unused injection of ImportEntryPreProcessor in the ImportEntryManager (they are used in the ImportEntryBuilder) details: https://code.openbravo.com/erp/devel/pi/rev/7148f9e42956 changeset: 36012:7148f9e42956 user: Carlos Aristu <carlos.aristu <at> openbravo.com> date: Thu Jun 06 10:59:01 2019 +0200 summary: related to issue 41033: add test cases diffstat: modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/testinfrastructure/CdiInfrastructure.java | 39 +++++++++- modules/org.openbravo.base.weld/src/org/openbravo/base/weld/WeldUtils.java | 15 +-- src/org/openbravo/service/importprocess/ImportEntryManager.java | 4 - 3 files changed, 43 insertions(+), 15 deletions(-) diffs (155 lines): diff -r f395c484a710 -r 7148f9e42956 modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/testinfrastructure/CdiInfrastructure.java --- a/modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/testinfrastructure/CdiInfrastructure.java Wed Jun 05 23:51:37 2019 +0000 +++ b/modules/org.openbravo.base.weld/src-test/org/openbravo/base/weld/test/testinfrastructure/CdiInfrastructure.java Thu Jun 06 10:59:01 2019 +0200 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2015 Openbravo SLU + * All portions are Copyright (C) 2015-2019 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -22,12 +22,22 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import static org.junit.Assert.assertThat; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import javax.enterprise.inject.Any; +import javax.enterprise.inject.Instance; import javax.inject.Inject; import org.jboss.arquillian.junit.InSequence; import org.junit.Test; +import org.openbravo.base.weld.WeldUtils; import org.openbravo.base.weld.test.WeldBaseTest; /** @@ -48,12 +58,17 @@ @Inject private RequestScopedBean requestBean; + @Inject + @Any + private Instance<ExtensionBean> extensionBeans; + /** beans are correctly injected */ @Test public void beansAreInjected() { assertThat("application bean is injected", applicationBean, notNullValue()); assertThat("session bean is injected", sessionBean, notNullValue()); assertThat("request bean is injected", requestBean, notNullValue()); + assertThat("beans are injected with @Any", extensionBeans.isUnsatisfied(), equalTo(false)); } /** starts application and session scopes */ @@ -77,4 +92,26 @@ assertThat(sessionBean.getValue(), equalTo("session")); assertThat(requestBean.getValue(), nullValue()); } + + /** get any instance of a particular bean type */ + @Test + public void expectedBeanInstancesAreInjected() { + assertExtensionBeansInjection(extensionBeans.stream()); + } + + /** get any instance of a particular bean type (using WeldUtils) */ + @Test + public void expectedBeanInstancesAreInjectedWithWeldUtils() { + assertExtensionBeansInjection(WeldUtils.getInstances(ExtensionBean.class).stream()); + } + + private void assertExtensionBeansInjection(Stream<ExtensionBean> beans) { + int numberOfExtensionBeans = 2; + String[] expectedNames = { "qualifiedBean", "unqualifiedBean" }; + + List<String> names = beans.map(ExtensionBean::getName).collect(Collectors.toList()); + + assertThat("Retrieved the expected beans", names, + allOf(hasSize(numberOfExtensionBeans), containsInAnyOrder(expectedNames))); + } } diff -r f395c484a710 -r 7148f9e42956 modules/org.openbravo.base.weld/src/org/openbravo/base/weld/WeldUtils.java --- a/modules/org.openbravo.base.weld/src/org/openbravo/base/weld/WeldUtils.java Wed Jun 05 23:51:37 2019 +0000 +++ b/modules/org.openbravo.base.weld/src/org/openbravo/base/weld/WeldUtils.java Thu Jun 06 10:59:01 2019 +0200 @@ -11,7 +11,7 @@ * under the License. * The Original Code is Openbravo ERP. * The Initial Developer of the Original Code is Openbravo SLU - * All portions are Copyright (C) 2010-2017 Openbravo SLU + * All portions are Copyright (C) 2010-2019 Openbravo SLU * All Rights Reserved. * Contributor(s): ______________________________________. ************************************************************************ @@ -26,7 +26,6 @@ import javax.enterprise.inject.Any; import javax.enterprise.inject.spi.Bean; import javax.enterprise.inject.spi.BeanManager; -import javax.enterprise.util.AnnotationLiteral; import javax.inject.Inject; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -75,10 +74,6 @@ staticBeanManager = theBeanManager; } - @SuppressWarnings("serial") - public static final AnnotationLiteral<Any> ANY_LITERAL = new AnnotationLiteral<Any>() { - }; - /** * Method which uses the static instance of the bean manager cached in this class. This method * should only be used by objects which are not created by Weld. Objects created by Weld should @@ -89,7 +84,7 @@ @SuppressWarnings("unchecked") public static <T> T getInstanceFromStaticBeanManager(Class<T> type) { final BeanManager theBeanManager = getStaticInstanceBeanManager(); - final Set<Bean<?>> beans = theBeanManager.getBeans(type, ANY_LITERAL); + final Set<Bean<?>> beans = theBeanManager.getBeans(type, Any.Literal.INSTANCE); for (Bean<?> bean : beans) { if (bean.getBeanClass() == type) { return (T) theBeanManager.getReference(bean, type, @@ -114,7 +109,7 @@ */ @SuppressWarnings("unchecked") public <T> T getInstance(Class<T> type) { - final Set<Bean<?>> beans = beanManager.getBeans(type, ANY_LITERAL); + final Set<Bean<?>> beans = beanManager.getBeans(type, Any.Literal.INSTANCE); for (Bean<?> bean : beans) { if (bean.getBeanClass() == type) { return (T) beanManager.getReference(bean, type, beanManager.createCreationalContext(bean)); @@ -129,9 +124,9 @@ @SuppressWarnings("unchecked") public static <T> List<T> getInstances(Class<T> type) { final BeanManager beanManager = WeldUtils.getStaticInstanceBeanManager(); - final Set<Bean<?>> beans = beanManager.getBeans(type); + final Set<Bean<?>> beans = beanManager.getBeans(type, Any.Literal.INSTANCE); - final List<T> instances = new ArrayList<T>(); + final List<T> instances = new ArrayList<>(); for (Bean<?> bean : beans) { T instance = (T) beanManager.getReference(bean, type, beanManager.createCreationalContext(bean)); diff -r f395c484a710 -r 7148f9e42956 src/org/openbravo/service/importprocess/ImportEntryManager.java --- a/src/org/openbravo/service/importprocess/ImportEntryManager.java Wed Jun 05 23:51:37 2019 +0000 +++ b/src/org/openbravo/service/importprocess/ImportEntryManager.java Thu Jun 06 10:59:01 2019 +0200 @@ -125,10 +125,6 @@ @Inject @Any - private Instance<ImportEntryPreProcessor> entryPreProcessors; - - @Inject - @Any private Instance<ImportEntryProcessor> entryProcessors; @Inject _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits