Coverage Report - liquibase.exception.ValidationFailedException
 
Classes in this File Line Coverage Branch Coverage Complexity
ValidationFailedException
0%
0/75
0%
0/50
7.25
 
 1  
 package liquibase.exception;
 2  
 
 3  
 import liquibase.change.CheckSum;
 4  
 import liquibase.changelog.ChangeSet;
 5  
 import liquibase.changelog.RanChangeSet;
 6  
 import liquibase.changelog.visitor.ValidatingVisitor;
 7  
 import liquibase.database.Database;
 8  
 import liquibase.logging.LogFactory;
 9  
 import liquibase.precondition.core.ErrorPrecondition;
 10  
 import liquibase.precondition.core.FailedPrecondition;
 11  
 import liquibase.util.StreamUtil;
 12  
 
 13  
 import java.io.PrintStream;
 14  
 import java.util.List;
 15  
 import java.util.Set;
 16  
 
 17  
 public class ValidationFailedException extends MigrationFailedException {
 18  
 
 19  
     private List<ChangeSet> invalidMD5Sums;
 20  
     private List<FailedPrecondition> failedPreconditions;
 21  
     private List<ErrorPrecondition> errorPreconditions;
 22  
     private Set<ChangeSet> duplicateChangeSets;
 23  
     private List<SetupException> setupExceptions;
 24  
     private List<Throwable> changeValidationExceptions;
 25  
     private ValidationErrors validationErrors;
 26  
     private Database database;
 27  
 
 28  0
     public ValidationFailedException(ValidatingVisitor changeLogHandler) {
 29  0
         this.invalidMD5Sums = changeLogHandler.getInvalidMD5Sums();
 30  0
         this.failedPreconditions = changeLogHandler.getFailedPreconditions();
 31  0
         this.errorPreconditions = changeLogHandler.getErrorPreconditions();
 32  0
         this.duplicateChangeSets = changeLogHandler.getDuplicateChangeSets();
 33  0
         this.setupExceptions = changeLogHandler.getSetupExceptions();
 34  0
         this.changeValidationExceptions = changeLogHandler.getChangeValidationExceptions();
 35  0
         this.validationErrors = changeLogHandler.getValidationErrors();
 36  0
         this.database = changeLogHandler.getDatabase();
 37  0
     }
 38  
 
 39  
     @Override
 40  
     public String getMessage() {
 41  0
         StringBuffer message = new StringBuffer();
 42  0
         message.append("Validation Failed:").append(StreamUtil.getLineSeparator());
 43  0
         if (invalidMD5Sums.size() > 0) {
 44  0
             message.append("     ").append(invalidMD5Sums.size()).append(" change sets check sum")
 45  
                     .append(StreamUtil.getLineSeparator());
 46  0
             for (int i = 0; i < invalidMD5Sums.size(); i++) {
 47  0
                 if (i > 25) {
 48  0
                     break;
 49  
                 }
 50  0
                 ChangeSet invalid = invalidMD5Sums.get(i);
 51  
 
 52  0
                 message.append("          ").append(invalid.toString(false)).append(" is now: ")
 53  
                         .append(invalid.generateCheckSum());
 54  0
                 message.append(StreamUtil.getLineSeparator());
 55  
             }
 56  
         }
 57  0
         if (failedPreconditions.size() > 0) {
 58  0
             message.append("     ").append(failedPreconditions.size()).append(" preconditions failed")
 59  
                     .append(StreamUtil.getLineSeparator());
 60  0
             for (FailedPrecondition invalid : failedPreconditions) {
 61  0
                 message.append("     ").append(invalid.toString());
 62  0
                 message.append(StreamUtil.getLineSeparator());
 63  
             }
 64  
         }
 65  0
         if (errorPreconditions.size() > 0) {
 66  0
             message.append("     ").append(errorPreconditions.size()).append(" preconditions generated an error")
 67  
                     .append(StreamUtil.getLineSeparator());
 68  0
             for (ErrorPrecondition invalid : errorPreconditions) {
 69  0
                 message.append("     ").append(invalid.toString());
 70  0
                 message.append(StreamUtil.getLineSeparator());
 71  
             }
 72  
         }
 73  0
         if (duplicateChangeSets.size() > 0) {
 74  0
             message.append("     ").append(duplicateChangeSets.size()).append(" change sets had duplicate identifiers")
 75  
                     .append(StreamUtil.getLineSeparator());
 76  0
             for (ChangeSet invalid : duplicateChangeSets) {
 77  0
                 message.append("          ").append(invalid.toString(false));
 78  0
                 message.append(StreamUtil.getLineSeparator());
 79  
             }
 80  
         }
 81  0
         if (setupExceptions.size() > 0) {
 82  0
             message.append("     ").append(setupExceptions.size()).append(" changes have failures")
 83  
                     .append(StreamUtil.getLineSeparator());
 84  0
             for (SetupException invalid : setupExceptions) {
 85  0
                 message.append("          ").append(invalid.toString());
 86  0
                 message.append(StreamUtil.getLineSeparator());
 87  
             }
 88  
         }
 89  0
         if (changeValidationExceptions.size() > 0) {
 90  0
             message.append("     ").append(changeValidationExceptions.size()).append(" changes have validation errors")
 91  
                     .append(StreamUtil.getLineSeparator());
 92  0
             for (Throwable invalid : changeValidationExceptions) {
 93  0
                 LogFactory.getLogger().debug("validation exception", invalid);
 94  0
                 message.append("          ").append(invalid.toString());
 95  0
                 message.append(StreamUtil.getLineSeparator());
 96  
             }
 97  
         }
 98  0
         if (validationErrors.hasErrors()) {
 99  0
             message.append("     ").append(validationErrors.getErrorMessages().size())
 100  
                     .append(" changes have validation failures").append(StreamUtil.getLineSeparator());
 101  0
             for (String invalid : validationErrors.getErrorMessages()) {
 102  0
                 message.append("          ").append(invalid);
 103  0
                 message.append(StreamUtil.getLineSeparator());
 104  
             }
 105  
         }
 106  
 
 107  0
         return message.toString();
 108  
     }
 109  
 
 110  
     public List<ChangeSet> getInvalidMD5Sums() {
 111  0
         return invalidMD5Sums;
 112  
     }
 113  
 
 114  
     public void printDescriptiveError(PrintStream out) {
 115  0
         out.println("Validation Error: ");
 116  0
         if (invalidMD5Sums.size() > 0) {
 117  0
             out.println("     " + invalidMD5Sums.size()
 118  
                     + " change sets have changed since they were ran against the database");
 119  0
             for (ChangeSet changeSet : invalidMD5Sums) {
 120  0
                 out.println("          " + changeSet.toString(false));
 121  
             }
 122  
         }
 123  
 
 124  0
         if (failedPreconditions.size() > 0) {
 125  0
             out.println("     " + failedPreconditions.size() + " preconditions failed");
 126  0
             for (FailedPrecondition failedPrecondition : failedPreconditions) {
 127  0
                 out.println("          " + failedPrecondition.toString());
 128  
             }
 129  
         }
 130  0
         if (errorPreconditions.size() > 0) {
 131  0
             out.println("     " + errorPreconditions.size() + " preconditions generated an error");
 132  0
             for (ErrorPrecondition errorPrecondition : errorPreconditions) {
 133  0
                 out.println("          " + errorPrecondition.toString());
 134  
             }
 135  
         }
 136  
 
 137  0
         if (duplicateChangeSets.size() > 0) {
 138  0
             out.println("     " + duplicateChangeSets.size() + " change sets had duplicate identifiers");
 139  0
             for (ChangeSet duplicate : duplicateChangeSets) {
 140  0
                 out.println("          " + duplicate.toString(false));
 141  
             }
 142  
         }
 143  
 
 144  0
         if (setupExceptions.size() > 0) {
 145  0
             out.println("     " + setupExceptions.size() + " changes had errors");
 146  0
             for (SetupException setupEx : setupExceptions) {
 147  0
                 out.println("          " + setupEx.getMessage());
 148  
             }
 149  
         }
 150  0
     }
 151  
 }