/
ResourceTest.java
300 lines (254 loc) · 10.3 KB
/
ResourceTest.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under
// the terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0
//
// This Source Code may also be made available under the following
// Secondary Licenses when the conditions for such availability set
// forth in the Eclipse Public License, v. 2.0 are satisfied:
// the Apache License v2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.util.resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.stream.Stream;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.IO;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class ResourceTest
{
private static final boolean DIR = true;
private static final boolean EXISTS = true;
static class Scenario
{
Resource resource;
String test;
boolean exists;
boolean dir;
String content;
Scenario(Scenario data, String path, boolean exists, boolean dir)
throws Exception
{
this.test = data.resource + "+" + path;
resource = data.resource.addPath(path);
this.exists = exists;
this.dir = dir;
}
Scenario(Scenario data, String path, boolean exists, boolean dir, String content)
throws Exception
{
this.test = data.resource + "+" + path;
resource = data.resource.addPath(path);
this.exists = exists;
this.dir = dir;
this.content = content;
}
Scenario(URL url, boolean exists, boolean dir)
throws Exception
{
this.test = url.toString();
this.exists = exists;
this.dir = dir;
resource = Resource.newResource(url);
}
Scenario(String url, boolean exists, boolean dir)
throws Exception
{
this.test = url;
this.exists = exists;
this.dir = dir;
resource = Resource.newResource(url);
}
Scenario(URI uri, boolean exists, boolean dir)
throws Exception
{
this.test = uri.toASCIIString();
this.exists = exists;
this.dir = dir;
resource = Resource.newResource(uri);
}
Scenario(File file, boolean exists, boolean dir)
{
this.test = file.toString();
this.exists = exists;
this.dir = dir;
resource = Resource.newResource(file);
}
Scenario(String url, boolean exists, boolean dir, String content)
throws Exception
{
this.test = url;
this.exists = exists;
this.dir = dir;
this.content = content;
resource = Resource.newResource(url);
}
@Override
public String toString()
{
return this.test;
}
}
static class Scenarios extends ArrayList<Arguments>
{
final File fileRef;
final URI uriRef;
final String relRef;
final Scenario[] baseCases;
public Scenarios(String ref) throws Exception
{
// relative directory reference
this.relRef = FS.separators(ref);
// File object reference
this.fileRef = MavenTestingUtils.getProjectDir(relRef);
// URI reference
this.uriRef = fileRef.toURI();
// create baseline cases
baseCases = new Scenario[]{
new Scenario(relRef, EXISTS, DIR),
new Scenario(uriRef, EXISTS, DIR),
new Scenario(fileRef, EXISTS, DIR)
};
// add all baseline cases
for (Scenario bcase : baseCases)
{
addCase(bcase);
}
}
public void addCase(Scenario ucase)
{
add(Arguments.of(ucase));
}
public void addAllSimpleCases(String subpath, boolean exists, boolean dir)
throws Exception
{
addCase(new Scenario(FS.separators(relRef + subpath), exists, dir));
addCase(new Scenario(uriRef.resolve(subpath).toURL(), exists, dir));
addCase(new Scenario(new File(fileRef, subpath), exists, dir));
}
public Scenario addAllAddPathCases(String subpath, boolean exists, boolean dir) throws Exception
{
Scenario bdata = null;
for (Scenario bcase : baseCases)
{
bdata = new Scenario(bcase, subpath, exists, dir);
addCase(bdata);
}
return bdata;
}
}
public static Stream<Arguments> scenarios() throws Exception
{
Scenarios cases = new Scenarios("src/test/resources/");
File testDir = MavenTestingUtils.getTargetTestingDir(ResourceTest.class.getName());
FS.ensureEmpty(testDir);
File tmpFile = new File(testDir, "test.tmp");
FS.touch(tmpFile);
cases.addCase(new Scenario(tmpFile.toString(), EXISTS, !DIR));
// Some resource references.
cases.addAllSimpleCases("resource.txt", EXISTS, !DIR);
cases.addAllSimpleCases("NoName.txt", !EXISTS, !DIR);
// Some addPath() forms
cases.addAllAddPathCases("resource.txt", EXISTS, !DIR);
cases.addAllAddPathCases("/resource.txt", EXISTS, !DIR);
cases.addAllAddPathCases("//resource.txt", EXISTS, !DIR);
cases.addAllAddPathCases("NoName.txt", !EXISTS, !DIR);
cases.addAllAddPathCases("/NoName.txt", !EXISTS, !DIR);
cases.addAllAddPathCases("//NoName.txt", !EXISTS, !DIR);
Scenario tdata1 = cases.addAllAddPathCases("TestData", EXISTS, DIR);
Scenario tdata2 = cases.addAllAddPathCases("TestData/", EXISTS, DIR);
cases.addCase(new Scenario(tdata1, "alphabet.txt", EXISTS, !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
cases.addCase(new Scenario(tdata2, "alphabet.txt", EXISTS, !DIR, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
cases.addCase(new Scenario("jar:file:/somejar.jar!/content/", !EXISTS, !DIR));
cases.addCase(new Scenario("jar:file:/somejar.jar!/", !EXISTS, !DIR));
String urlRef = cases.uriRef.toASCIIString();
Scenario zdata = new Scenario("jar:" + urlRef + "TestData/test.zip!/", EXISTS, DIR);
cases.addCase(zdata);
cases.addCase(new Scenario(zdata, "Unknown", !EXISTS, !DIR));
cases.addCase(new Scenario(zdata, "/Unknown/", !EXISTS, !DIR));
cases.addCase(new Scenario(zdata, "subdir", EXISTS, DIR));
cases.addCase(new Scenario(zdata, "/subdir/", EXISTS, DIR));
cases.addCase(new Scenario(zdata, "alphabet", EXISTS, !DIR,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
cases.addCase(new Scenario(zdata, "/subdir/alphabet", EXISTS, !DIR,
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
cases.addAllAddPathCases("/TestData/test/subdir/subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("//TestData/test/subdir/subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("/TestData//test/subdir/subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("/TestData/test//subdir/subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("/TestData/test/subdir//subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("TestData/test/subdir/subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("TestData/test/subdir/subsubdir//", EXISTS, DIR);
cases.addAllAddPathCases("TestData/test/subdir//subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("TestData/test//subdir/subsubdir/", EXISTS, DIR);
cases.addAllAddPathCases("/TestData/../TestData/test/subdir/subsubdir/", EXISTS, DIR);
return cases.stream();
}
@ParameterizedTest
@MethodSource("scenarios")
public void testResourceExists(Scenario data)
{
assertThat("Exists: " + data.resource.getName(), data.resource.exists(), equalTo(data.exists));
}
@ParameterizedTest
@MethodSource("scenarios")
public void testResourceDir(Scenario data)
{
assertThat("Is Directory: " + data.test, data.resource.isDirectory(), equalTo(data.dir));
}
@ParameterizedTest
@MethodSource("scenarios")
public void testEncodeAddPath(Scenario data)
throws Exception
{
if (data.dir)
{
Resource r = data.resource.addPath("foo%/b r");
assertThat(r.getURI().toString(), Matchers.endsWith("/foo%25/b%20r"));
}
}
@ParameterizedTest
@MethodSource("scenarios")
public void testResourceContent(Scenario data)
throws Exception
{
Assumptions.assumeTrue(data.content != null);
InputStream in = data.resource.getInputStream();
String c = IO.toString(in);
assertThat("Content: " + data.test, c, startsWith(data.content));
}
@Test
@DisabledOnOs(OS.WINDOWS) // this uses forbidden characters on some Windows Environments
public void testGlobPath() throws IOException
{
Path testDir = MavenTestingUtils.getTargetTestingPath("testGlobPath");
FS.ensureEmpty(testDir);
String globReference = testDir.toAbsolutePath().toString() + File.separator + '*';
Resource globResource = Resource.newResource(globReference);
assertNotNull(globResource, "Should have produced a Resource");
}
}