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