Author: hardy.ferentschik
Date: 2009-06-09 12:47:42 -0400 (Tue, 09 Jun 2009)
New Revision: 16726
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/DefaultBootstrapTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/Person.java
Log:
bootstrap tests
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/DefaultBootstrapTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/DefaultBootstrapTest.java 2009-06-09
14:32:46 UTC (rev 16725)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/DefaultBootstrapTest.java 2009-06-09
16:47:42 UTC (rev 16726)
@@ -17,24 +17,43 @@
*/
package org.hibernate.jsr303.tck.tests.bootstrap.defaultprovider;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
+import javax.validation.spi.ValidationProvider;
+import org.jboss.test.audit.annotations.SpecAssertion;
import org.jboss.testharness.AbstractTest;
import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.ArtifactType;
+import org.jboss.testharness.impl.packaging.Classes;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
+import org.hibernate.jsr303.tck.util.TestUtil;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertConstraintViolation;
+import static org.hibernate.jsr303.tck.util.TestUtil.assertInvalidPropertyPaths;
+
/**
* @author Hardy Ferentschik
*/
@Artifact(artifactType = ArtifactType.JSR303)
+(a)Classes(TestUtil.class)
public class DefaultBootstrapTest extends AbstractTest {
+ private static final String SERVICES_FILE = "META-INF/services/" +
ValidationProvider.class.getName();
+
@Test
public void testGetDefaultValidator() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
@@ -42,14 +61,297 @@
assertNotNull( validator, "We should be able to get a validator." );
Person person = new Person();
- person.setFirstName( "John" );
+ person.setPersonalNumber( 12345678900l );
Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
person );
- assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ assertEquals( constraintViolations.size(), 3, "Wrong number of constraints"
);
+ assertInvalidPropertyPaths(
+ constraintViolations,
+ new String[] { "firstName", "lastName",
"personalNumber" }
+ );
+ person.setFirstName( "John" );
person.setLastName( "Doe" );
constraintViolations = validator.validate( person );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ assertConstraintViolation(
+ constraintViolations.iterator().next(), Person.class, 12345678900l,
"personalNumber"
+ );
+
+ person.setPersonalNumber( 1234567890l );
+ constraintViolations = validator.validate( person );
assertEquals( constraintViolations.size(), 0, "Wrong number of constraints"
);
}
+
+ @Test
+ @SpecAssertion(section = "4.4.4.1", id = "c")
+ public void testServiceFileExista() {
+ List<ValidationProvider> providers = readBeanValidationServiceFile();
+ assertTrue( !providers.isEmpty(), "There should be at least one provider" );
+ }
+
+ private List<ValidationProvider> readBeanValidationServiceFile() {
+ ClassLoader classloader = Thread.currentThread().getContextClassLoader();
+ if ( classloader == null ) {
+ classloader = DefaultBootstrapTest.class.getClassLoader();
+ }
+ List<ValidationProvider> providers = new ArrayList<ValidationProvider>();
+ try {
+
+ Enumeration<URL> providerDefinitions = classloader.getResources( SERVICES_FILE
);
+ while ( providerDefinitions.hasMoreElements() ) {
+ URL url = providerDefinitions.nextElement();
+ addProviderToList( providers, url );
+ }
+ }
+ catch ( Exception e ) {
+ throw new RuntimeException( "Unable to load service file", e );
+ }
+ return providers;
+ }
+
+ private void addProviderToList(List<ValidationProvider> providers, URL url)
+ throws IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException {
+ InputStream stream = url.openStream();
+ try {
+ BufferedReader reader = new BufferedReader( new InputStreamReader( stream ), 100 );
+ String name = reader.readLine();
+ while ( name != null ) {
+ name = name.trim();
+ if ( !name.startsWith( "#" ) ) {
+ final Class<?> providerClass = loadClass(
+ name,
+ DefaultBootstrapTest.class
+ );
+
+ providers.add(
+ ( ValidationProvider ) providerClass.newInstance()
+ );
+ }
+ name = reader.readLine();
+ }
+ }
+ finally {
+ stream.close();
+ }
+ }
+
+ private static Class<?> loadClass(String name, Class caller) throws
ClassNotFoundException {
+ try {
+ //try context classloader, if fails try caller classloader
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if ( loader != null ) {
+ return loader.loadClass( name );
+ }
+ }
+ catch ( ClassNotFoundException e ) {
+ //trying caller classloader
+ if ( caller == null ) {
+ throw e;
+ }
+ }
+ return Class.forName( name, true, caller.getClassLoader() );
+ }
+
+
+//
+// @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();
+// 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.constraintValidatorFactory(
+// new ConstraintValidatorFactory() {
+//
+// public <T extends ConstraintValidator<?, ?>> T
getInstance(Class<T> key) {
+// if ( key == NotNullValidator.class ) {
+// return ( T ) new BadlyBehavedNotNullConstraintValidator();
+// }
+// return new ConstraintValidatorFactoryImpl().getInstance( key );
+// }
+// }
+// );
+// factory = configuration.buildValidatorFactory();
+// validator = factory.getValidator();
+// constraintViolations = validator.validate( customer );
+// 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 );
+//
+// ValidatorFactory factory = configuration.buildValidatorFactory();
+// assertDefaultFactory( factory );
+// }
+//
+// private void assertDefaultFactory(ValidatorFactory factory) {
+// assertNotNull( factory );
+// assertTrue( factory instanceof ValidatorFactoryImpl );
+// }
+//
+// class BadlyBehavedNotNullConstraintValidator extends NotNullValidator {
+// @Override
+// public boolean isValid(Object object, ConstraintValidatorContext
constraintValidatorContext) {
+// 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"
);
+// }
+//}
+
+
}
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/Person.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/Person.java 2009-06-09
14:32:46 UTC (rev 16725)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/Person.java 2009-06-09
16:47:42 UTC (rev 16726)
@@ -17,6 +17,7 @@
*/
package org.hibernate.jsr303.tck.tests.bootstrap.defaultprovider;
+import javax.validation.constraints.Digits;
import javax.validation.constraints.NotNull;
/**
@@ -29,7 +30,10 @@
@NotNull
private String lastName;
+ @Digits(integer = 10, fraction = 0)
+ private long personalNumber;
+
public String getFirstName() {
return firstName;
}
@@ -45,4 +49,12 @@
public void setLastName(String lastName) {
this.lastName = lastName;
}
+
+ public long getPersonalNumber() {
+ return personalNumber;
+ }
+
+ public void setPersonalNumber(long personalNumber) {
+ this.personalNumber = personalNumber;
+ }
}