Skip to content

Commit 5a228e0

Browse files
committedJan 8, 2014
Sonar blocker: obj could be null and is guaranteed to be dereferenced in
org.openmrs.validator.UserValidator.validate - TRUNK-4199
1 parent 9826ac2 commit 5a228e0

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed
 

‎api/src/main/java/org/openmrs/validator/UserValidator.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,12 @@ public boolean supports(Class c) {
6262
* @should pass validation if all required fields have proper values
6363
* @should fail validation if email as username enabled and email invalid
6464
* @should fail validation if email as username disabled and email provided
65+
* @should not throw NPE when user is null
6566
*/
6667
public void validate(Object obj, Errors errors) {
6768
User user = (User) obj;
6869
if (user == null) {
69-
errors.rejectValue("user", "error.general");
70+
errors.reject("error.general");
7071
} else {
7172
if (user.isRetired() && StringUtils.isBlank(user.getRetireReason()))
7273
errors.rejectValue("retireReason", "error.null");
@@ -84,19 +85,20 @@ public void validate(Object obj, Errors errors) {
8485
if (person.getPersonName() == null || StringUtils.isEmpty(person.getPersonName().toString()))
8586
errors.rejectValue("person", "Person.names.length");
8687
}
87-
}
88-
AdministrationService as = Context.getAdministrationService();
89-
boolean emailAsUsername = Boolean.parseBoolean(as.getGlobalProperty(
90-
OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "false"));
91-
if (emailAsUsername) {
92-
boolean isValidUserName = isUserNameAsEmailValid(user.getUsername());
93-
if (!isValidUserName) {
94-
errors.rejectValue("username", "error.username.email");
95-
}
96-
} else {
97-
boolean isValidUserName = isUserNameValid(user.getUsername());
98-
if (!isValidUserName) {
99-
errors.rejectValue("username", "error.username.pattern");
88+
89+
AdministrationService as = Context.getAdministrationService();
90+
boolean emailAsUsername = Boolean.parseBoolean(as.getGlobalProperty(
91+
OpenmrsConstants.GLOBAL_PROPERTY_USER_REQUIRE_EMAIL_AS_USERNAME, "false"));
92+
if (emailAsUsername) {
93+
boolean isValidUserName = isUserNameAsEmailValid(user.getUsername());
94+
if (!isValidUserName) {
95+
errors.rejectValue("username", "error.username.email");
96+
}
97+
} else {
98+
boolean isValidUserName = isUserNameValid(user.getUsername());
99+
if (!isValidUserName) {
100+
errors.rejectValue("username", "error.username.pattern");
101+
}
100102
}
101103
}
102104
}

‎api/src/test/java/org/openmrs/validator/UserValidatorTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,16 @@ public void validate_shouldFailValidationIfEmailAsUsernameDisabledAndEmailProvid
248248

249249
Assert.assertTrue(errors.hasFieldErrors("username"));
250250
}
251+
252+
/**
253+
* @see UserValidator#validate(Object,Errors)
254+
*/
255+
@Test
256+
@Verifies(value = "not throw NPE when user is null", method = "validate(Object,Errors)")
257+
public void validate_shouldNotThrowNPEWhenUserIsNull() throws Exception {
258+
UserValidator userValidator = new UserValidator();
259+
Errors errors = new BindException(new User(), "user");
260+
userValidator.validate(null, errors);
261+
Assert.assertTrue(true);
262+
}
251263
}

0 commit comments

Comments
 (0)
Please sign in to comment.