forked from checkstyle/checkstyle
/
CustomImportOrderTest.java
143 lines (116 loc) · 6.68 KB
/
CustomImportOrderTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2017 the original author or authors.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
////////////////////////////////////////////////////////////////////////////////
package com.google.checkstyle.test.chapter3filestructure.rule333orderingandspacing;
import org.junit.Test;
import com.google.checkstyle.test.base.AbstractModuleTestSupport;
import com.puppycrawl.tools.checkstyle.api.Configuration;
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck;
import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
public class CustomImportOrderTest extends AbstractModuleTestSupport {
/** Shortcuts to make code more compact. */
private static final String MSG_LINE_SEPARATOR = CustomImportOrderCheck.MSG_LINE_SEPARATOR;
private static final String MSG_LEX = CustomImportOrderCheck.MSG_LEX;
private static final String MSG_NONGROUP_EXPECTED =
CustomImportOrderCheck.MSG_NONGROUP_EXPECTED;
private static final String STATIC = CustomImportOrderCheck.STATIC_RULE_GROUP;
private final Class<CustomImportOrderCheck> clazz = CustomImportOrderCheck.class;
@Override
protected String getPackageLocation() {
return "com/google/checkstyle/test/chapter3filestructure/rule333orderingandspacing";
}
@Test
public void testCustomImport1() throws Exception {
final String[] expected = {
"4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
"java.io.File.createTempFile"),
"6: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Button"),
"8: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Dialog", "java.awt.Frame"),
"12: " + getCheckMessage(clazz, MSG_LEX, "java.io.File", "javax.swing.JTable"),
"13: " + getCheckMessage(clazz, MSG_LEX, "java.io.IOException", "javax.swing.JTable"),
"14: " + getCheckMessage(clazz, MSG_LEX, "java.io.InputStream", "javax.swing.JTable"),
"15: " + getCheckMessage(clazz, MSG_LEX, "java.io.Reader", "javax.swing.JTable"),
"17: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.base.Ascii",
"javax.swing.JTable"),
};
final Configuration checkConfig = getCheckConfig("CustomImportOrder");
final String filePath = getPath("InputCustomImportOrder1.java");
final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}
@Test
public void testCustomImport2() throws Exception {
final String[] expected = {
"4: " + getCheckMessage(clazz, MSG_LEX, "java.awt.Button.ABORT",
"java.io.File.createTempFile"),
"9: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
"11: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
"java.util.concurrent.AbstractExecutorService"),
"13: " + getCheckMessage(clazz, MSG_LEX,
"com.google.checkstyle.test.chapter2filebasic.rule21filename.*",
"java.util.concurrent.AbstractExecutorService"),
"14: " + getCheckMessage(clazz, MSG_LEX, "com.sun.xml.internal.xsom.impl.scd.Iterators",
"java.util.concurrent.AbstractExecutorService"),
"16: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
"java.util.concurrent.AbstractExecutorService"),
};
final Configuration checkConfig = getCheckConfig("CustomImportOrder");
final String filePath = getPath("InputCustomImportOrder2.java");
final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}
@Test
public void testCustomImport3() throws Exception {
final String[] expected = {
"4: " + getCheckMessage(clazz, MSG_LINE_SEPARATOR, "java.awt.Dialog"),
"5: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
"javax.swing.WindowConstants.*"),
"7: " + getCheckMessage(clazz, MSG_LEX,
"com.google.checkstyle.test.chapter2filebasic.rule21filename.*", "java.awt.Dialog"),
"8: " + getCheckMessage(clazz, MSG_LEX, "com.google.common.reflect.*",
"java.awt.Dialog"),
"9: " + getCheckMessage(clazz, MSG_LEX, "com.sun.xml.internal.xsom.impl.scd.Iterators",
"java.awt.Dialog"),
"11: " + getCheckMessage(clazz, MSG_NONGROUP_EXPECTED, STATIC,
"java.io.File.createTempFile"),
"13: " + getCheckMessage(clazz, MSG_LEX, "java.util.*", "java.util.StringTokenizer"),
"15: " + getCheckMessage(clazz, MSG_LEX, "java.util.concurrent.*",
"java.util.concurrent.AbstractExecutorService"),
};
final Configuration checkConfig = getCheckConfig("CustomImportOrder");
final String filePath = getPath("InputCustomImportOrder3.java");
final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}
@Test
public void testValid() throws Exception {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
final Configuration checkConfig = getCheckConfig("CustomImportOrder");
final String filePath = getPath("InputCustomImportOrderValid.java");
final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}
@Test
public void testValidGoogleStyleOrderOfImports() throws Exception {
final String[] expected = CommonUtils.EMPTY_STRING_ARRAY;
final Configuration checkConfig = getCheckConfig("CustomImportOrder");
final String filePath = getPath("InputCustomImportOrderNoImports.java");
final Integer[] warnList = getLinesWithWarn(filePath);
verify(checkConfig, filePath, expected, warnList);
}
}