Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ValidationErrors |
|
| 2.7142857142857144;2.714 |
1 | package liquibase.exception; | |
2 | ||
3 | import liquibase.database.Database; | |
4 | import liquibase.changelog.ChangeSet; | |
5 | ||
6 | import java.util.ArrayList; | |
7 | import java.util.Collection; | |
8 | import java.util.List; | |
9 | ||
10 | 1025 | public class ValidationErrors { |
11 | ||
12 | 1025 | protected List<String> errorMessages = new ArrayList<String>(); |
13 | ||
14 | public boolean hasErrors() { | |
15 | 571 | return errorMessages.size() > 0; |
16 | } | |
17 | ||
18 | public void checkRequiredField(String requiredFieldName, Object value) { | |
19 | 1080 | if (value == null) { |
20 | 757 | addError(requiredFieldName + " is required"); |
21 | 323 | } else if (value instanceof Collection && ((Collection) value).size() == 0) { |
22 | 16 | addError(requiredFieldName + " is empty"); |
23 | 307 | } else if (value instanceof Object[] && ((Object[]) value).length == 0) { |
24 | 15 | addError(requiredFieldName + " is empty"); |
25 | } | |
26 | 1080 | } |
27 | ||
28 | public void checkDisallowedField(String disallowedFieldName, Object value, Database database, | |
29 | Class<? extends Database>... disallowedDatabases) { | |
30 | 98 | boolean isDisallowed = false; |
31 | 98 | if (disallowedDatabases == null || disallowedDatabases.length == 0) { |
32 | 0 | isDisallowed = true; |
33 | } else { | |
34 | 278 | for (Class<? extends Database> databaseClass : disallowedDatabases) { |
35 | 180 | if (databaseClass.isAssignableFrom(database.getClass())) { |
36 | 18 | isDisallowed = true; |
37 | } | |
38 | } | |
39 | } | |
40 | ||
41 | 98 | if (isDisallowed && value != null) { |
42 | 0 | addError(disallowedFieldName + " is not allowed on " + database.getTypeName()); |
43 | } | |
44 | 98 | } |
45 | ||
46 | public void addError(String message) { | |
47 | 840 | errorMessages.add(message); |
48 | 840 | } |
49 | ||
50 | public List<String> getErrorMessages() { | |
51 | 449 | return errorMessages; |
52 | } | |
53 | ||
54 | public void addAll(ValidationErrors validationErrors) { | |
55 | 431 | this.errorMessages.addAll(validationErrors.getErrorMessages()); |
56 | 431 | } |
57 | ||
58 | public void addAll(ValidationErrors validationErrors, ChangeSet changeSet) { | |
59 | 1 | for (String message : validationErrors.getErrorMessages()) { |
60 | 2 | this.errorMessages.add(message + ", " + changeSet); |
61 | } | |
62 | 1 | } |
63 | } |