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