|
| 1 | +package com.baeldung.param; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +/** |
| 6 | + * Somewhat contrived validation class to illustrate unit test |
| 7 | + * concepts. |
| 8 | + * |
| 9 | + * @author J Steven Perry |
| 10 | + * |
| 11 | + */ |
| 12 | +public class PersonValidator { |
| 13 | + |
| 14 | + /** |
| 15 | + * Contrived checked exception to illustrate one possible |
| 16 | + * way to handle validation errors (via a checked exception). |
| 17 | + * |
| 18 | + * @author J Steven Perry |
| 19 | + * |
| 20 | + */ |
| 21 | + public static class ValidationException extends Exception { |
| 22 | + |
| 23 | + /** |
| 24 | + * |
| 25 | + */ |
| 26 | + private static final long serialVersionUID = -134518049431883102L; |
| 27 | + |
| 28 | + // Probably should implement some more constructors, but don't want |
| 29 | + /// to tarnish the lesson... |
| 30 | + |
| 31 | + /** |
| 32 | + * The one and only way to create this checked exception. |
| 33 | + * |
| 34 | + * @param message |
| 35 | + * The message accompanying the exception. Should be meaningful. |
| 36 | + */ |
| 37 | + public ValidationException(String message) { |
| 38 | + super(message); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + } |
| 43 | + |
| 44 | + private static final String[] ILLEGAL_NAME_CHARACTERS = { |
| 45 | + ",", |
| 46 | + "_", |
| 47 | + "{", |
| 48 | + "}", |
| 49 | + "!" |
| 50 | + }; |
| 51 | + |
| 52 | + /** |
| 53 | + * Validate the first name of the specified Person object. |
| 54 | + * |
| 55 | + * @param person |
| 56 | + * The Person object to validate. |
| 57 | + * |
| 58 | + * @return - returns true if the specified Person is valid |
| 59 | + * |
| 60 | + * @throws ValidationException |
| 61 | + * - this Exception is thrown if any kind of validation error occurs. |
| 62 | + */ |
| 63 | + public static boolean validateFirstName(Person person) throws ValidationException { |
| 64 | + boolean ret = true; |
| 65 | + // The validation rules go here. |
| 66 | + // Naive: use simple ifs |
| 67 | + if (person == null) { |
| 68 | + throw new ValidationException("Person is null (not allowed)!"); |
| 69 | + } |
| 70 | + if (person.getFirstName() == null) { |
| 71 | + throw new ValidationException("Person FirstName is null (not allowed)!"); |
| 72 | + } |
| 73 | + if (person.getFirstName().isEmpty()) { |
| 74 | + throw new ValidationException("Person FirstName is an empty String (not allowed)!"); |
| 75 | + } |
| 76 | + if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) { |
| 77 | + throw new ValidationException( |
| 78 | + "Person FirstName (" + person.getFirstName() + ") may not contain any of the following characters: " |
| 79 | + + Arrays.toString(ILLEGAL_NAME_CHARACTERS) |
| 80 | + + "!"); |
| 81 | + } |
| 82 | + return ret; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Validate the last name of the specified Person object. Looks the same as first |
| 87 | + * name? Look closer. Just kidding. It's the same. But real world code can (and will) diverge. |
| 88 | + * |
| 89 | + * @param person |
| 90 | + * The Person object to validate. |
| 91 | + * |
| 92 | + * @return - returns true if the specified Person is valid |
| 93 | + * |
| 94 | + * @throws ValidationException |
| 95 | + * - this Exception is thrown if any kind of validation error occurs. |
| 96 | + */ |
| 97 | + public static boolean validateLastName(Person person) throws ValidationException { |
| 98 | + boolean ret = true; |
| 99 | + // The validation rules go here. |
| 100 | + // Naive: use simple ifs |
| 101 | + if (person == null) { |
| 102 | + throw new ValidationException("Person is null (not allowed)!"); |
| 103 | + } |
| 104 | + if (person.getFirstName() == null) { |
| 105 | + throw new ValidationException("Person FirstName is null (not allowed)!"); |
| 106 | + } |
| 107 | + if (person.getFirstName().isEmpty()) { |
| 108 | + throw new ValidationException("Person FirstName is an empty String (not allowed)!"); |
| 109 | + } |
| 110 | + if (!isStringValid(person.getFirstName(), ILLEGAL_NAME_CHARACTERS)) { |
| 111 | + throw new ValidationException( |
| 112 | + "Person LastName (" + person.getLastName() + ") may not contain any of the following characters: " |
| 113 | + + Arrays.toString(ILLEGAL_NAME_CHARACTERS) |
| 114 | + + "!"); |
| 115 | + } |
| 116 | + return ret; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Validates the specified name. If it contains any of the illegalCharacters, |
| 121 | + * this method returns false (indicating the name is illegal). Otherwise it returns true. |
| 122 | + * |
| 123 | + * @param candidate |
| 124 | + * The candidate String to validate |
| 125 | + * |
| 126 | + * @param illegalCharacters |
| 127 | + * The characters the String is not allowed to have |
| 128 | + * |
| 129 | + * @return - boolean - true if the name is valid, false otherwise. |
| 130 | + */ |
| 131 | + private static boolean isStringValid(String candidate, String[] illegalCharacters) { |
| 132 | + boolean ret = true; |
| 133 | + for (String illegalChar : illegalCharacters) { |
| 134 | + if (candidate.contains(illegalChar)) { |
| 135 | + ret = false; |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | + return ret; |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments