View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.utils;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.File;
26  import java.util.Arrays;
27  import java.util.List;
28  import java.util.function.Function;
29  
30  import org.junit.Test;
31  
32  import com.puppycrawl.tools.checkstyle.AbstractPathTestSupport;
33  import com.puppycrawl.tools.checkstyle.JavaParser;
34  import com.puppycrawl.tools.checkstyle.api.DetailAST;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
37  
38  public class BlockCommentPositionTest extends AbstractPathTestSupport {
39  
40      @Test
41      public void testPrivateConstr() throws Exception {
42          assertTrue("Constructor is not private",
43                  TestUtil.isUtilsClassHasPrivateConstructor(BlockCommentPosition.class, true));
44      }
45  
46      @Test
47      public void testJavaDocsRecognition() throws Exception {
48          final List<BlockCommentPositionTestMetadata> metadataList = Arrays.asList(
49                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnClass.java",
50                          BlockCommentPosition::isOnClass, 3),
51                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnMethod.java",
52                          BlockCommentPosition::isOnMethod, 4),
53                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnField.java",
54                          BlockCommentPosition::isOnField, 3),
55                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnum.java",
56                          BlockCommentPosition::isOnEnum, 3),
57                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnConstructor.java",
58                          BlockCommentPosition::isOnConstructor, 3),
59                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnInterface.java",
60                          BlockCommentPosition::isOnInterface, 3),
61                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnAnnotation.java",
62                          BlockCommentPosition::isOnAnnotationDef, 3),
63                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnumMember.java",
64                          BlockCommentPosition::isOnEnumConstant, 2),
65                  new BlockCommentPositionTestMetadata(
66                          "InputBlockCommentPositionOnAnnotationField.java",
67                          BlockCommentPosition::isOnAnnotationField, 4)
68          );
69  
70          for (BlockCommentPositionTestMetadata metadata : metadataList) {
71              final DetailAST ast = JavaParser.parseFile(new File(getPath(metadata.getFileName())),
72                  JavaParser.Options.WITH_COMMENTS);
73              final int matches = getJavadocsCount(ast, metadata.getAssertion());
74              assertEquals("Invalid javadoc count", metadata.getMatchesNum(), matches);
75          }
76      }
77  
78      private static int getJavadocsCount(DetailAST detailAST,
79                                          Function<DetailAST, Boolean> assertion) {
80          int matchFound = 0;
81          DetailAST node = detailAST;
82          while (node != null) {
83              if (node.getType() == TokenTypes.BLOCK_COMMENT_BEGIN
84                      && JavadocUtils.isJavadocComment(node)) {
85                  if (!assertion.apply(node)) {
86                      throw new IllegalStateException("Position of comment is defined correctly");
87                  }
88                  matchFound++;
89              }
90              matchFound += getJavadocsCount(node.getFirstChild(), assertion);
91              node = node.getNextSibling();
92          }
93          return matchFound;
94      }
95  
96      @Override
97      protected String getPackageLocation() {
98          return "com/puppycrawl/tools/checkstyle/utils/blockcommentposition";
99      }
100 
101     private static final class BlockCommentPositionTestMetadata {
102 
103         private final String fileName;
104         private final Function<DetailAST, Boolean> assertion;
105         private final int matchesNum;
106 
107         BlockCommentPositionTestMetadata(String fileName, Function<DetailAST,
108                 Boolean> assertion, int matchesNum) {
109             this.fileName = fileName;
110             this.assertion = assertion;
111             this.matchesNum = matchesNum;
112         }
113 
114         public String getFileName() {
115             return fileName;
116         }
117 
118         public Function<DetailAST, Boolean> getAssertion() {
119             return assertion;
120         }
121 
122         public int getMatchesNum() {
123             return matchesNum;
124         }
125 
126     }
127 
128 }