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