View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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 com.google.common.truth.Truth.assertWithMessage;
23  
24  import java.io.File;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.function.Function;
28  
29  import org.junit.jupiter.api.Test;
30  
31  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
32  import com.puppycrawl.tools.checkstyle.JavaParser;
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 AbstractModuleTestSupport {
38  
39      @Test
40      public void testPrivateConstr() throws Exception {
41          assertWithMessage("Constructor is not private")
42                  .that(TestUtil.isUtilsClassHasPrivateConstructor(BlockCommentPosition.class))
43                  .isTrue();
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, 4),
51                  new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnMethod.java",
52                          BlockCommentPosition::isOnMethod, 6),
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, 5),
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                  new BlockCommentPositionTestMetadata(
69                          "inputs/normal/package-info.java",
70                          BlockCommentPosition::isOnPackage, 1),
71                  new BlockCommentPositionTestMetadata(
72                          "inputs/annotation/package-info.java",
73                          BlockCommentPosition::isOnPackage, 1)
74          );
75  
76          for (BlockCommentPositionTestMetadata metadata : metadataList) {
77              final DetailAST ast = JavaParser.parseFile(new File(getPath(metadata.getFileName())),
78                  JavaParser.Options.WITH_COMMENTS);
79              final int matches = getJavadocsCount(ast, metadata.getAssertion());
80              assertWithMessage("Invalid javadoc count")
81                      .that(matches)
82                      .isEqualTo(metadata.getMatchesNum());
83          }
84      }
85  
86      @Test
87      public void testJavaDocsRecognitionNonCompilable() throws Exception {
88          final List<BlockCommentPositionTestMetadata> metadataList = Arrays.asList(
89              new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnRecord.java",
90                  BlockCommentPosition::isOnRecord, 3),
91              new BlockCommentPositionTestMetadata("InputBlockCommentPositionOnCompactCtor.java",
92                  BlockCommentPosition::isOnCompactConstructor, 3)
93          );
94  
95          for (BlockCommentPositionTestMetadata metadata : metadataList) {
96              final DetailAST ast = JavaParser.parseFile(
97                  new File(getNonCompilablePath(metadata.getFileName())),
98                      JavaParser.Options.WITH_COMMENTS);
99              final int matches = getJavadocsCount(ast, metadata.getAssertion());
100             assertWithMessage("Invalid javadoc count")
101                     .that(matches)
102                     .isEqualTo(metadata.getMatchesNum());
103         }
104     }
105 
106     private static int getJavadocsCount(DetailAST detailAST,
107                                         Function<DetailAST, Boolean> assertion) {
108         int matchFound = 0;
109         DetailAST node = detailAST;
110         while (node != null) {
111             if (node.getType() == TokenTypes.BLOCK_COMMENT_BEGIN
112                     && JavadocUtil.isJavadocComment(node)) {
113                 if (!assertion.apply(node)) {
114                     throw new IllegalStateException("Position of comment is defined correctly");
115                 }
116                 matchFound++;
117             }
118             matchFound += getJavadocsCount(node.getFirstChild(), assertion);
119             node = node.getNextSibling();
120         }
121         return matchFound;
122     }
123 
124     @Override
125     protected String getPackageLocation() {
126         return "com/puppycrawl/tools/checkstyle/utils/blockcommentposition";
127     }
128 
129     private static final class BlockCommentPositionTestMetadata {
130 
131         private final String fileName;
132         private final Function<DetailAST, Boolean> assertion;
133         private final int matchesNum;
134 
135         private BlockCommentPositionTestMetadata(String fileName, Function<DetailAST,
136                 Boolean> assertion, int matchesNum) {
137             this.fileName = fileName;
138             this.assertion = assertion;
139             this.matchesNum = matchesNum;
140         }
141 
142         public String getFileName() {
143             return fileName;
144         }
145 
146         public Function<DetailAST, Boolean> getAssertion() {
147             return assertion;
148         }
149 
150         public int getMatchesNum() {
151             return matchesNum;
152         }
153 
154     }
155 
156 }