generated from microsoft/vscode-remote-try-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImfsContextTest.java
445 lines (369 loc) · 16.9 KB
/
ImfsContextTest.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package com.imfs;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.net.URI;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Test;
// Deliberately not constrained to unit testing,
// while still being developer testing.
public class ImfsContextTest {
@Before
public void setup() throws IOException {
var path = Paths.get(URI.create("imfs://ImfsContextTest/"));
var fs = (ImfsFileSystem) path.getFileSystem();
fs.reset();
}
@Test
public void testPwd() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
assertEquals("/", context.pwd());
}
@Test
public void testMkdir() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
assertEquals("/", context.pwd());
var kids = context.ls();
// Magic: 3 is because "*Test" is pre-populated with 3 entries
assertEquals(3, kids.size());
context.mkdir("foo");
kids = context.ls();
assertEquals(4, kids.size());
assertArrayEquals(new String[] { "Spanish", "foo", "history", "math" }, kids.toArray());
}
@Test
public void testCdSuccess() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var math = context.cd("math");
assertEquals(3, context.ls().size());
assertEquals(0, math.ls().size());
assertEquals("/math", math.pwd());
var parent = math.cd("..");
assertEquals(3, context.ls().size());
assertEquals(0, math.ls().size());
assertEquals(3, parent.ls().size());
assertEquals("/", parent.pwd());
}
private void assertDefaultKids(List<String> kids) {
assertArrayEquals(new String[] { "Spanish", "history", "math" }, kids.toArray());
}
@Test
public void testCdFailure() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var math = context.cd("foo");
assertEquals(3, context.ls().size());
assertEquals(null, math);
}
@Test
public void testCdLevels() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var math = context.cd("math");
assertEquals(3, context.ls().size());
assertEquals(0, math.ls().size());
assertEquals("/math", math.pwd());
var foo = math.mkdir("foo");
kids = context.ls();
assertDefaultKids(kids);
assertEquals(1, math.ls().size());
assertArrayEquals(new String[] { "foo" }, math.ls().toArray());
assertEquals(3, context.ls().size());
assertEquals(0, foo.ls().size());
}
@Test
public void testRmdir() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var history = Paths.get(URI.create("imfs://ImfsContextTest/history"));
assertEquals(true, Files.isDirectory(history));
context.rmdir("history");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "math" }, kids.toArray());
assertEquals(false, Files.isDirectory(history));
}
@Test
public void testMkfile() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
context.mkfile("fun.txt");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "fun.txt", "history", "math" }, kids.toArray());
var fun = Paths.get(URI.create("imfs://ImfsContextTest/fun.txt"));
assertEquals(false, Files.isDirectory(fun));
}
@Test
public void testMkfileFails() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
assertThrows(FileAlreadyExistsException.class,
() -> context.mkfile("math"));
}
@Test
public void testWriteLines() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
context.write("fun.txt", new String[] { "hello", "world" });
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "fun.txt", "history", "math" }, kids.toArray());
var fun = Paths.get(URI.create("imfs://ImfsContextTest/fun.txt"));
assertEquals(false, Files.isDirectory(fun));
var path = Paths.get(URI.create("imfs://ImfsContextTest/fun.txt"));
var fileSystem = (ImfsFileSystem) path.getFileSystem();
var blob = fileSystem.getBlob("fun.txt");
assertArrayEquals(new byte[] { 'h', 'e', 'l', 'l', 'o', '\n', 'w', 'o', 'r', 'l', 'd', '\n' }, blob);
}
@Test
public void testReadLines() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
context.write("fun.txt", new String[] { "hello", "world", "again" });
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "fun.txt", "history", "math" }, kids.toArray());
var fun = Paths.get(URI.create("imfs://ImfsContextTest/fun.txt"));
assertEquals(false, Files.isDirectory(fun));
var lines = context.readLines("fun.txt");
assertArrayEquals(new String[] { "hello", "world", "again" }, lines.toArray());
}
@Test
public void testMoveDir() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var fun = Paths.get(URI.create("imfs://ImfsContextTest/arithmetics"));
assertEquals(false, Files.isDirectory(fun));
context.mv("math", "arithmetics");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "arithmetics", "history" }, kids.toArray());
assertEquals(true, Files.isDirectory(fun));
}
@Test
public void testMoveFile() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var fun = Paths.get(URI.create("imfs://ImfsContextTest/fun.txt"));
assertEquals(false, Files.isRegularFile(fun));
context.write("dumb.txt", new String[] { "hello", "world", "again" });
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "dumb.txt", "history", "math" }, kids.toArray());
context.mv("dumb.txt", "fun.txt");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "fun.txt", "history", "math" }, kids.toArray());
assertEquals(true, Files.isRegularFile(fun));
}
@Test
public void testCopyDir() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var fun = Paths.get(URI.create("imfs://ImfsContextTest/arithmetics"));
assertEquals(false, Files.isDirectory(fun));
context.cp("math", "arithmetics");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "arithmetics", "history", "math" }, kids.toArray());
assertEquals(true, Files.isDirectory(fun));
}
@Test
public void testCopyFile() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var fun = Paths.get(URI.create("imfs://ImfsContextTest/fun.txt"));
assertEquals(false, Files.isRegularFile(fun));
context.write("dumb.txt", new String[] { "hello", "world", "again" });
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "dumb.txt", "history", "math" }, kids.toArray());
context.cp("dumb.txt", "fun.txt");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "dumb.txt", "fun.txt", "history", "math" }, kids.toArray());
assertEquals(true, Files.isRegularFile(fun));
}
@Test
public void testCopyColllision() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var fun = Paths.get(URI.create("imfs://ImfsContextTest/fun.txt"));
assertEquals(false, Files.isRegularFile(fun));
context.write("dumb.txt", new String[] { "hello", "world", "again" });
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "dumb.txt", "history", "math" }, kids.toArray());
assertThrows(FileAlreadyExistsException.class, () -> context.cp("dumb.txt", "math"));
}
@Test
public void testFindName() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
assertEquals("imfs://ImfsContextTest/math", context.find("math").toString());
assertEquals(null, context.find("fun.txt"));
}
@Test
public void testRmdirNonEmpty() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var math = context.cd("math");
assertEquals(3, context.ls().size());
assertEquals(0, math.ls().size());
assertEquals("/math", math.pwd());
var foo = math.mkdir("foo");
kids = context.ls();
assertDefaultKids(kids);
assertEquals(1, math.ls().size());
assertArrayEquals(new String[] { "foo" }, math.ls().toArray());
assertEquals(3, context.ls().size());
assertEquals(0, foo.ls().size());
assertThrows(DirectoryNotEmptyException.class, () -> context.rmdir("math"));
}
@Test
public void testRmdirsNonEmpty() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
assertDefaultKids(kids);
var math = context.cd("math");
assertEquals(3, context.ls().size());
assertEquals(0, math.ls().size());
assertEquals("/math", math.pwd());
var foo = math.mkdir("foo");
kids = context.ls();
assertDefaultKids(kids);
assertEquals(1, math.ls().size());
assertArrayEquals(new String[] { "foo" }, math.ls().toArray());
assertEquals(3, context.ls().size());
assertEquals(0, foo.ls().size());
context.rmdirs("math");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "history" }, kids.toArray());
assertEquals(0, math.ls().size());
assertEquals(false, Files.isDirectory(foo.getPath()));
}
@Test
public void testImportFiles() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
// use working dir to copy the source code files into memory
context.importFiles("src", "src");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "history", "math", "src" }, kids.toArray());
Pattern pattern = Pattern.compile(".*Test\\.java$");
var tests = Files.find(context.getPath(), Integer.MAX_VALUE, (path, attr) -> {
var pathString = path.toUri().toString();
var matched = pattern.matcher(pathString).matches();
return matched;
}).map(path -> path.toUri().getPath()).collect(Collectors.toList());
assertArrayEquals(new String[] { "/src/test/java/com/imfs/ImfsContextTest.java",
"/src/test/java/com/imfs/ImfsProviderTest.java",
"/src/test/java/com/imfs/app/AppTest.java" }, tests.toArray());
}
@Test
public void testImportFilesCollision() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var kids = context.ls();
// use working dir to copy the source code files into memory
context.importFiles("src", "src");
kids = context.ls();
assertArrayEquals(new String[] { "Spanish", "history", "math", "src" }, kids.toArray());
assertThrows(RuntimeException.class, () -> context.importFiles("src", "src"));
}
@Test
public void testMergeDirs() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
var original = Paths.get(URI.create("imfs://ImfsContextTest/src/test/java/com/imfs/ImfsContextTest.java"));
var copy = Paths
.get(URI.create("imfs://ImfsContextTest/src/test/java/com/imfs/Copy-1-of-ImfsContextTest.java"));
assertFalse(Files.isRegularFile(original));
assertFalse(Files.isRegularFile(copy));
context.importFiles("src", "src");
assertTrue(Files.isRegularFile(original));
assertFalse(Files.isRegularFile(copy));
context.mergeDirs("src", "src");
assertTrue(Files.isRegularFile(original));
assertTrue(Files.isRegularFile(copy));
}
@Test
public void testGrepTree() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
context.importFiles("src", "src");
var results = context.grepTree("src", ".*testGrepTree.*").collect(Collectors.toList());
assertEquals(" public void testGrepTree() throws IOException {", results.get(0));
assertEquals(3, results.size());
}
@Test
public void testWalkSpeed() throws IOException {
var context = new ImfsContext("imfs://ImfsContextTest/");
context.importFiles("src", "src");
var results = Files.walk(context.getPath())
.filter(Files::isRegularFile)
.collect(Collectors.toList());
assertEquals(15, results.size());
}
// Debug console from testWalkSpeed() while using Paths to do parenting
//
// ImfsDirectoryStream:
// path=imfs://ImfsContextTest/src/main/resources/META-INF/services, version=0,
// size=1, total=1, kids=1, elapsed=0
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/main/resources/META-INF,
// version=0, size=2, total=2, kids=1, elapsed=0
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/main/resources,
// version=0, size=3, total=3, kids=1, elapsed=0
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/main, version=0,
// size=20, total=20, kids=2, elapsed=5
// ...
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/test/java/com/imfs/app,
// version=0, size=1, total=1, kids=1, elapsed=0
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/test/java/com/imfs,
// version=0, size=4, total=4, kids=3, elapsed=0
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/test/java/com,
// version=0, size=5, total=5, kids=1, elapsed=1
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/test/java, version=0,
// size=6, total=6, kids=1, elapsed=1
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src/test, version=0, size=7,
// total=7, kids=1, elapsed=2
// ImfsDirectoryStream: path=imfs://ImfsContextTest/src, version=0, size=29,
// total=29, kids=2, elapsed=9
// ImfsDirectoryStream: path=imfs://ImfsContextTest/, version=0, size=33,
// total=33, kids=4, elapsed=32
//
// Debug console from testWalkSpeed() while using indexOf to do parenting
// ImfsDirectoryStream: path=src/main/resources/META-INF/services, version=0,
// size=1, total=1, kids=1, elapsed=0
// ImfsDirectoryStream: path=src/main/resources/META-INF, version=0, size=2,
// total=2, kids=1, elapsed=0
// ImfsDirectoryStream: path=src/main/resources, version=0, size=3, total=3,
// kids=1, elapsed=0
// ImfsDirectoryStream: path=src/main, version=0, size=19, total=19, kids=2,
// elapsed=3
// ...
// ImfsDirectoryStream: path=src/test/java/com/imfs/app, version=0, size=1,
// total=1, kids=1, elapsed=0
// ImfsDirectoryStream: path=src/test/java/com/imfs, version=0, size=4, total=4,
// kids=3, elapsed=0
// ImfsDirectoryStream: path=src/test/java/com, version=0, size=5, total=5,
// kids=1, elapsed=0
// ImfsDirectoryStream: path=src/test/java, version=0, size=6, total=6, kids=1,
// elapsed=0
// ImfsDirectoryStream: path=src/test, version=0, size=7, total=7, kids=1,
// elapsed=0
// ImfsDirectoryStream: path=src, version=0, size=28, total=28, kids=2,
// elapsed=4
// ImfsDirectoryStream: path=, version=0, size=32, total=32, kids=4, elapsed=25
}