Author: hardy.ferentschik
Date: 2009-06-10 10:04:41 -0400 (Wed, 10 Jun 2009)
New Revision: 16738
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
Log:
Moved some bootstrap tests into the TCK
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-10
10:47:12 UTC (rev 16737)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/bootstrap/ValidationTest.java 2009-06-10
14:04:41 UTC (rev 16738)
@@ -17,33 +17,21 @@
*/
package org.hibernate.validation.bootstrap;
-import java.lang.annotation.ElementType;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Locale;
import java.util.Set;
import javax.validation.Configuration;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.ConstraintValidatorFactory;
import javax.validation.ConstraintViolation;
-import javax.validation.MessageInterpolator;
-import javax.validation.TraversableResolver;
import javax.validation.Validation;
-import javax.validation.ValidationException;
-import javax.validation.ValidationProviderResolver;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
-import javax.validation.bootstrap.ProviderSpecificBootstrap;
-import javax.validation.spi.ValidationProvider;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
-import static org.testng.Assert.fail;
import org.testng.annotations.Test;
-import org.hibernate.validation.HibernateValidationProvider;
import org.hibernate.validation.constraints.NotNullValidator;
import org.hibernate.validation.engine.ConfigurationImpl;
import org.hibernate.validation.engine.ConstraintValidatorFactoryImpl;
@@ -73,65 +61,6 @@
}
@Test
- public void testGetCustomerValidator() {
- Configuration<?> configuration = Validation.byDefaultProvider().configure();
- assertDefaultBuilderAndFactory( configuration );
-
- ValidatorFactory factory = configuration.buildValidatorFactory();
- Validator validator = factory.getValidator();
-
- Customer customer = new Customer();
- customer.setFirstName( "John" );
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
-
- customer.setLastName( "Doe" );
-
- constraintViolations = validator.validate( customer );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
- }
-
- @Test
- public void testCustomMessageInterpolator() {
-
- // first try with the default message resolver
- Configuration<?> configuration = Validation.byDefaultProvider().configure();
- assertDefaultBuilderAndFactory( configuration );
-
- ValidatorFactory factory = configuration.buildValidatorFactory();
- Validator validator = factory.getValidator();
-
- Customer customer = new Customer();
- customer.setFirstName( "John" );
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- ConstraintViolation<Customer> constraintViolation =
constraintViolations.iterator().next();
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
-
- // now we modify the configuration, get a new factory and valiator and try again
- configuration = Validation.byDefaultProvider().configure();
- configuration.messageInterpolator(
- new MessageInterpolator() {
- public String interpolate(String message, Context context) {
- return "my custom message";
- }
-
- public String interpolate(String message, Context context, Locale locale) {
- throw new UnsupportedOperationException( "No specific locale is possible"
);
- }
- }
- );
- factory = configuration.buildValidatorFactory();
- validator = factory.getValidator();
- constraintViolations = validator.validate( customer );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- constraintViolation = constraintViolations.iterator().next();
- assertEquals( "my custom message", constraintViolation.getMessage(),
"Wrong message" );
- }
-
- @Test
public void testCustomConstraintValidatorFactory() {
Configuration<?> configuration = Validation.byDefaultProvider().configure();
@@ -167,71 +96,6 @@
assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
}
- @Test
- public void testCustomResolverAndType() {
- ValidationProviderResolver resolver = new ValidationProviderResolver() {
-
- public List<ValidationProvider> getValidationProviders() {
- List<ValidationProvider> list = new ArrayList<ValidationProvider>();
- list.add( new HibernateValidationProvider() );
- return list;
- }
- };
-
-
- HibernateValidatorConfiguration configuration = Validation
- .byProvider( HibernateValidatorConfiguration.class )
- .providerResolver( resolver )
- .configure();
- assertDefaultBuilderAndFactory( configuration );
- }
-
- @Test
- public void testCustomResolver() {
- ValidationProviderResolver resolver = new ValidationProviderResolver() {
-
- public List<ValidationProvider> getValidationProviders() {
- List<ValidationProvider> list = new ArrayList<ValidationProvider>();
- list.add( new HibernateValidationProvider() );
- return list;
- }
- };
-
-
- Configuration<?> configuration = Validation
- .byDefaultProvider()
- .providerResolver( resolver )
- .configure();
- assertDefaultBuilderAndFactory( configuration );
- }
-
- @Test
- public void testFailingCustomResolver() {
- ValidationProviderResolver resolver = new ValidationProviderResolver() {
-
- public List<ValidationProvider> getValidationProviders() {
- return new ArrayList<ValidationProvider>();
- }
- };
-
- final ProviderSpecificBootstrap<HibernateValidatorConfiguration>
providerSpecificBootstrap =
- Validation
- .byProvider( HibernateValidatorConfiguration.class )
- .providerResolver( resolver );
-
- try {
- providerSpecificBootstrap.configure();
- fail();
- }
- catch ( ValidationException e ) {
- assertEquals(
- "Unable to find provider: interface
org.hibernate.validation.engine.HibernateValidatorConfiguration",
- e.getMessage(),
- "Wrong error message"
- );
- }
- }
-
private void assertDefaultBuilderAndFactory(Configuration configuration) {
assertNotNull( configuration );
assertTrue( configuration instanceof ConfigurationImpl );
@@ -251,40 +115,4 @@
return true;
}
}
-
- @Test
- public void testCustomTraversableResolver() {
-
- Configuration<?> configuration = Validation.byDefaultProvider().configure();
- assertDefaultBuilderAndFactory( configuration );
-
- ValidatorFactory factory = configuration.buildValidatorFactory();
- Validator validator = factory.getValidator();
-
- Customer customer = new Customer();
- customer.setFirstName( "John" );
-
- Set<ConstraintViolation<Customer>> constraintViolations =
validator.validate( customer );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
- ConstraintViolation<Customer> constraintViolation =
constraintViolations.iterator().next();
- assertEquals( "may not be null", constraintViolation.getMessage(),
"Wrong message" );
-
- // get a new factory using a custom configuration
- configuration = Validation.byDefaultProvider().configure();
- configuration.traversableResolver(
- new TraversableResolver() {
- public boolean isReachable(Object traversableObject, String traversableProperty,
Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
- return false;
- }
-
- public boolean isCascadable(Object traversableObject, String traversableProperty,
Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
- return false;
- }
- }
- );
- factory = configuration.buildValidatorFactory();
- validator = factory.getValidator();
- constraintViolations = validator.validate( customer );
- assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
- }
}
Show replies by date