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