Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

WELD-991 Incorrect check of bean type when obtaining contextual refer…

…ence via BeanManager#getReference()

Conflicts:

	jboss-tck-runner/1.1/src/test/resources/tck-tests.xml
  • Loading branch information...
commit bb1d1351d787d4901475ae0c29ec06421edd868f 1 parent 27b6a45
Jozef Hartinger jharting authored
3  impl/src/main/java/org/jboss/weld/manager/BeanManagerImpl.java
@@ -110,6 +110,7 @@
110 110 import org.jboss.weld.util.BeansClosure;
111 111 import org.jboss.weld.util.Observers;
112 112 import org.jboss.weld.util.Proxies;
  113 +import org.jboss.weld.util.Types;
113 114 import org.jboss.weld.util.collections.Arrays2;
114 115 import org.jboss.weld.util.collections.IterableToIteratorFunction;
115 116 import org.jboss.weld.util.reflection.Reflections;
@@ -656,7 +657,7 @@ public Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> cre
656 657 if (creationalContext == null) {
657 658 throw new IllegalArgumentException(NULL_CREATIONAL_CONTEXT_ARGUMENT);
658 659 }
659   - if (!Reflections.isAssignableFrom(bean.getTypes(), beanType)) {
  660 + if (!Reflections.matches(beanType, bean.getTypes())) {
660 661 throw new IllegalArgumentException(SPECIFIED_TYPE_NOT_BEAN_TYPE, beanType, bean);
661 662 }
662 663 return getReference(bean, creationalContext, false);
27 tests-arquillian/src/test/java/org/jboss/weld/tests/cditck11/lookup/manager/FishFarmOffice.java
... ... @@ -0,0 +1,27 @@
  1 +/*
  2 + * JBoss, Home of Professional Open Source
  3 + * Copyright 2010, Red Hat, Inc., and individual contributors
  4 + * by the @authors tag. See the copyright.txt in the distribution for a
  5 + * full listing of individual contributors.
  6 + *
  7 + * Licensed under the Apache License, Version 2.0 (the "License");
  8 + * you may not use this file except in compliance with the License.
  9 + * You may obtain a copy of the License at
  10 + * http://www.apache.org/licenses/LICENSE-2.0
  11 + * Unless required by applicable law or agreed to in writing, software
  12 + * distributed under the License is distributed on an "AS IS" BASIS,
  13 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 + * See the License for the specific language governing permissions and
  15 + * limitations under the License.
  16 + */
  17 +package org.jboss.weld.tests.cditck11.lookup.manager;
  18 +
  19 +import javax.enterprise.inject.spi.BeanManager;
  20 +import javax.inject.Inject;
  21 +
  22 +public class FishFarmOffice {
  23 +
  24 + @Inject
  25 + public BeanManager beanManager;
  26 +
  27 +}
92 tests-arquillian/src/test/java/org/jboss/weld/tests/cditck11/lookup/manager/ManagerTest.java
... ... @@ -0,0 +1,92 @@
  1 +/*
  2 + * JBoss, Home of Professional Open Source
  3 + * Copyright 2010, Red Hat, Inc., and individual contributors
  4 + * by the @authors tag. See the copyright.txt in the distribution for a
  5 + * full listing of individual contributors.
  6 + *
  7 + * Licensed under the Apache License, Version 2.0 (the "License");
  8 + * you may not use this file except in compliance with the License.
  9 + * You may obtain a copy of the License at
  10 + * http://www.apache.org/licenses/LICENSE-2.0
  11 + * Unless required by applicable law or agreed to in writing, software
  12 + * distributed under the License is distributed on an "AS IS" BASIS,
  13 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14 + * See the License for the specific language governing permissions and
  15 + * limitations under the License.
  16 + */
  17 +package org.jboss.weld.tests.cditck11.lookup.manager;
  18 +
  19 +
  20 +import static org.jboss.weld.util.reflection.Reflections.cast;
  21 +
  22 +import java.io.Serializable;
  23 +import java.math.BigDecimal;
  24 +
  25 +import javax.enterprise.context.Dependent;
  26 +import javax.enterprise.inject.spi.Bean;
  27 +import javax.enterprise.inject.spi.BeanManager;
  28 +
  29 +import org.jboss.arquillian.container.test.api.Deployment;
  30 +import org.jboss.arquillian.junit.Arquillian;
  31 +import org.jboss.shrinkwrap.api.Archive;
  32 +import org.jboss.shrinkwrap.api.BeanArchive;
  33 +import org.jboss.shrinkwrap.api.ShrinkWrap;
  34 +import org.jboss.weld.literal.DefaultLiteral;
  35 +import org.junit.Test;
  36 +import org.junit.runner.RunWith;
  37 +
  38 +/**
  39 + * Taken from org.jboss.cdi.tck.tests.lookup.manager.ManagerTest
  40 + *
  41 + */
  42 +@RunWith(Arquillian.class)
  43 +public class ManagerTest {
  44 +
  45 + @Deployment
  46 + public static Archive<?> createTestArchive() {
  47 + return ShrinkWrap.create(BeanArchive.class).addPackage(ManagerTest.class.getPackage());
  48 + }
  49 +
  50 + @Test
  51 + public void testInjectingManager(FishFarmOffice fishFarmOffice) {
  52 + assert fishFarmOffice.beanManager != null;
  53 + }
  54 +
  55 + @Test
  56 + public void testContainerProvidesManagerBean(BeanManager manager) {
  57 + assert manager.getBeans(BeanManager.class).size() > 0;
  58 + }
  59 +
  60 + @Test
  61 + public void testManagerBeanIsDependentScoped(BeanManager manager) {
  62 + Bean<BeanManager> beanManager = cast(manager.getBeans(BeanManager.class).iterator().next());
  63 + assert beanManager.getScope().equals(Dependent.class);
  64 + }
  65 +
  66 + @Test
  67 + public void testManagerBeanHasCurrentBinding(BeanManager manager) {
  68 + Bean<BeanManager> beanManager = cast(manager.getBeans(BeanManager.class).iterator().next());
  69 + assert beanManager.getQualifiers().contains(DefaultLiteral.INSTANCE);
  70 + }
  71 +
  72 + @Test
  73 + public void testManagerBeanIsPassivationCapable(BeanManager manager) {
  74 + assert isSerializable(manager.getClass());
  75 + }
  76 +
  77 + @Test
  78 + public void testGetReferenceReturnsContextualInstance(BeanManager manager) {
  79 + Bean<FishFarmOffice> bean = cast(manager.getBeans(FishFarmOffice.class).iterator().next());
  80 + assert manager.getReference(bean, FishFarmOffice.class, manager.createCreationalContext(bean)) instanceof FishFarmOffice;
  81 + }
  82 +
  83 + @Test(expected = IllegalArgumentException.class )
  84 + public void testGetReferenceWithIllegalBeanType(BeanManager manager) {
  85 + Bean<FishFarmOffice> bean = cast(manager.getBeans(FishFarmOffice.class).iterator().next());
  86 + manager.getReference(bean, BigDecimal.class, manager.createCreationalContext(bean));
  87 + }
  88 +
  89 + private boolean isSerializable(Class<?> clazz) {
  90 + return clazz.isPrimitive() || Serializable.class.isAssignableFrom(clazz);
  91 + }
  92 +}
2  tests-arquillian/src/test/java/org/jboss/weld/tests/producer/receiver/ReceiverBeanLifecycleTest.java
@@ -53,7 +53,7 @@
53 53 public void testDependentReceiverInstanceDestroyedAfterProducerFieldInvocation(BeanManager manager) {
54 54 Producer.reset();
55 55
56   - Product1 product = Utils.getReference(manager, Utils.<Product1> getBean(manager, Product1.class));
  56 + Product1 product = Utils.getReference(manager, Utils.<Product1> getBean(manager, Product1.class), Product1.class);
57 57 assertNotNull(product);
58 58
59 59 assertTrue(Producer.isDestroyed());
8 tests/src/main/java/org/jboss/weld/test/util/Utils.java
@@ -136,9 +136,13 @@ public static boolean isExceptionInHierarchy(Throwable exception, Class<? extend
136 136 return (T) beanManager.getReference(bean, beanType, beanManager.createCreationalContext(bean));
137 137 }
138 138
139   - @SuppressWarnings("unchecked")
140 139 public static <T> T getReference(BeanManager beanManager, Bean<T> bean) {
141   - return (T) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean));
  140 + return getReference(beanManager, bean, bean.getBeanClass());
  141 + }
  142 +
  143 + @SuppressWarnings("unchecked")
  144 + public static <T> T getReference(BeanManager beanManager, Bean<T> bean, Type beanType) {
  145 + return (T) beanManager.getReference(bean, beanType, beanManager.createCreationalContext(bean));
142 146 }
143 147
144 148 @SuppressWarnings("unchecked")
4 tests/src/test/java/org/jboss/weld/tests/unit/cluster/NaiveClusterTest.java
@@ -139,7 +139,7 @@ public void testMultipleDependentObjectsSessionReplication() throws Exception {
139 139
140 140 use(1);
141 141 // Set a value into Foo1
142   - Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Foo.class, beanManager1.createCreationalContext(stableBean1));
  142 + Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Stable.class, beanManager1.createCreationalContext(stableBean1));
143 143 stable1.getFodder().setAmount(10);
144 144 stable1.getHorse().setName("George");
145 145
@@ -187,7 +187,7 @@ public void testVariableBeanDeploymentStructure() throws Exception {
187 187
188 188 use(1);
189 189 // Set a value into Foo1
190   - Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Foo.class, beanManager1.createCreationalContext(stableBean1));
  190 + Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Stable.class, beanManager1.createCreationalContext(stableBean1));
191 191 stable1.getFodder().setAmount(10);
192 192 stable1.getHorse().setName("George");
193 193

0 comments on commit bb1d135

Please sign in to comment.
Something went wrong with that request. Please try again.