以下测试类使用Weld-Se启动容器。此容器通常应运行3个类 CdiBean1,CdiHelperBean 和 DummyBean 。 DummyBean 仅由 CdiHelperBean 使用。 CdiHelperBean 被注入到 CdiBean1 中。给定下面的代码,我以某种方式启动容器,即 CdiHelperBean 被替换为 CdiHelperBeanAlt 。
我希望 CdiHelperBean 通常使用的Bean不必包含在内,因为无论如何它们都不会被创建。这是行不通的,因为Weld-Verifier会检查是否所有注入到替换类中的注入都是可能的。
有办法避免这种情况吗?
它在焊缝2.3.5和焊缝3.0.4上表现出相同的行为。
TestClass:
package purecdi;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.spi.DeploymentException;
import javax.enterprise.inject.spi.Extension;
import javax.inject.Inject;
import org.jboss.weld.bootstrap.api.CDI11Bootstrap;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.bootstrap.spi.Scanning;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author aschoerk
*/
public class PureWeldSeAltTest extends TestBaseClass {
@Override
public Collection<String> getBeanClasses() {
return Arrays.asList(CdiBean1.class.getName(),
// DummyClass.class.getName(),
CdiHelperBean.class.getName(),
CdiHelperBeanAlt.class.getName());
}
@Override
public List<Metadata<String>> getAlternativeClasses() {
return Arrays.asList(new Metadata<String>() {
@Override
public String getValue() {
return CdiHelperBeanAlt.class.getName();
}
@Override
public String getLocation() {
return "In Testcode";
}
}
);
}
public interface CdiHelperBeanIntf {
boolean callHelper();
}
static class DummyClass {
}
@Alternative
static class CdiHelperBeanAlt implements CdiHelperBeanIntf {
@Override
public boolean callHelper() {
return false;
}
}
static class CdiHelperBean implements CdiHelperBeanIntf {
@Inject
private DummyClass value;
@Override
public boolean callHelper() {
return true;
}
}
static class CdiBean1 {
@Inject
CdiHelperBeanIntf cdiHelperBean;
public boolean callThis() {
return true;
}
public CdiHelperBeanIntf getCdiHelperBean() {
return cdiHelperBean;
}
}
@Test
public void test() {
assertTrue(container.instance().select(CdiBean1.class).get().callThis());
assertFalse(container.instance().select(CdiBean1.class).get().getCdiHelperBean().callHelper());
}
}
基类:
package purecdi;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import javax.enterprise.inject.spi.Extension;
import org.jboss.weld.bootstrap.api.CDI11Bootstrap;
import org.jboss.weld.bootstrap.api.ServiceRegistry;
import org.jboss.weld.bootstrap.api.helpers.SimpleServiceRegistry;
import org.jboss.weld.bootstrap.spi.BeanDeploymentArchive;
import org.jboss.weld.bootstrap.spi.BeanDiscoveryMode;
import org.jboss.weld.bootstrap.spi.BeansXml;
import org.jboss.weld.bootstrap.spi.Deployment;
import org.jboss.weld.bootstrap.spi.Metadata;
import org.jboss.weld.bootstrap.spi.Scanning;
import org.jboss.weld.ejb.spi.EjbDescriptor;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.jboss.weld.resources.spi.ResourceLoader;
import org.junit.After;
import org.junit.Before;
/**
* @author aschoerk
*/
public abstract class TestBaseClass {
WeldContainer container;
protected Collection<String> getBeanClasses() {
return Collections.EMPTY_LIST;
}
protected List<Metadata<String>> getAlternativeClasses() {
return Collections.EMPTY_LIST;
}
protected List<Metadata<String>> getEnabledAlternativeStereotypes() {
return Collections.EMPTY_LIST;
}
@Before
public void setUp() {
Weld weld = new Weld() {
@Override
protected Deployment createDeployment(final ResourceLoader resourceLoader, final CDI11Bootstrap bootstrap) {
final ServiceRegistry services = new SimpleServiceRegistry();
final BeanDeploymentArchive oneDeploymentArchive = createOneDeploymentArchive(services);
oneDeploymentArchive.getServices().add(ResourceLoader.class, resourceLoader);
Deployment res = new Deployment() {
@Override
public Collection<BeanDeploymentArchive> getBeanDeploymentArchives() {
return Arrays.asList(oneDeploymentArchive);
}
@Override
public BeanDeploymentArchive loadBeanDeploymentArchive(final Class<?> beanClass) {
return oneDeploymentArchive;
}
@Override
public ServiceRegistry getServices() {
return oneDeploymentArchive.getServices();
}
@Override
public Iterable<Metadata<Extension>> getExtensions() {
return Collections.emptyList();
}
};
return res;
}
};
container = weld.initialize();
}
private BeanDeploymentArchive createOneDeploymentArchive(final ServiceRegistry services) {
return new BeanDeploymentArchive() {
@Override
public Collection<BeanDeploymentArchive> getBeanDeploymentArchives() {
return Collections.EMPTY_LIST;
}
@Override
public Collection<String> getBeanClasses() {
return TestBaseClass.this.getBeanClasses();
}
@Override
public BeansXml getBeansXml() {
return createBeansXml();
}
@Override
public Collection<EjbDescriptor<?>> getEjbs() {
return null;
}
@Override
public ServiceRegistry getServices() {
return services;
}
@Override
public String getId() {
return this.getClass().getSimpleName();
}
};
}
private BeansXml createBeansXml() {
return new BeansXml() {
@Override
public boolean isTrimmed() {
return false;
}
@Override
public List<Metadata<String>> getEnabledAlternativeStereotypes() {
return TestBaseClass.this.getEnabledAlternativeStereotypes();
}
@Override
public List<Metadata<String>> getEnabledAlternativeClasses() {
return TestBaseClass.this.getAlternativeClasses();
}
@Override
public List<Metadata<String>> getEnabledDecorators() {
return Collections.EMPTY_LIST;
}
@Override
public List<Metadata<String>> getEnabledInterceptors() {
return Collections.EMPTY_LIST;
}
@Override
public Scanning getScanning() {
return Scanning.EMPTY_SCANNING;
}
@Override
public URL getUrl() {
return null;
}
@Override
public BeanDiscoveryMode getBeanDiscoveryMode() {
return BeanDiscoveryMode.ALL;
}
@Override
public String getVersion() {
return "1.0";
}
};
}
@After
public void tearDown() {
if (container != null)
container.close();
}
}