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.coding;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_ACCESS;
23  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_CONSTRUCTOR;
24  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_INSTANCE;
25  import static com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck.MSG_STATIC;
26  import static org.junit.Assert.assertEquals;
27  
28  import java.util.SortedSet;
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.DetailAST;
36  import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
37  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
38  
39  public class DeclarationOrderCheckTest
40      extends AbstractModuleTestSupport {
41      @Override
42      protected String getPackageLocation() {
43          return "com/puppycrawl/tools/checkstyle/checks/coding/declarationorder";
44      }
45  
46      @Test
47      public void testDefault() throws Exception {
48          final DefaultConfiguration checkConfig =
49              createModuleConfig(DeclarationOrderCheck.class);
50  
51          final String[] expected = {
52              "8:5: " + getCheckMessage(MSG_ACCESS),
53              "13:5: " + getCheckMessage(MSG_ACCESS),
54              "18:5: " + getCheckMessage(MSG_ACCESS),
55              "21:5: " + getCheckMessage(MSG_ACCESS),
56              "27:5: " + getCheckMessage(MSG_STATIC),
57              "34:9: " + getCheckMessage(MSG_ACCESS),
58              "52:9: " + getCheckMessage(MSG_STATIC),
59              "61:5: " + getCheckMessage(MSG_CONSTRUCTOR),
60              "87:5: " + getCheckMessage(MSG_INSTANCE),
61              "99:9: " + getCheckMessage(MSG_ACCESS),
62              "107:9: " + getCheckMessage(MSG_STATIC),
63              "113:5: " + getCheckMessage(MSG_ACCESS),
64              "118:5: " + getCheckMessage(MSG_ACCESS),
65              "123:5: " + getCheckMessage(MSG_ACCESS),
66              "126:5: " + getCheckMessage(MSG_ACCESS),
67              "132:5: " + getCheckMessage(MSG_STATIC),
68              "139:9: " + getCheckMessage(MSG_ACCESS),
69              "150:9: " + getCheckMessage(MSG_STATIC),
70              "159:5: " + getCheckMessage(MSG_CONSTRUCTOR),
71              "185:5: " + getCheckMessage(MSG_INSTANCE),
72              "189:9: " + getCheckMessage(MSG_ACCESS),
73          };
74          verify(checkConfig, getPath("InputDeclarationOrder.java"), expected);
75      }
76  
77      @Test
78      public void testOnlyConstructors() throws Exception {
79          final DefaultConfiguration checkConfig =
80              createModuleConfig(DeclarationOrderCheck.class);
81          checkConfig.addAttribute("ignoreConstructors", "false");
82          checkConfig.addAttribute("ignoreModifiers", "true");
83  
84          final String[] expected = {
85              "52:9: " + getCheckMessage(MSG_STATIC),
86              "61:5: " + getCheckMessage(MSG_CONSTRUCTOR),
87              "87:5: " + getCheckMessage(MSG_INSTANCE),
88              "107:9: " + getCheckMessage(MSG_STATIC),
89              "150:9: " + getCheckMessage(MSG_STATIC),
90              "159:5: " + getCheckMessage(MSG_CONSTRUCTOR),
91              "185:5: " + getCheckMessage(MSG_INSTANCE),
92          };
93          verify(checkConfig, getPath("InputDeclarationOrder.java"), expected);
94      }
95  
96      @Test
97      public void testOnlyModifiers() throws Exception {
98          final DefaultConfiguration checkConfig =
99              createModuleConfig(DeclarationOrderCheck.class);
100         checkConfig.addAttribute("ignoreConstructors", "true");
101         checkConfig.addAttribute("ignoreModifiers", "false");
102 
103         final String[] expected = {
104             "8:5: " + getCheckMessage(MSG_ACCESS),
105             "13:5: " + getCheckMessage(MSG_ACCESS),
106             "18:5: " + getCheckMessage(MSG_ACCESS),
107             "21:5: " + getCheckMessage(MSG_ACCESS),
108             "27:5: " + getCheckMessage(MSG_STATIC),
109             "34:9: " + getCheckMessage(MSG_ACCESS),
110             "52:9: " + getCheckMessage(MSG_STATIC),
111             "87:5: " + getCheckMessage(MSG_INSTANCE),
112             "99:9: " + getCheckMessage(MSG_ACCESS),
113             "107:9: " + getCheckMessage(MSG_STATIC),
114             "113:5: " + getCheckMessage(MSG_ACCESS),
115             "118:5: " + getCheckMessage(MSG_ACCESS),
116             "123:5: " + getCheckMessage(MSG_ACCESS),
117             "126:5: " + getCheckMessage(MSG_ACCESS),
118             "132:5: " + getCheckMessage(MSG_STATIC),
119             "139:9: " + getCheckMessage(MSG_ACCESS),
120             "150:9: " + getCheckMessage(MSG_STATIC),
121             "185:5: " + getCheckMessage(MSG_INSTANCE),
122             "189:9: " + getCheckMessage(MSG_ACCESS),
123         };
124         verify(checkConfig, getPath("InputDeclarationOrder.java"), expected);
125     }
126 
127     @Test
128     public void testTokensNotNull() {
129         final DeclarationOrderCheck check = new DeclarationOrderCheck();
130         Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens());
131         Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens());
132         Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens());
133     }
134 
135     @Test
136     public void testParents() {
137         final DetailAST parent = new DetailAST();
138         parent.setType(TokenTypes.STATIC_INIT);
139         final DetailAST method = new DetailAST();
140         method.setType(TokenTypes.METHOD_DEF);
141         parent.setFirstChild(method);
142         final DetailAST ctor = new DetailAST();
143         ctor.setType(TokenTypes.CTOR_DEF);
144         method.setNextSibling(ctor);
145 
146         final DeclarationOrderCheck check = new DeclarationOrderCheck();
147 
148         check.visitToken(method);
149         final SortedSet<LocalizedMessage> messages1 = check.getMessages();
150 
151         assertEquals("No exception messages expected", 0, messages1.size());
152 
153         check.visitToken(ctor);
154         final SortedSet<LocalizedMessage> messages2 = check.getMessages();
155 
156         assertEquals("No exception messages expected", 0, messages2.size());
157     }
158 
159     @Test
160     public void testImproperToken() {
161         final DetailAST parent = new DetailAST();
162         parent.setType(TokenTypes.STATIC_INIT);
163         final DetailAST array = new DetailAST();
164         array.setType(TokenTypes.ARRAY_INIT);
165         parent.setFirstChild(array);
166 
167         final DeclarationOrderCheck check = new DeclarationOrderCheck();
168 
169         check.visitToken(array);
170         final SortedSet<LocalizedMessage> messages = check.getMessages();
171 
172         assertEquals("No exception messages expected", 0, messages.size());
173     }
174 
175     @Test
176     public void testForwardReference() throws Exception {
177         final DefaultConfiguration checkConfig = createModuleConfig(DeclarationOrderCheck.class);
178         final String[] expected = {
179             "12:5: " + getCheckMessage(MSG_ACCESS),
180             "13:5: " + getCheckMessage(MSG_ACCESS),
181             "14:5: " + getCheckMessage(MSG_ACCESS),
182             "15:5: " + getCheckMessage(MSG_ACCESS),
183             "16:5: " + getCheckMessage(MSG_ACCESS),
184             "17:5: " + getCheckMessage(MSG_ACCESS),
185             "23:5: " + getCheckMessage(MSG_ACCESS),
186             "41:5: " + getCheckMessage(MSG_STATIC),
187         };
188         verify(checkConfig, getPath("InputDeclarationOrderForwardReference.java"), expected);
189     }
190 
191     @Test
192     public void testAvoidDuplicatesForStaticFinalFields() throws Exception {
193         final DefaultConfiguration checkConfig = createModuleConfig(DeclarationOrderCheck.class);
194         final String[] expected = {
195             "6:5: " + getCheckMessage(MSG_STATIC),
196         };
197         verify(checkConfig,
198                 getPath("InputDeclarationOrderAvoidDuplicatesInStaticFinalFields.java"),
199                 expected);
200     }
201 }