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.google.checkstyle.test.chapter3filestructure.rule341onetoplevel;
21  
22  import org.junit.jupiter.api.Test;
23  
24  import com.google.checkstyle.test.base.AbstractGoogleModuleTestSupport;
25  import com.puppycrawl.tools.checkstyle.api.Configuration;
26  import com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck;
27  import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
28  
29  public class OneTopLevelClassTest extends AbstractGoogleModuleTestSupport {
30  
31      @Override
32      protected String getPackageLocation() {
33          return "com/google/checkstyle/test/chapter3filestructure/rule341onetoplevel";
34      }
35  
36      @Test
37      public void testBad() throws Exception {
38          final Class<OneTopLevelClassCheck> clazz = OneTopLevelClassCheck.class;
39          final String messageKey = "one.top.level.class";
40  
41          final String[] expected = {
42              "25:1: " + getCheckMessage(clazz, messageKey, "NoSuperClone"),
43              "33:1: " + getCheckMessage(clazz, messageKey, "InnerClone"),
44              "50:1: " + getCheckMessage(clazz, messageKey, "CloneWithTypeArguments"),
45              "55:1: " + getCheckMessage(clazz, messageKey, "CloneWithTypeArgumentsAndNoSuper"),
46              "60:1: " + getCheckMessage(clazz, messageKey, "MyClassWithGenericSuperMethod"),
47              "77:1: " + getCheckMessage(clazz, messageKey, "AnotherClass"),
48          };
49  
50          final Configuration checkConfig = getModuleConfig("OneTopLevelClass");
51          final String filePath = getPath("InputOneTopLevelClassBasic.java");
52  
53          final Integer[] warnList = getLinesWithWarn(filePath);
54          verify(checkConfig, filePath, expected, warnList);
55      }
56  
57      @Test
58      public void testGood() throws Exception {
59          final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
60  
61          final Configuration checkConfig = getModuleConfig("OneTopLevelClass");
62          final String filePath = getPath("InputOneTopLevelClassGood.java");
63  
64          final Integer[] warnList = getLinesWithWarn(filePath);
65          verify(checkConfig, filePath, expected, warnList);
66      }
67  
68      @Test
69      public void testBad1() throws Exception {
70          final Class<OneTopLevelClassCheck> clazz = OneTopLevelClassCheck.class;
71          final String messageKey = "one.top.level.class";
72  
73          final String[] expected = {
74              "4:1: " + getCheckMessage(clazz, messageKey, "FooEnum"),
75              "5:1: " + getCheckMessage(clazz, messageKey, "FooAt"),
76          };
77  
78          final Configuration checkConfig = getModuleConfig("OneTopLevelClass");
79          final String filePath = getPath("InputOneTopLevelClassBad1.java");
80  
81          final Integer[] warnList = getLinesWithWarn(filePath);
82          verify(checkConfig, filePath, expected, warnList);
83      }
84  
85      @Test
86      public void testBad2() throws Exception {
87          final Class<OneTopLevelClassCheck> clazz = OneTopLevelClassCheck.class;
88          final String messageKey = "one.top.level.class";
89  
90          final String[] expected = {
91              "5:1: " + getCheckMessage(clazz, messageKey, "FooIn"),
92              "7:1: " + getCheckMessage(clazz, messageKey, "FooClass"),
93          };
94  
95          final Configuration checkConfig = getModuleConfig("OneTopLevelClass");
96          final String filePath = getPath("InputOneTopLevelClassBad2.java");
97  
98          final Integer[] warnList = getLinesWithWarn(filePath);
99          verify(checkConfig, filePath, expected, warnList);
100     }
101 
102 }