Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2013
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.39 KB | None | 0 0
  1. /**
  2. * The contents of this file are subject to the OpenMRS Public License
  3. * Version 1.0 (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. *
  6. * Software distributed under the License is distributed on an "AS IS"
  7. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  8. * License for the specific language governing rights and limitations
  9. * under the License.
  10. *
  11. * Copyright (C) OpenMRS, LLC. All Rights Reserved.
  12. */
  13. package org.openmrs.validator;
  14.  
  15. import org.apache.commons.logging.Log;
  16. import org.apache.commons.logging.LogFactory;
  17. import org.openmrs.RelationshipType;
  18. import org.openmrs.annotation.Handler;
  19. import org.springframework.validation.Errors;
  20. import org.springframework.validation.ValidationUtils;
  21. import org.springframework.validation.Validator;
  22.  
  23. /**
  24. * Validates attributes on the {@link RelationshipType} object.
  25. *
  26. * @since 1.10
  27. */
  28. @Handler(supports = { RelationshipType.class }, order = 50)
  29. public class RelationshipTypeValidator implements Validator {
  30.  
  31. /** Log for this class and subclasses */
  32. protected final Log log = LogFactory.getLog(getClass());
  33.  
  34. /**
  35. * Determines if the command object being submitted is a valid type
  36. *
  37. * @see org.springframework.validation.Validator#supports(java.lang.Class)
  38. */
  39. @SuppressWarnings("unchecked")
  40. public boolean supports(Class c) {
  41. return c.equals(RelationshipType.class);
  42. }
  43.  
  44. /**
  45. * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
  46. * @should fail validation if aIsToB(or A is To B) is null or empty or whitespace
  47. * @should fail validation if bIsToA(or B is To A) is null or empty or whitespace
  48. * @should fail validation if Description is null or empty or whitespace
  49. *
  50. */
  51. public void validate(Object obj, Errors errors) {
  52. RelationshipType relationshipType = (RelationshipType) obj;
  53. if (relationshipType == null) {
  54. errors.rejectValue("relationshipType", "error.general");
  55.  
  56. }
  57. else {
  58.  
  59. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aIsToB", "RelationshipType.aIsToB.required");
  60. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bIsToA", "RelationshipType.bIsToA.required");
  61. ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "error.description");
  62.  
  63. }
  64. }
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. package org.openmrs.validator;
  74.  
  75.  
  76. import org.junit.Assert;
  77. import org.junit.Test;
  78. import org.openmrs.RelationshipType;
  79. import org.springframework.validation.BindException;
  80. import org.springframework.validation.Errors;
  81.  
  82. public class RelationshipTypeValidatorTest {
  83. /**
  84. * @see RelationshipTypeValidator#validate(Object,Errors)
  85. * @verifies fail validation if aIsToB(or A is To B) is null or empty or whitespace
  86. */
  87. @Test
  88. public void validate_shouldFailValidationIfaIsToBIsNullOrEmptyOrWhitespace()
  89. throws Exception {
  90. RelationshipType type = new RelationshipType();
  91. type.setaIsToB(null);
  92. type.setDescription("A is To B");
  93.  
  94. Errors errors = new BindException(type, "type");
  95. new RelationshipTypeValidator().validate(type, errors);
  96. Assert.assertTrue(errors.hasFieldErrors("aIsToB"));
  97.  
  98. type.setaIsToB("");
  99. errors = new BindException(type, "type");
  100. new RelationshipTypeValidator().validate(type, errors);
  101. Assert.assertTrue(errors.hasFieldErrors("aIsToB"));
  102.  
  103. type.setaIsToB(" ");
  104. errors = new BindException(type, "type");
  105. new RelationshipTypeValidator().validate(type, errors);
  106. Assert.assertTrue(errors.hasFieldErrors("aIsToB"));
  107.  
  108. }
  109.  
  110. /**
  111. * @see RelationshipTypeValidator#validate(Object,Errors)
  112. * @verifies fail validation if bIsToA(or B is To A) is null or empty or whitespace
  113. */
  114. @Test
  115. public void validate_shouldFailValidationIfbIsToAIsNullOrEmptyOrWhitespace()
  116. throws Exception {
  117. RelationshipType type = new RelationshipType();
  118. type.setbIsToA(null);
  119. type.setDescription("B is To A");
  120.  
  121. Errors errors = new BindException(type, "type");
  122. new RelationshipTypeValidator().validate(type, errors);
  123. Assert.assertTrue(errors.hasFieldErrors("bIsToA"));
  124.  
  125. type.setbIsToA("");
  126. errors = new BindException(type, "type");
  127. new RelationshipTypeValidator().validate(type, errors);
  128. Assert.assertTrue(errors.hasFieldErrors("bIsToA"));
  129.  
  130. type.setbIsToA(" ");
  131. errors = new BindException(type, "type");
  132. new RelationshipTypeValidator().validate(type, errors);
  133. Assert.assertTrue(errors.hasFieldErrors("bIsToA"));
  134.  
  135. }
  136.  
  137. /**
  138. * @see RelationshipTypeValidator#validate(Object,Errors)
  139. * @verifies fail validation if description is null or empty or whitespace
  140. */
  141. @Test
  142. public void validate_shouldFailValidationIfDescriptionIsNullOrEmptyOrWhitespace()
  143. throws Exception {
  144. RelationshipType type = new RelationshipType();
  145. type.setName("name");
  146. type.setDescription(null);
  147.  
  148. Errors errors = new BindException(type, "type");
  149. new RelationshipTypeValidator().validate(type, errors);
  150. Assert.assertFalse(errors.hasFieldErrors("description"));
  151.  
  152. type.setDescription("");
  153. errors = new BindException(type, "type");
  154. new RelationshipTypeValidator().validate(type, errors);
  155. Assert.assertFalse(errors.hasFieldErrors("description"));
  156.  
  157. type.setDescription(" ");
  158. errors = new BindException(type, "type");
  159. new RelationshipTypeValidator().validate(type, errors);
  160. Assert.assertFalse(errors.hasFieldErrors("description"));
  161.  
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement