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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import com.yahoo.config.FileReference;
import com.yahoo.filedistribution.fileacquirer.*;
import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Test of public API of MockFileAcquirer, since it is intended to be used by 3rd parties.
* Do not place it in the same package as MockFileAcquirer.
* @author tonytv
*/
public class MockFileAcquirerTest {
@org.junit.Test
public void testReturnFile() throws Exception {
File file = new File("/test");
assertThat(waitFor(MockFileAcquirer.returnFile(file)),
equalTo(file));
}
@org.junit.Test
public void testReturnFiles() throws Exception {
File file1 = new File("/test1");
File file2 = new File("/test2");
HashMap<String, File> map = new HashMap<String, File>();
map.put("1", file1);
map.put("2", file2);
FileAcquirer fileAcquirer = MockFileAcquirer.returnFiles(map);
assertThat(waitFor(fileAcquirer, createFileReference("1")),
equalTo(file1));
assertThat(waitFor(fileAcquirer, createFileReference("2")),
equalTo(file2));
}
@org.junit.Test(expected = TimeoutException.class)
public void testThrowTimeoutException() throws Exception {
waitFor(MockFileAcquirer.throwTimeoutException());
}
@org.junit.Test(expected = FileReferenceDoesNotExistException.class)
public void testThrowFileReferenceDoesNotExistException() throws Exception {
waitFor(MockFileAcquirer.throwFileReferenceDoesNotExistException());
}
private File waitFor(FileAcquirer fileAcquirer) throws InterruptedException {
return waitFor(fileAcquirer, null);
}
private File waitFor(FileAcquirer fileAcquirer, FileReference reference)
throws InterruptedException {
return fileAcquirer.waitFor(reference, 100, TimeUnit.SECONDS);
}
private FileReference createFileReference(String value) {
try {
Constructor<FileReference> constructors = FileReference.class.getDeclaredConstructor(String.class);
constructors.setAccessible(true);
return constructors.newInstance(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
|