Skip to content

Commit

Permalink
Adding unit test for: Sonar blocker: obj could be null and is guaranteed
Browse files Browse the repository at this point in the history
to be dereferenced in org.openmrs.validator.UserValidator.validate -
TRUNK-4199
  • Loading branch information
dkayiwa committed Jan 8, 2014
1 parent 4d9b5f4 commit 5993bd4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion api/src/main/java/org/openmrs/validator/UserValidator.java
Expand Up @@ -63,11 +63,12 @@ public boolean supports(Class c) {
* @should pass validation if all required fields have proper values
* @should fail validation if email as username enabled and email invalid
* @should fail validation if email as username disabled and email provided
* @should not throw NPE when user is null
*/
public void validate(Object obj, Errors errors) {
User user = (User) obj;
if (user == null) {
errors.rejectValue("user", "error.general");
errors.reject("error.general");
} else {
if (user.isRetired() && StringUtils.isBlank(user.getRetireReason()))
errors.rejectValue("retireReason", "error.null");
Expand Down
12 changes: 12 additions & 0 deletions api/src/test/java/org/openmrs/validator/UserValidatorTest.java
Expand Up @@ -248,4 +248,16 @@ public void validate_shouldFailValidationIfEmailAsUsernameDisabledAndEmailProvid

Assert.assertTrue(errors.hasFieldErrors("username"));
}

/**
* @see UserValidator#validate(Object,Errors)
*/
@Test
@Verifies(value = "not throw NPE when user is null", method = "validate(Object,Errors)")
public void validate_shouldNotThrowNPEWhenUserIsNull() throws Exception {
UserValidator userValidator = new UserValidator();
Errors errors = new BindException(new User(), "user");
userValidator.validate(null, errors);
Assert.assertTrue(true);
}
}

0 comments on commit 5993bd4

Please sign in to comment.