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