-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
PackageNamesLoaderTest.java
266 lines (234 loc) · 10.2 KB
/
PackageNamesLoaderTest.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
///////////////////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
// Copyright (C) 2001-2022 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.puppycrawl.tools.checkstyle;
import static com.google.common.truth.Truth.assertWithMessage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;
import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
/**
* Tests loading of package names from XML files.
*
* @noinspection ClassLoaderInstantiation
* @noinspectionreason ClassLoaderInstantiation - Custom class loader is needed to pass URLs to
* pretend these are loaded from the classpath though we can't add/change the files for
* testing. The class loader is nested in this class, so the custom class loader we
* are using is safe.
*/
public class PackageNamesLoaderTest extends AbstractPathTestSupport {
@Override
protected String getPackageLocation() {
return "com/puppycrawl/tools/checkstyle/packagenamesloader";
}
@Test
public void testDefault()
throws CheckstyleException {
final Set<String> packageNames = PackageNamesLoader
.getPackageNames(Thread.currentThread()
.getContextClassLoader());
assertWithMessage("pkgNames.length.")
.that(packageNames)
.isEmpty();
}
@Test
public void testNoPackages() throws Exception {
final Set<String> actualPackageNames = PackageNamesLoader
.getPackageNames(new TestUrlsClassLoader(Collections.emptyEnumeration()));
assertWithMessage("Invalid package names length.")
.that(actualPackageNames)
.isEmpty();
}
@Test
public void testPackagesFile() throws Exception {
final Enumeration<URL> enumeration = Collections.enumeration(Collections.singleton(
new File(getPath("InputPackageNamesLoaderFile.xml")).toURI().toURL()));
final Set<String> actualPackageNames = PackageNamesLoader
.getPackageNames(new TestUrlsClassLoader(enumeration));
final String[] expectedPackageNames = {
"com.puppycrawl.tools.checkstyle",
"com.puppycrawl.tools.checkstyle.checks",
"com.puppycrawl.tools.checkstyle.checks.annotation",
"com.puppycrawl.tools.checkstyle.checks.blocks",
"com.puppycrawl.tools.checkstyle.checks.coding",
"com.puppycrawl.tools.checkstyle.checks.design",
"com.puppycrawl.tools.checkstyle.checks.header",
"com.puppycrawl.tools.checkstyle.checks.imports",
"com.puppycrawl.tools.checkstyle.checks.indentation",
"com.puppycrawl.tools.checkstyle.checks.javadoc",
"com.puppycrawl.tools.checkstyle.checks.metrics",
"com.puppycrawl.tools.checkstyle.checks.modifier",
"com.puppycrawl.tools.checkstyle.checks.naming",
"com.puppycrawl.tools.checkstyle.checks.regexp",
"com.puppycrawl.tools.checkstyle.checks.sizes",
"com.puppycrawl.tools.checkstyle.checks.whitespace",
"com.puppycrawl.tools.checkstyle.filefilters",
"com.puppycrawl.tools.checkstyle.filters",
};
assertWithMessage("Invalid package names length.")
.that(actualPackageNames)
.hasSize(expectedPackageNames.length);
final Set<String> checkstylePackagesSet =
new HashSet<>(Arrays.asList(expectedPackageNames));
assertWithMessage("Invalid names set.")
.that(actualPackageNames)
.isEqualTo(checkstylePackagesSet);
}
@Test
public void testPackagesWithDots() throws Exception {
final Enumeration<URL> enumeration = Collections.enumeration(Collections.singleton(
new File(getPath("InputPackageNamesLoaderWithDots.xml")).toURI().toURL()));
final Set<String> actualPackageNames = PackageNamesLoader
.getPackageNames(new TestUrlsClassLoader(enumeration));
final String[] expectedPackageNames = {
"coding.",
};
assertWithMessage("Invalid package names length.")
.that(actualPackageNames)
.hasSize(expectedPackageNames.length);
final Set<String> checkstylePackagesSet =
new HashSet<>(Arrays.asList(expectedPackageNames));
assertWithMessage("Invalid names set.")
.that(actualPackageNames)
.isEqualTo(checkstylePackagesSet);
}
@Test
public void testPackagesWithDotsEx() throws Exception {
final Enumeration<URL> enumeration = Collections.enumeration(Collections.singleton(
new File(getPath("InputPackageNamesLoaderWithDotsEx.xml")).toURI().toURL()));
final Set<String> actualPackageNames = PackageNamesLoader
.getPackageNames(new TestUrlsClassLoader(enumeration));
final String[] expectedPackageNames = {
"coding.specific",
"coding.",
};
assertWithMessage("Invalid package names length.")
.that(actualPackageNames)
.hasSize(expectedPackageNames.length);
final Set<String> checkstylePackagesSet =
new HashSet<>(Arrays.asList(expectedPackageNames));
assertWithMessage("Invalid names set.")
.that(actualPackageNames)
.isEqualTo(checkstylePackagesSet);
}
@Test
public void testPackagesWithSaxException() throws Exception {
final Enumeration<URL> enumeration = Collections.enumeration(Collections.singleton(
new File(getPath("InputPackageNamesLoaderNotXml.java")).toURI().toURL()));
try {
PackageNamesLoader.getPackageNames(new TestUrlsClassLoader(enumeration));
assertWithMessage("CheckstyleException is expected").fail();
}
catch (CheckstyleException ex) {
assertWithMessage("Invalid exception cause class")
.that(ex)
.hasCauseThat()
.isInstanceOf(SAXException.class);
}
}
@Test
public void testPackagesWithIoException() throws Exception {
final URLConnection urlConnection = new URLConnection(null) {
@Override
public void connect() {
// no code
}
@Override
public InputStream getInputStream() {
return null;
}
};
final URL url = new URL("test", null, 0, "", new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) {
return urlConnection;
}
});
final Enumeration<URL> enumeration = Collections.enumeration(Collections.singleton(url));
try {
PackageNamesLoader.getPackageNames(new TestUrlsClassLoader(enumeration));
assertWithMessage("CheckstyleException is expected").fail();
}
catch (CheckstyleException ex) {
assertWithMessage("Invalid exception cause class")
.that(ex)
.hasCauseThat()
.isInstanceOf(IOException.class);
assertWithMessage("Invalid exception message")
.that(ex)
.hasMessageThat()
.isNotEqualTo("unable to get package file resources");
}
}
@Test
public void testPackagesWithIoExceptionGetResources() {
try {
PackageNamesLoader.getPackageNames(new TestIoExceptionClassLoader());
assertWithMessage("CheckstyleException is expected").fail();
}
catch (CheckstyleException ex) {
assertWithMessage("Invalid exception cause class")
.that(ex)
.hasCauseThat()
.isInstanceOf(IOException.class);
assertWithMessage("Invalid exception message")
.that(ex.getMessage())
.isEqualTo("unable to get package file resources");
}
}
/**
* Mocked ClassLoader for testing URL loading.
*
* @noinspection CustomClassloader
* @noinspectionreason CustomClassloader - needed to pass URLs to pretend these are loaded
* from the classpath though we can't add/change the files for testing
*/
private static class TestUrlsClassLoader extends ClassLoader {
private final Enumeration<URL> urls;
/* package */ TestUrlsClassLoader(Enumeration<URL> urls) {
this.urls = urls;
}
@Override
public Enumeration<URL> getResources(String name) {
return urls;
}
}
/**
* Mocked ClassLoader for testing exceptions.
*
* @noinspection CustomClassloader
* @noinspectionreason CustomClassloader - needed to throw an exception to
* test a catch statement
*/
private static class TestIoExceptionClassLoader extends ClassLoader {
@Override
public Enumeration<URL> getResources(String name) throws IOException {
throw new IOException("test");
}
}
}