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.LinkedHashSet;
23  import java.util.Set;
24  
25  import org.junit.Assert;
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
31  import com.puppycrawl.tools.checkstyle.api.DetailAST;
32  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
33  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
34  
35  public class AllSinglelineCommentsTest extends AbstractModuleTestSupport {
36      private static final Set<String> ALL_COMMENTS = new LinkedHashSet<>();
37  
38      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
39  
40      @Override
41      protected String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/grammars/comments";
43      }
44  
45      @Test
46      public void testAllBlockComments() throws Exception {
47          final DefaultConfiguration checkConfig =
48              createModuleConfig(SinglelineCommentListenerCheck.class);
49          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
50          verify(checkConfig, getPath("InputFullOfSinglelineComments.java"), expected);
51          Assert.assertTrue("All comments should be empty", ALL_COMMENTS.isEmpty());
52      }
53  
54      private static class SinglelineCommentListenerCheck extends AbstractCheck {
55          @Override
56          public boolean isCommentNodesRequired() {
57              return true;
58          }
59  
60          @Override
61          public int[] getDefaultTokens() {
62              return getAcceptableTokens();
63          }
64  
65          @Override
66          public int[] getAcceptableTokens() {
67              return new int[] {TokenTypes.SINGLE_LINE_COMMENT};
68          }
69  
70          @Override
71          public int[] getRequiredTokens() {
72              return getAcceptableTokens();
73          }
74  
75          @Override
76          public void init() {
77              final int lines = 63;
78              for (int i = 0; i < lines; i++) {
79                  ALL_COMMENTS.add(i + LINE_SEPARATOR);
80              }
81              ALL_COMMENTS.add(String.valueOf(lines));
82          }
83  
84          @Override
85          public void visitToken(DetailAST ast) {
86              final String commentContent = ast.getFirstChild().getText();
87              if (!ALL_COMMENTS.remove(commentContent)) {
88                  Assert.fail("Unexpected comment: " + commentContent);
89              }
90          }
91      }
92  }