Skip to content

Commit

Permalink
WELD-991 Incorrect check of bean type when obtaining contextual refer…
Browse files Browse the repository at this point in the history
…ence via BeanManager#getReference()

Conflicts:

	jboss-tck-runner/1.1/src/test/resources/tck-tests.xml
  • Loading branch information
jharting committed Aug 2, 2012
1 parent 27b6a45 commit bb1d135
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 6 deletions.
Expand Up @@ -110,6 +110,7 @@
import org.jboss.weld.util.BeansClosure;
import org.jboss.weld.util.Observers;
import org.jboss.weld.util.Proxies;
import org.jboss.weld.util.Types;
import org.jboss.weld.util.collections.Arrays2;
import org.jboss.weld.util.collections.IterableToIteratorFunction;
import org.jboss.weld.util.reflection.Reflections;
Expand Down Expand Up @@ -656,7 +657,7 @@ public Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> cre
if (creationalContext == null) {
throw new IllegalArgumentException(NULL_CREATIONAL_CONTEXT_ARGUMENT);
}
if (!Reflections.isAssignableFrom(bean.getTypes(), beanType)) {
if (!Reflections.matches(beanType, bean.getTypes())) {
throw new IllegalArgumentException(SPECIFIED_TYPE_NOT_BEAN_TYPE, beanType, bean);
}
return getReference(bean, creationalContext, false);
Expand Down
@@ -0,0 +1,27 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.tests.cditck11.lookup.manager;

import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;

public class FishFarmOffice {

@Inject
public BeanManager beanManager;

}
@@ -0,0 +1,92 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.tests.cditck11.lookup.manager;


import static org.jboss.weld.util.reflection.Reflections.cast;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.BeanArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.weld.literal.DefaultLiteral;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* Taken from org.jboss.cdi.tck.tests.lookup.manager.ManagerTest
*
*/
@RunWith(Arquillian.class)
public class ManagerTest {

@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create(BeanArchive.class).addPackage(ManagerTest.class.getPackage());
}

@Test
public void testInjectingManager(FishFarmOffice fishFarmOffice) {
assert fishFarmOffice.beanManager != null;
}

@Test
public void testContainerProvidesManagerBean(BeanManager manager) {
assert manager.getBeans(BeanManager.class).size() > 0;
}

@Test
public void testManagerBeanIsDependentScoped(BeanManager manager) {
Bean<BeanManager> beanManager = cast(manager.getBeans(BeanManager.class).iterator().next());
assert beanManager.getScope().equals(Dependent.class);
}

@Test
public void testManagerBeanHasCurrentBinding(BeanManager manager) {
Bean<BeanManager> beanManager = cast(manager.getBeans(BeanManager.class).iterator().next());
assert beanManager.getQualifiers().contains(DefaultLiteral.INSTANCE);
}

@Test
public void testManagerBeanIsPassivationCapable(BeanManager manager) {
assert isSerializable(manager.getClass());
}

@Test
public void testGetReferenceReturnsContextualInstance(BeanManager manager) {
Bean<FishFarmOffice> bean = cast(manager.getBeans(FishFarmOffice.class).iterator().next());
assert manager.getReference(bean, FishFarmOffice.class, manager.createCreationalContext(bean)) instanceof FishFarmOffice;
}

@Test(expected = IllegalArgumentException.class )
public void testGetReferenceWithIllegalBeanType(BeanManager manager) {
Bean<FishFarmOffice> bean = cast(manager.getBeans(FishFarmOffice.class).iterator().next());
manager.getReference(bean, BigDecimal.class, manager.createCreationalContext(bean));
}

private boolean isSerializable(Class<?> clazz) {
return clazz.isPrimitive() || Serializable.class.isAssignableFrom(clazz);
}
}
Expand Up @@ -53,7 +53,7 @@ public static Archive<?> getDeployment() {
public void testDependentReceiverInstanceDestroyedAfterProducerFieldInvocation(BeanManager manager) {
Producer.reset();

Product1 product = Utils.getReference(manager, Utils.<Product1> getBean(manager, Product1.class));
Product1 product = Utils.getReference(manager, Utils.<Product1> getBean(manager, Product1.class), Product1.class);
assertNotNull(product);

assertTrue(Producer.isDestroyed());
Expand Down
8 changes: 6 additions & 2 deletions tests/src/main/java/org/jboss/weld/test/util/Utils.java
Expand Up @@ -136,9 +136,13 @@ public static <T> T getReference(BeanManager beanManager, Class<T> beanType, Ann
return (T) beanManager.getReference(bean, beanType, beanManager.createCreationalContext(bean));
}

@SuppressWarnings("unchecked")
public static <T> T getReference(BeanManager beanManager, Bean<T> bean) {
return (T) beanManager.getReference(bean, bean.getBeanClass(), beanManager.createCreationalContext(bean));
return getReference(beanManager, bean, bean.getBeanClass());
}

@SuppressWarnings("unchecked")
public static <T> T getReference(BeanManager beanManager, Bean<T> bean, Type beanType) {
return (T) beanManager.getReference(bean, beanType, beanManager.createCreationalContext(bean));
}

@SuppressWarnings("unchecked")
Expand Down
Expand Up @@ -139,7 +139,7 @@ public void testMultipleDependentObjectsSessionReplication() throws Exception {

use(1);
// Set a value into Foo1
Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Foo.class, beanManager1.createCreationalContext(stableBean1));
Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Stable.class, beanManager1.createCreationalContext(stableBean1));
stable1.getFodder().setAmount(10);
stable1.getHorse().setName("George");

Expand Down Expand Up @@ -187,7 +187,7 @@ public void testVariableBeanDeploymentStructure() throws Exception {

use(1);
// Set a value into Foo1
Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Foo.class, beanManager1.createCreationalContext(stableBean1));
Stable stable1 = (Stable) beanManager1.getReference(stableBean1, Stable.class, beanManager1.createCreationalContext(stableBean1));
stable1.getFodder().setAmount(10);
stable1.getHorse().setName("George");

Expand Down

0 comments on commit bb1d135

Please sign in to comment.