001: package liquibase.exception;
002:
003: import liquibase.ChangeSet;
004: import liquibase.parser.visitor.ValidatingVisitor;
005: import liquibase.preconditions.FailedPrecondition;
006: import liquibase.util.StreamUtil;
007:
008: import java.io.PrintStream;
009: import java.util.List;
010: import java.util.Set;
011:
012: public class ValidationFailedException extends MigrationFailedException {
013:
014: private static final long serialVersionUID = 1L;
015:
016: private List<ChangeSet> invalidMD5Sums;
017: private List<FailedPrecondition> failedPreconditions;
018: private Set<ChangeSet> duplicateChangeSets;
019: private List<SetupException> setupExceptions;
020:
021: public ValidationFailedException(ValidatingVisitor changeLogHandler) {
022: this .invalidMD5Sums = changeLogHandler.getInvalidMD5Sums();
023: this .failedPreconditions = changeLogHandler
024: .getFailedPreconditions();
025: this .duplicateChangeSets = changeLogHandler
026: .getDuplicateChangeSets();
027: this .setupExceptions = changeLogHandler.getSetupExceptions();
028:
029: }
030:
031: public String getMessage() {
032: StringBuffer message = new StringBuffer();
033: message.append("Validation Failed:").append(
034: StreamUtil.getLineSeparator());
035: if (invalidMD5Sums.size() > 0) {
036: message.append(" ").append(invalidMD5Sums.size())
037: .append(" change sets failed MD5Sum Check").append(
038: StreamUtil.getLineSeparator());
039: for (int i = 0; i < invalidMD5Sums.size(); i++) {
040: if (i > 25) {
041: break;
042: }
043: ChangeSet invalid = invalidMD5Sums.get(i);
044: message.append(" ").append(
045: invalid.toString(true));
046: message.append(StreamUtil.getLineSeparator());
047: }
048: }
049: if (failedPreconditions.size() > 0) {
050: message.append(" ").append(failedPreconditions.size())
051: .append(" preconditions failed").append(
052: StreamUtil.getLineSeparator());
053: for (FailedPrecondition invalid : failedPreconditions) {
054: message.append(" ").append(invalid.toString());
055: message.append(StreamUtil.getLineSeparator());
056: }
057: }
058: if (duplicateChangeSets.size() > 0) {
059: message.append(" ").append(duplicateChangeSets.size())
060: .append(" change sets had duplicate identifiers")
061: .append(StreamUtil.getLineSeparator());
062: for (ChangeSet invalid : duplicateChangeSets) {
063: message.append(" ").append(
064: invalid.toString(false));
065: message.append(StreamUtil.getLineSeparator());
066: }
067: }
068: if (setupExceptions.size() > 0) {
069: message.append(" ").append(setupExceptions.size())
070: .append(" changes have failures").append(
071: StreamUtil.getLineSeparator());
072: for (SetupException invalid : setupExceptions) {
073: message.append(" ").append(invalid.toString());
074: message.append(StreamUtil.getLineSeparator());
075: }
076: }
077:
078: return message.toString();
079: }
080:
081: public List<ChangeSet> getInvalidMD5Sums() {
082: return invalidMD5Sums;
083: }
084:
085: public void printDescriptiveError(PrintStream out) {
086: out.println("Validation Error: ");
087: if (invalidMD5Sums.size() > 0) {
088: out
089: .println(" "
090: + invalidMD5Sums.size()
091: + " change sets have changed since they were ran against the database");
092: for (ChangeSet changeSet : invalidMD5Sums) {
093: out.println(" " + changeSet.toString(false));
094: }
095: }
096:
097: if (failedPreconditions.size() > 0) {
098: out.println(" " + failedPreconditions.size()
099: + " preconditions failed");
100: for (FailedPrecondition failedPrecondition : failedPreconditions) {
101: out.println(" "
102: + failedPrecondition.toString());
103: }
104: }
105:
106: if (duplicateChangeSets.size() > 0) {
107: out.println(" " + duplicateChangeSets.size()
108: + " change sets had duplicate identifiers");
109: for (ChangeSet duplicate : duplicateChangeSets) {
110: out.println(" " + duplicate.toString(false));
111: }
112: }
113:
114: if (setupExceptions.size() > 0) {
115: out.println(" " + setupExceptions.size()
116: + " changes had errors");
117: for (SetupException setupEx : setupExceptions) {
118: out.println(" " + setupEx.getMessage());
119: }
120: }
121: }
122: }
|