Skip to content

Commit

Permalink
HV-546 Adding tests for LazyValidatorFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik committed Jan 8, 2012
1 parent 62f650a commit 38af0bc
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 1 deletion.
Expand Up @@ -54,7 +54,7 @@ public static Archive<?> createTestArchive() {
return ShrinkWrap
.create( WebArchive.class, WAR_FILE_NAME )
.addClasses( User.class )
.addAsLibraries( IntegrationTestUtil.createCustomBeanValidationProviderJar() )
.addAsLibrary( IntegrationTestUtil.createCustomBeanValidationProviderJar() )
.addAsLibraries( IntegrationTestUtil.bundleLoggingDependencies() )
.addAsResource( "log4j.properties" )
.addAsResource( persistenceXml(), "META-INF/persistence.xml" )
Expand Down
@@ -0,0 +1,82 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc. and/or its affiliates, 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.hibernate.validator.integration.lazyfactory;

import javax.inject.Inject;

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.ArchivePaths;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.hibernate.validator.engine.ValidatorImpl;
import org.hibernate.validator.integration.util.IntegrationTestUtil;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.fail;

/**
* Tests for {@code LazyValidatorFactory}. See HV-546.
*
* @author Hardy Ferentschik
*/
@RunWith(Arquillian.class)
public class LazyValidatorFactoryWithValidationXmlButNoProviderTestIT {
private static final String WAR_FILE_NAME = LazyValidatorFactoryWithValidationXmlButNoProviderTestIT.class.getSimpleName() + ".war";

@Deployment
public static Archive<?> createTestArchive() {
Archive<?> beanValidationJarWithMissingProvider = IntegrationTestUtil.createCustomBeanValidationProviderJar();
Node providerClass = beanValidationJarWithMissingProvider.delete(
"org/hibernate/validator/integration/util/MyValidationProvider.class"
);
if ( providerClass == null ) {
fail( "MyValidationProvider was not as expected in the custom jar" );
}
return ShrinkWrap.create( WebArchive.class, WAR_FILE_NAME )
.addClasses(
MyValidatorCreator.class
)
.addAsLibraries( IntegrationTestUtil.bundleHibernateValidatorWithDependencies( true ) )
.addAsLibrary( beanValidationJarWithMissingProvider )
.addAsResource( "log4j.properties" )
.addAsWebInfResource( EmptyAsset.INSTANCE, ArchivePaths.create( "beans.xml" ) )
.addAsWebInfResource( "jboss-deployment-structure.xml" );
}

@Inject
private MyValidatorCreator validatorCreator;

@Test
public void testBootstrappingDoesNotFailDueToMissingCustomProvider() throws Exception {
assertNotNull( "The creator bean should have been injected", validatorCreator );
assertEquals(
"Since the custom provider cannot be loaded, Hibernate Validator should be the default",
ValidatorImpl.class.getName(),
validatorCreator.getValidator().getClass().getName()
);
}
}


@@ -0,0 +1,74 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc. and/or its affiliates, 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.hibernate.validator.integration.lazyfactory;

import javax.inject.Inject;

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.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.hibernate.validator.integration.util.IntegrationTestUtil;
import org.hibernate.validator.integration.util.MyValidator;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

/**
* Tests for {@code LazyValidatorFactory}. See HV-546.
*
* @author Hardy Ferentschik
*/
@RunWith(Arquillian.class)
public class LazyValidatorFactoryWithValidationXmlTestIT {
private static final String WAR_FILE_NAME = LazyValidatorFactoryWithValidationXmlTestIT.class.getSimpleName() + ".war";

@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create( WebArchive.class, WAR_FILE_NAME )
.addClasses(
MyValidatorCreator.class
)
.addAsLibraries( IntegrationTestUtil.bundleHibernateValidatorWithDependencies( true ) )
.addAsLibraries( IntegrationTestUtil.createCustomBeanValidationProviderJar() )
.addAsResource( "validation.xml", "META-INF/validation.xml" )
.addAsResource( "log4j.properties" )
.addAsWebInfResource( EmptyAsset.INSTANCE, ArchivePaths.create( "beans.xml" ) )
.addAsWebInfResource( "jboss-deployment-structure.xml" );
}

@Inject
private MyValidatorCreator validatorCreator;

@Test
public void testBootstrapCustomProviderWithLazyFactory() throws Exception {
assertNotNull( "The creator bean should have been injected", validatorCreator );
assertEquals(
"The custom validator should have been created",
MyValidator.class.getName(),
validatorCreator.getValidator().getClass().getName()
);
}
}


@@ -0,0 +1,74 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc. and/or its affiliates, 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.hibernate.validator.integration.lazyfactory;

import javax.inject.Inject;

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.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.hibernate.validator.engine.ValidatorImpl;
import org.hibernate.validator.integration.util.IntegrationTestUtil;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

/**
* Tests for {@code LazyValidatorFactory}. See HV-546.
*
* @author Hardy Ferentschik
*/
@RunWith(Arquillian.class)
public class LazyValidatorFactoryWithoutValidationXmlTestIT {
private static final String WAR_FILE_NAME = LazyValidatorFactoryWithoutValidationXmlTestIT.class.getSimpleName() + ".war";

@Deployment
public static Archive<?> createTestArchive() {
return ShrinkWrap.create( WebArchive.class, WAR_FILE_NAME )
.addClasses(
MyValidatorCreator.class
)
.addAsLibraries( IntegrationTestUtil.bundleHibernateValidatorWithDependencies( true ) )
.addAsLibrary( IntegrationTestUtil.createCustomBeanValidationProviderJar() )
.addAsResource( "log4j.properties" )
.addAsWebInfResource( EmptyAsset.INSTANCE, ArchivePaths.create( "beans.xml" ) )
.addAsWebInfResource( "jboss-deployment-structure.xml" );
}

@Inject
private MyValidatorCreator validatorCreator;

@Test
public void testBootstrapWithoutValidationXmlCreatesHibernateValidatorInstance() throws Exception {
assertNotNull( "The creator bean should have been injected", validatorCreator );
assertEquals(
"Hibernate Validator should be the chosen provider. " +
"Even though we bundle another provider it does not get explicitly configured.",
ValidatorImpl.class.getName(),
validatorCreator.getValidator().getClass().getName()
);
}
}


@@ -0,0 +1,35 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2012, Red Hat, Inc. and/or its affiliates, 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.hibernate.validator.integration.lazyfactory;

import javax.ejb.Stateful;
import javax.validation.Validator;

import org.hibernate.validator.util.LazyValidatorFactory;

/**
* @author Hardy Ferentschik
*/
@Stateful
public class MyValidatorCreator {
public Validator getValidator() {
LazyValidatorFactory factory = new LazyValidatorFactory();
return factory.getValidator();
}
}


@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ JBoss, Home of Professional Open Source
~ Copyright 2012, Red Hat, Inc. and/or its affiliates, 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.
-->
<jboss-deployment-structure>
<deployment>
<exclusions>
<!--
In order to test with the Hibernate Validator classes bundled in the war file we have to make sure
that the default org.hibernate.validator module classes are not loaded/used instead.
System dependencies have precedence - see https://docs.jboss.org/author/display/AS7/Class+Loading+in+AS7
-->
<module name="org.hibernate.validator"/>
</exclusions>
</deployment>
</jboss-deployment-structure>

0 comments on commit 38af0bc

Please sign in to comment.