1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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, 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, 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 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 assertEquals("Invalid javadoc count", metadata.getMatchesNum(), matches);
81 }
82 }
83
84 private static int getJavadocsCount(DetailAST detailAST,
85 Function<DetailAST, Boolean> assertion) {
86 int matchFound = 0;
87 DetailAST node = detailAST;
88 while (node != null) {
89 if (node.getType() == TokenTypes.BLOCK_COMMENT_BEGIN
90 && JavadocUtil.isJavadocComment(node)) {
91 if (!assertion.apply(node)) {
92 throw new IllegalStateException("Position of comment is defined correctly");
93 }
94 matchFound++;
95 }
96 matchFound += getJavadocsCount(node.getFirstChild(), assertion);
97 node = node.getNextSibling();
98 }
99 return matchFound;
100 }
101
102 @Override
103 protected String getPackageLocation() {
104 return "com/puppycrawl/tools/checkstyle/utils/blockcommentposition";
105 }
106
107 private static final class BlockCommentPositionTestMetadata {
108
109 private final String fileName;
110 private final Function<DetailAST, Boolean> assertion;
111 private final int matchesNum;
112
113 BlockCommentPositionTestMetadata(String fileName, Function<DetailAST,
114 Boolean> assertion, int matchesNum) {
115 this.fileName = fileName;
116 this.assertion = assertion;
117 this.matchesNum = matchesNum;
118 }
119
120 public String getFileName() {
121 return fileName;
122 }
123
124 public Function<DetailAST, Boolean> getAssertion() {
125 return assertion;
126 }
127
128 public int getMatchesNum() {
129 return matchesNum;
130 }
131
132 }
133
134 }