Permalink
Browse files

HV-882 Updating to CDI 1.2; Don't relying on processBean() being call…

…ed immediately for custom beans
  • Loading branch information...
1 parent 0972d34 commit 168731eb93f18ff29bd8dcb92da59f5e0b233085 @gunnarmorling gunnarmorling committed with hferentschik May 6, 2014
@@ -112,9 +112,8 @@
private Bean<?> defaultValidatorFactoryBean;
private Bean<?> hibernateValidatorFactoryBean;
- private boolean defaultValidatorAlreadyRegistered = false;
- private boolean hibernateValidatorAlreadyRegistered = false;
-
+ private Bean<?> defaultValidatorBean;
+ private Bean<?> hibernateValidatorBean;
public ValidationExtension() {
Configuration<?> config = Validation.byDefaultProvider().configure();
@@ -156,35 +155,35 @@ public void afterBeanDiscovery(@Observes AfterBeanDiscovery afterBeanDiscoveryEv
ValidationProviderHelper defaultProviderHelper = ValidationProviderHelper.forDefaultProvider( validatorFactory );
ValidationProviderHelper hvProviderHelper = ValidationProviderHelper.forHibernateValidator();
- // register a @Default VF; note that this will fire a ProcessBean event, so when coming to the next if-block,
- // hibernateValidatorFactoryBean will already have been set to that newly registered bean, causing not
- // another bean for HV to be set
+ // register default VF if none has been provided by the application or another PE
if ( defaultValidatorFactoryBean == null ) {
- afterBeanDiscoveryEvent.addBean( new ValidatorFactoryBean( beanManager, defaultProviderHelper ) );
+ defaultValidatorFactoryBean = new ValidatorFactoryBean( beanManager, defaultProviderHelper );
+ if ( hibernateValidatorFactoryBean == null && defaultProviderHelper.isHibernateValidator() ) {
+ hibernateValidatorFactoryBean = defaultValidatorFactoryBean;
+ }
+ afterBeanDiscoveryEvent.addBean( defaultValidatorFactoryBean );
}
+ // register VF with @HibernateValidator qualifier in case it hasn't been contributed by the application and the
+ // default VF registered by ourselves isn't for Hibernate Validator
if ( hibernateValidatorFactoryBean == null ) {
- afterBeanDiscoveryEvent.addBean( new ValidatorFactoryBean( beanManager, hvProviderHelper ) );
+ hibernateValidatorFactoryBean = new ValidatorFactoryBean( beanManager, hvProviderHelper );
+ afterBeanDiscoveryEvent.addBean( hibernateValidatorFactoryBean );
}
- if ( !defaultValidatorAlreadyRegistered ) {
- afterBeanDiscoveryEvent.addBean(
- new ValidatorBean(
- beanManager,
- defaultValidatorFactoryBean,
- defaultProviderHelper
- )
- );
+ // register default validator if required
+ if ( defaultValidatorBean == null ) {
+ defaultValidatorBean = new ValidatorBean( beanManager, defaultValidatorFactoryBean, defaultProviderHelper );
+ if ( hibernateValidatorBean == null && defaultProviderHelper.isHibernateValidator() ) {
+ hibernateValidatorBean = defaultValidatorBean;
+ }
+ afterBeanDiscoveryEvent.addBean( defaultValidatorBean );
}
- if ( !hibernateValidatorAlreadyRegistered ) {
- afterBeanDiscoveryEvent.addBean(
- new ValidatorBean(
- beanManager,
- hibernateValidatorFactoryBean,
- hvProviderHelper
- )
- );
+ // register validator with @HibernateValidator if required
+ if ( hibernateValidatorBean == null ) {
+ hibernateValidatorBean = new ValidatorBean( beanManager, hibernateValidatorFactoryBean, hvProviderHelper );
+ afterBeanDiscoveryEvent.addBean( hibernateValidatorBean );
}
}
@@ -209,10 +208,10 @@ public void processBean(@Observes ProcessBean<?> processBeanEvent) {
}
else if ( bean.getTypes().contains( Validator.class ) || bean instanceof ValidatorBean ) {
if ( bean.getQualifiers().contains( defaultQualifier ) ) {
- defaultValidatorAlreadyRegistered = true;
+ defaultValidatorBean = bean;
}
if ( bean.getQualifiers().contains( hibernateValidatorQualifier ) ) {
- hibernateValidatorAlreadyRegistered = true;
+ hibernateValidatorBean = bean;
}
}
}
@@ -197,29 +197,21 @@ public void testNoRegistrationRequired() {
expect( validatorFactoryBeanMock.getQualifiers() ).andReturn( qualifiers );
expect( validatorFactoryBeanMock.getQualifiers() ).andReturn( qualifiers );
- afterBeanDiscoveryMock.addBean( isA( ValidatorFactoryBean.class ) );
- expectLastCall();
-
expect( processBeanMock.getBean() ).andReturn( validatorBeanMock );
expect( validatorBeanMock.getTypes() ).andReturn( validatorBeanTypes );
expect( validatorBeanMock.getTypes() ).andReturn( validatorBeanTypes );
expect( validatorBeanMock.getQualifiers() ).andReturn( qualifiers );
expect( validatorBeanMock.getQualifiers() ).andReturn( qualifiers );
- afterBeanDiscoveryMock.addBean( isA( ValidatorBean.class ) );
- expectLastCall();
-
-
// get the mocks ready
- replay( processBeanMock, validatorFactoryBeanMock, validatorBeanMock, afterBeanDiscoveryMock, beanManagerMock );
+ replay( processBeanMock, validatorFactoryBeanMock, validatorBeanMock, beanManagerMock );
// run the code
extension.processBean( processBeanMock );
extension.processBean( processBeanMock );
- extension.afterBeanDiscovery( afterBeanDiscoveryMock, beanManagerMock );
// verify the mocks
- verify( processBeanMock, validatorFactoryBeanMock, validatorBeanMock, afterBeanDiscoveryMock, beanManagerMock );
+ verify( processBeanMock, validatorFactoryBeanMock, validatorBeanMock, beanManagerMock );
}
@Test
@@ -40,7 +40,7 @@
import static org.junit.Assert.assertNotNull;
/**
- * Tests the case where {@code @Default}-scoped beans for validator and validator factory have already beenr registered
+ * Tests the case where {@code @Default}-scoped beans for validator and validator factory have already been registered
* by another component and only the {@code @HibernateValidator}-scoped beans must be registered.
*
* @author Hardy Ferentschik
View
@@ -217,12 +217,12 @@
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
- <version>1.1</version>
+ <version>1.2</version>
</dependency>
<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core</artifactId>
- <version>2.1.1.Final</version>
+ <version>2.2.0.SP1</version>
</dependency>
<dependency>
<groupId>org.wildfly</groupId>

0 comments on commit 168731e

Please sign in to comment.