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

View File

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

View File

@@ -14,51 +14,26 @@
package com.google.gerrit.server.git; package com.google.gerrit.server.git;
import java.util.Objects; import com.google.auto.value.AutoValue;
/** Indicates a problem with Git based data. */ /** Indicates a problem with Git based data. */
public class ValidationError { @AutoValue
private final String message; public abstract class ValidationError {
public abstract String getMessage();
public ValidationError(String file, String message) { public static ValidationError create(String file, String message) {
this(file + ": " + message); return create(file + ": " + message);
} }
public ValidationError(String file, int line, String message) { public static ValidationError create(String file, int line, String message) {
this(file + ":" + line + ": " + message); return create(file + ":" + line + ": " + message);
} }
public ValidationError(String message) { public static ValidationError create(String message) {
this.message = message; return new AutoValue_ValidationError(message);
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "ValidationError[" + message + "]";
} }
public interface Sink { public interface Sink {
void error(ValidationError error); 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'); int tab = s.indexOf('\t');
if (tab < 0) { if (tab < 0) {
errors.error(new ValidationError(filename, lineNumber, "missing tab delimiter")); errors.error(ValidationError.create(filename, lineNumber, "missing tab delimiter"));
continue; continue;
} }

View File

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

View File

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

View File

@@ -101,7 +101,8 @@ public class QueryListTest {
public void testParseBad() throws Exception { public void testParseBad() throws Exception {
List<ValidationError> errors = new ArrayList<>(); List<ValidationError> errors = new ArrayList<>();
assertThat(QueryList.parse(L_BAD, errors::add).asText()).isNull(); 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 @Test

View File

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