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.AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import org.junit.Test;
26  
27  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
28  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
29  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
30  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
31  
32  public class AnnotationOnSameLineCheckTest extends AbstractModuleTestSupport {
33  
34      @Override
35      protected String getPackageLocation() {
36          return "com/puppycrawl/tools/checkstyle/checks/annotation/annotationonsameline";
37      }
38  
39      @Test
40      public void testGetRequiredTokens() {
41          final AnnotationOnSameLineCheck check = new AnnotationOnSameLineCheck();
42          assertArrayEquals(
43              "AnnotationOnSameLineCheck#getRequiredTokens should return empty array by default",
44              CommonUtils.EMPTY_INT_ARRAY, check.getRequiredTokens());
45      }
46  
47      @Test
48      public void testGetAcceptableTokens() {
49          final AnnotationOnSameLineCheck constantNameCheckObj = new AnnotationOnSameLineCheck();
50          final int[] actual = constantNameCheckObj.getAcceptableTokens();
51          final int[] expected = {
52              TokenTypes.CLASS_DEF,
53              TokenTypes.INTERFACE_DEF,
54              TokenTypes.ENUM_DEF,
55              TokenTypes.METHOD_DEF,
56              TokenTypes.CTOR_DEF,
57              TokenTypes.VARIABLE_DEF,
58              TokenTypes.PARAMETER_DEF,
59              TokenTypes.ANNOTATION_DEF,
60              TokenTypes.TYPECAST,
61              TokenTypes.LITERAL_THROWS,
62              TokenTypes.IMPLEMENTS_CLAUSE,
63              TokenTypes.TYPE_ARGUMENT,
64              TokenTypes.LITERAL_NEW,
65              TokenTypes.DOT,
66              TokenTypes.ANNOTATION_FIELD_DEF,
67          };
68          assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
69      }
70  
71      @Test
72      public void testCheck() throws Exception {
73          final DefaultConfiguration config = createModuleConfig(AnnotationOnSameLineCheck.class);
74          final String[] expected = {
75              "9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
76              "10: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
77              "11: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
78          };
79          verify(config, getPath("InputAnnotationOnSameLineCheck.java"), expected);
80      }
81  
82      @Test
83      public void testCheckAcceptableTokens() throws Exception {
84          final DefaultConfiguration config = createModuleConfig(AnnotationOnSameLineCheck.class);
85          config.addAttribute("tokens", "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
86                  + "CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, "
87                  + "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
88                  + "ANNOTATION_FIELD_DEF");
89          final String[] expected = {
90              "9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
91              "10: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
92              "11: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
93          };
94          verify(config, getPath("InputAnnotationOnSameLineCheck.java"), expected);
95      }
96  
97      @Test
98      public void testCheck2() throws Exception {
99          final DefaultConfiguration config = createModuleConfig(AnnotationOnSameLineCheck.class);
100         final String[] expected = {
101             "11: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
102             "16: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
103             "19: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
104             "20: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
105         };
106         verify(config, getPath("InputAnnotationOnSameLineCheck2.java"), expected);
107     }
108 
109     @Test
110     public void testCheckOnDifferentTokens() throws Exception {
111         final DefaultConfiguration config = createModuleConfig(AnnotationOnSameLineCheck.class);
112         config.addAttribute("tokens", "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
113                 + "CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, "
114                 + "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
115                 + "ANNOTATION_FIELD_DEF");
116         final String[] expected = {
117             "5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
118             "8: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
119             "13: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
120             "14: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
121             "17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
122             "18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
123             "21: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
124             "24: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
125             "29: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
126             "33: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
127             "34: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
128             "35: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
129             "37: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
130             "43: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
131             "53: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
132             "56: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
133         };
134         verify(config, getPath("InputAnnotationOnSameLineCheckOnDifferentTokens.java"), expected);
135     }
136 
137 }