View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 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.annotation;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_INCORRECT_STYLE;
23  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_PARENS_MISSING;
24  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_PARENS_PRESENT;
25  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING;
26  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck.MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT;
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertTrue;
29  
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
34  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
35  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
36  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
37  
38  public class AnnotationUseStyleCheckTest extends AbstractModuleTestSupport {
39  
40      @Override
41      protected String getPackageLocation() {
42          return "com/puppycrawl/tools/checkstyle/checks/annotation/annotationusestyle";
43      }
44  
45      /* Additional test for jacoco, since valueOf()
46       * is generated by javac and jacoco reports that
47       * valueOf() is uncovered.
48       */
49      @Test
50      public void testElementStyleValueOf() {
51          final AnnotationUseStyleCheck.ElementStyle option =
52              AnnotationUseStyleCheck.ElementStyle.valueOf("COMPACT");
53          assertEquals("Invalid valueOf result",
54              AnnotationUseStyleCheck.ElementStyle.COMPACT, option);
55      }
56  
57      /* Additional test for jacoco, since valueOf()
58       * is generated by javac and jacoco reports that
59       * valueOf() is uncovered.
60       */
61      @Test
62      public void testTrailingArrayCommaValueOf() {
63          final AnnotationUseStyleCheck.TrailingArrayComma option =
64              AnnotationUseStyleCheck.TrailingArrayComma.valueOf("ALWAYS");
65          assertEquals("Invalid valueOf result",
66              AnnotationUseStyleCheck.TrailingArrayComma.ALWAYS, option);
67      }
68  
69      /* Additional test for jacoco, since valueOf()
70       * is generated by javac and jacoco reports that
71       * valueOf() is uncovered.
72       */
73      @Test
74      public void testClosingParensValueOf() {
75          final AnnotationUseStyleCheck.ClosingParens option =
76              AnnotationUseStyleCheck.ClosingParens.valueOf("ALWAYS");
77          assertEquals("Invalid valueOf result",
78              AnnotationUseStyleCheck.ClosingParens.ALWAYS, option);
79      }
80  
81      /**
82       * Test that annotation parens are always present.
83       */
84      @Test
85      public void testParensAlways() throws Exception {
86          final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
87          checkConfig.addAttribute("closingParens", "ALWAYS");
88          checkConfig.addAttribute("elementStyle", "ignore");
89          checkConfig.addAttribute("trailingArrayComma", "ignore");
90          final String[] expected = {
91              "3: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
92              "18: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
93              "23: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_MISSING),
94          };
95  
96          verify(checkConfig, getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
97      }
98  
99      /**
100      * Test that annotation parens are never present.
101      */
102     @Test
103     public void testParensNever() throws Exception {
104         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
105         checkConfig.addAttribute("closingParens", "NEVER");
106         checkConfig.addAttribute("elementStyle", "ignore");
107         checkConfig.addAttribute("trailingArrayComma", "ignore");
108         final String[] expected = {
109             "13: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
110             "30: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
111             "33: " + getCheckMessage(MSG_KEY_ANNOTATION_PARENS_PRESENT),
112         };
113 
114         verify(checkConfig, getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
115     }
116 
117     @Test
118     public void testStyleExpanded() throws Exception {
119         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
120         checkConfig.addAttribute("closingParens", "ignore");
121         checkConfig.addAttribute("elementStyle", "EXPANDED");
122         checkConfig.addAttribute("trailingArrayComma", "ignore");
123         final String[] expected = {
124             "5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
125             "12: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
126             "20: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
127             "26: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
128             "39: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
129             "41: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
130             "58: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "EXPANDED"),
131         };
132 
133         verify(checkConfig, getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
134     }
135 
136     @Test
137     public void testStyleCompact() throws Exception {
138         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
139         checkConfig.addAttribute("closingParens", "ignore");
140         checkConfig.addAttribute("elementStyle", "COMPACT");
141         checkConfig.addAttribute("trailingArrayComma", "ignore");
142         final String[] expected = {
143             "43: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
144             "47: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
145         };
146 
147         verify(checkConfig, getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
148     }
149 
150     @Test
151     public void testStyleCompactNoArray() throws Exception {
152         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
153         checkConfig.addAttribute("closingParens", "ignore");
154         checkConfig.addAttribute("elementStyle", "COMPACT_NO_ARRAY");
155         checkConfig.addAttribute("trailingArrayComma", "ignore");
156         final String[] expected = {
157             "5: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
158             "20: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
159             "41: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
160             "43: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
161             "47: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
162         };
163 
164         verify(checkConfig, getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
165     }
166 
167     @Test
168     public void testCommaAlwaysViolations() throws Exception {
169         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
170         checkConfig.addAttribute("closingParens", "ignore");
171         checkConfig.addAttribute("elementStyle", "ignore");
172         checkConfig.addAttribute("trailingArrayComma", "ALWAYS");
173         final String[] expected = {
174             "3:20: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
175             "6:30: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
176             "10:40: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
177             "13:44: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
178             "16:54: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
179             "24:37: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
180             "24:65: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
181             "26:21: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
182             "26:30: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
183             "29:39: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
184             "29:49: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
185             "32:21: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
186             "32:56: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING),
187         };
188 
189         verify(checkConfig, getPath("InputAnnotationUseStyleNoTrailingComma.java"), expected);
190     }
191 
192     @Test
193     public void testCommaAlwaysNoViolations() throws Exception {
194         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
195         checkConfig.addAttribute("closingParens", "ignore");
196         checkConfig.addAttribute("elementStyle", "ignore");
197         checkConfig.addAttribute("trailingArrayComma", "ALWAYS");
198         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
199 
200         verify(checkConfig, getPath("InputAnnotationUseStyleWithTrailingComma.java"), expected);
201     }
202 
203     @Test
204     public void testCommaNeverViolations() throws Exception {
205         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
206         checkConfig.addAttribute("closingParens", "ignore");
207         checkConfig.addAttribute("elementStyle", "ignore");
208         checkConfig.addAttribute("trailingArrayComma", "NEVER");
209         final String[] expected = {
210             "9:32: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
211             "13:42: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
212             "16:46: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
213             "19:56: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
214             "27:38: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
215             "27:67: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
216             "33:39: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
217             "33:50: " + getCheckMessage(MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT),
218         };
219 
220         verify(checkConfig, getPath("InputAnnotationUseStyleWithTrailingComma.java"), expected);
221     }
222 
223     @Test
224     public void testCommaNeverNoViolations() throws Exception {
225         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
226         checkConfig.addAttribute("closingParens", "ignore");
227         checkConfig.addAttribute("elementStyle", "ignore");
228         checkConfig.addAttribute("trailingArrayComma", "NEVER");
229         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
230 
231         verify(checkConfig, getPath("InputAnnotationUseStyleNoTrailingComma.java"), expected);
232     }
233 
234     @Test
235     public void testEverythingMixed() throws Exception {
236         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
237         checkConfig.addAttribute("closingParens", "ignore");
238         checkConfig.addAttribute("elementStyle", "ignore");
239         checkConfig.addAttribute("trailingArrayComma", "ignore");
240         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
241 
242         verify(checkConfig, getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
243     }
244 
245     @Test
246     public void testAnnotationsWithoutDefaultValues() throws Exception {
247         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
248         checkConfig.addAttribute("closingParens", "NEVER");
249         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
250 
251         verify(checkConfig, getPath("InputAnnotationUseStyleParams.java"), expected);
252     }
253 
254     @Test
255     public void testGetAcceptableTokens() {
256         final AnnotationUseStyleCheck constantNameCheckObj = new AnnotationUseStyleCheck();
257         final int[] actual = constantNameCheckObj.getAcceptableTokens();
258         final int[] expected = {TokenTypes.ANNOTATION };
259         Assert.assertArrayEquals("Invalid acceptable tokens", expected, actual);
260     }
261 
262     @Test
263     public void testGetOption() {
264         final AnnotationUseStyleCheck check = new AnnotationUseStyleCheck();
265         try {
266             check.setElementStyle("SHOULD_PRODUCE_ERROR");
267             Assert.fail("ConversionException is expected");
268         }
269         catch (IllegalArgumentException ex) {
270             final String messageStart = "unable to parse";
271 
272             assertTrue("Invalid exception message, should start with: " + messageStart,
273                 ex.getMessage().startsWith(messageStart));
274         }
275     }
276 
277     @Test
278     public void testStyleNotInList() throws Exception {
279         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
280         checkConfig.addAttribute("closingParens", "ignore");
281         checkConfig.addAttribute("elementStyle", "COMPACT_NO_ARRAY");
282         checkConfig.addAttribute("trailingArrayComma", "ignore");
283         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
284 
285         verify(checkConfig, getPath("InputAnnotationUseStyle.java"), expected);
286     }
287 
288 }