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.AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION;
23  import static com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationLocationCheck.MSG_KEY_ANNOTATION_LOCATION_ALONE;
24  import static org.junit.Assert.assertArrayEquals;
25  
26  import org.junit.Test;
27  
28  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
29  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
30  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
31  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
32  
33  public class AnnotationLocationCheckTest extends AbstractModuleTestSupport {
34  
35      @Override
36      protected String getPackageLocation() {
37          return "com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation";
38      }
39  
40      @Test
41      public void testGetRequiredTokens() {
42          final AnnotationLocationCheck checkObj = new AnnotationLocationCheck();
43          assertArrayEquals(
44              "AnnotationLocationCheck#getRequiredTokens should return empty array by default",
45              CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
46      }
47  
48      @Test
49      public void testCorrect() throws Exception {
50          final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
51          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
52  
53          verify(checkConfig, getPath("InputAnnotationLocationCorrect.java"), expected);
54      }
55  
56      @Test
57      public void testIncorrect() throws Exception {
58          final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
59          final String[] expected = {
60              "6: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
61              "11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
62              "17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
63              "25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
64              "29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
65              "29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation2"),
66              "32: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
67              "36: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 8, 4),
68              "37: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation3", 6, 4),
69              "38: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation4", 10, 4),
70              "41: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
71              "48: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 12, 8),
72              "61: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
73              "65: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
74              "70: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
75              "73: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
76              "85: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 11, 8),
77              "88: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 10, 8),
78              "98: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 0, 3),
79          };
80          verify(checkConfig, getPath("InputAnnotationLocationIncorrect.java"), expected);
81      }
82  
83      @Test
84      public void testGetAcceptableTokens() {
85          final AnnotationLocationCheck constantNameCheckObj = new AnnotationLocationCheck();
86          final int[] actual = constantNameCheckObj.getAcceptableTokens();
87          final int[] expected = {
88              TokenTypes.CLASS_DEF,
89              TokenTypes.INTERFACE_DEF,
90              TokenTypes.ENUM_DEF,
91              TokenTypes.METHOD_DEF,
92              TokenTypes.CTOR_DEF,
93              TokenTypes.VARIABLE_DEF,
94              TokenTypes.PARAMETER_DEF,
95              TokenTypes.ANNOTATION_DEF,
96              TokenTypes.TYPECAST,
97              TokenTypes.LITERAL_THROWS,
98              TokenTypes.IMPLEMENTS_CLAUSE,
99              TokenTypes.TYPE_ARGUMENT,
100             TokenTypes.LITERAL_NEW,
101             TokenTypes.DOT,
102             TokenTypes.ANNOTATION_FIELD_DEF,
103             };
104         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
105     }
106 
107     @Test
108     public void testWithoutAnnotations() throws Exception {
109         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
110         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
111         verify(checkConfig, getPath("InputAnnotationLocationEmpty.java"), expected);
112     }
113 
114     @Test
115     public void testWithParameters() throws Exception {
116         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
117         checkConfig.addAttribute("allowSamelineSingleParameterlessAnnotation", "true");
118         checkConfig.addAttribute("allowSamelineParameterizedAnnotation", "true");
119         checkConfig.addAttribute("allowSamelineMultipleAnnotations", "true");
120         final String[] expected = {
121             "17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
122             "25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
123             "32: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
124             "36: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 8, 4),
125             "37: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation3", 6, 4),
126             "38: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation4", 10, 4),
127             "48: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 12, 8),
128             "61: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
129             "65: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
130             "70: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
131             "85: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 11, 8),
132             "88: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 10, 8),
133             "98: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 0, 3),
134             };
135         verify(checkConfig, getPath("InputAnnotationLocationIncorrect.java"), expected);
136     }
137 
138     @Test
139     public void testWithMultipleAnnotations() throws Exception {
140         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
141         checkConfig.addAttribute("allowSamelineSingleParameterlessAnnotation", "false");
142         final String[] expected = {
143             "3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation11"),
144             "3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation12"),
145             "3: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation13"),
146             };
147         verify(checkConfig, getPath("InputAnnotationLocationCustomAnnotationsDeclared.java"),
148                 expected);
149     }
150 
151     @Test
152     public void testAllTokens() throws Exception {
153         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
154         checkConfig.addAttribute("tokens", "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
155                 + "CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, "
156                 + "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
157                 + "ANNOTATION_FIELD_DEF");
158         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
159         verify(checkConfig, getPath("InputAnnotationLocationWithoutAnnotations.java"), expected);
160     }
161 
162     @Test
163     public void testAnnotationInForEachLoopParameterAndVariableDef() throws Exception {
164         final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
165         checkConfig.addAttribute("tokens", "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF,"
166             + " CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, LITERAL_THROWS,"
167             + " IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, ANNOTATION_FIELD_DEF,"
168             + " TYPE_ARGUMENT");
169         checkConfig.addAttribute("allowSamelineMultipleAnnotations", "false");
170         checkConfig.addAttribute("allowSamelineSingleParameterlessAnnotation", "false");
171         checkConfig.addAttribute("allowSamelineParameterizedAnnotation", "false");
172         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
173         verify(checkConfig, getPath("InputAnnotationLocationDeprecatedAndCustom.java"), expected);
174     }
175 
176 }