View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.grammars.comments;
21  
22  import java.util.Arrays;
23  import java.util.LinkedHashSet;
24  import java.util.Set;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
32  import com.puppycrawl.tools.checkstyle.api.DetailAST;
33  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
35  
36  public class AllBlockCommentsTest extends AbstractModuleTestSupport {
37      private static final Set<String> ALL_COMMENTS = new LinkedHashSet<>();
38  
39      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
40  
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/grammars/comments";
44      }
45  
46      @Test
47      public void testAllBlockComments() throws Exception {
48          final DefaultConfiguration checkConfig =
49                  createModuleConfig(BlockCommentListenerCheck.class);
50          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
51          verify(checkConfig, getPath("InputFullOfBlockComments.java"), expected);
52          Assert.assertTrue("All comments should be empty", ALL_COMMENTS.isEmpty());
53      }
54  
55      private static class BlockCommentListenerCheck extends AbstractCheck {
56          @Override
57          public boolean isCommentNodesRequired() {
58              return true;
59          }
60  
61          @Override
62          public int[] getDefaultTokens() {
63              return getAcceptableTokens();
64          }
65  
66          @Override
67          public int[] getAcceptableTokens() {
68              return new int[] {TokenTypes.BLOCK_COMMENT_BEGIN};
69          }
70  
71          @Override
72          public int[] getRequiredTokens() {
73              return getAcceptableTokens();
74          }
75  
76          @Override
77          public void init() {
78              ALL_COMMENTS.addAll(Arrays.asList("0", "1", "2", "3", "4", "5",
79                      "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
80                      "16", "17", "18", "19", "20",
81                      LINE_SEPARATOR + "21" + LINE_SEPARATOR,
82                      "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32",
83                      "33", "34", "35", "36", "37", "38", "  39  ", "40", "41",
84                      "42", "43", "44", "45", "46", "47", "48", "49", "50",
85                      "51", "52", "53", "54", "55", "56", "57", "58", "59",
86                      "60", "61"));
87          }
88  
89          @Override
90          public void visitToken(DetailAST ast) {
91              final String commentContent = ast.getFirstChild().getText();
92              if (!ALL_COMMENTS.remove(commentContent)) {
93                  Assert.fail("Unexpected comment: " + commentContent);
94              }
95          }
96      }
97  }