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.design;
21  
22  import static com.puppycrawl.tools.checkstyle.checks.design.OneTopLevelClassCheck.MSG_KEY;
23  import static org.junit.Assert.assertArrayEquals;
24  
25  import java.io.File;
26  import java.util.Arrays;
27  import java.util.Collections;
28  import java.util.List;
29  
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  import com.google.common.collect.ImmutableMap;
34  import com.puppycrawl.tools.checkstyle.AbstractModuleTestSupport;
35  import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
36  import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
37  
38  public class OneTopLevelClassCheckTest extends AbstractModuleTestSupport {
39      @Override
40      protected String getPackageLocation() {
41          return "com/puppycrawl/tools/checkstyle/checks/design/onetoplevelclass";
42      }
43  
44      @Test
45      public void testGetRequiredTokens() {
46          final OneTopLevelClassCheck checkObj = new OneTopLevelClassCheck();
47          assertArrayEquals("Required tokens array is not empty",
48                  CommonUtils.EMPTY_INT_ARRAY, checkObj.getRequiredTokens());
49      }
50  
51      @Test
52      public void testClearState() throws Exception {
53          final DefaultConfiguration checkConfig =
54                  createModuleConfig(OneTopLevelClassCheck.class);
55          final String firstInputFilePath = getPath("InputOneTopLevelClassDeclarationOrder.java");
56          final String secondInputFilePath = getPath("InputOneTopLevelClassInterface2.java");
57  
58          final File[] inputs = {
59              new File(firstInputFilePath),
60              new File(secondInputFilePath),
61          };
62  
63          final List<String> expectedFirstInput = Collections.singletonList(
64              "10: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderEnum"));
65          final List<String> expectedSecondInput = Arrays.asList(
66              "3: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface2inner1"),
67              "11: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface2inner2"));
68  
69          verify(createChecker(checkConfig), inputs,
70              ImmutableMap.of(firstInputFilePath, expectedFirstInput,
71                  secondInputFilePath, expectedSecondInput));
72      }
73  
74      @Test
75      public void testAcceptableTokens() {
76          final OneTopLevelClassCheck check = new OneTopLevelClassCheck();
77          check.getAcceptableTokens();
78          // ZERO tokens as Check do Traverse of Tree himself, he does not need to subscribed to
79          // Tokens
80          Assert.assertEquals("Acceptable tokens array size larger than 0",
81                  0, check.getAcceptableTokens().length);
82      }
83  
84      @Test
85      public void testFileWithOneTopLevelClass() throws Exception {
86          final DefaultConfiguration checkConfig =
87              createModuleConfig(OneTopLevelClassCheck.class);
88          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
89          verify(checkConfig, getPath("InputOneTopLevelClass.java"), expected);
90      }
91  
92      @Test
93      public void testFileWithOneTopLevelInterface() throws Exception {
94          final DefaultConfiguration checkConfig =
95                  createModuleConfig(OneTopLevelClassCheck.class);
96          final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
97          verify(checkConfig, getPath("InputOneTopLevelClassInterface.java"), expected);
98      }
99  
100     @Test
101     public void testFileWithOneTopLevelEnum() throws Exception {
102         final DefaultConfiguration checkConfig =
103                 createModuleConfig(OneTopLevelClassCheck.class);
104         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
105         verify(checkConfig, getPath("InputOneTopLevelClassEnum.java"), expected);
106     }
107 
108     @Test
109     public void testFileWithNoPublicTopLevelClass() throws Exception {
110         final DefaultConfiguration checkConfig =
111                 createModuleConfig(OneTopLevelClassCheck.class);
112         final String[] expected = {
113             "8: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassNoPublic2"),
114         };
115         verify(checkConfig, getPath("InputOneTopLevelClassNoPublic.java"), expected);
116     }
117 
118     @Test
119     public void testFileWithThreeTopLevelInterface() throws Exception {
120         final DefaultConfiguration checkConfig =
121                 createModuleConfig(OneTopLevelClassCheck.class);
122         final String[] expected = {
123             "3: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface2inner1"),
124             "11: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassInterface2inner2"),
125         };
126         verify(checkConfig, getPath("InputOneTopLevelClassInterface2.java"), expected);
127     }
128 
129     @Test
130     public void testFileWithThreeTopLevelEnum() throws Exception {
131         final DefaultConfiguration checkConfig =
132                 createModuleConfig(OneTopLevelClassCheck.class);
133         final String[] expected = {
134             "3: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassEnum2inner1"),
135             "11: " + getCheckMessage(MSG_KEY, "InputOneTopLevelClassEnum2inner2"),
136         };
137         verify(checkConfig, getPath("InputOneTopLevelClassEnum2.java"), expected);
138     }
139 
140     @Test
141     public void testFileWithFewTopLevelClasses() throws Exception {
142         final DefaultConfiguration checkConfig =
143             createModuleConfig(OneTopLevelClassCheck.class);
144         final String[] expected = {
145             "25: " + getCheckMessage(MSG_KEY, "NoSuperClone"),
146             "29: " + getCheckMessage(MSG_KEY, "InnerClone"),
147             "33: " + getCheckMessage(MSG_KEY, "CloneWithTypeArguments"),
148             "37: " + getCheckMessage(MSG_KEY, "CloneWithTypeArgumentsAndNoSuper"),
149             "41: " + getCheckMessage(MSG_KEY, "MyClassWithGenericSuperMethod"),
150             "45: " + getCheckMessage(MSG_KEY, "AnotherClass"),
151             "48: " + getCheckMessage(MSG_KEY, "NativeTest"),
152         };
153         verify(checkConfig, getPath("InputOneTopLevelClassClone.java"), expected);
154     }
155 
156     @Test
157     public void testFileWithSecondEnumTopLevelClass() throws Exception {
158         final DefaultConfiguration checkConfig =
159             createModuleConfig(OneTopLevelClassCheck.class);
160         final String[] expected = {
161             "10: " + getCheckMessage(MSG_KEY, "InputDeclarationOrderEnum"),
162         };
163         verify(checkConfig, getPath("InputOneTopLevelClassDeclarationOrder.java"), expected);
164     }
165 
166     @Test
167     public void testPackageInfoWithNoTypesDeclared() throws Exception {
168         final DefaultConfiguration checkConfig = createModuleConfig(OneTopLevelClassCheck.class);
169         final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
170         verify(checkConfig, getNonCompilablePath("package-info.java"), expected);
171     }
172 }