Skip to content

Commit

Permalink
Issue #6854: make SuppressWithNearbyCommentFilter.Tag private
Browse files Browse the repository at this point in the history
  • Loading branch information
pbludov authored and romani committed Jul 3, 2019
1 parent 1f30d57 commit 619be72
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
Expand Up @@ -281,7 +281,7 @@ private void addTag(String text, int line) {
/** /**
* A Tag holds a suppression comment and its location. * A Tag holds a suppression comment and its location.
*/ */
public static class Tag { private static final class Tag {


/** The text of the tag. */ /** The text of the tag. */
private final String text; private final String text;
Expand All @@ -305,7 +305,7 @@ public static class Tag {
* @param filter the {@code SuppressWithNearbyCommentFilter} with the context * @param filter the {@code SuppressWithNearbyCommentFilter} with the context
* @throws IllegalArgumentException if unable to parse expanded text. * @throws IllegalArgumentException if unable to parse expanded text.
*/ */
public Tag(String text, int line, SuppressWithNearbyCommentFilter filter) { /* package */ Tag(String text, int line, SuppressWithNearbyCommentFilter filter) {
this.text = text; this.text = text;


//Expand regexp for check and message //Expand regexp for check and message
Expand Down
Expand Up @@ -287,8 +287,11 @@ public void testVariableCheckOnVariableNumberOfLines() throws Exception {


@Test @Test
public void testEqualsAndHashCodeOfTagClass() { public void testEqualsAndHashCodeOfTagClass() {
final SuppressWithNearbyCommentFilter filter = new SuppressWithNearbyCommentFilter();
final Object tag =
getTagsAfterExecution(filter, "filename", "//SUPPRESS CHECKSTYLE ignore").get(0);
final EqualsVerifierReport ev = EqualsVerifier final EqualsVerifierReport ev = EqualsVerifier
.forClass(SuppressWithNearbyCommentFilter.Tag.class).usingGetClass().report(); .forClass(tag.getClass()).usingGetClass().report();
assertEquals("Error: " + ev.getMessage(), EqualsVerifierReport.SUCCESS, ev); assertEquals("Error: " + ev.getMessage(), EqualsVerifierReport.SUCCESS, ev);
} }


Expand Down Expand Up @@ -411,11 +414,11 @@ public void testAcceptNullLocalizedMessage() {


@Test @Test
public void testToStringOfTagClass() { public void testToStringOfTagClass() {
final SuppressWithNearbyCommentFilter.Tag tag = new SuppressWithNearbyCommentFilter.Tag( final SuppressWithNearbyCommentFilter filter = new SuppressWithNearbyCommentFilter();
"text", 7, new SuppressWithNearbyCommentFilter() final Object tag =
); getTagsAfterExecution(filter, "filename", "//SUPPRESS CHECKSTYLE ignore").get(0);
assertEquals("Invalid toString result", assertEquals("Invalid toString result",
"Tag[text='text', firstLine=7, lastLine=7, " "Tag[text='SUPPRESS CHECKSTYLE ignore', firstLine=1, lastLine=1, "
+ "tagCheckRegexp=.*, tagMessageRegexp=null]", tag.toString()); + "tagCheckRegexp=.*, tagMessageRegexp=null]", tag.toString());
} }


Expand Down Expand Up @@ -469,21 +472,31 @@ public void testSuppressById() throws Exception {
public void testTagsAreClearedEachRun() { public void testTagsAreClearedEachRun() {
final SuppressWithNearbyCommentFilter suppressionCommentFilter = final SuppressWithNearbyCommentFilter suppressionCommentFilter =
new SuppressWithNearbyCommentFilter(); new SuppressWithNearbyCommentFilter();
final FileContents contents = final List<?> tags1 = getTagsAfterExecution(suppressionCommentFilter,
new FileContents("filename", "//SUPPRESS CHECKSTYLE ignore", "line2"); "filename1", "//SUPPRESS CHECKSTYLE ignore this");
assertEquals("Invalid tags size", 1, tags1.size());
final List<?> tags2 = getTagsAfterExecution(suppressionCommentFilter,
"filename2", "No comments in this file");
assertEquals("Invalid tags size", 0, tags2.size());
}

/**
* Calls the filter with a minimal set of inputs and returns a list of
* {@link SuppressWithNearbyCommentFilter} internal type {@code Tag}.
* Our goal is 100% test coverage, for this we use white-box testing.
* So we need access to the implementation details. For this reason,
* it is necessary to use reflection to gain access to the inner field here.
*
* @return {@code Tag} list
*/
private static List<?> getTagsAfterExecution(SuppressWithNearbyCommentFilter filter,
String filename, String... lines) {
final FileContents contents = new FileContents(filename, lines);
contents.reportSingleLineComment(1, 0); contents.reportSingleLineComment(1, 0);
final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, "filename", final TreeWalkerAuditEvent dummyEvent = new TreeWalkerAuditEvent(contents, filename,
new LocalizedMessage(1, null, null, null, null, Object.class, null), null);
suppressionCommentFilter.accept(dummyEvent);
final FileContents contents2 =
new FileContents("filename2", "some line", "//SUPPRESS CHECKSTYLE ignore");
contents2.reportSingleLineComment(2, 0);
final TreeWalkerAuditEvent dummyEvent2 = new TreeWalkerAuditEvent(contents2, "filename",
new LocalizedMessage(1, null, null, null, null, Object.class, null), null); new LocalizedMessage(1, null, null, null, null, Object.class, null), null);
suppressionCommentFilter.accept(dummyEvent2); filter.accept(dummyEvent);
final List<SuppressionCommentFilter.Tag> tags = return Whitebox.getInternalState(filter, "tags");
Whitebox.getInternalState(suppressionCommentFilter, "tags");
assertEquals("Invalid tags size", 1, tags.size());
} }


} }

0 comments on commit 619be72

Please sign in to comment.