Make ValidationError an AutoValue

Change-Id: I4da4e0bd67f4127f3f37b68f7166b3c024b3f9cf
This commit is contained in:
Patrick Hiesel
2020-06-22 15:48:38 +02:00
parent 7f5182251e
commit 83bd811410
8 changed files with 47 additions and 69 deletions

View File

@@ -219,7 +219,7 @@ public class ProjectWatches {
int i = notifyValue.lastIndexOf('[');
if (i < 0 || notifyValue.charAt(notifyValue.length() - 1) != ']') {
validationErrorSink.error(
new ValidationError(
ValidationError.create(
WATCH_CONFIG,
String.format(
"Invalid project watch of account %d for project %s: %s",
@@ -240,7 +240,7 @@ public class ProjectWatches {
NotifyType notifyType = Enums.getIfPresent(NotifyType.class, nt).orNull();
if (notifyType == null) {
validationErrorSink.error(
new ValidationError(
ValidationError.create(
WATCH_CONFIG,
String.format(
"Invalid notify type %s in project watch "

View File

@@ -183,7 +183,7 @@ public class StoredPreferences {
return PreferencesParserUtil.parseGeneralPreferences(cfg, defaultCfg, input);
} catch (ConfigInvalidException e) {
validationErrorSink.error(
new ValidationError(
ValidationError.create(
PREFERENCES_CONFIG,
String.format(
"Invalid general preferences for account %d: %s",
@@ -197,7 +197,7 @@ public class StoredPreferences {
return PreferencesParserUtil.parseDiffPreferences(cfg, defaultCfg, input);
} catch (ConfigInvalidException e) {
validationErrorSink.error(
new ValidationError(
ValidationError.create(
PREFERENCES_CONFIG,
String.format(
"Invalid diff preferences for account %d: %s", accountId.get(), e.getMessage())));
@@ -210,7 +210,7 @@ public class StoredPreferences {
return PreferencesParserUtil.parseEditPreferences(cfg, defaultCfg, input);
} catch (ConfigInvalidException e) {
validationErrorSink.error(
new ValidationError(
ValidationError.create(
PREFERENCES_CONFIG,
String.format(
"Invalid edit preferences for account %d: %s", accountId.get(), e.getMessage())));

View File

@@ -14,51 +14,26 @@
package com.google.gerrit.server.git;
import java.util.Objects;
import com.google.auto.value.AutoValue;
/** Indicates a problem with Git based data. */
public class ValidationError {
private final String message;
@AutoValue
public abstract class ValidationError {
public abstract String getMessage();
public ValidationError(String file, String message) {
this(file + ": " + message);
public static ValidationError create(String file, String message) {
return create(file + ": " + message);
}
public ValidationError(String file, int line, String message) {
this(file + ":" + line + ": " + message);
public static ValidationError create(String file, int line, String message) {
return create(file + ":" + line + ": " + message);
}
public ValidationError(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "ValidationError[" + message + "]";
public static ValidationError create(String message) {
return new AutoValue_ValidationError(message);
}
public interface Sink {
void error(ValidationError error);
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof ValidationError) {
ValidationError that = (ValidationError) o;
return Objects.equals(this.message, that.message);
}
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(message);
}
}

View File

@@ -59,7 +59,7 @@ public class TabFile {
int tab = s.indexOf('\t');
if (tab < 0) {
errors.error(new ValidationError(filename, lineNumber, "missing tab delimiter"));
errors.error(ValidationError.create(filename, lineNumber, "missing tab delimiter"));
continue;
}

View File

@@ -584,7 +584,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
if (rc.getStringList(ACCESS, null, KEY_INHERIT_FROM).length > 1) {
// The config must not contain more than one parent to inherit from
// as there is no guarantee which of the parents would be used then.
error(new ValidationError(PROJECT_CONFIG, "Cannot inherit from multiple projects"));
error(ValidationError.create(PROJECT_CONFIG, "Cannot inherit from multiple projects"));
}
p.setParentName(rc.getString(ACCESS, null, KEY_INHERIT_FROM));
@@ -634,7 +634,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
String lower = name.toLowerCase();
if (lowerNames.containsKey(lower)) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Extension Panels \"%s\" conflicts with \"%s\"", name, lowerNames.get(lower))));
@@ -663,7 +663,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
ca.setAutoVerify(null);
} else if (rules.size() > 1) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
"Invalid rule in "
+ CONTRIBUTOR_AGREEMENT
@@ -674,7 +674,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
+ ": at most one group may be set"));
} else if (rules.get(0).getAction() != Action.ALLOW) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
"Invalid rule in "
+ CONTRIBUTOR_AGREEMENT
@@ -730,18 +730,18 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
n.addEmail(ref);
} else {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
"group \"" + ref.getName() + "\" not in " + GroupList.FILE_NAME));
}
} else if (dst.startsWith("user ")) {
error(new ValidationError(PROJECT_CONFIG, dst + " not supported"));
error(ValidationError.create(PROJECT_CONFIG, dst + " not supported"));
} else {
try {
n.addEmail(Address.parse(dst));
} catch (IllegalArgumentException err) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
"notify section \"" + sectionName + "\" has invalid email \"" + dst + "\""));
}
@@ -801,7 +801,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
try {
RefPattern.validateRegExp(refPattern);
} catch (InvalidNameException e) {
error(new ValidationError(PROJECT_CONFIG, "Invalid ref name: " + e.getMessage()));
error(ValidationError.create(PROJECT_CONFIG, "Invalid ref name: " + e.getMessage()));
return false;
}
return true;
@@ -822,7 +822,9 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
// to fail fast if any of the patterns are invalid.
patterns.add(Pattern.compile(patternString).pattern());
} catch (PatternSyntaxException e) {
error(new ValidationError(PROJECT_CONFIG, "Invalid regular expression: " + e.getMessage()));
error(
ValidationError.create(
PROJECT_CONFIG, "Invalid regular expression: " + e.getMessage()));
continue;
}
}
@@ -849,7 +851,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
rule = PermissionRule.fromString(ruleString, useRange);
} catch (IllegalArgumentException notRule) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
"Invalid rule in "
+ section
@@ -869,7 +871,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
//
ref = groupList.resolve(rule.getGroup());
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG, "group \"" + ref.getName() + "\" not in " + GroupList.FILE_NAME));
}
@@ -896,7 +898,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
String lower = name.toLowerCase();
if (lowerNames.containsKey(lower)) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format("Label \"%s\" conflicts with \"%s\"", name, lowerNames.get(lower))));
}
@@ -911,13 +913,13 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
values.add(labelValue);
} else {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format("Duplicate %s \"%s\" for label \"%s\"", KEY_VALUE, value, name)));
}
} catch (IllegalArgumentException notValue) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Invalid %s \"%s\" for label \"%s\": %s",
@@ -929,7 +931,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
try {
label = new LabelType(name, values);
} catch (IllegalArgumentException badName) {
error(new ValidationError(PROJECT_CONFIG, String.format("Invalid label \"%s\"", name)));
error(ValidationError.create(PROJECT_CONFIG, String.format("Invalid label \"%s\"", name)));
continue;
}
@@ -940,7 +942,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
: Optional.of(LabelFunction.MAX_WITH_BLOCK);
if (!function.isPresent()) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Invalid %s for label \"%s\". Valid names are: %s",
@@ -954,7 +956,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
label.setDefaultValue(dv);
} else {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Invalid %s \"%s\" for label \"%s\"", KEY_DEFAULT_VALUE, dv, name)));
@@ -1000,14 +1002,14 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
short copyValue = Shorts.checkedCast(PermissionRule.parseInt(value));
if (!copyValues.add(copyValue)) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Duplicate %s \"%s\" for label \"%s\"", KEY_COPY_VALUE, value, name)));
}
} catch (IllegalArgumentException notValue) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Invalid %s \"%s\" for label \"%s\": %s",
@@ -1045,14 +1047,14 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
commentLinkSections.put(name, buildCommentLink(rc, name, false));
} catch (PatternSyntaxException e) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Invalid pattern \"%s\" in commentlink.%s.match: %s",
rc.getString(COMMENTLINK, name, KEY_MATCH), name, e.getMessage())));
} catch (IllegalArgumentException e) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG,
String.format(
"Error in pattern \"%s\" in commentlink.%s.match: %s",
@@ -1099,7 +1101,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
GroupReference ref = groupList.byName(groupName);
if (ref == null) {
error(
new ValidationError(
ValidationError.create(
PROJECT_CONFIG, "group \"" + groupName + "\" not in " + GroupList.FILE_NAME));
}
rc.setString(PLUGIN, plugin, name, value);
@@ -1572,7 +1574,7 @@ public class ProjectConfig extends VersionedMetaData implements ValidationError.
try {
return rc.getEnum(section, subsection, name, defaultValue);
} catch (IllegalArgumentException err) {
error(new ValidationError(PROJECT_CONFIG, err.getMessage()));
error(ValidationError.create(PROJECT_CONFIG, err.getMessage()));
return defaultValue;
}
}

View File

@@ -132,7 +132,7 @@ public class DestinationListTest {
List<ValidationError> errors = new ArrayList<>();
new DestinationList().parseLabel(LABEL, L_BAD, errors::add);
assertThat(errors)
.containsExactly(new ValidationError("destinationslabel", 1, "missing tab delimiter"));
.containsExactly(ValidationError.create("destinationslabel", 1, "missing tab delimiter"));
}
@Test

View File

@@ -101,7 +101,8 @@ public class QueryListTest {
public void testParseBad() throws Exception {
List<ValidationError> errors = new ArrayList<>();
assertThat(QueryList.parse(L_BAD, errors::add).asText()).isNull();
assertThat(errors).containsExactly(new ValidationError("queries", 1, "missing tab delimiter"));
assertThat(errors)
.containsExactly(ValidationError.create("queries", 1, "missing tab delimiter"));
}
@Test

View File

@@ -571,7 +571,7 @@ public class ProjectConfigTest {
assertThat(cfg.getCommentLinkSections()).isEmpty();
assertThat(cfg.getValidationErrors())
.containsExactly(
new ValidationError(
ValidationError.create(
"project.config: Invalid pattern \"(bugs{+#?)(d+)\" in commentlink.bugzilla.match: "
+ "Illegal repetition near index 4\n"
+ "(bugs{+#?)(d+)\n"
@@ -592,7 +592,7 @@ public class ProjectConfigTest {
assertThat(cfg.getCommentLinkSections()).isEmpty();
assertThat(cfg.getValidationErrors())
.containsExactly(
new ValidationError(
ValidationError.create(
"project.config: Error in pattern \"(bugs#?)(d+)\" in commentlink.bugzilla.match: "
+ "Raw html replacement not allowed"));
}
@@ -607,7 +607,7 @@ public class ProjectConfigTest {
assertThat(cfg.getCommentLinkSections()).isEmpty();
assertThat(cfg.getValidationErrors())
.containsExactly(
new ValidationError(
ValidationError.create(
"project.config: Error in pattern \"(bugs#?)(d+)\" in commentlink.bugzilla.match: "
+ "commentlink.bugzilla must have either link or html"));
}