1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }