Merge "Fix test for skipOnlyOnSpecificRef"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
index a84b62d..5c5dc12 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidator.java
@@ -59,8 +59,8 @@
import java.util.regex.Pattern;
public class BlockedKeywordValidator implements CommitValidationListener {
- private static String KEY_CHECK_BLOCKED_KEYWORD = "blockedKeyword";
- private static String KEY_CHECK_BLOCKED_KEYWORD_PATTERN =
+ private static final String KEY_CHECK_BLOCKED_KEYWORD = "blockedKeyword";
+ private static final String KEY_CHECK_BLOCKED_KEYWORD_PATTERN =
KEY_CHECK_BLOCKED_KEYWORD + "Pattern";
public static AbstractModule module() {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java
index ab1752d..493a108 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeUtil.java
@@ -38,7 +38,7 @@
import java.util.regex.Pattern;
public class ContentTypeUtil {
- private static String KEY_BINARY_TYPES = "binaryTypes";
+ private static final String KEY_BINARY_TYPES = "binaryTypes";
public static AbstractModule module() {
return new AbstractModule() {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java
index 99fd624..ed236e1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidator.java
@@ -70,8 +70,8 @@
};
}
- public static String KEY_BLOCKED_CONTENT_TYPE = "blockedContentType";
- public static String KEY_BLOCKED_CONTENT_TYPE_WHITELIST =
+ public static final String KEY_BLOCKED_CONTENT_TYPE = "blockedContentType";
+ public static final String KEY_BLOCKED_CONTENT_TYPE_WHITELIST =
"blockedContentTypeWhitelist";
@VisibleForTesting
@@ -147,8 +147,8 @@
String contentType = contentTypeUtil.getContentType(os, path);
if ((contentTypeUtil.matchesAny(contentType, blockedTypes)
&& !whitelist)
- || !contentTypeUtil.matchesAny(contentType, blockedTypes)
- && whitelist) {
+ || (!contentTypeUtil.matchesAny(contentType, blockedTypes)
+ && whitelist)) {
messages.add(new CommitValidationMessage(
"found blocked content type (" + contentType + ") in file: "
+ path, true));
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java
index 6eb0276..72ae551 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidator.java
@@ -88,9 +88,9 @@
};
}
- public static String KEY_REJECT_DUPLICATE_PATHNAMES =
+ public static final String KEY_REJECT_DUPLICATE_PATHNAMES =
"rejectDuplicatePathnames";
- public static String KEY_REJECT_DUPLICATE_PATHNAMES_LOCALE =
+ public static final String KEY_REJECT_DUPLICATE_PATHNAMES_LOCALE =
"rejectDuplicatePathnamesLocale";
@VisibleForTesting
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java
index b8d2ae3..79cac3f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FileExtensionValidator.java
@@ -59,7 +59,8 @@
};
}
- public static String KEY_BLOCKED_FILE_EXTENSION = "blockedFileExtension";
+ public static final String KEY_BLOCKED_FILE_EXTENSION =
+ "blockedFileExtension";
private final String pluginName;
private final PluginConfigFactory cfgFactory;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FooterValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FooterValidator.java
index 6127cbe..f4c5810 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FooterValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/FooterValidator.java
@@ -58,7 +58,7 @@
};
}
- public static String KEY_REQUIRED_FOOTER = "requiredFooter";
+ public static final String KEY_REQUIRED_FOOTER = "requiredFooter";
private final String pluginName;
private final PluginConfigFactory cfgFactory;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java
index e347ca5..e2fd516 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidator.java
@@ -59,8 +59,9 @@
};
}
- public static String KEY_INVALID_FILENAME = "invalidFilename";
- public static String KEY_INVALID_FILENAME_PATTERN = KEY_INVALID_FILENAME + "Pattern";
+ public static final String KEY_INVALID_FILENAME = "invalidFilename";
+ public static final String KEY_INVALID_FILENAME_PATTERN =
+ KEY_INVALID_FILENAME + "Pattern";
private final String pluginName;
private final PluginConfigFactory cfgFactory;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java
index 1d5868c..559b745 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidator.java
@@ -66,7 +66,7 @@
};
}
- public static String KEY_CHECK_REJECT_WINDOWS_LINE_ENDINGS =
+ public static final String KEY_CHECK_REJECT_WINDOWS_LINE_ENDINGS =
"rejectWindowsLineEndings";
private final String pluginName;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java
index 9c2e612..53dc2e3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidator.java
@@ -56,7 +56,7 @@
};
}
- public static String KEY_MAX_PATH_LENGTH = "maxPathLength";
+ public static final String KEY_MAX_PATH_LENGTH = "maxPathLength";
private final String pluginName;
private final PluginConfigFactory cfgFactory;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java
index 5a24b59..5dd9ee5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidator.java
@@ -58,7 +58,7 @@
};
}
- public static String KEY_CHECK_SUBMODULE = "rejectSubmodule";
+ public static final String KEY_CHECK_SUBMODULE = "rejectSubmodule";
private final String pluginName;
private final PluginConfigFactory cfgFactory;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java
index eebd14c..572600c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidator.java
@@ -59,7 +59,7 @@
};
}
- public static String KEY_CHECK_SYMLINK = "rejectSymlink";
+ public static final String KEY_CHECK_SYMLINK = "rejectSymlink";
private final String pluginName;
private final PluginConfigFactory cfgFactory;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ValidatorConfig.java b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ValidatorConfig.java
index e3d9260..cf80a6c 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ValidatorConfig.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/uploadvalidator/ValidatorConfig.java
@@ -27,6 +27,7 @@
import org.slf4j.LoggerFactory;
import java.util.Arrays;
+import java.util.regex.Pattern;
import java.util.stream.Stream;
public class ValidatorConfig {
@@ -49,6 +50,7 @@
return conf != null
&& isValidConfig(conf, projectName)
&& (activeForRef(conf, refName))
+ && (activeForEmail(conf, user.getAccount().getPreferredEmail()))
&& (!hasCriteria(conf, "skipGroup")
|| !canSkipValidation(conf, validatorOp)
|| !canSkipRef(conf, refName)
@@ -79,22 +81,27 @@
}
private boolean activeForRef(PluginConfig config, String ref) {
- return matchCriteria(config, "ref", ref, true);
+ return matchCriteria(config, "ref", ref, true, true);
+ }
+
+ private boolean activeForEmail(PluginConfig config, String email) {
+ return matchCriteria(config, "email", email, true, false);
}
private boolean canSkipValidation(PluginConfig config, String validatorOp) {
- return matchCriteria(config, "skipValidation", validatorOp, false);
+ return matchCriteria(config, "skipValidation", validatorOp, false, false);
}
private boolean canSkipRef(PluginConfig config, String ref) {
- return matchCriteria(config, "skipRef", ref, true);
+ return matchCriteria(config, "skipRef", ref, true, true);
}
private boolean matchCriteria(PluginConfig config, String criteria,
- String value, boolean allowRegex) {
+ String value, boolean allowRegex, boolean refMatcher) {
boolean match = true;
for (String s : config.getStringList(criteria)) {
- if ((allowRegex && match(value, s)) || (!allowRegex && s.equals(value))) {
+ if ((allowRegex && match(value, s, refMatcher)) ||
+ (!allowRegex && s.equals(value))) {
return true;
}
match = false;
@@ -102,8 +109,13 @@
return match;
}
- private static boolean match(String value, String pattern) {
- return RefPatternMatcher.getMatcher(pattern).match(value, null);
+ private static boolean match(String value, String pattern,
+ boolean refMatcher) {
+ if (refMatcher) {
+ return RefPatternMatcher.getMatcher(pattern).match(value, null);
+ } else {
+ return Pattern.matches(pattern, value);
+ }
}
private boolean canSkipGroup(PluginConfig conf, IdentifiedUser user) {
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 398a771..a4151a0 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -225,6 +225,22 @@
ref = ^refs/heads/stable-.*
```
+Email-specific validations
+---------------------------
+
+By default, the validation will be enabled for all users. However, it can
+be limited to users with particular emails by setting `plugin.@PLUGIN@.email`.
+The emails may be configured using specific emails, patterns, or regular
+expressions. Multiple emails may be specified.
+
+E.g. to limit the validation to all users whose emails match `.*@example.com$`
+the following could be configured:
+
+```
+ [plugin "@PLUGIN@"]
+ email = .*@example.com$
+```
+
Permission to skip the rules
----------------------------
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
index 0a2722f..f201592 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/BlockedKeywordValidatorTest.java
@@ -23,7 +23,6 @@
import com.google.gerrit.server.git.validators.CommitValidationMessage;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
@@ -46,8 +45,7 @@
.build();
}
- private RevCommit makeCommit()
- throws NoFilepatternException, IOException, GitAPIException {
+ private RevCommit makeCommit() throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
// invalid files
String content = "http://foo.bar.tld/?pw=myp4ssw0rdTefoobarstline2\n";
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java
index 7a509ce..edad751 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ContentTypeValidatorTest.java
@@ -21,7 +21,6 @@
import com.google.gerrit.server.git.validators.CommitValidationMessage;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Test;
@@ -35,7 +34,7 @@
public class ContentTypeValidatorTest extends ValidatorTestCase {
- private static byte[] TEST_PDF = ("%PDF-1.4\n"
+ private static final byte[] TEST_PDF = ("%PDF-1.4\n"
+ "1 0 obj << /Type /Catalog /Outlines 2 0 R /Pages 3 0 R >>\n"
+ "endobj 2 0 obj << /Type /Outlines /Count 0 >>\n"
+ "endobj 3 0 obj << /Type /Pages /Kids [4 0 R] /Count 1\n"
@@ -90,8 +89,7 @@
assertThat(ContentTypeValidator.isWhitelist(EMPTY_PLUGIN_CONFIG)).isFalse();
}
- private RevCommit makeCommit()
- throws NoFilepatternException, IOException, GitAPIException {
+ private RevCommit makeCommit() throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
String content = "<?xml version=\"1.0\"?><a><b>c</b></a>";
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java
index 5ebb53f..2461bf6 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/DuplicatePathnameValidatorTest.java
@@ -43,14 +43,16 @@
import java.util.Set;
public class DuplicatePathnameValidatorTest extends ValidatorTestCase {
- private static final List<String> INITIAL_PATHNAMES = ImmutableList.of(
- "a" , "ab",
- "f1/a", "f1/ab",
- "f2/a", "f2/ab", "f2/sF1/a", "f2/sF1/ab");
+ private static final ImmutableList<String> INITIAL_PATHNAMES =
+ ImmutableList.of(
+ "a" , "ab",
+ "f1/a", "f1/ab",
+ "f2/a", "f2/ab", "f2/sF1/a", "f2/sF1/ab");
+
+ private final List<String> vistedPaths = Lists.newArrayList();
+ private final List<CommitValidationMessage> messages = Lists.newArrayList();
private TestRepository<Repository> testRepo;
- private List<String> vistedPaths = Lists.newArrayList();
- private List<CommitValidationMessage> messages = Lists.newArrayList();
private Set<String> changedPaths;
private DuplicatePathnameValidator validator;
@@ -82,8 +84,8 @@
runCheck(INITIAL_PATHNAMES, changedPaths, messages, vistedPaths);
assertThat(transformMessages(messages))
.containsExactly(transformMessage(conflict("f1/A", "f1/a")));
- assertThat(vistedPaths).containsExactlyElementsIn(ImmutableList.of("a",
- "ab", "f1", "f1/a", "f1/ab", "f2"));
+ assertThat(vistedPaths).containsExactly(
+ "a", "ab", "f1", "f1/a", "f1/ab", "f2");
}
@Test
@@ -92,8 +94,7 @@
runCheck(INITIAL_PATHNAMES, changedPaths, messages, vistedPaths);
assertThat(transformMessages(messages))
.containsExactly(transformMessage(conflict("F1", "f1")));
- assertThat(vistedPaths).containsExactlyElementsIn(
- ImmutableList.of("a", "ab", "f1", "f2"));
+ assertThat(vistedPaths).containsExactly("a", "ab", "f1", "f2");
}
@Test
@@ -103,8 +104,8 @@
assertThat(transformMessages(messages)).containsExactly(
transformMessage(conflict("F1", "f1")),
transformMessage(conflict("f2/sf1", "f2/sF1")));
- assertThat(vistedPaths).containsExactlyElementsIn(
- ImmutableList.of("a", "ab", "f1", "f2", "f2/a", "f2/ab", "f2/sF1"));
+ assertThat(vistedPaths).containsExactly(
+ "a", "ab", "f1", "f2", "f2/a", "f2/ab", "f2/sF1");
}
@Test
@@ -115,9 +116,8 @@
transformMessage(conflict("AB", "ab")),
transformMessage(conflict("f1/A", "f1/a")),
transformMessage(conflict("f2/Ab", "f2/ab")));
- assertThat(vistedPaths).containsExactlyElementsIn(
- ImmutableList.of("a", "ab", "f1", "f1/a", "f1/ab",
- "f2", "f2/a", "f2/ab", "f2/sF1"));
+ assertThat(vistedPaths).containsExactly(
+ "a", "ab", "f1", "f1/a", "f1/ab", "f2", "f2/a", "f2/ab", "f2/sF1");
}
@Test
@@ -125,8 +125,8 @@
changedPaths = Sets.newHashSet("a", "ab", "f1/ab");
runCheck(INITIAL_PATHNAMES, changedPaths, messages, vistedPaths);
assertThat(messages).isEmpty();
- assertThat(vistedPaths).containsExactlyElementsIn(ImmutableList.of("a",
- "ab", "f1", "f1/a", "f1/ab", "f2"));
+ assertThat(vistedPaths).containsExactly(
+ "a", "ab", "f1", "f1/a", "f1/ab", "f2");
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/EmailAwareValidatorConfigTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/EmailAwareValidatorConfigTest.java
new file mode 100644
index 0000000..46bf3c7
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/EmailAwareValidatorConfigTest.java
@@ -0,0 +1,108 @@
+// Copyright (C) 2017 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.uploadvalidator;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import com.google.gerrit.reviewdb.client.Project;
+import com.google.gerrit.server.IdentifiedUser;
+
+import org.eclipse.jgit.errors.ConfigInvalidException;
+import org.junit.Test;
+
+public class EmailAwareValidatorConfigTest {
+ private Project.NameKey projectName = new Project.NameKey("testProject");
+ private IdentifiedUser anyUser = new FakeUserProvider().get();
+
+ @Test
+ public void isEnabledForAllEmailsByDefault() throws Exception {
+ ValidatorConfig config =
+ getConfig("[plugin \"uploadvalidator\"]\n"
+ + "blockedFileExtension = jar");
+
+ assertThat(
+ config.isEnabledForRef(anyUser, projectName, "anyRef",
+ "blockedFileExtension")).isTrue();
+ }
+
+ @Test
+ public void isEnabledForSingleEmail() throws Exception {
+ ValidatorConfig config =
+ getConfig("[plugin \"uploadvalidator\"]\n"
+ + " email = " + FakeUserProvider.FAKE_EMAIL + "\n"
+ + " blockedFileExtension = jar");
+
+ assertThat(
+ config.isEnabledForRef(anyUser, projectName, "refs/heads/anyref",
+ "blockedFileExtension")).isTrue();
+ }
+
+ @Test
+ public void isDisabledForInvalidEmail() throws Exception {
+ ValidatorConfig config =
+ getConfig("[plugin \"uploadvalidator\"]\n"
+ + " email = anInvalidEmail@example.com\n"
+ + " blockedFileExtension = jar");
+
+ assertThat(
+ config.isEnabledForRef(anyUser, projectName, "refs/heads/anyref",
+ "blockedFileExtension")).isFalse();
+ }
+
+ @Test
+ public void isEnabledForRegexEmail() throws Exception {
+ IdentifiedUser exampleOrgUser = new FakeUserProvider().get("a@example.org");
+ ValidatorConfig config =
+ getConfig("[plugin \"uploadvalidator\"]\n"
+ + " email = .*@example.org$\n"
+ + " blockedFileExtension = jar");
+
+ assertThat(
+ config.isEnabledForRef(anyUser, projectName, "refs/heads/anyref",
+ "blockedFileExtension")).isFalse();
+ assertThat(
+ config.isEnabledForRef(exampleOrgUser, projectName,
+ "refs/heads/anyref", "blockedFileExtension")).isTrue();
+ }
+
+ @Test
+ public void isEnabledForMultipleEmails() throws Exception {
+ IdentifiedUser exampleOrgUser = new FakeUserProvider().get("a@example.org");
+ IdentifiedUser xUser = new FakeUserProvider().get("x@example.com");
+ ValidatorConfig config =
+ getConfig("[plugin \"uploadvalidator\"]\n"
+ + " email = .*@example.org$\n"
+ + " email = x@example.com\n"
+ + " blockedFileExtension = jar");
+
+ assertThat(
+ config.isEnabledForRef(exampleOrgUser, projectName, "refs/heads/anyref",
+ "blockedFileExtension")).isTrue();
+ assertThat(
+ config.isEnabledForRef(xUser, projectName, "refs/heads/anyref",
+ "blockedFileExtension")).isTrue();
+ assertThat(
+ config.isEnabledForRef(anyUser, projectName, "refs/heads/anyref",
+ "blockedFileExtension")).isFalse();
+ }
+
+ private ValidatorConfig getConfig(String defaultConfig)
+ throws ConfigInvalidException {
+ ValidatorConfig config =
+ new ValidatorConfig(new FakeConfigFactory(projectName, defaultConfig),
+ new FakeGroupCacheUUIDByName());
+ return config;
+ }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeGroupMembership.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeGroupMembership.java
index 9edec40..102c6a4 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeGroupMembership.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeGroupMembership.java
@@ -17,7 +17,7 @@
import com.google.gerrit.reviewdb.client.AccountGroup.UUID;
import com.google.gerrit.server.account.GroupMembership;
-import java.util.Arrays;
+import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@@ -27,7 +27,7 @@
private final Set<String> memberOfGroup = new HashSet<>();
public FakeGroupMembership(String... memberOfGroup) {
- this.memberOfGroup.addAll(Arrays.asList(memberOfGroup));
+ Collections.addAll(this.memberOfGroup, memberOfGroup);
}
@Override
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeUserProvider.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeUserProvider.java
index f634090..e49e974 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeUserProvider.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/FakeUserProvider.java
@@ -18,10 +18,14 @@
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
+import com.google.gerrit.common.TimeUtil;
+import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.IdentifiedUser;
import com.google.inject.Provider;
public class FakeUserProvider implements Provider<IdentifiedUser> {
+ public static String FAKE_EMAIL = "fake@example.com";
+
private final String[] groupUUID;
public FakeUserProvider(String... groupUUID) {
@@ -30,13 +34,20 @@
@Override
public IdentifiedUser get() {
- return createNew();
+ return createNew(FAKE_EMAIL);
}
- private IdentifiedUser createNew() {
+ public IdentifiedUser get(String email) {
+ return createNew(email);
+ }
+
+ private IdentifiedUser createNew(String email) {
IdentifiedUser user = createMock(IdentifiedUser.class);
+ Account account = new Account(new Account.Id(1), TimeUtil.nowTs());
+ account.setPreferredEmail(email);
expect(user.isIdentifiedUser()).andReturn(true);
expect(user.asIdentifiedUser()).andReturn(user);
+ expect(user.getAccount()).andStubReturn(account);
expect(user.getEffectiveGroups()).andReturn(
new FakeGroupMembership(groupUUID));
replay(user);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java
index a278496..a620824 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidFilenameValidatorTest.java
@@ -20,7 +20,6 @@
import com.google.gerrit.server.git.validators.CommitValidationMessage;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
@@ -31,7 +30,7 @@
import java.util.Set;
public class InvalidFilenameValidatorTest extends ValidatorTestCase {
- private Set<String> getInvalidFilenames() {
+ private static Set<String> getInvalidFilenames() {
Set<String> filenames = new HashSet<>();
filenames.add("test#");
filenames.add("test%");
@@ -43,8 +42,7 @@
return filenames;
}
- private RevCommit makeCommit()
- throws NoFilepatternException, IOException, GitAPIException {
+ private RevCommit makeCommit() throws IOException, GitAPIException {
Set<File> files = new HashSet<>();
for (String filenames : getInvalidFilenames()) {
files.add(new File(repo.getDirectory().getParent(), filenames));
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java
index 50981be..04f3e09 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/InvalidLineEndingValidatorTest.java
@@ -22,7 +22,6 @@
import com.google.gerrit.server.git.validators.CommitValidationMessage;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
@@ -34,8 +33,7 @@
import java.util.Map;
public class InvalidLineEndingValidatorTest extends ValidatorTestCase {
- private RevCommit makeCommit()
- throws NoFilepatternException, IOException, GitAPIException {
+ private RevCommit makeCommit() throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
// invalid line endings
String content = "Testline1\r\n"
@@ -62,9 +60,8 @@
new ContentTypeUtil(PATTERN_CACHE), null, null, null);
List<CommitValidationMessage> m = validator.performValidation(repo, c,
EMPTY_PLUGIN_CONFIG);
- assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(ImmutableSet.of(
- "ERROR: found carriage return (CR) character in file: foo.txt"));
+ assertThat(TestUtils.transformMessages(m)).containsExactly(
+ "ERROR: found carriage return (CR) character in file: foo.txt");
}
@Test
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ListVisitedPathsFilter.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ListVisitedPathsFilter.java
index b5672b0..23aa5a4 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ListVisitedPathsFilter.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/ListVisitedPathsFilter.java
@@ -14,8 +14,6 @@
package com.googlesource.gerrit.plugins.uploadvalidator;
-import org.eclipse.jgit.errors.IncorrectObjectTypeException;
-import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.TreeFilter;
@@ -31,8 +29,7 @@
}
@Override
- public boolean include(TreeWalk walker)
- throws MissingObjectException, IncorrectObjectTypeException, IOException {
+ public boolean include(TreeWalk walker) throws IOException {
visitedPaths.add(walker.getPathString());
return true;
}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java
index c9b98f8..4ce9dba 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/MaxPathLengthValidatorTest.java
@@ -22,7 +22,6 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
@@ -40,8 +39,7 @@
return (TOO_LONG.length() + GOOD.length()) / 2;
}
- private RevCommit makeCommit()
- throws NoFilepatternException, IOException, GitAPIException {
+ private RevCommit makeCommit() throws IOException, GitAPIException {
Set<File> files = new HashSet<>();
files.add(TestUtils.createEmptyFile(TOO_LONG, repo));
files.add(TestUtils.createEmptyFile(GOOD, repo));
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/RefAwareValidatorConfigTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/RefAwareValidatorConfigTest.java
index 80b78ea..111b16b 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/RefAwareValidatorConfigTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/RefAwareValidatorConfigTest.java
@@ -23,8 +23,9 @@
import org.junit.Test;
public class RefAwareValidatorConfigTest {
- private Project.NameKey projectName = new Project.NameKey("testProject");
- private IdentifiedUser anyUser = new FakeUserProvider().get();
+ private final Project.NameKey projectName =
+ new Project.NameKey("testProject");
+ private final IdentifiedUser anyUser = new FakeUserProvider().get();
@Test
public void isEnabledForAllRefsByDefault() throws Exception {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SkipValidationTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SkipValidationTest.java
index 3f4276d..bfbd069 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SkipValidationTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SkipValidationTest.java
@@ -23,8 +23,9 @@
import org.junit.Test;
public class SkipValidationTest {
- private Project.NameKey projectName = new Project.NameKey("testProject");
- private IdentifiedUser anyUser = new FakeUserProvider().get();
+ private final Project.NameKey projectName =
+ new Project.NameKey("testProject");
+ private final IdentifiedUser anyUser = new FakeUserProvider().get();
@Test
public void dontSkipByDefault() throws Exception {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java
index bb14073..5019764 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SubmoduleValidatorTest.java
@@ -23,7 +23,6 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.SubmoduleAddCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
@@ -35,7 +34,7 @@
public class SubmoduleValidatorTest extends ValidatorTestCase {
private RevCommit makeCommitWithSubmodule()
- throws NoFilepatternException, IOException, GitAPIException {
+ throws IOException, GitAPIException {
try (Git git = new Git(repo)) {
SubmoduleAddCommand addCommand = git.submoduleAdd();
addCommand.setURI(repo.getDirectory().getCanonicalPath());
@@ -52,12 +51,11 @@
List<CommitValidationMessage> m =
SubmoduleValidator.performValidation(repo, c);
assertThat(TestUtils.transformMessages(m))
- .containsExactlyElementsIn(ImmutableSet.of(
- "ERROR: submodules are not allowed: modules/library"));
+ .containsExactly("ERROR: submodules are not allowed: modules/library");
}
private RevCommit makeCommitWithoutSubmodule()
- throws NoFilepatternException, IOException, GitAPIException {
+ throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
files.put(new File(repo.getDirectory().getParent(), "foo.txt"), null);
return TestUtils.makeCommit(repo, "Commit with empty test files.", files);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java
index 9529a0b..a4c2127 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/SymlinkValidatorTest.java
@@ -21,7 +21,6 @@
import com.google.gerrit.server.git.validators.CommitValidationMessage;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Test;
@@ -36,7 +35,7 @@
public class SymlinkValidatorTest extends ValidatorTestCase {
private RevCommit makeCommitWithSymlink()
- throws NoFilepatternException, IOException, GitAPIException {
+ throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
File link = new File(repo.getDirectory().getParent(), "foo.txt");
Files.createSymbolicLink(link.toPath(), Paths.get("bar.txt"));
@@ -62,7 +61,7 @@
}
private RevCommit makeCommitWithoutSymlink()
- throws NoFilepatternException, IOException, GitAPIException {
+ throws IOException, GitAPIException {
Map<File, byte[]> files = new HashMap<>();
files.put(new File(repo.getDirectory().getParent(), "foo.txt"), null);
return TestUtils.makeCommit(repo, "Commit with empty test files.", files);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java
index 0f102fa..ab17832 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/uploadvalidator/TestUtils.java
@@ -27,7 +27,6 @@
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.RmCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
-import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Config;
@@ -71,8 +70,7 @@
}
public static RevCommit makeCommit(Repository repo, String message,
- Set<File> files)
- throws IOException, NoFilepatternException, GitAPIException {
+ Set<File> files) throws IOException, GitAPIException {
Map<File, byte[]> tmp = new HashMap<>();
for (File f : files) {
tmp.put(f, null);
@@ -81,8 +79,7 @@
}
public static RevCommit makeCommit(Repository repo, String message,
- Map<File, byte[]> files)
- throws IOException, NoFilepatternException, GitAPIException {
+ Map<File, byte[]> files) throws IOException, GitAPIException {
try (Git git = new Git(repo)) {
if (files != null) {
addFiles(git, files);
@@ -98,7 +95,7 @@
}
public static void removeFiles(Git git, Set<File> files)
- throws NoFilepatternException, GitAPIException {
+ throws GitAPIException {
RmCommand rmc = git.rm();
for (File f : files) {
rmc.addFilepattern(generateFilePattern(f, git));
@@ -107,7 +104,7 @@
}
private static void addFiles(Git git, Map<File, byte[]> files)
- throws IOException, NoFilepatternException, GitAPIException {
+ throws IOException, GitAPIException {
AddCommand ac = git.add();
for (File f : files.keySet()) {
if (!f.exists()) {