Fix Java coding style issues.
* Remove unused parameters.
* Use map.computeIfAbsent.
* Use Immutable{Map,Set} classes.
* Add @RunWith and @Override.
Change-Id: Ib0a7c1957b69f5649dabf221628cb90aec96d3f2
diff --git a/src/main/java/com/googlesource/gerrit/plugins/findowners/OwnersValidator.java b/src/main/java/com/googlesource/gerrit/plugins/findowners/OwnersValidator.java
index ce3b36e..225cbd6 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/findowners/OwnersValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/findowners/OwnersValidator.java
@@ -140,7 +140,7 @@
try (Repository repo = repoManager.openRepository(project)) {
String name = getOwnersFileName(project);
messages =
- performValidation(repo, receiveEvent.commit, receiveEvent.revWalk, name, false);
+ performValidation(receiveEvent.commit, receiveEvent.revWalk, name, false);
}
}
} catch (NoSuchProjectException | IOException | ExecutionException e) {
@@ -154,13 +154,13 @@
@VisibleForTesting
List<CommitValidationMessage> performValidation(
- Repository repo, RevCommit c, RevWalk revWalk, String ownersFileName, boolean verbose)
+ RevCommit c, RevWalk revWalk, String ownersFileName, boolean verbose)
throws IOException, ExecutionException {
// Collect all messages from all files.
List<CommitValidationMessage> messages = new LinkedList<>();
// Collect all email addresses from all files and check each address only once.
Map<String, Set<String>> email2lines = new HashMap<>();
- Map<String, ObjectId> content = getChangedOwners(repo, c, revWalk, ownersFileName);
+ Map<String, ObjectId> content = getChangedOwners(c, revWalk, ownersFileName);
for (String path : content.keySet()) {
ObjectLoader ol = revWalk.getObjectReader().open(content.get(path));
try (InputStream in = ol.openStream()) {
@@ -228,7 +228,7 @@
int line = 0;
for (String l = br.readLine(); l != null; l = br.readLine()) {
line++;
- checkLine(messages, email2lines, path, line, l, verbose);
+ checkLine(messages, email2lines, path, line, l);
}
}
}
@@ -247,9 +247,7 @@
private static void collectEmail(
Map<String, Set<String>> map, String email, String file, int lineNumber) {
if (!email.equals("*")) {
- if (map.get(email) == null) {
- map.put(email, new HashSet<>());
- }
+ map.computeIfAbsent(email, (String k) -> new HashSet<>());
map.get(email).add(file + ":" + lineNumber);
}
}
@@ -272,8 +270,7 @@
Map<String, Set<String>> email2lines,
String path,
int lineNumber,
- String line,
- boolean verbose) {
+ String line) {
Matcher m;
if (patComment.matcher(line).find()
|| patNoParent.matcher(line).find()
@@ -294,10 +291,9 @@
* from "Path to the changed file" to "ObjectId of the file".
*/
private static Map<String, ObjectId> getChangedOwners(
- Repository repo, RevCommit c, RevWalk revWalk, String ownersFileName) throws IOException {
+ RevCommit c, RevWalk revWalk, String ownersFileName) throws IOException {
final Map<String, ObjectId> content = new HashMap<>();
visitChangedEntries(
- repo,
c,
revWalk,
new TreeWalkVisitor() {
@@ -321,7 +317,7 @@
* is found this method calls the onVisit() method of the class TreeWalkVisitor.
*/
private static void visitChangedEntries(
- Repository repo, RevCommit c, RevWalk revWalk, TreeWalkVisitor visitor) throws IOException {
+ RevCommit c, RevWalk revWalk, TreeWalkVisitor visitor) throws IOException {
try (TreeWalk tw = new TreeWalk(revWalk.getObjectReader())) {
tw.setRecursive(true);
tw.setFilter(TreeFilter.ANY_DIFF);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/findowners/OwnersValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/findowners/OwnersValidatorTest.java
index dfbc0fa..7f1c39a 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/findowners/OwnersValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/findowners/OwnersValidatorTest.java
@@ -48,11 +48,14 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
/** Test OwnersValidator, which checks syntax of changed OWNERS files. */
+@RunWith(JUnit4.class)
public class OwnersValidatorTest {
- private class MockedEmails extends Emails {
+ private static class MockedEmails extends Emails {
Set<String> registered;
MockedEmails() {
@@ -62,6 +65,7 @@
"u1@g.com", "u2@g.com", "u2.m@g.com", "user1@google.com", "u1+review@g.com");
}
+ @Override
public ImmutableSetMultimap<String, Account.Id> getAccountsFor(String... emails) {
// Used by checkEmails; each email should have exactly one Account.Id
ImmutableSetMultimap.Builder<String, Account.Id> builder = ImmutableSetMultimap.builder();
@@ -112,7 +116,7 @@
assertThat(OwnersValidator.getOwnersFileName(ANDROID_CONFIG)).isEqualTo(OWNERS_ANDROID);
}
- private static final Map<String, String> FILES_WITHOUT_OWNERS =
+ private static final ImmutableMap<String, String> FILES_WITHOUT_OWNERS =
ImmutableMap.of("README", "any\n", "d1/test.c", "int x;\n");
@Test
@@ -124,7 +128,7 @@
}
}
- private static final Map<String, String> FILES_WITH_NO_ERROR =
+ private static final ImmutableMap<String, String> FILES_WITH_NO_ERROR =
ImmutableMap.of(
OWNERS,
"\n\n#comments ...\n ### more comments\n"
@@ -136,7 +140,7 @@
+ "per-file *.txt = * # everyone can approve\n"
+ "set noparent # comment\n");
- private static final Set<String> EXPECTED_VERBOSE_OUTPUT =
+ private static final ImmutableSet<String> EXPECTED_VERBOSE_OUTPUT =
ImmutableSet.of(
"MSG: validate: " + OWNERS,
"MSG: owner: user1@google.com",
@@ -153,7 +157,7 @@
}
}
- private static final Map<String, String> FILES_WITH_WRONG_SYNTAX =
+ private static final ImmutableMap<String, String> FILES_WITH_WRONG_SYNTAX =
ImmutableMap.of(
"README",
"# some content\nu2@g.com\n",
@@ -164,13 +168,13 @@
"d3/" + OWNERS,
"\nfile: common/Owners\n");
- private static final Set<String> EXPECTED_WRONG_SYNTAX =
+ private static final ImmutableSet<String> EXPECTED_WRONG_SYNTAX =
ImmutableSet.of(
"ERROR: syntax: " + OWNERS + ":2: wrong syntax",
"ERROR: unknown: u3@g.com at d2/" + OWNERS + ":2",
"ERROR: ignored: d3/" + OWNERS + ":2: file: common/Owners");
- private static final Set<String> EXPECTED_VERBOSE_WRONG_SYNTAX =
+ private static final ImmutableSet<String> EXPECTED_VERBOSE_WRONG_SYNTAX =
ImmutableSet.of(
"MSG: validate: d3/" + OWNERS,
"MSG: validate: d2/" + OWNERS,
@@ -193,13 +197,13 @@
}
}
- private static final Map<String, String> FILES_WITH_WRONG_EMAILS =
+ private static final ImmutableMap<String, String> FILES_WITH_WRONG_EMAILS =
ImmutableMap.of("d1/" + OWNERS, "u1@g.com\n", "d2/" + OWNERS_ANDROID, "u2@g.com\n");
- private static final Set<String> EXPECTED_VERBOSE_DEFAULT =
+ private static final ImmutableSet<String> EXPECTED_VERBOSE_DEFAULT =
ImmutableSet.of("MSG: validate: d1/" + OWNERS, "MSG: owner: u1@g.com");
- private static final Set<String> EXPECTED_VERBOSE_ANDROID =
+ private static final ImmutableSet<String> EXPECTED_VERBOSE_ANDROID =
ImmutableSet.of("MSG: validate: d2/" + OWNERS_ANDROID, "MSG: owner: u2@g.com");
@Test
@@ -255,7 +259,7 @@
OwnersValidator validator = new OwnersValidator(null, null, null, myEmails);
String ownersFileName = OwnersValidator.getOwnersFileName(cfg);
List<CommitValidationMessage> m =
- validator.performValidation(repo, c, rw, ownersFileName, verbose);
+ validator.performValidation(c, rw, ownersFileName, verbose);
return transformMessages(m);
}