Skip to content

Commit

Permalink
HV-620 Improving error messages and adding/updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hferentschik authored and gunnarmorling committed Nov 1, 2012
1 parent a5c5173 commit 4141674
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 8 deletions.
Expand Up @@ -101,7 +101,7 @@ public A createAnnotationProxy() {
return AnnotationFactory.create( annotationDescriptor );
}
catch ( RuntimeException e ) {
throw log.getUnableToCreateAnnotationForConfiguredConstraintException( e.getMessage(), e );
throw log.getUnableToCreateAnnotationForConfiguredConstraintException( e );
}
}

Expand Down
Expand Up @@ -80,7 +80,10 @@ else if ( m.getDefaultValue() != null ) {
result.put( m.getName(), m.getDefaultValue() );
}
else {
throw log.getNoValueProvidedForAnnotationParameterException( m.getName() );
throw log.getNoValueProvidedForAnnotationParameterException(
m.getName(),
annotationType.getSimpleName()
);
}
}
if ( processedValuesFromDescriptor != descriptor.numberOfElements() ) {
Expand Down
Expand Up @@ -97,8 +97,8 @@ public interface Log extends BasicLogger {
@Message(id = 11, value = "Unable to create schema for %1$s: %2$s")
void unableToCreateSchema(String fileName, String message);

@Message(id = 12, value = "Unable to create annotation for configured constraint: %s.")
ValidationException getUnableToCreateAnnotationForConfiguredConstraintException(String message, @Cause RuntimeException e);
@Message(id = 12, value = "Unable to create annotation for configured constraint")
ValidationException getUnableToCreateAnnotationForConfiguredConstraintException(@Cause RuntimeException e);

@Message(id = 13, value = "The class %1$s does not have a property '%2$s' with access %3$s.")
ValidationException getUnableToFindPropertyWithAccessException(Class<?> beanClass, String property, ElementType elementType);
Expand Down Expand Up @@ -328,8 +328,8 @@ public interface Log extends BasicLogger {
@Message(id = 84, value = "Unable to get '%1$s' from %2$s.")
ValidationException getUnableToGetAnnotationParameterException(String parameterName, String annotationName, @Cause Exception e);

@Message(id = 85, value = "No value provided for %s.")
IllegalArgumentException getNoValueProvidedForAnnotationParameterException(String parameterName);
@Message(id = 85, value = "No value provided for parameter '%1$s' of annotation @%2$s.")
IllegalArgumentException getNoValueProvidedForAnnotationParameterException(String parameterName, String annotation);

@Message(id = 86, value = "Trying to instantiate %1$s with unknown parameter(s): %2$s.")
RuntimeException getTryingToInstantiateAnnotationWithUnknownParametersException(Class<?> annotationType, Set<String> unknownParameters);
Expand Down
Expand Up @@ -382,7 +382,7 @@ private <A extends Annotation, T> MetaConstraint<?> createMetaConstraint(Constra
annotation = AnnotationFactory.create( annotationDescriptor );
}
catch ( RuntimeException e ) {
throw log.getUnableToCreateAnnotationForConfiguredConstraintException( e.getMessage(), e );
throw log.getUnableToCreateAnnotationForConfiguredConstraintException( e );
}

java.lang.annotation.ElementType type = java.lang.annotation.ElementType.TYPE;
Expand Down
Expand Up @@ -360,7 +360,7 @@ public void testMultipleConstraintOfTheSameType() {

@Test(
expectedExceptions = ValidationException.class,
expectedExceptionsMessageRegExp = ".*No value provided for minRunner.*"
expectedExceptionsMessageRegExp = "HV000012.*"
)
public void testCustomConstraintTypeMissingParameter() {
mapping.type( Marathon.class )
Expand Down
@@ -0,0 +1,26 @@
/*
* 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.test.internal.xml.exception;

/**
* @author Hardy Ferentschik
*/
public class TestEntity {
String testString;
}


@@ -0,0 +1,56 @@
/*
* 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.test.internal.xml.exception;

import javax.validation.Configuration;
import javax.validation.ValidationException;

import org.testng.annotations.Test;

import org.hibernate.validator.testutil.TestForIssue;
import org.hibernate.validator.testutil.ValidatorUtil;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

/**
* @author Hardy Ferentschik
*/
public class XmlConfigurationExceptionTest {

@Test
@TestForIssue(jiraKey = "HV-620")
public void testMissingAnnotationParameter() {
final Configuration<?> configuration = ValidatorUtil.getConfiguration();
configuration.addMapping( XmlConfigurationExceptionTest.class.getResourceAsStream( "hv-620-mapping.xml" ) );

try {
configuration.buildValidatorFactory();
fail();
}
catch ( ValidationException e ) {
assertTrue( e.getMessage().startsWith( "HV000012" ) );
Throwable cause = e.getCause();
assertEquals(
cause.getMessage(),
"HV000085: No value provided for parameter 'regexp' of annotation @Pattern.",
"Wrong error message"
);
}
}
}
@@ -0,0 +1,15 @@
<constraint-mappings
xmlns="http://jboss.org/xml/ns/javax/validation/mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd">

<default-package>org.hibernate.validator.internal.xml</default-package>

<bean class="org.hibernate.validator.test.internal.xml.exception.TestEntity" ignore-annotations="true">
<field name="testString">
<constraint annotation="javax.validation.constraints.Pattern"/>
</field>
</bean>

</constraint-mappings>

0 comments on commit 4141674

Please sign in to comment.