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.meta;
21  
22  import static com.google.common.truth.Truth.assertThat;
23  import static com.google.common.truth.Truth.assertWithMessage;
24  import static com.puppycrawl.tools.checkstyle.meta.JavadocMetadataScraper.MSG_DESC_MISSING;
25  import static org.junit.Assert.assertThrows;
26  
27  import java.util.Map;
28  import java.util.Map.Entry;
29  
30  import org.junit.jupiter.api.Test;
31  
32  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
33  import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
34  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
35  
36  public class JavadocMetadataScraperTest extends AbstractModuleTestSupport {
37  
38      @Override
39      protected String getPackageLocation() {
40          return "com/puppycrawl/tools/checkstyle/meta/javadocmetadatascraper";
41      }
42  
43      @Test
44      public void testAtclauseOrderCheck() throws Exception {
45          JavadocMetadataScraper.resetModuleDetailsStore();
46          verifyWithInlineConfigParser(getPath("InputJavadocMetadataScraperAtclauseOrderCheck.java"),
47                  CommonUtil.EMPTY_STRING_ARRAY);
48          assertWithMessage("expected correct parse")
49                  .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
50                  .isEqualTo(
51                          readFile(getPath("ExpectedJavadocMetadataScraperAtclauseOrderCheck.txt")));
52      }
53  
54      @Test
55      public void testAnnotationUseStyleCheck() throws Exception {
56          JavadocMetadataScraper.resetModuleDetailsStore();
57          verifyWithInlineConfigParser(
58                  getPath("InputJavadocMetadataScraperAnnotationUseStyleCheck.java"),
59                  CommonUtil.EMPTY_STRING_ARRAY);
60          assertWithMessage("expected correct parse")
61                  .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
62                  .isEqualTo(readFile(
63                          getPath("ExpectedJavadocMetadataScraperAnnotationUseStyleCheck.txt")));
64      }
65  
66      @Test
67      public void testBeforeExecutionExclusionFileFilter() throws Exception {
68          JavadocMetadataScraper.resetModuleDetailsStore();
69          verifyWithInlineConfigParser(
70                  getPath("InputJavadocMetadataScraperBeforeExecutionExclusionFileFilter.java"),
71                  CommonUtil.EMPTY_STRING_ARRAY);
72          assertWithMessage("expected correct parse")
73                  .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
74                  .isEqualTo(readFile(getPath(
75                          "ExpectedJavadocMetadataScraperBeforeExecutionExclusionFileFilter.txt")));
76      }
77  
78      @Test
79      public void testNoCodeInFileCheck() throws Exception {
80          JavadocMetadataScraper.resetModuleDetailsStore();
81          verifyWithInlineConfigParser(getPath("InputJavadocMetadataScraperNoCodeInFileCheck.java"),
82                  CommonUtil.EMPTY_STRING_ARRAY);
83          assertWithMessage("expected correct parse")
84                  .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
85                  .isEqualTo(
86                          readFile(getPath("ExpectedJavadocMetadataScraperNoCodeInFileCheck.txt")));
87      }
88  
89      @Test
90      public void testPropertyMisplacedDefaultValueCheck() {
91          JavadocMetadataScraper.resetModuleDetailsStore();
92          final CheckstyleException exc = assertThrows(CheckstyleException.class, () -> {
93              verifyWithInlineConfigParser(
94                      getPath("InputJavadocMetadataScraperPropertyMisplacedDefaultValueCheck.java"),
95                      CommonUtil.EMPTY_STRING_ARRAY);
96          });
97          assertThat(exc.getCause()).isInstanceOf(MetadataGenerationException.class);
98          assertThat(exc.getCause().getMessage())
99                  .isEqualTo("Default value for property 'misplacedDefaultValue' is missing");
100     }
101 
102     @Test
103     public void testPropertyMisplacedTypeCheck() {
104         JavadocMetadataScraper.resetModuleDetailsStore();
105         final CheckstyleException exc = assertThrows(CheckstyleException.class, () -> {
106             verifyWithInlineConfigParser(
107                     getPath("InputJavadocMetadataScraperPropertyMisplacedTypeCheck.java"),
108                     CommonUtil.EMPTY_STRING_ARRAY);
109             assertWithMessage("Exception expected").fail();
110         });
111         assertThat(exc.getCause()).isInstanceOf(MetadataGenerationException.class);
112         assertThat(exc.getCause().getMessage())
113                 .isEqualTo("Type for property 'misplacedType' is missing");
114     }
115 
116     @Test
117     public void testPropertyMissingDefaultValueCheck() {
118         JavadocMetadataScraper.resetModuleDetailsStore();
119         final CheckstyleException exc = assertThrows(CheckstyleException.class, () -> {
120             verifyWithInlineConfigParser(
121                     getPath("InputJavadocMetadataScraperPropertyMissingDefaultValueCheck.java"),
122                     CommonUtil.EMPTY_STRING_ARRAY);
123             assertWithMessage("Exception expected").fail();
124         });
125         assertThat(exc.getCause()).isInstanceOf(MetadataGenerationException.class);
126         assertThat(exc.getCause().getMessage())
127                 .isEqualTo("Default value for property 'missingDefaultValue' is missing");
128     }
129 
130     @Test
131     public void testPropertyMissingTypeCheck() {
132         JavadocMetadataScraper.resetModuleDetailsStore();
133         final CheckstyleException exc = assertThrows(CheckstyleException.class, () -> {
134             verifyWithInlineConfigParser(
135                     getPath("InputJavadocMetadataScraperPropertyMissingTypeCheck.java"),
136                     CommonUtil.EMPTY_STRING_ARRAY);
137             assertWithMessage("Exception expected").fail();
138         });
139         assertThat(exc.getCause()).isInstanceOf(MetadataGenerationException.class);
140         assertThat(exc.getCause().getMessage())
141                 .isEqualTo("Type for property 'missingType' is missing");
142     }
143 
144     @Test
145     public void testPropertyWithNoCodeTagCheck() throws Exception {
146         JavadocMetadataScraper.resetModuleDetailsStore();
147         verifyWithInlineConfigParser(
148                 getPath("InputJavadocMetadataScraperPropertyWithNoCodeTagCheck.java"),
149                 CommonUtil.EMPTY_STRING_ARRAY);
150         assertWithMessage("expected correct parse")
151                 .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
152                 .isEqualTo(readFile(
153                         getPath("ExpectedJavadocMetadataScraperPropertyWithNoCodeTagCheck.txt")));
154     }
155 
156     @Test
157     public void testRightCurlyCheck() throws Exception {
158         JavadocMetadataScraper.resetModuleDetailsStore();
159         verifyWithInlineConfigParser(getPath("InputJavadocMetadataScraperRightCurlyCheck.java"),
160                 CommonUtil.EMPTY_STRING_ARRAY);
161         assertWithMessage("expected correct parse")
162                 .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
163                 .isEqualTo(readFile(getPath("ExpectedJavadocMetadataScraperRightCurlyCheck.txt")));
164     }
165 
166     @Test
167     public void testSummaryJavadocCheck() throws Exception {
168         JavadocMetadataScraper.resetModuleDetailsStore();
169         verifyWithInlineConfigParser(getPath("InputJavadocMetadataScraperSummaryJavadocCheck.java"),
170                 CommonUtil.EMPTY_STRING_ARRAY);
171         assertWithMessage("expected correct parse")
172                 .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
173                 .isEqualTo(
174                         readFile(getPath("ExpectedJavadocMetadataScraperSummaryJavadocCheck.txt")));
175     }
176 
177     @Test
178     public void testSuppressWarningsFilter() throws Exception {
179         JavadocMetadataScraper.resetModuleDetailsStore();
180         verifyWithInlineConfigParser(
181                 getPath("InputJavadocMetadataScraperSuppressWarningsFilter.java"),
182                 CommonUtil.EMPTY_STRING_ARRAY);
183         assertWithMessage("expected correct parse")
184                 .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
185                 .isEqualTo(readFile(
186                         getPath("ExpectedJavadocMetadataScraperSuppressWarningsFilter.txt")));
187     }
188 
189     @Test
190     public void testWriteTagCheck() throws Exception {
191         JavadocMetadataScraper.resetModuleDetailsStore();
192         verifyWithInlineConfigParser(getPath("InputJavadocMetadataScraperWriteTagCheck.java"),
193                 CommonUtil.EMPTY_STRING_ARRAY);
194         assertWithMessage("expected correct parse")
195                 .that(convertToString(JavadocMetadataScraper.getModuleDetailsStore()))
196                 .isEqualTo(readFile(getPath("ExpectedJavadocMetadataScraperWriteTagCheck.txt")));
197     }
198 
199     @Test
200     public void testEmptyDescription() throws Exception {
201         JavadocMetadataScraper.resetModuleDetailsStore();
202 
203         final String[] expected = {
204             "19: " + getCheckMessage(MSG_DESC_MISSING,
205                     "InputJavadocMetadataScraperAbstractSuperCheck"),
206         };
207         verifyWithInlineConfigParser(getPath(
208                 "InputJavadocMetadataScraperAbstractSuperCheck.java"), expected);
209     }
210 
211     private static String convertToString(Map<String, ModuleDetails> moduleDetailsStore) {
212         final StringBuilder builder = new StringBuilder(128);
213         for (Entry<String, ModuleDetails> entry : moduleDetailsStore.entrySet()) {
214             final ModuleDetails details = entry.getValue();
215 
216             append(builder, "Key: ", split(entry.getKey(), 70));
217             append(builder, "Name: ", details.getName());
218             append(builder, "FullQualifiedName: ", split(details.getFullQualifiedName(), 70));
219             append(builder, "Parent: ", details.getParent());
220             append(builder, "Description: ", details.getDescription());
221             append(builder, "ModuleType: ", details.getModuleType());
222 
223             for (ModulePropertyDetails property : details.getProperties()) {
224                 append(builder, "Property Type: ", split(property.getType(), 70));
225                 append(builder, "Property DefaultValue: ", split(property.getDefaultValue(), 70));
226                 append(builder, "Property ValidationType: ", property.getValidationType());
227                 append(builder, "Property Description: ", property.getDescription());
228             }
229 
230             for (String key : details.getViolationMessageKeys()) {
231                 append(builder, "ViolationMessageKey: ", key);
232             }
233         }
234         return builder.toString();
235     }
236 
237     private static void append(StringBuilder builder, String title, Object object) {
238         builder.append(title).append(object).append('\n');
239     }
240 
241     private static String split(String text, int size) {
242         final StringBuilder builder = new StringBuilder(80);
243         if (text == null) {
244             builder.append("null");
245         }
246         else {
247             final int length = text.length();
248             int position = 0;
249             while (position < length) {
250                 if (position != 0) {
251                     builder.append("<split>\n");
252                 }
253                 builder.append(text, position, Math.min(length, position + size));
254                 position += size;
255             }
256         }
257         return builder.toString();
258     }
259 }