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.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.api.DetailAST;
34  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
35  import com.puppycrawl.tools.checkstyle.internal.utils.TestUtil;
36  
37  public class BlockCommentPositionTest extends AbstractPathTestSupport {
38  
39      @Test
40      public void testPrivateConstr() throws Exception {
41          assertTrue("Constructor is not private",
42                  TestUtil.isUtilsClassHasPrivateConstructor(BlockCommentPosition.class, true));
43      }
44  
45      @Test
46      public void testJavaDocsRecognition() throws Exception {
47          final List<BlockCommentPositionTestMetadata> metadataList = Arrays.asList(
48                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnClass.java",
49                          BlockCommentPosition::isOnClass, 3),
50                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnMethod.java",
51                          BlockCommentPosition::isOnMethod, 4),
52                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnField.java",
53                          BlockCommentPosition::isOnField, 3),
54                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnum.java",
55                          BlockCommentPosition::isOnEnum, 3),
56                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnConstructor.java",
57                          BlockCommentPosition::isOnConstructor, 3),
58                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnInterface.java",
59                          BlockCommentPosition::isOnInterface, 3),
60                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnAnnotation.java",
61                          BlockCommentPosition::isOnAnnotationDef, 3),
62                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnEnumMember.java",
63                          BlockCommentPosition::isOnEnumConstant, 2),
64                  new BlockCommentPositionTestMetadata(
65                          "InputBlockCommentPositionOnAnnotationField.java",
66                          BlockCommentPosition::isOnAnnotationField, 4)
67          );
68  
69          for (BlockCommentPositionTestMetadata metadata : metadataList) {
70              final DetailAST ast = TestUtil.parseFile(
71                      new File(getPath(metadata.getFileName()))
72              );
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 }