View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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.checks.javadoc;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocLinkFirstOccurrenceCheck.MSG_KEY;
23  
24  import java.io.File;
25  
26  import org.junit.jupiter.api.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
31  
32  public class JavadocLinkFirstOccurrenceCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      public String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/javadoc/javadoclinkfirstoccurrence";
37      }
38  
39      @Test
40      public void testDefault() throws Exception {
41          final String[] expected = {
42              "17:12: " + getCheckMessage(MSG_KEY, "String"),
43              "29:12: " + getCheckMessage(MSG_KEY, "String"),
44              "29:46: " + getCheckMessage(MSG_KEY, "Object"),
45              "35:12: " + getCheckMessage(MSG_KEY, "String"),
46              "71:15: " + getCheckMessage(MSG_KEY, "String"),
47              "72:20: " + getCheckMessage(MSG_KEY, "String"),
48              "81:13: " + getCheckMessage(MSG_KEY, "String"),
49              "82:13: " + getCheckMessage(MSG_KEY, "Object"),
50              "88:15: " + getCheckMessage(MSG_KEY, "#method"),
51              "94:15: " + getCheckMessage(MSG_KEY, "String#length()"),
52              "109:12: " + getCheckMessage(MSG_KEY, "java.lang.String"),
53              "115:12: " + getCheckMessage(MSG_KEY, "java.util.List"),
54          };
55          verifyWithInlineConfigParser(
56                  getPath("InputJavadocLinkFirstOccurrence.java"), expected);
57      }
58  
59      @Test
60      public void testAdditionalCases() throws Exception {
61          final String[] expected = {
62              "18:12: " + getCheckMessage(MSG_KEY, "java.util.Map.Entry"),
63              "39:12: " + getCheckMessage(MSG_KEY, "String"),
64          };
65          verifyWithInlineConfigParser(
66                  getPath("InputJavadocLinkFirstOccurrenceAdditional.java"), expected);
67      }
68  
69      @Test
70      public void testMemberFullyQualifiedName() throws Exception {
71          final String[] expected = {
72              "12:12: " + getCheckMessage(MSG_KEY, "java.util.List#add(Object)"),
73          };
74          verifyWithInlineConfigParser(
75                  getPath("InputJavadocLinkFirstOccurrenceMemberFullyQualifiedName.java"), expected);
76      }
77  
78      @Test
79      public void testStarImport() throws Exception {
80          final String[] expected = {
81              "12:12: " + getCheckMessage(MSG_KEY, "java.util.List"),
82          };
83          verifyWithInlineConfigParser(
84                  getPath("InputJavadocLinkFirstOccurrenceStarImport.java"), expected);
85      }
86  
87      @Test
88      public void testEmptyFile() throws Exception {
89          final DefaultConfiguration checkConfig =
90                  createModuleConfig(JavadocLinkFirstOccurrenceCheck.class);
91          final String path = getPath("InputJavadocLinkFirstOccurrenceEmptyFile.java");
92          verify(createChecker(checkConfig), new File[] {
93              new File(path),
94          }, path, CommonUtil.EMPTY_STRING_ARRAY);
95      }
96  
97      @Test
98      public void testClearStateBetweenFiles() throws Exception {
99          final DefaultConfiguration checkConfig =
100                 createModuleConfig(JavadocLinkFirstOccurrenceCheck.class);
101         final String pathFile2 = getPath("InputJavadocLinkFirstOccurrenceClearState2.java");
102         final String[] expected = {
103             "6:36: " + getCheckMessage(MSG_KEY, "String"),
104         };
105         verify(createChecker(checkConfig), new File[] {
106             new File(getPath("InputJavadocLinkFirstOccurrenceClearState1.java")),
107             new File(pathFile2),
108         }, pathFile2, expected);
109     }
110 
111 }