Skip to content

Commit

Permalink
Disallow BugPattern names that includes spaces.
Browse files Browse the repository at this point in the history
RELNOTES: Disallow Error Prone BugPattern names that includes spaces.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=185215138
  • Loading branch information
eaftan authored and cushon committed Feb 12, 2018
1 parent b81f60f commit b4d64b7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions annotation/pom.xml
Expand Up @@ -47,5 +47,11 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.truth</groupId>
<artifactId>truth</artifactId>
<version>0.39</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -16,6 +16,7 @@

package com.google.errorprone;

import com.google.common.base.CharMatcher;
import com.google.common.collect.ImmutableSet;
import java.lang.annotation.Annotation;
import java.util.Set;
Expand All @@ -32,6 +33,11 @@ public static void validate(BugPattern pattern) throws ValidationException {
throw new ValidationException("No @BugPattern provided");
}

// name must not contain spaces
if (CharMatcher.whitespace().matchesAnyOf(pattern.name())) {
throw new ValidationException("Name must not contain whitespace: " + pattern.name());
}

// linkType must be consistent with link element.
switch (pattern.linkType()) {
case CUSTOM:
Expand Down
Expand Up @@ -16,7 +16,9 @@

package com.google.errorprone;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.expectThrows;

import com.google.errorprone.BugPattern.Category;
import com.google.errorprone.BugPattern.LinkType;
Expand Down Expand Up @@ -183,4 +185,19 @@ final class BugPatternTestClass {}
BugPattern annotation = BugPatternTestClass.class.getAnnotation(BugPattern.class);
assertThrows(ValidationException.class, () -> BugPatternValidator.validate(annotation));
}

@Test
public void spacesInNameNotAllowed() {
@BugPattern(
name = "name with spaces",
summary = "Has a name with spaces",
severity = SeverityLevel.ERROR
)
final class BugPatternTestClass {}

BugPattern annotation = BugPatternTestClass.class.getAnnotation(BugPattern.class);
ValidationException e =
expectThrows(ValidationException.class, () -> BugPatternValidator.validate(annotation));
assertThat(e.getMessage()).contains("Name must not contain whitespace");
}
}

0 comments on commit b4d64b7

Please sign in to comment.