Author: hardy.ferentschik
Date: 2009-06-10 10:05:26 -0400 (Wed, 10 Jun 2009)
New Revision: 16739
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/CustomProviderResolverTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
Removed:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/DefaultBootstrapTest.java
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/ExplicitCustomProviderBootstrapTest.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/TCKValidatorConfiguration.java
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/xmldefined/CustomProviderInXmlBootstrapTest.java
beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml
Log:
More bootstrap tests.
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/CustomProviderResolverTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/CustomProviderResolverTest.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/CustomProviderResolverTest.java 2009-06-10
14:05:26 UTC (rev 16739)
@@ -0,0 +1,115 @@
+// $Id$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.bootstrap.customprovider;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.validation.Configuration;
+import javax.validation.Validation;
+import javax.validation.ValidationProviderResolver;
+import javax.validation.ValidatorFactory;
+import javax.validation.ValidationException;
+import javax.validation.bootstrap.ProviderSpecificBootstrap;
+import javax.validation.spi.ValidationProvider;
+
+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 org.jboss.testharness.impl.packaging.IntegrationTest;
+import org.jboss.testharness.impl.packaging.Resource;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.assertEquals;
+import org.testng.annotations.Test;
+import static org.testng.FileAssert.fail;
+
+import org.hibernate.jsr303.tck.util.TestUtil;
+
+/**
+ * @author Hardy Ferentschik
+ * @todo Review these tests. These tests are actually testing functionality within
Validation. They should maybe be moved to
+ * the Bean Validation API itself!?
+ *
+ */
+@Artifact(artifactType = ArtifactType.JSR303)
+(a)Classes(TestUtil.class)
+public class CustomProviderResolverTest extends AbstractTest {
+
+ @Test
+ public void testCustomResolverAndType() {
+ ValidationProviderResolver resolver = new ValidationProviderResolver() {
+
+ public List<ValidationProvider> getValidationProviders() {
+ List<ValidationProvider> list = new ArrayList<ValidationProvider>();
+ list.add( new TCKValidationProvider() );
+ return list;
+ }
+ };
+
+
+ TCKValidatorConfiguration configuration = Validation
+ .byProvider( TCKValidatorConfiguration.class )
+ .providerResolver( resolver )
+ .configure();
+
+ ValidatorFactory factory = configuration.buildValidatorFactory();
+ assertTrue(factory instanceof TCKValidationProvider.DummyValidatorFactory);
+ }
+
+ @Test
+ public void testCustomResolver() {
+ ValidationProviderResolver resolver = new ValidationProviderResolver() {
+
+ public List<ValidationProvider> getValidationProviders() {
+ List<ValidationProvider> list = new ArrayList<ValidationProvider>();
+ list.add( new TCKValidationProvider() );
+ return list;
+ }
+ };
+
+ Configuration<?> configuration = Validation
+ .byDefaultProvider()
+ .providerResolver( resolver )
+ .configure();
+ ValidatorFactory factory = configuration.buildValidatorFactory();
+ assertTrue(factory instanceof TCKValidationProvider.DummyValidatorFactory);
+ }
+
+ @Test
+ public void testFailingCustomResolver() {
+ ValidationProviderResolver resolver = new ValidationProviderResolver() {
+
+ public List<ValidationProvider> getValidationProviders() {
+ return new ArrayList<ValidationProvider>();
+ }
+ };
+
+ final ProviderSpecificBootstrap<TCKValidatorConfiguration>
providerSpecificBootstrap =
+ Validation
+ .byProvider( TCKValidatorConfiguration.class )
+ .providerResolver( resolver );
+
+ try {
+ providerSpecificBootstrap.configure();
+ fail();
+ }
+ catch ( ValidationException e ) {
+ // success
+ }
+ }
+}
\ No newline at end of file
Property changes on:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/CustomProviderResolverTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/ExplicitCustomProviderBootstrapTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/ExplicitCustomProviderBootstrapTest.java 2009-06-10
14:04:41 UTC (rev 16738)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/ExplicitCustomProviderBootstrapTest.java 2009-06-10
14:05:26 UTC (rev 16739)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -27,6 +27,7 @@
import org.jboss.testharness.impl.packaging.Resource;
import org.jboss.testharness.impl.packaging.IntegrationTest;
import org.jboss.test.audit.annotations.SpecAssertion;
+import org.jboss.test.audit.annotations.SpecAssertions;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;
@@ -43,7 +44,10 @@
public class ExplicitCustomProviderBootstrapTest extends AbstractTest {
@Test
- @SpecAssertion(section = "4.4.4.2", id = "a")
+ @SpecAssertions({
+ @SpecAssertion(section = "4.4", id = "a"),
+ @SpecAssertion(section = "4.4.4.2", id = "a")
+ })
public void testGetFactoryByProviderSpecifiedProgrammatically() {
TCKValidatorConfiguration configuration = Validation.byProvider(
TCKValidatorConfiguration.class ).configure();
ValidatorFactory factory = configuration.buildValidatorFactory();
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/TCKValidatorConfiguration.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/TCKValidatorConfiguration.java 2009-06-10
14:04:41 UTC (rev 16738)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/TCKValidatorConfiguration.java 2009-06-10
14:05:26 UTC (rev 16739)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -40,31 +40,31 @@
}
public TCKValidatorConfiguration ignoreXmlConfiguration() {
- return this;
+ throw new UnsupportedOperationException();
}
public TCKValidatorConfiguration messageInterpolator(MessageInterpolator interpolator)
{
- return this;
+ throw new UnsupportedOperationException();
}
public TCKValidatorConfiguration traversableResolver(TraversableResolver resolver) {
- return this;
+ throw new UnsupportedOperationException();
}
public TCKValidatorConfiguration constraintValidatorFactory(ConstraintValidatorFactory
constraintValidatorFactory) {
- return this;
+ throw new UnsupportedOperationException();
}
public TCKValidatorConfiguration addMapping(InputStream stream) {
- return this;
+ throw new UnsupportedOperationException();
}
public TCKValidatorConfiguration addProperty(String name, String value) {
- return this;
+ throw new UnsupportedOperationException();
}
public MessageInterpolator getDefaultMessageInterpolator() {
- return null;
+ throw new UnsupportedOperationException();
}
public ValidatorFactory buildValidatorFactory() {
Modified:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/xmldefined/CustomProviderInXmlBootstrapTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/xmldefined/CustomProviderInXmlBootstrapTest.java 2009-06-10
14:04:41 UTC (rev 16738)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/customprovider/xmldefined/CustomProviderInXmlBootstrapTest.java 2009-06-10
14:05:26 UTC (rev 16739)
@@ -1,4 +1,4 @@
-// $Id:$
+// $Id$
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
@@ -21,6 +21,7 @@
import javax.validation.ValidatorFactory;
import org.jboss.test.audit.annotations.SpecAssertion;
+import org.jboss.test.audit.annotations.SpecAssertions;
import org.jboss.testharness.AbstractTest;
import org.jboss.testharness.impl.packaging.Artifact;
import org.jboss.testharness.impl.packaging.ArtifactType;
@@ -53,7 +54,10 @@
public class CustomProviderInXmlBootstrapTest extends AbstractTest {
@Test
- @SpecAssertion(section = "4.4.4.2", id = "a")
+ @SpecAssertions({
+ @SpecAssertion(section = "4.4", id = "a"),
+ @SpecAssertion(section = "4.4.4.2", id = "a")
+ })
public void testGetFactoryByProviderSpecifiedInXml() {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
assertNotNull( factory );
Added:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
===================================================================
---
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
(rev 0)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java 2009-06-10
14:05:26 UTC (rev 16739)
@@ -0,0 +1,260 @@
+// $Id:$
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2008, Red Hat Middleware LLC, 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.jsr303.tck.tests.bootstrap.defaultprovider;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.annotation.ElementType;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Locale;
+import java.util.Set;
+import javax.validation.Configuration;
+import javax.validation.ConstraintViolation;
+import javax.validation.MessageInterpolator;
+import javax.validation.TraversableResolver;
+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.test.audit.annotations.SpecAssertions;
+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.assertFalse;
+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 BootstrapTest extends AbstractTest {
+ private static final String SERVICES_FILE = "META-INF/services/" +
ValidationProvider.class.getName();
+
+ @Test
+ public void testGetDefaultValidator() {
+ ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+ Validator validator = factory.getValidator();
+ assertNotNull( validator, "We should be able to get a validator." );
+
+ Person person = new Person();
+ person.setPersonalNumber( 12345678900l );
+
+ Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
person );
+ 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 testServiceFileExists() {
+ 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 = BootstrapTest.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;
+ }
+
+
+ @Test
+ public void testCustomMessageInterpolatorViaConfiguration() {
+ // create a configurtation with a custom message interpolator
+ Configuration<?> configuration = Validation.byDefaultProvider().configure();
+ configuration.messageInterpolator( new DummyMessageInterpolator() );
+
+ Validator validator = configuration.buildValidatorFactory().getValidator();
+ assertCustomMessageInterpolatorUsed( validator );
+ }
+
+ @Test
+ @SpecAssertions({
+ @SpecAssertion(section = "4.4.2", id = "a"),
+ @SpecAssertion(section = "4.4.2", id = "b"),
+ @SpecAssertion(section = "4.3.2", id = "b")
+ })
+ public void testCustomMessageInterpolatorViaValidatorContext() {
+ // create a configurtation with a custom message interpolator
+ ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+ DummyMessageInterpolator dummyMessageInterpolator = new DummyMessageInterpolator();
+ Validator validator = factory.usingContext().messageInterpolator(
dummyMessageInterpolator ).getValidator();
+ assertCustomMessageInterpolatorUsed( validator );
+ assertFalse(
+ factory.getMessageInterpolator().equals( dummyMessageInterpolator ),
+ "getMessageInterpolator() should return the default message interpolator."
+ );
+ }
+
+ @Test
+ @SpecAssertion(section = "3.5.2", id = "b")
+ public void testCustomTraversableResolverViaConfiguration() {
+
+ // get a new factory using a custom configuration
+ Configuration<?> configuration = Validation.byDefaultProvider().configure();
+ configuration.traversableResolver( new DummyTraversableResolver() );
+ ValidatorFactory factory = configuration.buildValidatorFactory();
+ Validator validator = factory.getValidator();
+
+ assertCustomTrversableResolverUsed( validator );
+ }
+
+ @Test
+ @SpecAssertion(section = "3.5.2", id = "b")
+ public void testCustomTraversableResolverViaValidatorContext() {
+ ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+ DummyTraversableResolver DummyTraversableResolver = new DummyTraversableResolver();
+ Validator validator = factory.usingContext().traversableResolver(
DummyTraversableResolver ).getValidator();
+
+ assertCustomTrversableResolverUsed( validator );
+ }
+
+ private void assertCustomTrversableResolverUsed(Validator validator) {
+ Person person = new Person();
+ Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
person );
+ assertEquals(
+ constraintViolations.size(),
+ 0,
+ "There should be no failures since the custom traversable resolver makes all
properties non traversable."
+ );
+ }
+
+ private void assertCustomMessageInterpolatorUsed(Validator validator) {
+ Person person = new Person();
+ person.setFirstName( "John" );
+ person.setPersonalNumber( 1234567890l );
+
+ Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
person );
+ assertEquals( constraintViolations.size(), 1, "Wrong number of constraints"
);
+ ConstraintViolation<Person> constraintViolation =
constraintViolations.iterator().next();
+ assertEquals( "my custom message", constraintViolation.getMessage(),
"Wrong message" );
+ }
+
+ 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,
+ BootstrapTest.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() );
+ }
+
+ private static class DummyMessageInterpolator implements 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"
);
+ }
+ }
+
+ private static class DummyTraversableResolver implements 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;
+ }
+
+ public boolean isTraversable(Object traversableObject, String traversableProperty,
Class<?> rootBeanType, String pathToTraversableObject, ElementType elementType) {
+ return false;
+ }
+ }
+}
Property changes on:
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/BootstrapTest.java
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Deleted:
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-10
14:04:41 UTC (rev 16738)
+++
beanvalidation/trunk/validation-tck/src/main/java/org/hibernate/jsr303/tck/tests/bootstrap/defaultprovider/DefaultBootstrapTest.java 2009-06-10
14:05:26 UTC (rev 16739)
@@ -1,357 +0,0 @@
-// $Id:$
-/*
-* JBoss, Home of Professional Open Source
-* Copyright 2008, Red Hat Middleware LLC, 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.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();
- Validator validator = factory.getValidator();
- assertNotNull( validator, "We should be able to get a validator." );
-
- Person person = new Person();
- person.setPersonalNumber( 12345678900l );
-
- Set<ConstraintViolation<Person>> constraintViolations = validator.validate(
person );
- 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 testServiceFileExists() {
- 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/resources/tck-audit.xml
===================================================================
--- beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-10
14:04:41 UTC (rev 16738)
+++ beanvalidation/trunk/validation-tck/src/main/resources/tck-audit.xml 2009-06-10
14:05:26 UTC (rev 16739)
@@ -677,7 +677,7 @@
</section>
<section id="4.4" title="Bootstrapping">
<assertion id="a">
- <text>a bootstrap implementation must be able to bootstrap any Bean
Validation provider
+ <text>A bootstrap implementation must be able to bootstrap any Bean
Validation provider
implementation</text>
</assertion>
</section>