Skip to content

Commit

Permalink
TRUNK-4296: Create new ValidationException and throw it instead of AP…
Browse files Browse the repository at this point in the history
…IException when validation fails
  • Loading branch information
mogoodrich committed Mar 10, 2014
1 parent 671f44a commit dfa3966
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
45 changes: 45 additions & 0 deletions api/src/main/java/org/openmrs/api/ValidationException.java
Expand Up @@ -13,6 +13,8 @@
*/
package org.openmrs.api;

import org.springframework.validation.Errors;

/**
* Represents often fatal errors that occur when an object fails validation
*
Expand All @@ -22,6 +24,13 @@ public class ValidationException extends APIException {

public static final long serialVersionUID = 1L;

/**
* Spring Errors object associated with the validation failure
* @since 1.11
*/

private Errors errors;

/**
* Default empty constructor. If at all possible, don't use this one, but use the
*/
Expand Down Expand Up @@ -57,4 +66,40 @@ public ValidationException(String message, Throwable cause) {
public ValidationException(Throwable cause) {
super(cause);
}

/**
* Constructor used to associate an Spring Errors object with a ValidationException
*
* @param errors
* @since 1.11
*/
public ValidationException(Errors errors) {
this.errors = errors;
}

/**
* Constructor to give the end user a helpful message and to associate an Spring Errors object
* with a ValidationException
*
* @param errors
* @since 1.11
*/
public ValidationException(String message, Errors errors) {
super(message);
this.errors = errors;
}

/**
* @since 1.11
*/
public Errors getErrors() {
return errors;
}

/**
* @since 1.11
*/
public void setErrors(Errors errors) {
this.errors = errors;
}
}
5 changes: 2 additions & 3 deletions api/src/main/java/org/openmrs/validator/ValidateUtil.java
Expand Up @@ -18,7 +18,6 @@
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.openmrs.api.APIException;
import org.openmrs.api.ValidationException;
import org.openmrs.api.context.Context;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -64,7 +63,7 @@ public void setValidators(List<Validator> newValidators) {
* @should throw APIException if errors occur during validation
*/
public static void validate(Object obj) throws ValidationException {
BindException errors = new BindException(obj, "");
Errors errors = new BindException(obj, "");

Context.getAdministrationService().validate(obj, errors);

Expand All @@ -81,7 +80,7 @@ public static void validate(Object obj) throws ValidationException {

String exceptionMessage = "'" + obj + "' failed to validate with reason: ";
exceptionMessage += StringUtils.join(uniqueErrorMessages, ", ");
throw new ValidationException(exceptionMessage, errors.getCause());
throw new ValidationException(exceptionMessage, errors);
}
}

Expand Down
30 changes: 23 additions & 7 deletions api/src/test/java/org/openmrs/validator/ValidateUtilTest.java
@@ -1,17 +1,18 @@
package org.openmrs.validator;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openmrs.Location;
import org.openmrs.PatientIdentifierType;
import org.openmrs.api.APIException;
import org.openmrs.api.ValidationException;
import org.openmrs.test.BaseContextSensitiveTest;
import org.openmrs.test.Verifies;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
* Tests methods on the {@link ValidateUtil} class.
*/
Expand All @@ -20,13 +21,28 @@ public class ValidateUtilTest extends BaseContextSensitiveTest {
/**
* @see {@link ValidateUtil#validate(Object)}
*/
@Test(expected = APIException.class)
@Verifies(value = "should throw APIException if errors occur during validation", method = "validate(Object)")
public void validate_shouldThrowAPIExceptionIfErrorsOccurDuringValidation() throws Exception {
@Test(expected = ValidationException.class)
@Verifies(value = "should throw ValidationException if errors occur during validation", method = "validate(Object)")
public void validate_shouldThrowValidationExceptionIfErrorsOccurDuringValidation() throws Exception {
Location loc = new Location();
ValidateUtil.validate(loc);
}

@Test
@Verifies(value = "should return Spring errors in ValidationException", method = "validate(Object)")
public void validate_shouldThrowAPIExceptionIfErrorsOccurDuringValidation() throws Exception {
Location loc = new Location();

try {
ValidateUtil.validate(loc);
}
catch (ValidationException validationException) {
assertNotNull(validationException.getErrors());
assertTrue(validationException.getErrors().hasErrors());
}

}

/**
* @see {@link ValidateUtil#validateFieldLengths(org.springframework.validation.Errors, Class, String...)}
*/
Expand Down

0 comments on commit dfa3966

Please sign in to comment.