View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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.design;
21  
22  import static com.google.common.truth.Truth.assertWithMessage;
23  import static com.puppycrawl.tools.checkstyle.checks.design.InnerTypeLastCheck.MSG_KEY;
24  
25  import java.io.File;
26  
27  import org.junit.jupiter.api.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.CommonUtil;
33  
34  public class InnerTypeLastCheckTest extends AbstractModuleTestSupport {
35  
36      @Override
37      protected String getPackageLocation() {
38          return "com/puppycrawl/tools/checkstyle/checks/design/innertypelast";
39      }
40  
41      @Test
42      public void testGetRequiredTokens() {
43          final InnerTypeLastCheck checkObj = new InnerTypeLastCheck();
44          final int[] expected = {
45              TokenTypes.CLASS_DEF,
46              TokenTypes.INTERFACE_DEF,
47              TokenTypes.RECORD_DEF,
48          };
49          assertWithMessage("Default required tokens are invalid")
50              .that(checkObj.getRequiredTokens())
51              .isEqualTo(expected);
52      }
53  
54      @Test
55      public void testMembersBeforeInner() throws Exception {
56          final String[] expected = {
57              "50:9: " + getCheckMessage(MSG_KEY),
58              "71:9: " + getCheckMessage(MSG_KEY),
59              "75:9: " + getCheckMessage(MSG_KEY),
60              "84:5: " + getCheckMessage(MSG_KEY),
61              "101:9: " + getCheckMessage(MSG_KEY),
62          };
63          verifyWithInlineConfigParser(
64                  getPath("InputInnerTypeLastClass.java"), expected);
65      }
66  
67      @Test
68      public void testIfRootClassChecked() throws Exception {
69          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
70          verifyWithInlineConfigParser(
71                  getPath("InputInnerTypeLastClassRootClass.java"), expected);
72      }
73  
74      @Test
75      public void testIfRootClassChecked2() throws Exception {
76          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
77          verifyWithInlineConfigParser(
78                  getPath("InputInnerTypeLastClassRootClass2.java"), expected);
79      }
80  
81      @Test
82      public void testIfRootClassChecked3() throws Exception {
83          final DefaultConfiguration checkConfig =
84              createModuleConfig(InnerTypeLastCheck.class);
85          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
86          verify(createChecker(checkConfig), new File[] {
87              new File(getPath("InputInnerTypeLastClassRootClass.java")),
88              new File(getPath("InputInnerTypeLastClassRootClass.java")),
89          }, "InputInnerTypeLastClassRootClass.java", expected);
90      }
91  
92      @Test
93      public void testInnerTypeBeforeCtor() throws Exception {
94          final String[] expected = {
95              "13:5: " + getCheckMessage(MSG_KEY),
96              "22:5: " + getCheckMessage(MSG_KEY),
97              "31:5: " + getCheckMessage(MSG_KEY),
98          };
99          verifyWithInlineConfigParser(
100                 getPath("InputInnerTypeLastClassCtorsInitBlocks.java"), expected);
101     }
102 
103     @Test
104     public void testInnerTypeLastRecords() throws Exception {
105 
106         final String[] expected = {
107             "17:9: " + getCheckMessage(MSG_KEY),
108             "21:5: " + getCheckMessage(MSG_KEY),
109             "30:9: " + getCheckMessage(MSG_KEY),
110             "40:13: " + getCheckMessage(MSG_KEY),
111             "46:13: " + getCheckMessage(MSG_KEY),
112             "50:9: " + getCheckMessage(MSG_KEY),
113             "52:9: " + getCheckMessage(MSG_KEY),
114         };
115         verifyWithInlineConfigParser(
116                 getNonCompilablePath("InputInnerTypeLastRecords.java"), expected);
117     }
118 
119     @Test
120     public void testInnerTypeLastCstyleArray() throws Exception {
121         final String[] expected = {
122             "11:5: " + getCheckMessage(MSG_KEY),
123             "12:5: " + getCheckMessage(MSG_KEY),
124             "13:5: " + getCheckMessage(MSG_KEY),
125             "14:5: " + getCheckMessage(MSG_KEY),
126         };
127         verifyWithInlineConfigParser(
128                 getPath("InputInnerTypeLastArray.java"), expected);
129     }
130 
131     @Test
132     public void testGetAcceptableTokens() {
133         final InnerTypeLastCheck obj = new InnerTypeLastCheck();
134         final int[] expected = {
135             TokenTypes.CLASS_DEF,
136             TokenTypes.INTERFACE_DEF,
137             TokenTypes.RECORD_DEF,
138         };
139         assertWithMessage("Default acceptable tokens are invalid")
140             .that(obj.getAcceptableTokens())
141             .isEqualTo(expected);
142     }
143 
144 }