Author: hardy.ferentschik
Date: 2009-05-19 11:56:55 -0400 (Tue, 19 May 2009)
New Revision: 16603
Added:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation-invalid-xml.xml
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
Log:
HV-133 Extended the TestUtil class using custom context classloaders
Modified:
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java
===================================================================
---
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java 2009-05-19
13:54:01 UTC (rev 16602)
+++
validator/trunk/hibernate-validator/src/main/java/org/hibernate/validation/engine/xml/ValidationXmlParser.java 2009-05-19
15:56:55 UTC (rev 16603)
@@ -224,15 +224,18 @@
}
catch ( JAXBException e ) {
log.error( "Error parsing validation.xml: {}", e.getMessage() );
+ throw new ValidationException( "Unable to parse " + VALIDATION_XML_FILE);
}
return validationConfig;
}
private InputStream getInputStreamForPath(String path) {
- InputStream inputStream = this.getClass().getResourceAsStream( path );
- // try absolute path
- if ( inputStream == null && !path.startsWith( "/" ) ) {
- inputStream = this.getClass().getResourceAsStream( "/" + path );
+ // try the context class loader first
+ InputStream inputStream =
Thread.currentThread().getContextClassLoader().getResourceAsStream( path );
+
+ // try the current class loader
+ if ( inputStream == null ) {
+ inputStream = this.getClass().getResourceAsStream( path );
}
return inputStream;
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java 2009-05-19
13:54:01 UTC (rev 16602)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/engine/xml/XmlConfigurationTest.java 2009-05-19
15:56:55 UTC (rev 16603)
@@ -19,6 +19,7 @@
import java.util.Set;
import javax.validation.ConstraintViolation;
+import javax.validation.ValidationException;
import javax.validation.Validator;
import org.testng.annotations.Test;
@@ -26,6 +27,7 @@
import org.hibernate.validation.util.TestUtil;
import static org.hibernate.validation.util.TestUtil.assertNumberOfViolations;
import static org.hibernate.validation.util.TestUtil.getValidator;
+import static
org.hibernate.validation.util.TestUtil.getValidatorWithCustomConfiguration;
/**
* @author Hardy Ferentschik
@@ -124,4 +126,9 @@
constraintViolations = validator.validate( user );
assertNumberOfViolations( constraintViolations, 0 );
}
+
+ @Test(expectedExceptions = ValidationException.class)
+ public void testInvalidValidationXml() {
+ getValidatorWithCustomConfiguration( "META-INF/validation-invalid-xml.xml"
);
+ }
}
Modified:
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java
===================================================================
---
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-05-19
13:54:01 UTC (rev 16602)
+++
validator/trunk/hibernate-validator/src/test/java/org/hibernate/validation/util/TestUtil.java 2009-05-19
15:56:55 UTC (rev 16603)
@@ -17,6 +17,7 @@
*/
package org.hibernate.validation.util;
+import java.io.InputStream;
import java.util.Set;
import javax.validation.ConstraintDescriptor;
import javax.validation.ConstraintViolation;
@@ -25,6 +26,7 @@
import javax.validation.Validation;
import javax.validation.Validator;
+import org.slf4j.Logger;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
@@ -36,6 +38,7 @@
* @author Hardy Ferentschik
*/
public class TestUtil {
+ private static final Logger log = LoggerFactory.make();
private static Validator hibernateValidator;
@@ -52,6 +55,34 @@
return hibernateValidator;
}
+ /**
+ * @param path The path to the xml file which should server as
<code>validation.xml</code> for the returned
+ * <code>Validator</code>.
+ *
+ * @return A <code>Validator</code> instance which respects the
configuration specified in the file with the path
+ * <code>path</code>.
+ */
+ public static Validator getValidatorWithCustomConfiguration(String path) {
+ Thread.currentThread().setContextClassLoader( new CustomValidationXmlClassLoader( path
) );
+
+ HibernateValidatorConfiguration configuration = Validation
+ .byProvider( HibernateValidatorConfiguration.class )
+ .configure();
+ return configuration.buildValidatorFactory().getValidator();
+ }
+
+ /**
+ * @return A <code>Validator</code> instance which ignores
<i>validation.xml</code>.
+ */
+ public static Validator getValidatorIgnoringValidationXml() {
+ Thread.currentThread().setContextClassLoader( new IgnoringValidationXmlClassLoader()
);
+
+ HibernateValidatorConfiguration configuration = Validation
+ .byProvider( HibernateValidatorConfiguration.class )
+ .configure();
+ return configuration.buildValidatorFactory().getValidator();
+ }
+
public static ConstraintDescriptor<?>
getSingleConstraintDescriptorFor(Class<?> clazz, String property) {
Set<ConstraintDescriptor<?>> constraintDescriptors =
getConstraintDescriptorsFor( clazz, property );
assertTrue(
@@ -114,4 +145,36 @@
public static void assertNumberOfViolations(Set violations, int expectedViolations) {
assertEquals( violations.size(), expectedViolations, "Wrong number of constraint
violations" );
}
+
+ private static class CustomValidationXmlClassLoader extends ClassLoader {
+ private final String customValidationXmlPath;
+
+ CustomValidationXmlClassLoader(String pathToCustomValidationXml) {
+ super( CustomValidationXmlClassLoader.class.getClassLoader() );
+ customValidationXmlPath = pathToCustomValidationXml;
+ }
+
+ public InputStream getResourceAsStream(String path) {
+ String finalPath = path;
+ if ( "META-INF/validation.xml".equals( path ) ) {
+ log.info( "Using {} as validation.xml", customValidationXmlPath );
+ finalPath = customValidationXmlPath;
+ }
+ return super.getResourceAsStream( finalPath );
+ }
+ }
+
+ private static class IgnoringValidationXmlClassLoader extends ClassLoader {
+ IgnoringValidationXmlClassLoader() {
+ super( IgnoringValidationXmlClassLoader.class.getClassLoader() );
+ }
+
+ public InputStream getResourceAsStream(String path) {
+ if ( "META-INF/validation.xml".equals( path ) ) {
+ log.info( "Ignoring call to load validation.xml" );
+ return null;
+ }
+ return super.getResourceAsStream( path );
+ }
+ }
}
Added:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation-invalid-xml.xml
===================================================================
---
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation-invalid-xml.xml
(rev 0)
+++
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation-invalid-xml.xml 2009-05-19
15:56:55 UTC (rev 16603)
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<validation-config
xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
+
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configu...
validation-configuration-1.0.xsd"
+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+</validation-confi>
\ No newline at end of file
Property changes on:
validator/trunk/hibernate-validator/src/test/resources/META-INF/validation-invalid-xml.xml
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native