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.modifier;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck.MSG_ANNOTATION_ORDER;
23  import static com.puppycrawl.tools.checkstyle.checks.modifier.ModifierOrderCheck.MSG_MODIFIER_ORDER;
24  import static org.junit.Assert.assertArrayEquals;
25  
26  import org.junit.Assert;
27  import org.junit.Test;
28  
29  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
30  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
31  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
32  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
33  
34  public class ModifierOrderCheckTest
35      extends AbstractModuleTestSupport {
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/modifier/modifierorder";
39      }
40  
41      @Test
42      public void testGetRequiredTokens() {
43          final ModifierOrderCheck checkObj = new ModifierOrderCheck();
44          final int[] expected = {TokenTypes.MODIFIERS};
45          assertArrayEquals("Default required tokens are invalid",
46              expected, checkObj.getRequiredTokens());
47      }
48  
49      @Test
50      public void testIt() throws Exception {
51          final DefaultConfiguration checkConfig =
52              createModuleConfig(ModifierOrderCheck.class);
53          final String[] expected = {
54              "14:10: " + getCheckMessage(MSG_MODIFIER_ORDER, "final"),
55              "18:12: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"),
56              "24:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "private"),
57              "34:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"),
58              "39:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation2"),
59              "49:35: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MyAnnotation4"),
60              "157:14: " + getCheckMessage(MSG_MODIFIER_ORDER, "default"),
61          };
62          verify(checkConfig, getPath("InputModifierOrderIt.java"), expected);
63      }
64  
65      @Test
66      public void testDefaultMethods()
67              throws Exception {
68          final DefaultConfiguration checkConfig =
69                  createModuleConfig(ModifierOrderCheck.class);
70          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
71          verify(checkConfig, getPath("InputModifierOrderDefaultMethods.java"), expected);
72      }
73  
74      @Test
75      public void testGetDefaultTokens() {
76          final ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
77          final int[] actual = modifierOrderCheckObj.getDefaultTokens();
78          final int[] expected = {TokenTypes.MODIFIERS};
79          final int[] unexpectedArray = {
80              TokenTypes.MODIFIERS,
81              TokenTypes.OBJBLOCK,
82          };
83          assertArrayEquals("Default tokens are invalid", expected, actual);
84          final int[] unexpectedEmptyArray = CommonUtils.EMPTY_INT_ARRAY;
85          Assert.assertNotSame("Default tokens should not be empty array",
86                  unexpectedEmptyArray, actual);
87          Assert.assertNotSame("Invalid default tokens", unexpectedArray, actual);
88          Assert.assertNotNull("Default tokens should not be null", actual);
89      }
90  
91      @Test
92      public void testGetAcceptableTokens() {
93          final ModifierOrderCheck modifierOrderCheckObj = new ModifierOrderCheck();
94          final int[] actual = modifierOrderCheckObj.getAcceptableTokens();
95          final int[] expected = {TokenTypes.MODIFIERS};
96          final int[] unexpectedArray = {
97              TokenTypes.MODIFIERS,
98              TokenTypes.OBJBLOCK,
99          };
100         assertArrayEquals("Default acceptable tokens are invalid", expected, actual);
101         final int[] unexpectedEmptyArray = CommonUtils.EMPTY_INT_ARRAY;
102         Assert.assertNotSame("Default tokens should not be empty array",
103                 unexpectedEmptyArray, actual);
104         Assert.assertNotSame("Invalid acceptable tokens", unexpectedArray, actual);
105         Assert.assertNotNull("Acceptable tokens should not be null", actual);
106     }
107 
108     @Test
109     public void testSkipTypeAnnotations() throws Exception {
110         // Type Annotations are available only in Java 8
111         // We skip type annotations from validation
112         // See https://github.com/checkstyle/checkstyle/issues/903#issuecomment-172228013
113         final DefaultConfiguration checkConfig = createModuleConfig(ModifierOrderCheck.class);
114         final String[] expected = {
115             "103:13: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@MethodAnnotation"),
116         };
117         verify(checkConfig, getNonCompilablePath("InputModifierOrderTypeAnnotations.java"),
118             expected);
119     }
120 
121     @Test
122     public void testAnnotationOnAnnotationDeclaration() throws Exception {
123         final DefaultConfiguration checkConfig = createModuleConfig(ModifierOrderCheck.class);
124         final String[] expected = {
125             "3:8: " + getCheckMessage(MSG_ANNOTATION_ORDER, "@InterfaceAnnotation"),
126         };
127         verify(checkConfig, getPath("InputModifierOrderAnnotationDeclaration.java"), expected);
128     }
129 }