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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.filedistribution;
import com.yahoo.config.FileReference;
import com.yahoo.io.IOUtils;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public class FileServerTest {
FileServer fs = new FileServer(new File("."));
List<File> created = new LinkedList<>();
private void createCleanDir(String name) throws IOException{
File dir = new File(name);
IOUtils.recursiveDeleteDir(dir);
IOUtils.createDirectory(dir.getName());
File dummy = new File(dir.getName() +"/dummy");
IOUtils.writeFile(dummy, "test", true);
assertTrue(dummy.delete());
created.add(dir);
}
@Test
public void requireThatExistingFileCanbeFound() throws IOException {
createCleanDir("123");
IOUtils.writeFile("123/f1", "test", true);
assertTrue(fs.hasFile("123"));
cleanup();
}
@Test
public void requireThatNonExistingFileCanNotBeFound() throws IOException {
assertFalse(fs.hasFile("12x"));
createCleanDir("12x");
assertFalse(fs.hasFile("12x"));
cleanup();
}
private static class FileReceiver implements FileServer.Receiver {
CompletableFuture<byte []> content;
FileReceiver(CompletableFuture<byte []> content) {
this.content = content;
}
@Override
public void receive(FileReference reference, String filename, byte[] content, FileServer.ReplayStatus status) {
this.content.complete(content);
}
}
@Test
public void requireThatWeCanReplayFile() throws IOException, InterruptedException, ExecutionException {
createCleanDir("12y");
IOUtils.writeFile("12y/f1", "dummy-data", true);
CompletableFuture<byte []> content = new CompletableFuture<>();
fs.startFileServing("12y", new FileReceiver(content));
assertEquals(new String(content.get()), "dummy-data");
cleanup();
}
private void cleanup() {
created.forEach((file) -> IOUtils.recursiveDeleteDir(file));
created.clear();
}
@Override
protected void finalize() throws Throwable {
super.finalize();
cleanup();
}
}
|